package nc.bs.ic.m45.sign;

import com.yonyou.cloud.ncc.plugin.entity.OperationInfo;
import nc.bs.ic.general.sign.ISignBP;
import nc.bs.ic.general.sign.ISignRuleProvider;
import nc.bs.ic.general.sign.SignBPTemplate;
import nc.bs.ic.m45.sign.rule.PurchaseReceiptRuleMES;
import nc.bs.ic.m45.base.BPPlugInPoint;
import nc.bs.ic.m45.insert.rule.InsertOrSignRewritePayPlanInvoiceRule;
import nc.bs.ic.m45.sign.rule.*;
import nc.bs.ic.pub.util.DebugUtils;
import nc.bs.ic.pub.util.SagasUtils;
import nc.bs.scmpub.rule.VOSagaFrozenValidateRule;
import nc.impl.pubapp.pattern.rule.processer.AroundProcesser;
import nc.itf.ic.m45.compensate.IPurchaseInSagasCompensate;
import nc.vo.ic.m45.entity.PurchaseInVO;
import nc.vo.ic.pub.util.VOEntityUtil;
import nc.vo.scmpub.res.billtype.ICBillType;
import nccloud.bs.ic.mobile.inbound.operation.rewrite.MobAfterPurchaseInSign;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class SignBP implements ISignBP<PurchaseInVO>, ISignRuleProvider<PurchaseInVO> {
    public SignBP() {
    }

    public PurchaseInVO[] sign(PurchaseInVO[] bills) {
        DebugUtils.debug("purchasein-sign-begin");
        SignBPTemplate<PurchaseInVO> signBP = new SignBPTemplate(BPPlugInPoint.SignAction, this);
        SagasUtils.frozenAndAddSaga(bills, ICBillType.PurchaseIn.getCode(), "1", (OperationInfo) null);
        Map<String, Serializable> paramMap = new HashMap();
        paramMap.put("actionname", "sign_45");
        paramMap.put("hid", VOEntityUtil.getPksFromAggVO(bills));
        paramMap.put("vos", bills);
        SagasUtils.compensate(paramMap, IPurchaseInSagasCompensate.class);
        PurchaseInVO[] resultVOs = (PurchaseInVO[]) signBP.sign(bills);
        DebugUtils.debug("purchasein-sign-end");
        return resultVOs;
    }

    public void addAfterRule(PurchaseInVO[] vos, AroundProcesser<PurchaseInVO> processor) {
        processor.addAfterRule(new SignRewritePayPlanConfirmDataRule());
        processor.addAfterRule(new InsertOrSignRewritePayPlanInvoiceRule("1001Z01000000000F04P"));
        processor.addAfterRule(new AfterRuleForRewritePUSettle());
        processor.addAfterRule(new AfterRuleForPush5X());
        processor.addAfterRule(new AfterSignRuleForFinanceProcess());
        processor.addAfterRule(new AfterSignRuleForLiabilityProcess());
        processor.addAfterRule(new MobAfterPurchaseInSign());
        processor.addAfterRule(new SignM45AndRewriteCTPayPlan());
        processor.addAfterRule(new SignInsertFeeDetailRule());
        // 签字后推送至MES系统 推送销售出库单之前 先推送前置通知单
        processor.addAfterRule(new PurchaseReceiptRuleMES());
        processor.addAfterRule(new AfterSigningSynchronizeRuleMES());
    }

    public void addBeforeRule(PurchaseInVO[] vos, AroundProcesser<PurchaseInVO> processor) {
        processor.addBeforeRule(new VOSagaFrozenValidateRule(true));
        processor.addBeforeRule(new BeforeRuleForCheckPU());
    }
}
