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

package nc.impl.sc.m61.action.maintain.rule;


import nc.bs.framework.common.NCLocator;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.itf.mmpac.pickm.IPickmMaintainService;
import nc.itf.sc.m61.compenstate.IScOrderSagasCompensate;
import nc.itf.sc.reference.mm.PubMMPACService;
import nc.itf.scmpub.reference.uap.pf.PfServiceScmUtil;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpac.pickm.entity.PickmHeadVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.sc.m61.entity.SCOrderItemVO;
import nc.vo.sc.m61.entity.SCOrderVO;
import nc.vo.scmpub.res.billtype.MMBillType;
import nc.vo.scmpub.res.billtype.SCBillType;
import nc.vo.scmpub.util.ArrayUtil;
import nccloud.commons.lang.ArrayUtils;
import nccloud.pubitf.sc.pub.util.ScSagasUtil;

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

public class SCOrderPushMoRule implements IRule<SCOrderVO> {
    public SCOrderPushMoRule() {
    }

    public void process(SCOrderVO[] vos) {
        if (!ArrayUtils.isEmpty(vos)) {
            try {
                // 分组处理逻辑
                List<SCOrderVO> specialList = new ArrayList<>();
                List<SCOrderVO> normalList = new ArrayList<>();
                for (SCOrderVO vo : vos) {
                    // 假设通过 getBilltype() 获取单据类型，请根据实际字段调整
                    if ("61-Cxx-01".equals(vo.getParentVO().getVtrantypecode()) ){
                        specialList.add(vo);
                    } else {
                        normalList.add(vo);
                    }
                }

                List<PickmHeadVO> headList = new ArrayList<>();
                // 特殊类型处理
                if (!specialList.isEmpty()) {
                    AggPickmVO[] specialPickVos = (AggPickmVO[]) PfServiceScmUtil.exeVOChangeByBillItfDef(
                            "61-Cxx-01", // 特殊类型编码
                            MMBillType.PickMo.getCode(),
                            vos);
                    IPickmMaintainService ipickmMaintainService = NCLocator.getInstance().lookup(IPickmMaintainService.class);
                    ipickmMaintainService.insert(specialPickVos);
                }

                // 常规处理
                if (!normalList.isEmpty()) {
                    AggPickmVO[] normalPickVos = (AggPickmVO[]) PfServiceScmUtil.exeVOChangeByBillItfDef(
                            SCBillType.Order.getCode(),
                            MMBillType.PickMo.getCode(),
                            normalList.toArray(new SCOrderVO[0]));
                    for(AggPickmVO vo : normalPickVos) {
                        PickmHeadVO head = (PickmHeadVO)vo.getParent();
                        headList.add(head);
                    }
                }
                if (headList.size() > 0) {
                    this.addSagaAndCompensate(vos);
                    PubMMPACService.pushMMPac((PickmHeadVO[])headList.toArray(new PickmHeadVO[headList.size()]));
                }
            } catch (BusinessException e) {
                ExceptionUtils.wrappException(e);
            }

        }
    }

    private void addSagaAndCompensate(SCOrderVO[] vos) {
        ScSagasUtil.frozenAndAddSaga(vos, "pickMo", SCBillType.Order.getCode());
        if (!ArrayUtil.isEmpty(vos)) {
            Map<String, Serializable> map = new HashMap();
            map.put("opertaion", "pickMo");
            List<String> bids = new ArrayList();

            for(SCOrderVO scOrderVO : vos) {
                SCOrderItemVO[] itemVOs = scOrderVO.getChildrenVO();

                for(SCOrderItemVO bvo : itemVOs) {
                    bids.add(bvo.getPk_order_b());
                }
            }

            map.put("pk_order_b", bids.toArray(new String[0]));

            try {
                ScSagasUtil.compensate(IScOrderSagasCompensate.class, map);
            } catch (BusinessException e) {
                ExceptionUtils.wrappException(e);
            }
        }

    }
}
