package nc.bs.mmpac.pickm.bp.rule;

import java.util.HashSet;
import java.util.Set;
import nc.impl.pubapp.pattern.rule.IRule;
import java.util.Arrays;
import java.util.Map;
import nc.bs.framework.common.NCLocator;
import nc.itf.mmpac.pmo.pac0002.IPMOQueryService;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpac.pickm.entity.PickmItemVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOItemVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class PickmFillInfoFormMORule implements IRule<AggPickmVO> {

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

        if (MMValueCheck.isEmpty(vos)) {
            return;
        }
        try {
            //流程生产订单表体主键
            Set<String> bmopksSet = new HashSet<>();
            Arrays.stream(vos).forEach(aggPickmVO -> bmopksSet.add(aggPickmVO.getParentVO().getVsourcemorowid()));
            IPMOQueryService ipmoQueryService =  NCLocator.getInstance().lookup(IPMOQueryService.class);
            Map<String, PMOItemVO> pMOItemVOMap = ipmoQueryService.queryItemBybid(bmopksSet.toArray(new String[0]));

            Arrays.stream(vos).forEach(aggPickmVO -> {
                //生产订单行主键
                String vsourcemorowid = aggPickmVO.getParentVO().getVsourcemorowid();
                PickmItemVO[] children = (PickmItemVO[])aggPickmVO.getChildren(PickmItemVO.class);
                if(!MMValueCheck.isNotEmpty(children)){
                    return;
                }
                for (PickmItemVO pickmItemVO : children) {
                    PMOItemVO pmoItemVO = pMOItemVOMap.get(vsourcemorowid);
                    pickmItemVO.setVbdef5(pmoItemVO.getVdef1());//合同编号
                    pickmItemVO.setVbdef6(pmoItemVO.getVdef2());//合同销售订单号
                    pickmItemVO.setVbdef7(pmoItemVO.getVdef7());//标前项目号
                }
            });
        } catch (BusinessException e) {
            ExceptionUtils.wrappException(e);
        }
    }

}
