package nc.bs.mmpac.wr.rule;

import nc.impl.pubapp.pattern.rule.IRule;
import nc.util.mmf.framework.base.MMArrayUtil;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.util.mmpac.wr.WrCheckRuleUtil;
import nc.vo.mmpac.wr.entity.AggWrVO;
import nc.vo.mmpac.wr.entity.WrItemVO;
import nc.vo.mmpac.wr.entity.WrVO;
import nc.vo.mmpac.wr.enumeration.WrProdModeEnum;
import nc.vo.pub.VOStatus;
import nc.vo.pubapp.AppContext;

/**
 * 生产报告新增时，设置默认值规则
 * 
 * @since 6.1
 * @author zhoudi
 */
public class WrSetDefaultValueWhenInsertRule implements IRule<AggWrVO> {

	@Override
	public void process(AggWrVO[] vos) {

		if (MMValueCheck.isEmpty(vos)) {
			return;
		}

		WrVO headVO;

		for (AggWrVO aggVO : vos) {

			headVO = (WrVO) aggVO.getParent();

			if (MMValueCheck.isEmpty(headVO)) {
				continue;
			}

			// 新增状态
			headVO.setStatus(VOStatus.NEW);

			// 制单人、制单时间
			headVO.setBillmaker(AppContext.getInstance().getPkUser());
			if (headVO.getDmakedate() == null) {
				headVO.setDmakedate(AppContext.getInstance().getBusiDate());
			}

			if (!WrProdModeEnum.FLOW.equalsValue(vos[0].getParentVO().getFprodmode())) {
				continue;
			}
			WrItemVO[] wrItemVOs = (WrItemVO[]) aggVO.getChildren(WrItemVO.class);
			if (MMArrayUtil.isEmpty(wrItemVOs)) {
				continue;
			}
			// 生产部门等，表体字段均相等，则同步表体字段到表头
			for (String[] headitemKey : this.headKeyToitemKey) {
				if (WrCheckRuleUtil.isItemKeyValueAllSame(wrItemVOs, headitemKey[1])) {
					headVO.setAttributeValue(headitemKey[0], wrItemVOs[0].getAttributeValue(headitemKey[1]));
				}
			}
			if (MMValueCheck.isEmpty(headVO.getCdeptvid()) && MMValueCheck.isNotEmpty(headVO.getCdeptid())) {
				headVO.setCdeptid(null);
			}
		}
	}

	private String[][] headKeyToitemKey = { { WrVO.CDEPTID, WrItemVO.CBDEPTID }, { WrVO.CDEPTVID, WrItemVO.CBDEPTVID },
			{ WrVO.CWKID, WrItemVO.CBWKID }, { WrVO.CTEAMID, WrItemVO.CBTEAMID }, { WrVO.CSHIFTID, WrItemVO.CBSHIFTID },
			{ WrVO.CWORKMANID, WrItemVO.CBWORKMANID } };
}
