package nc.bs.mmpac.pmo.pac0002.bp.rule;

import nc.bs.logging.Logger;
import nc.bs.uapbd.util.MyHelper;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.vo.bd.bom.bom0202.entity.BomVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOAggVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOHeadVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOItemVO;
import nc.vo.org.FactoryVO;
import nc.vo.pub.BusinessException;
import nc.vo.am.common.util.StringUtils;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

import java.util.Map;

/**
 * 流程生产订单赋值生产BOM字段
 *
 * @author mzr
 * @date 2025/7/15
 */
public class BeforePmoBomRule implements IRule<PMOAggVO> {

    @Override
    public void process(PMOAggVO[] vos) {
        try {
            Map<String, String> configParams = MyHelper.getConfigParams("xb-config", null);
            if (configParams.isEmpty()) {
                return;
            }
            for (PMOAggVO vo : vos) {
                PMOHeadVO headVO = vo.getParentVO();
                String pkOrg = headVO.getPk_org();

                String orgCode = MyHelper.getStrValByCondition(FactoryVO.getDefaultTableName(), FactoryVO.CODE,
                        FactoryVO.PK_FACTORY + " = '" + pkOrg + "'");
                // 只有箱变公司才赋值生产BOM字段
                if (checkIfOrg(orgCode, configParams)) {
                    continue;
                }
                // 箱变公司赋值生产BOM的相关字段
                PMOItemVO[] pmoItemVOS = vo.getChildrenVO();
                for (PMOItemVO pmoItemVO : pmoItemVOS) {
                    // vsrctype 来源单据类型 来源销售订单的需要赋值
                    if ("30".equals(pmoItemVO.getVsrctype())) {
                        // BOM状态 (FBomBillstatusEnum) 是否需要判断BOM的状态
                        // 根据bom版本号查询BOM(销售订单号+行号)
                        String bomVersion = pmoItemVO.getVsrccode() + pmoItemVO.getVsrcrowno();
                        String whereSql = BomVO.PK_ORG + " = '" + pkOrg + "' AND " + BomVO.HVERSION + " = '" + bomVersion + "'";
                        String bomId = MyHelper.getStrValByCondition(BomVO.TABLE_NAME, BomVO.CBOMID, whereSql);
                        if (StringUtils.isEmpty(bomId)) {
                            throw new BusinessException("未找到对应的BOM");
                        }
                        pmoItemVO.setVbomversion(bomVersion); // 生产BOM版本号
                        pmoItemVO.setCbomversionid(bomId); // 生产BOM版本
                    }
                }
            }
        } catch (BusinessException e) {
            Logger.error("BeforePmoBomRule-exp:" + e.getMessage(), e);
            ExceptionUtils.wrappException(e);
        }
    }

    private boolean checkIfOrg(String code, Map<String, String> configParams) throws BusinessException {
        String targetCode = configParams.get("xbOrg");
        if (targetCode == null || StringUtils.isEmpty(targetCode)) {
            throw new BusinessException("未配置组织参数");
        }
        String[] orgItem = targetCode.split(",");
        for (String orgCode : orgItem) {
            if (!orgCode.isEmpty() && orgCode.equals(code)) {
                return false;
            }
        }
        return true;
    }
}
