//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.bs.mmpac.pmo.pac0002.bp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.bs.mmpac.pmo.pac0002.bp.service.PMOBPService;
import nc.bs.mmpac.pmo.pac0002.bp.util.PMOBPUtil;
import nc.bs.mmpac.pmo.pac0002.pluginpoint.PMOPluginPoint;
import nc.bs.mmpac.pmo.pac0002.rule.PMOFilterBillStatusRule;
import nc.bs.mmpac.pmo.pac0002.rule.PMOFilterRowStatusRule;
import nc.bs.mmpac.pmo.pac0002.rule.PMOFinishAutoFinishPutPlanRule;
import nc.bs.mmpac.pmo.pac0002.rule.fill.PMOFillFinishValueRule;
import nc.bs.mmpub.rule.MMVOSagaFrozenValidateRule;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.util.mmf.framework.gc.GCBillTransferTool;
import nc.util.mmf.framework.gc.GCBillUpdate;
import nc.util.mmf.framework.gc.GCUpdateBPTemplate;
import nc.vo.mmpac.pmo.pac0002.constant.PMOConstLang;
import nc.vo.mmpac.pmo.pac0002.entity.PMOAggVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOItemVO;
import nc.vo.mmpac.pmo.pac0002.enumeration.PMOFBillstatusEnum;
import nc.vo.mmpac.pmo.pac0002.enumeration.PMOFItemStatusEnum;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class PMORowFinishBP {
    public PMORowFinishBP() {
    }

    public PMOAggVO[] rowfinish(PMOAggVO[] vos) {
        GCUpdateBPTemplate<PMOAggVO> bp = new GCUpdateBPTemplate(PMOPluginPoint.ROWFINISH);
        CompareAroundProcesser<PMOAggVO> processer = bp.getAroundProcesser();
        this.addRowFinishBeforeRule(processer);
        this.addRowFinishAfterRule(processer);
        PMOAggVO[] returnvos = (PMOAggVO[])processer.before(vos, vos);
        List<PMOItemVO> upditemList = new ArrayList();
        Map<String, PMOItemVO> upditemMap = new HashMap();

        for(PMOAggVO aggvo : returnvos) {
            PMOItemVO[] items = aggvo.getChildrenVO();

            for(PMOItemVO item : items) {
                upditemList.add(item);
                upditemMap.put(item.getCmoid(), item);
            }
        }

        try {
            PMOAggVO aggvo = PMOBPService.getIPMOQueryService().queryByPk(vos[0].getParentVO().getCpmohid());
            PMOItemVO[] olditems = aggvo.getChildrenVO();
            List<PMOItemVO> tempitems = new ArrayList();

            for(PMOItemVO item : olditems) {
                if (!upditemMap.containsKey(item.getCmoid())) {
                    tempitems.add(item);
                } else {
                    ((PMOItemVO)upditemMap.get(item.getCmoid())).setStatus(1);
                    PMOBPUtil.setGrandVOStatusUpdate((PMOItemVO)upditemMap.get(item.getCmoid()));
                    tempitems.add((PMOItemVO)upditemMap.get(item.getCmoid()));
                }
            }

            aggvo.setChildrenVO((CircularlyAccessibleValueObject[])tempitems.toArray(new PMOItemVO[tempitems.size()]));
            PMOAggVO[] aggvos = new PMOAggVO[]{aggvo};
            GCBillTransferTool<PMOAggVO> transferTool = new GCBillTransferTool(aggvos);
            PMOAggVO[] originBills = (PMOAggVO[])transferTool.getOriginBills();
            GCBillUpdate<PMOAggVO> billupdate = new GCBillUpdate();
            PMOAggVO[] newaggvos = (PMOAggVO[])billupdate.update(aggvos, originBills);
            PMOItemVO[] totalitems = newaggvos[0].getChildrenVO();
            Map<String, PMOItemVO> newitemMap = new HashMap();

            for(PMOItemVO item : totalitems) {
                if (upditemMap.containsKey(item.getCmoid())) {
                    newitemMap.put(item.getCmoid(), item);
                }
            }

            returnvos[0].setChildrenVO((CircularlyAccessibleValueObject[])newitemMap.values().toArray(new PMOItemVO[0]));
            processer.after(returnvos, returnvos);

//            /** 同步修改备料计划完成(生产订单强制完工时) */
//            IRule<PMOAggVO> pMOToFinishPickMRule = new PMOToFinishPickMRule();
//            pMOToFinishPickMRule.process(newaggvos);
//            /** ******************** */

            PMOAggVO newaggvo = PMOBPService.getIPMOQueryService().queryByPk(vos[0].getParentVO().getCpmohid());
            return new PMOAggVO[]{newaggvo};
        } catch (BusinessException e) {
            ExceptionUtils.wrappException(e);
            return returnvos;
        }
    }

    private void addRowFinishBeforeRule(CompareAroundProcesser<PMOAggVO> processer) {
        IRule<PMOAggVO> checkFrozenRule = new MMVOSagaFrozenValidateRule(true);
        processer.addBeforeRule(checkFrozenRule);
        processer.addBeforeRule(new PMOFilterBillStatusRule(PMOConstLang.getMSG_ACTION_NO_AUDIT(), PMOFBillstatusEnum.AUDIT));
        processer.addBeforeRule(new PMOFilterRowStatusRule(PMOConstLang.getMSG_RULE_NO_ROWDATA(), PMOFItemStatusEnum.PUT));
        processer.addBeforeRule(new PMOFillFinishValueRule(false));
    }

    private void addRowFinishAfterRule(CompareAroundProcesser<PMOAggVO> processer) {
        processer.addAfterRule(new PMOFinishAutoFinishPutPlanRule());
    }
}
