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

package nc.bs.mmpac.pickm.bp;

import nc.bs.mmpac.pickm.plugin.PickmPluginPoint;
import nc.bs.mmpac.pickm.rule.*;
import nc.bs.mmpac.pickm.rule.ic.PickmATPCheckRule;
import nc.bs.mmpac.pickm.rule.ic.PickmAutoReserveRule;
import nc.bs.mmpac.pickm.rule.sc.PickmSetDeftValueForScRule;
import nc.bs.mmpac.pickm.rule.sc.PickmSetItemDeftValueForScRule;
import nc.bs.mmpub.rule.MMAutoMaterialAssignRule;
import nc.bs.pubapp.pub.rule.FieldLengthCheckRule;
import nc.bs.pubapp.pub.rule.OrgDisabledCheckRule;
import nc.bsutil.mmpac.pickm.PickmSagasUtil;
import nc.impl.pubapp.bd.userdef.UserDefSaveRule;
import nc.impl.pubapp.pattern.data.bill.template.BillInsertOperator;
import nc.impl.pubapp.pattern.data.bill.template.InsertBPTemplate;
import nc.impl.pubapp.pattern.data.bill.tool.BillTransferTool;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.impl.pubapp.pattern.rule.processer.AroundProcesser;
import nc.impl.pubapp.pattern.rule.template.IOperator;
import nc.util.mmf.busi.rule.MMBillCodeCheckAndTrantypeRule;
import nc.util.mmf.busi.rule.MMCreateBillcodeAndTrantypecodeRule;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpac.pickm.entity.PickmHeadVO;
import nc.vo.mmpac.pickm.entity.PickmItemVO;
import nc.vo.mmpac.pickm.enumeration.FsourcetypeEnum;

import java.util.ArrayList;
import java.util.List;

public class PickmInsertForScBP {
    public PickmInsertForScBP() {
    }

    public void savePickmForSc(PickmHeadVO[] headVOs) {
        if (!MMValueCheck.isEmpty(headVOs)) {
            List<AggPickmVO> aggPickmVOList = new ArrayList();
            AggPickmVO aggPickmVO = null;

            for(PickmHeadVO headVO : headVOs) {
                headVO.setFsourcetype(FsourcetypeEnum.WWBILL_STATE.toInteger());
                aggPickmVO = new AggPickmVO();
                aggPickmVO.setParent(headVO);
                aggPickmVOList.add(aggPickmVO);
            }

            AggPickmVO[] retVOs = this.savePickmForSc((AggPickmVO[])aggPickmVOList.toArray(new AggPickmVO[0]));
            IRule<AggPickmVO> pickmAutoApproveRule = new PickmAutoApproveAfterSaveRule();
            pickmAutoApproveRule.process(retVOs);
        }
    }

    public void savePickmForSc2(AggPickmVO[] vos) {
        if (!MMValueCheck.isEmpty(vos)) {
            AggPickmVO[] retVOs = this.savePickmForSc(vos);
            IRule<AggPickmVO> pickmAutoApproveRule = new PickmAutoApproveAfterSaveRule();
            pickmAutoApproveRule.process(retVOs);
        }
    }

    private void addAfterRule(AroundProcesser<AggPickmVO> processor) {
        IRule<AggPickmVO> billCodeCheckRule = new MMBillCodeCheckAndTrantypeRule("55A3", "vbillcode", "pk_group", "pk_org", "vbusitype");
        processor.addAfterRule(billCodeCheckRule);
        IRule<AggPickmVO> atpCheckRule = new PickmATPCheckRule(false, (AggPickmVO[])null);
        processor.addAfterRule(atpCheckRule);
        IRule<AggPickmVO> autoReserveRule = new PickmAutoReserveRule();
        processor.addAfterRule(autoReserveRule);
    }

    private void addBeforeRule(AroundProcesser<AggPickmVO> processor) {
        IRule<AggPickmVO> orgRule = new OrgDisabledCheckRule("pk_org", "FACTORYTYPE000000000");
        processor.addBeforeRule(orgRule);
        IRule<AggPickmVO> getMakeDateRule = new PickmMakeDateRule();
        processor.addBeforeRule(getMakeDateRule);
        IRule<AggPickmVO> setForScRule = new PickmSetDeftValueForScRule();
        processor.addBeforeRule(setForScRule);

        IRule<AggPickmVO> billcodeRule = new MMCreateBillcodeAndTrantypecodeRule("55A3", "vbillcode", "pk_group", "pk_org", "vbusitype");
        processor.addBeforeRule(billcodeRule);
        IRule<AggPickmVO> createSubItemRule = new PickmCreateSubItemRule(false);
        processor.addBeforeRule(createSubItemRule);
        IRule<AggPickmVO> setItemForScRule = new PickmSetItemDeftValueForScRule();
        processor.addBeforeRule(setItemForScRule);
        IRule<AggPickmVO> sameRule = new PickmCheckSameStockRule();
        processor.addBeforeRule(sameRule);

        IRule<AggPickmVO> checkSameRow = new PickmRowNoRule();
        processor.addBeforeRule(checkSameRow);
        IRule<AggPickmVO> lengthCheckRule = new FieldLengthCheckRule();
        processor.addBeforeRule(lengthCheckRule);
        IRule<AggPickmVO> autoHMaterialAssignRule = new MMAutoMaterialAssignRule("cmaterialvid", (String)null, new String[]{"pk_org"}, (String[])null);
        processor.addBeforeFinalRule(autoHMaterialAssignRule);
        IRule<AggPickmVO> autobMaterialAssignRule = new MMAutoMaterialAssignRule((String)null, "cbmaterialvid", new String[]{"pk_org"}, new String[]{"cdeliverorgid", "csupplyorgid"});
        processor.addBeforeFinalRule(autobMaterialAssignRule);
        IRule<AggPickmVO> marperRule = new PickmCheckMaterialPermissionRule();
        processor.addBeforeRule(marperRule);
        IRule<AggPickmVO> bmarperRule = new PickmCheckItemMaterialPermissionRule();
        processor.addBeforeRule(bmarperRule);
        IRule<AggPickmVO> pickmSaveRule = new PickmSaveRule();
        processor.addBeforeRule(pickmSaveRule);
        IRule<AggPickmVO> atpCheckRule = new PickmATPCheckRule(true, (AggPickmVO[])null);
        processor.addBeforeRule(atpCheckRule);
        IRule<AggPickmVO> userDefSaveRule = new UserDefSaveRule(new String[]{"vdef", "vbdef"}, new Class[]{PickmHeadVO.class, PickmItemVO.class});
        processor.addBeforeRule(userDefSaveRule);
    }

    private AggPickmVO[] savePickmForSc(AggPickmVO[] vos) {
        if (MMValueCheck.isEmpty(vos)) {
            return null;
        } else {
            new BillTransferTool(vos);
            InsertBPTemplate<AggPickmVO> bp = new InsertBPTemplate(PickmPluginPoint.INSERTFORSC);
            this.addBeforeRule(bp.getAroundProcesser());
            this.addAfterRule(bp.getAroundProcesser());
            bp.getAroundProcesser().before(vos);
            IOperator<AggPickmVO> operator = new BillInsertOperator();
            AggPickmVO[] aggvos = (AggPickmVO[])operator.operate(vos);
            PickmSagasUtil.addSagaAndCompensate4Insert(aggvos);
            bp.getAroundProcesser().after(aggvos);
            return aggvos;
        }
    }

    public void savePickmForScSagasCheck(PickmHeadVO[] headVOs) {
        if (!MMValueCheck.isEmpty(headVOs)) {
            List<AggPickmVO> aggPickmVOList = new ArrayList();
            AggPickmVO aggPickmVO = null;

            for(PickmHeadVO headVO : headVOs) {
                headVO.setFsourcetype(FsourcetypeEnum.WWBILL_STATE.toInteger());
                aggPickmVO = new AggPickmVO();
                aggPickmVO.setParent(headVO);
                aggPickmVOList.add(aggPickmVO);
            }

            new BillTransferTool((AggPickmVO[])aggPickmVOList.toArray(new AggPickmVO[0]));
            InsertBPTemplate<AggPickmVO> bp = new InsertBPTemplate(PickmPluginPoint.INSERTFORSC);
            this.addBeforeRuleSagasCheck(bp.getAroundProcesser());
            bp.getAroundProcesser().before((AggPickmVO[])aggPickmVOList.toArray(new AggPickmVO[0]));
        }
    }

    private void addBeforeRuleSagasCheck(AroundProcesser<AggPickmVO> processor) {
        IRule<AggPickmVO> orgRule = new OrgDisabledCheckRule("pk_org", "FACTORYTYPE000000000");
        processor.addBeforeRule(orgRule);
        IRule<AggPickmVO> getMakeDateRule = new PickmMakeDateRule();
        processor.addBeforeRule(getMakeDateRule);
        IRule<AggPickmVO> setForScRule = new PickmSetDeftValueForScRule();
        processor.addBeforeRule(setForScRule);
        IRule<AggPickmVO> createSubItemRule = new PickmCreateSubItemRule(false);
        processor.addBeforeRule(createSubItemRule);
        IRule<AggPickmVO> setItemForScRule = new PickmSetItemDeftValueForScRule();
        processor.addBeforeRule(setItemForScRule);
        IRule<AggPickmVO> sameRule = new PickmCheckSameStockRule();
        processor.addBeforeRule(sameRule);
        IRule<AggPickmVO> checkSameRow = new PickmRowNoRule();
        processor.addBeforeRule(checkSameRow);
        IRule<AggPickmVO> lengthCheckRule = new FieldLengthCheckRule();
        processor.addBeforeRule(lengthCheckRule);
        IRule<AggPickmVO> marperRule = new PickmCheckMaterialPermissionRule();
        processor.addBeforeRule(marperRule);
        IRule<AggPickmVO> bmarperRule = new PickmCheckItemMaterialPermissionRule();
        processor.addBeforeRule(bmarperRule);
        IRule<AggPickmVO> pickmSaveRule = new PickmSaveRule();
        processor.addBeforeRule(pickmSaveRule);
        IRule<AggPickmVO> atpCheckRule = new PickmATPCheckRule(true, (AggPickmVO[])null);
        processor.addBeforeRule(atpCheckRule);
        IRule<AggPickmVO> userDefSaveRule = new UserDefSaveRule(new String[]{"vdef", "vbdef"}, new Class[]{PickmHeadVO.class, PickmItemVO.class});
        processor.addBeforeRule(userDefSaveRule);
    }
}
