package nc.bs.ic.m4c.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.m4c.base.BPPlugInPoint;
import nc.bs.ic.m4c.base.rule.SaleOutFillInvoiceNumRule;
import nc.bs.ic.m4c.sign.rule.*;
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.m4c.compensate.ISaleOutSagasCompensate;
import nc.vo.ic.m4c.entity.SaleOutVO;
import nc.vo.ic.pub.util.VOEntityUtil;
import nc.vo.scmpub.res.billtype.ICBillType;
import nccloud.bs.ic.mobile.component.operation.rule.MobAfterSignMessageRule;
import nccloud.bs.ic.tms.kj.KJTMSConfirmAction;
import nccloud.vo.scmpub.tms.enumeration.BusiScene;

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

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

    public SaleOutVO[] sign(SaleOutVO[] bills) {
        SignBPTemplate<SaleOutVO> signBP = new SignBPTemplate(BPPlugInPoint.SignBP, this);
        SagasUtils.frozenAndAddSaga(bills, ICBillType.SaleOut.getCode(), "1", (OperationInfo) null);
        Map<String, Serializable> paramMap = new HashMap();
        paramMap.put("actionname", "sign_4C");
        paramMap.put("hid", VOEntityUtil.getPksFromAggVO(bills));
        SagasUtils.compensate(paramMap, ISaleOutSagasCompensate.class);
        SaleOutVO[] resultVOs = (SaleOutVO[]) signBP.sign(bills);
        KJTMSConfirmAction action = new KJTMSConfirmAction(BusiScene.SODelivery);
        action.confirm(resultVOs);
        return resultVOs;
    }

    public void addAfterRule(SaleOutVO[] vos, AroundProcesser<SaleOutVO> processor) {
        processor.addAfterRule(new PushSquareSignRule());
        processor.addAfterRule(new AfterSignRuleForSquareiaProcess());
        processor.addAfterRule(new AfterSignRuleForFinanceProcess());
        processor.addAfterRule(new SaleOutFillInvoiceNumRule());
        processor.addAfterRule(new AfterSignRuleForLiabilityProcess());
        processor.addAfterRule(new SaleOutProceedsRule());
        processor.addAfterRule(new AfterRedSignRuleTOArsubProcess());
        processor.addAfterRule(new ArsubToVoucherRule());
        processor.addAfterRule(new SaleOutProceedsRuleCG());
        processor.addAfterRule(new MobAfterSignMessageRule());
        // 销售出库 多一个步骤先提交销售交货单 与同步销售出库单的orderNo一致
//        processor.addAfterRule(new SalesDeliveryOrderRuleMES());
        // 销售出库 签字后 同步到MES金思维系统
//        processor.addAfterRule(new AfterSigningSynchronizeRuleMES());
        // 盘点（审批后传MES）

    }

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