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


import nc.bs.mmpac.pmo.pac0002.bp.rule.AfterApproveRuleSyncMes;
import nc.bs.mmpac.pmo.pac0002.bp.rule.AfterApprovingSynchronizeRuleRZ;
import nc.bs.mmpac.pmo.pac0002.pluginpoint.PMOPluginPoint;
import nc.bs.mmpac.pmo.pac0002.rule.*;
import nc.bs.mmpac.pmo.pac0002.rule.check.*;
import nc.bs.mmpac.pmo.pac0002.rule.fill.PMOFillApproveValueRule;
import nc.bs.mmpac.pmo.pac0002.rule.psc.PMOCreatePSCPlanRule;
import nc.bs.mmpac.pmo.pac0002.rule.psc.PMOUnApproveCheckNscNumRule;
import nc.bs.mmpub.rule.MMATOMaterialCheckRule;
import nc.bs.mmpub.rule.MMVOSagaFrozenValidateRule;
import nc.impl.pubapp.pattern.rule.ICompareRule;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.util.mmf.framework.gc.GCUpdateBPTemplate;
import nc.vo.mmpac.pmo.pac0002.entity.PMOAggVO;

public class PMOApproveBP {
    public PMOApproveBP() {
    }

    public PMOAggVO[] approve(PMOAggVO[] fullBills, PMOAggVO[] originBills) {
        GCUpdateBPTemplate<PMOAggVO> bp = new GCUpdateBPTemplate(PMOPluginPoint.APPROVE);
        this.addApproveBeforeRule(bp.getAroundProcesser());
        this.addApproveAfterRule(bp.getAroundProcesser());
        return (PMOAggVO[]) bp.update(fullBills, originBills);
    }

    private void addApproveBeforeRule(CompareAroundProcesser<PMOAggVO> processer) {
        IRule<PMOAggVO> checkFrozenRule = new MMVOSagaFrozenValidateRule(true);
        processer.addBeforeRule(checkFrozenRule);
        processer.addBeforeRule(new PMOCheckApproveStatusRule());
        IRule<PMOAggVO> checkDeptRule = new PMOCheckDeptNotNullRule();
        processer.addBeforeRule(checkDeptRule);
        IRule<PMOAggVO> approvefillvaluerule = new PMOFillApproveValueRule(false);
        processer.addBeforeRule(approvefillvaluerule);
        IRule<PMOAggVO> approvecheckrule = new PMOCheckProcedureNotNullRule();
        processer.addBeforeRule(approvecheckrule);
        IRule<PMOAggVO> operTypeRule = new PMOCheckApproveOperTypeRule();
        processer.addBeforeRule(operTypeRule);
        IRule<PMOAggVO> mmatoMaterialCheckRule = new MMATOMaterialCheckRule((String) null, "cmaterialvid", "cffileid");
        processer.addBeforeRule(mmatoMaterialCheckRule);
    }

    private void addApproveAfterRule(CompareAroundProcesser<PMOAggVO> processer) {
        IRule<PMOAggVO> pickmrule = new PMOInsertPickmRule();
        processer.addAfterRule(pickmrule);
        processer.addAfterRule(new PMOAuditAutoAuditPickmRule());
        processer.addAfterRule(new PMOCreatePSCPlanRule());
        ICompareRule<PMOAggVO> auditSupplyRule = new PMOApproveAuditSupplyRule();
        processer.addAfterRule(auditSupplyRule);
        // 审批后推送到RZ系统
        processer.addAfterRule(new AfterApprovingSynchronizeRuleRZ(true));
        // 审批后推送流程生产订单到MES
        processer.addAfterRule(new AfterApproveRuleSyncMes());

    }

    public PMOAggVO[] approveCancel(PMOAggVO[] fullBills, PMOAggVO[] originBills) {
        GCUpdateBPTemplate<PMOAggVO> bp = new GCUpdateBPTemplate(PMOPluginPoint.UNAPPROVE);
        this.addApproveCancelBeforeRule(bp.getAroundProcesser());
        this.addApproveCancelAfterRule(bp.getAroundProcesser());
        return (PMOAggVO[]) bp.update(fullBills, originBills);
    }

    private void addApproveCancelBeforeRule(CompareAroundProcesser<PMOAggVO> processer) {
        IRule<PMOAggVO> checkFrozenRule = new MMVOSagaFrozenValidateRule(true);
        processer.addBeforeRule(checkFrozenRule);
        processer.addBeforeRule(new PMOCheckUnApproveStatusRule());
        PMOUnApproveCheckNscNumRule checkNscNumRule = new PMOUnApproveCheckNscNumRule();
        processer.addBeforeRule(checkNscNumRule);
        IRule<PMOAggVO> cancelcheckrule = new PMOCheckApproveCancelRule();
        processer.addBeforeRule(cancelcheckrule);
        IRule<PMOAggVO> cancelCheckPutPlanStatusRule = new PMOApproveCancelCheckPutPlanStatusRule();
        processer.addBeforeRule(cancelCheckPutPlanStatusRule);
        IRule<PMOAggVO> approvefillvaluerule = new PMOFillApproveValueRule(true);
        processer.addBeforeRule(approvefillvaluerule);
    }

    private void addApproveCancelAfterRule(CompareAroundProcesser<PMOAggVO> processer) {
        ICompareRule<PMOAggVO> auditSupplyRule = new PMOApproveUnAuditSupplyRule();
        processer.addAfterRule(auditSupplyRule);
    }
}
