//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.bs.pu.m20.maintain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.bs.pu.m20.maintain.rule.ATPAfterUpdateRule;
import nc.bs.pu.m20.maintain.rule.ATPBeforeUpdateRule;
import nc.bs.pu.m20.maintain.rule.ReturnBillCodeRule;
import nc.bs.pu.m20.maintain.rule.delete.*;
import nc.bs.pu.m20.plugin.PraybillPluginPoint;
import nc.bs.scmpub.rule.VOSagaFrozenValidateRule;
import nc.impl.pubapp.pattern.data.bill.BillDelete;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.itf.pu.m20.compenstate.IBuyingreqM4A08SagasCompensate;
import nc.vo.pu.m20.entity.PraybillItemVO;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pu.m20.enumeration.EnumOperate;
import nc.vo.pu.pub.rule.WriteBack422xRule;
import nc.vo.pu.pub.rule.WriteBackPIMRule;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.scmpub.res.billtype.POBillType;
import nccloud.commons.lang.ArrayUtils;
import nccloud.pubitf.pu.pub.util.PuSagasUtil;

public class PraybillDeleteBP {
    public PraybillDeleteBP() {
    }

    public void delete(PraybillVO[] vos) {
        CompareAroundProcesser<PraybillVO> processer = new CompareAroundProcesser(PraybillPluginPoint.DELETE);
        this.addBeforeRule(processer);
        this.addAfterRule(processer);
        processer.before(vos, vos);
        this.addSagaAndCompensate(vos);
        BillDelete<PraybillVO> bo = new BillDelete();
        bo.delete(vos);
        processer.after(vos, vos);
    }

    private void addAfterRule(CompareAroundProcesser<PraybillVO> processer) {
        processer.addAfterRule(new DelOldVersionRule());
        processer.addAfterRule(new PraybillWriteBackSourceRule(EnumOperate.DELETE));
        processer.addAfterRule(new ATPAfterUpdateRule());
        processer.addAfterRule(new ReturnBillCodeRule());
        processer.addAfterRule(new WriteBackPIMRule(POBillType.PrayBill.getCode()));
        processer.addAfterRule(new WriteBack422xRule(POBillType.PrayBill.getCode()));
        processer.addAfterRule(new DeleteAfterEventRule());

        processer.addAfterRule(new UpdatePickmRule());
    }

    private void addBeforeRule(CompareAroundProcesser<PraybillVO> processer) {
        processer.addBeforeRule(new VOSagaFrozenValidateRule(true));
        processer.addBeforeFinalRule(new BillStatusRule());
        processer.addBeforeFinalRule(new CheckSrcRule());
        processer.addBeforeRule(new ATPBeforeUpdateRule());
        processer.addBeforeRule(new DeleteBeforeEventRule());
    }

    private void addSagaAndCompensate(PraybillVO[] vos) {
        try {
            PuSagasUtil.frozenAndAddSaga(vos, POBillType.PrayBill.getCode());
            List<String> hList = new ArrayList();
            List<String> bList = new ArrayList();

            for(PraybillVO praybillVO : vos) {
                PraybillItemVO[] praybillItemVOs = praybillVO.getBVO();
                if (!ArrayUtils.isEmpty(praybillItemVOs)) {
                    hList.add(praybillVO.getHVO().getPk_praybill());

                    for(PraybillItemVO praybillItemVO : praybillItemVOs) {
                        bList.add(praybillItemVO.getPk_praybill_b());
                    }
                }
            }

            Map<String, Serializable> map = new HashMap();
            map.put("opertaion", "praybillDelete");
            map.put("pk_praybill_b", bList.toArray(new String[bList.size()]));
            map.put("pk_praybill", hList.toArray(new String[hList.size()]));
            PuSagasUtil.compensate(IBuyingreqM4A08SagasCompensate.class, map);
        } catch (BusinessException e) {
            ExceptionUtils.wrappException(e);
        }

    }
}
