package nc.impl.sc.m61.action.approve;


import nc.bs.pub.compiler.AbstractCompiler2;
import nc.bs.sc.m61.referred.rule.ic.ApproveSupplyRule;
import nc.bs.sc.m61.referred.rule.pm.SCOrderApprovePMSupplyRule;
import nc.bs.sc.m61.referred.rule.pm.SCOrderRewritePMStartDateRule;
import nc.bs.sc.plugin.SCOrderPluginPoint;
import nc.bs.scmpub.pf.PfParameterUtil;
import nc.bs.scmpub.rule.VOSagaFrozenValidateRule;
import nc.impl.pubapp.pattern.data.bill.BillUpdate;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.impl.sc.m61.action.approve.rule.*;
import nc.itf.sc.m61.compenstate.IScOrderPmSagasCompensate;
import nc.itf.sc.reference.ic.ATPServices;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.sc.m61.entity.SCOrderVO;
import nc.vo.sc.m61.entity.context.SCOrderContxt;
import nc.vo.sc.pub.enumeration.SCBillStatus;
import nc.vo.scmpub.msg.rule.UpdateMsgStatusRule;
import nc.vo.scmpub.res.billtype.SCBillType;
import nc.vo.scmpub.util.AppInfoContext;
import nccloud.commons.lang.ArrayUtils;
import nccloud.pubitf.pu.pub.util.PuSagasUtil;
import nccloud.pubitf.sc.pub.util.SCOperationEnum;
import nccloud.pubitf.sc.pub.util.SCSagasOperationEnum;
import nccloud.pubitf.sc.pub.util.ScSagasUtil;
import nc.bs.sc.m61.referred.rule.pm.AfterApproceRuleSyncRZWMSProcess;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class SCOrderApproveAction {

    private void addRule(CompareAroundProcesser<SCOrderVO> prcr) {
        prcr.addBeforeRule(new VOSagaFrozenValidateRule(true));
        prcr.addBeforeFinalRule(new ApproveStatusChkRule());
        prcr.addBeforeFinalRule(new SupplierFrozeChkRule());
        prcr.addBeforeFinalRule(new UpdateMsgStatusRule("pk_order"));
        prcr.addBeforeRule(new ApproveFlowCheckMnyRule());
        prcr.addAfterFinalRule(new UpdatePFlowInfoRule());
        prcr.addAfterRule(new FilterOrderByStatusRule(SCBillStatus.APPROVED.toInt()));
        prcr.addAfterRule(new SCOrderRewritePMStartDateRule(true));
        prcr.addAfterRule(new ApproveSupplyRule());
        prcr.addAfterRule(new SCOrderApprovePMSupplyRule());

        //2025年5月18日15点12分-- 互感器委外订单审批推送锐制系统
        prcr.addAfterRule(new AfterApproceRuleSyncRZWMSProcess());
    }

    public SCOrderVO[] approve(SCOrderVO[] vos, SCOrderContxt contxt, AbstractCompiler2 script) {
        if (ArrayUtils.isEmpty(vos)) {
            return vos;
        } else {
            PfParameterUtil<SCOrderVO> util = new PfParameterUtil(script == null ? null : script.getPfParameterVO(), vos);
            SCOrderVO[] originBills = (SCOrderVO[]) util.getOrginBills();
            SCOrderVO[] clientBills = (SCOrderVO[]) util.getClientFullInfoBill();
            CompareAroundProcesser<SCOrderVO> prcr = new CompareAroundProcesser(SCOrderPluginPoint.APPROVE);
            this.addRule(prcr);
            prcr.before(clientBills, originBills);
            if (null != script) {
                try {
                    script.procFlowBacth(script.getPfParameterVO());
                } catch (Exception e) {
                    ExceptionUtils.wrappException(e);
                }
            }

            this.atpBeforeUpdate(clientBills);

            try {
                AppInfoContext.setProductCode(SCSagasOperationEnum.OREDERAPPROVE.getResCommon());
                AppInfoContext.setResId(SCSagasOperationEnum.OREDERAPPROVE.getResId());
                ScSagasUtil.frozenAndAddSaga(vos, "scorderApprove", SCBillType.Order.getCode());
                Map<String, Serializable> paramMap = new HashMap();
                paramMap.put("actionname", "scorderApprove");
                paramMap.put("hid", originBills[0].getParentVO().getPrimaryKey());
                paramMap.put(SCOperationEnum.APPROVER, originBills[0].getParentVO().getApprover());
                paramMap.put(SCOperationEnum.TAUDITTIME, originBills[0].getParentVO().getTaudittime());
                paramMap.put(SCOperationEnum.FBILLSTATUS, originBills[0].getParentVO().getFstatusflag());
                PuSagasUtil.compensate(IScOrderPmSagasCompensate.class, paramMap);
            } catch (BusinessException e) {
                ExceptionUtils.wrappException(e);
            }

            BillUpdate<SCOrderVO> update = new BillUpdate();
            SCOrderVO[] updateVOs = (SCOrderVO[]) update.update(clientBills, originBills);
            prcr.after(clientBills, originBills);
            this.atpAfterUpdate(updateVOs);
            return updateVOs;
        }
    }

    private void atpAfterUpdate(SCOrderVO[] updateVOs) {
        ATPServices.modifyATPAfter(SCBillType.Order.getCode(), updateVOs);
    }

    private void atpBeforeUpdate(SCOrderVO[] clientBills) {
        ATPServices.modifyATPBefore(SCBillType.Order.getCode(), clientBills);
    }

}
