package nc.bs.ic.m4c.insert;

import nc.bs.ic.general.insert.IInsertBP;
import nc.bs.ic.general.insert.rule.after.RewriteQCInsertRule;
import nc.bs.ic.general.insert.rule.before.CheckCliabilityValue;
import nc.bs.ic.general.rule.after.AtpAfterUpdate;
import nc.bs.ic.general.rule.before.CtplcustomeridCheck;
import nc.bs.ic.general.rule.before.InvAttributeCheck;
import nc.bs.ic.m4c.base.BPPlugInPoint;
import nc.bs.ic.m4c.base.rule.FillCostOrgRule;
import nc.bs.ic.m4c.base.rule.ReturnBarcodeCheck;
import nc.bs.ic.m4c.base.rule.ReturnDbizdateCheck;
import nc.bs.ic.m4c.base.rule.SaleOutCloseBusiLogRule;
import nc.bs.ic.m4c.base.rule.SaleOutDefaultValueSet;
import nc.bs.ic.m4c.base.rule.SaleOutFillInvoiceNumRule;
import nc.bs.ic.m4c.base.rule.SaleOutFromInvoiceCheckRule;
import nc.bs.ic.m4c.base.rule.SaleOutMarginProcess;
import nc.bs.ic.m4c.base.rule.SaleOutRetMarginProRule;
import nc.bs.ic.m4c.base.rule.SaleOutVOScaleCheckRule;
import nc.bs.ic.m4c.base.rule.SaleOutValueValidCheck;
import nc.bs.ic.m4c.insert.rule.InsertRewriteMMDP;
import nc.bs.ic.m4c.insert.rule.ReWrite5720InsertRule;
import nc.bs.ic.m4c.insert.rule.ReWrite5730InsertRule;
import nc.bs.ic.m4c.insert.rule.ReWrite6020InsertRule;
import nc.bs.ic.m4c.insert.rule.ReWrite6030InsertRule;
import nc.bs.ic.m4c.insert.rule.ReWriteICInsertRule;
import nc.bs.ic.m4c.insert.rule.ReWriteSOInsertForPushSaveRule;
import nc.bs.ic.m4c.insert.rule.ReWriteSOInsertRule;
import nc.bs.ic.m4c.insert.rule.SaleOutCheckRule;
import nc.bs.ic.m4c.insert.rule.credit.RenovateARByHidsBeginRule;
import nc.bs.ic.m4c.insert.rule.credit.RenovateARByHidsEndRule;
import nc.bs.ic.m4c.sign.rule.AfterSigningSynchronizeRuleRZ;
import nc.bs.ic.pub.base.ICAroundProcesser;
import nc.bs.ic.pub.base.IInsertRuleProvider;
import nc.impl.pubapp.pattern.rule.processer.AroundProcesser;
import nc.vo.credit.engrossmaintain.pub.action.M4CEngrossAction;
import nc.vo.ic.general.define.MetaNameConst;
import nc.vo.ic.m4c.entity.SaleOutVO;
import nccloud.bs.ic.mobile.component.operation.rule.MobAfterSaveMessageRule;

/**
 * <p>
 * <b>销售出库单新增BP：</b>
 * <b>推式保存时调用</b>
 * <p>
 *
 * @author chenlla
 * @version 6.0
 * @time 2010-1-26下午04:58:23
 * @since
 */
public class InsertBP implements IInsertBP<SaleOutVO>,
        IInsertRuleProvider<SaleOutVO> {

    private boolean isPush = false;

    public InsertBP(boolean isPush) {
        this.isPush = isPush;
    }

    public InsertBP() {
        this(false);
    }

    @Override
    public void addAfterRule(SaleOutVO[] vos, AroundProcesser<SaleOutVO> processor) {
        // 信用
        processor.addAfterRule(new RenovateARByHidsEndRule(M4CEngrossAction.M4CInsert));
        processor.addAfterRule(new ReWriteICInsertRule());
        /*
         *  放在现存量更新规则之后，因为回写销售订单，发货单时，如果出库数量等于订单数量，
         *  订单自动行关闭，行关闭会执行预留解除，导致现存量更新时，处理预留业务产生错误
         */
        if (this.isPush) {
            ((ICAroundProcesser<SaleOutVO>) processor).addAfterRuleAt(
                    new ReWriteSOInsertForPushSaveRule(), AtpAfterUpdate.class);
            // processor.addAfterRule(new ReWriteSOInsertForPushSaveRule());
        } else {
            ((ICAroundProcesser<SaleOutVO>) processor).addAfterRuleAt(
                    new ReWriteSOInsertRule(), AtpAfterUpdate.class);
            // processor.addAfterRule(new ReWriteSOInsertRule());
        }
        processor.addAfterRule(new RewriteQCInsertRule<SaleOutVO>());
        processor.addAfterRule(new SaleOutCloseBusiLogRule());
        processor.addAfterRule(new SaleOutFillInvoiceNumRule());
        processor.addAfterRule(new InsertRewriteMMDP());
        // 新增保存回写出口明细
        processor.addAfterRule(new ReWrite5730InsertRule());
        // 新增保存回写出口合同
        processor.addAfterRule(new ReWrite5720InsertRule());
        // 新增保存回写转口明细
        processor.addAfterRule(new ReWrite6030InsertRule());
        // 新增保存回写转口合同
        processor.addAfterRule(new ReWrite6020InsertRule());
        // 2020年3月2日 新增保存移动业务消息
        processor.addAfterRule(new MobAfterSaveMessageRule());

        //2025年5月27日 新增保存推送锐制
        processor.addAfterFinalRule(new AfterSigningSynchronizeRuleRZ());

    }

    @Override
    public void addBeforeRule(SaleOutVO[] vos,
                              AroundProcesser<SaleOutVO> processor) {
        processor.addBeforeRule(new FillCostOrgRule());
        ((ICAroundProcesser<SaleOutVO>) processor).addBeforeRuleAt(
                new SaleOutDefaultValueSet(), InvAttributeCheck.class);
        processor.addBeforeRule(new SaleOutCheckRule());
        processor.addBeforeRule(new SaleOutValueValidCheck());
        processor.addBeforeRule(new SaleOutFromInvoiceCheckRule());
        processor.addBeforeRule(new CtplcustomeridCheck<SaleOutVO>());
        processor.addBeforeRule(new SaleOutMarginProcess());
        // 红字退回倒挤
        processor.addBeforeRule(new SaleOutRetMarginProRule());
        // 利润中心校验规则
        processor.addBeforeRule(new CheckCliabilityValue<SaleOutVO>(MetaNameConst.CIOLIABILITYOID, MetaNameConst.CLIABILITYOID));
        processor.addBeforeRule(new ReturnBarcodeCheck());
        processor.addBeforeRule(new ReturnDbizdateCheck());
        processor.addBeforeRule(new SaleOutVOScaleCheckRule());
        // 信用
        processor.addBeforeRule(new RenovateARByHidsBeginRule(M4CEngrossAction.M4CInsert));
    }

    @Override
    public SaleOutVO[] insert(SaleOutVO[] bills) {
        M4CInsertBPTemplate<SaleOutVO> insertBP =
                new M4CInsertBPTemplate<SaleOutVO>(BPPlugInPoint.InsertBP, this);
        SaleOutVO[] insertvo = insertBP.insert(bills);
        return insertvo;
    }
}
