package nc.bs.ic.m4d.insert.rule;

import nc.bs.dao.BaseDAO;
import nc.bs.ic.pub.base.ICRule;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.ic.general.define.ICBillBodyVO;
import nc.vo.ic.general.define.ICBillVO;
import nc.vo.ic.m4d.entity.MaterialOutVO;
import nc.vo.pub.BusinessException;
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 MaterialOutProjectRule
 * @Description TODO
 * @Version 1.0.0
 * @Date 2025/6/13 14:59
 * @Created by ame
 */
public class MaterialOutProjectRule extends ICRule<MaterialOutVO> {
    private static BaseDAO dao=new BaseDAO();
    @Override
    public void process(MaterialOutVO[] vos) {
        if(vos==null || vos.length<1){
            return;
        }
        try {
            //获取表头计划组织及表体行物料
            for (ICBillVO vo : vos) {
                String pk_org = (String) vo.getParentVO().getAttributeValue("pk_org");
                ICBillBodyVO[] bodys = vo.getBodys();
                Map<String,String> material_construct = new HashMap<>();
                Map<String,String> material_stock = new HashMap<>();
                queryFix2ByMaterial(bodys,material_construct,material_stock,pk_org);
                for(ICBillBodyVO body:bodys){
                    String cmaterialvid = body.getCmaterialvid();
                    if(material_construct.containsKey(cmaterialvid)&& "Y".equals(material_construct.get(cmaterialvid))&& (!material_stock.containsKey(cmaterialvid)|| "N".equals(material_stock.get(cmaterialvid)))){
                        body.setCprojectid(null);
                    }
                }
            }
        } catch (BusinessException var6) {
            ExceptionUtils.wrappException(var6);
        }
    }
    private void queryFix2ByMaterial(ICBillBodyVO[] childrenVOs, Map<String, String> materialConstruct, Map<String, String> materialStock, String pkOrg) throws BusinessException {
        List<String> materials=new ArrayList<>();

        for (ICBillBodyVO 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);
        }
    }

}
