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

package nc.bs.mmpac.pickm.rule;


import nc.impl.pubapp.pattern.rule.IRule;
import nc.itf.ic.m4a.IGeneralInMaintain;
import nc.itf.pu.m20.IPraybillMaintain;
import nc.itf.uap.pf.busiflow.PfButtonClickContext;
import nc.util.mmf.busi.service.PFPubService;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.pu.m20.entity.PraybillHeaderVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pu.m20.entity.PraybillVO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.service.ServiceLocator;

import java.util.HashMap;
import java.util.Map;

public class ToBuyingreqRule implements IRule<AggPickmVO> {
    public ToBuyingreqRule() {
    }

    public void process(AggPickmVO[] aggVOs) {
        if (!MMValueCheck.isEmpty(aggVOs)) {
            try {
                PraybillVO[] prayVOs = convertToGeneralInVO(aggVOs, null);
                IPraybillMaintain maintain= ServiceLocator.find(IPraybillMaintain.class);
                PraybillVO[] insertVos = maintain.insert(prayVOs);
                Map<String, Object> returnMap = new HashMap<>();
                returnMap.put("data", insertVos);
                returnMap.put("success", true);
            } catch (BusinessException e) {
                throw new RuntimeException(e);
            }

        }
    }
    /**
     * 方法功能描述：批量执行VO转换，若来源及目标单据类型没有注册或传入来源单据不能转换，则抛出业务异常
     *
     * @param sourceBillVOs AggregatedValueObject[]型参数，来源单据VO数组
     * @param srcParaVo     PfParameterVO型参数，平台的运行参数类，一般为NULL即可
     * @return AggregatedValueObject[]型VO转换后的目标单据数组
     * @throws BusinessException 若VO转换失败，则抛出业务异常
     */
    private PraybillVO[] convertToGeneralInVO(AggregatedValueObject[] sourceBillVOs, PfParameterVO srcParaVo)
            throws BusinessException {
        // 来源交易类型或单据类型，需要在单据类型中注册
        String srcBillOrTranstype = "55A3";
        // 目标交易类型或单据类型，需要在单据类型中注册
        String destBillOrTranstype = "20";
        // classifyMode int型参数，调用模式，参照PfButtonClickContext中定义，代码中调用为接口方式 ClassifyByItfdef
        int classifyMode = PfButtonClickContext.ClassifyByItfdef;
        PraybillVO[] inVOS = (PraybillVO[]) PFPubService.runChangeData(srcBillOrTranstype, destBillOrTranstype, sourceBillVOs, srcParaVo, classifyMode);
        if (MMValueCheck.isEmpty(inVOS)) {
            ExceptionUtils.wrapBusinessException("备料计划转换请购单失败");
        }
        for (PraybillVO inVO : inVOS) {
            // 其它入库单的仓库、交易类型取自【业务参数设置】的默认值
            PraybillHeaderVO head = inVO.getHVO();
            head.setCtrantypeid("0001A110000000001S1E");
            // head.setCwarehouseid("");
        }
        return inVOS;
    }
}
