package nc.bs.bd.bom.bom0202.rule;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.bd.bom.bom0202.entity.AggBomVO;
import nc.vo.bd.bom.bom0202.entity.BomItemVO;
import nc.vo.org.OrgVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.SqlBuilder;

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

/**
 * @Classname BOMBprojectmaterialEnableRule
 * @Description TODO
 * @Version 1.0.0
 * @Date 2025/6/13 9:43
 * @Created by ame
 */
public class BOMBprojectmaterialEnableRule implements IRule<AggBomVO> {
    private static BaseDAO dao = new BaseDAO();

    @Override
    public void process(AggBomVO[] vos) {
        if (!MMValueCheck.isEmpty(vos)) {
            try {
                //获取表头计划组织及表体行物料
                for (AggBomVO vo : vos) {
                    String pk_org = (String) vo.getParentVO().getAttributeValue("pk_org");
                    BomItemVO[] childrenVOs = vo.getChildrenVO();
                    Map<String,String> material_construct = new HashMap<>();
                    Map<String,String> material_stock = new HashMap<>();
                    queryFix2ByMaterial(childrenVOs,material_construct,material_stock,pk_org);
                    for(BomItemVO bomItemVO:childrenVOs){
                        String cmaterialvid = bomItemVO.getCmaterialvid();
                        if(material_construct.containsKey(cmaterialvid)&& "Y".equals(material_construct.get(cmaterialvid))&& material_stock.containsKey(cmaterialvid)&& "Y".equals(material_stock.get(cmaterialvid))){
                            bomItemVO.setBprojectmaterial(UFBoolean.TRUE);
                        }
                    }
                }
            } catch (BusinessException var6) {
                ExceptionUtils.wrappException(var6);
            }
        }
    }



    private void queryFix2ByMaterial(BomItemVO[] childrenVOs, Map<String, String> materialConstruct, Map<String, String> materialStock, String pkOrg) throws BusinessException {
        List<String> materials=new ArrayList<>();

        for (BomItemVO childrenVO : childrenVOs){
            String cmaterialvid = childrenVO.getCmaterialvid();
            materials.add(cmaterialvid);
        }
        SqlBuilder sql =new SqlBuilder();
        sql.append(" SELECT bdm.pk_material ,bdmf.fix2,bdms.fixasst2 ");
        sql.append(" FROM bd_material bdm ");
        sql.append(" LEFT JOIN bd_marasstframe bdmf ON bdm.pk_marasstframe =bdmf.pk_marasstframe ");
        sql.append(" LEFT JOIN bd_materialstock bdms ON bdms.PK_MATERIAL =bdm.PK_MATERIAL AND bdms.PK_ORG ='"+pkOrg+"' ");
        sql.append(" where ");
        sql.append("bdm.pk_material",materials.toArray(new String[0]));
        List<Map<String,Object>> remain = (List<Map<String, Object>>) dao.executeQuery(sql.toString(), new MapListProcessor());
        if(remain==null || remain.isEmpty()){
            throw new BusinessException("物料查询辅助属性结构失败,sql【"+sql+"】");
        }
        for (Map<String,Object> map:remain){
            String fix2=map.get("fix2")==null?"N":map.get("fix2").toString();
            String fixasst2=map.get("fixasst2")==null?"N":map.get("fixasst2").toString();
            materialConstruct.put(map.get("pk_material").toString(),fix2);
            materialStock.put(map.get("pk_material").toString(),fixasst2);
        }
    }

}
