package nc.impl.bd.material.baseinfo;

import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.businessevent.bd.BDCommonEvent;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.vo.bd.material.MaterialVO;
import nc.vo.bd.material.stock.MaterialStockVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.util.BDPKLockUtil;

import java.util.ArrayList;
import java.util.List;

public class MaterialStockAsstsChangedListener implements IBusinessListener {
    private BaseDAO baseDAO = new BaseDAO();
    public void doAction(IBusinessEvent iBusinessEvent) throws BusinessException {
        if (iBusinessEvent instanceof BDCommonEvent event) {
            //1、库存信息修改生产厂商和项目后 更新计划信息，生产信息和成本信息
            this.dealMaterialStockAssts1(event, false);
        }
    }
    private void dealMaterialStockAssts1(BDCommonEvent event, boolean isBatchUp) throws BusinessException {
        for(int i = 0; i < event.getNewObjs().length; ++i) {
            MaterialStockVO newVO = (MaterialStockVO)event.getNewObjs()[i];
            String pk_material = newVO.getPk_material();
            String pk_marasstframe = this.getmarasstframe(pk_material).get(0);
            if (!isBatchUp) {
                BDPKLockUtil.lockString(new String[]{pk_material});
            }
            if(pk_marasstframe == null){
                return;
            }
            //如果库存信息生产厂商和项目被选中则 更新计划信息，生产信息和成本信息
//            if(newVO.getFixasst2() == null ){
//                newVO.setFixasst2(UFBoolean.valueOf("N"));
//            }
//            if(newVO.getFixasst4() == null ){
//                newVO.setFixasst4(UFBoolean.valueOf("N"));
//            }
            UFBoolean fixasst2 = newVO.getFixasst2();
            UFBoolean fixasst4 = newVO.getFixasst4();

            List<String> fixlist=new ArrayList();


            fixlist.add(fixasst2.toString());
            fixlist.add(fixasst4.toString());
//            if(fixasst2.booleanValue()){
//                fixlist.add("Y");
//            }
//            if(fixasst4.booleanValue()){
//                fixlist.add("Y");
//            }
//            if(fixlist.isEmpty()){
//                return;
//            }else{
                //成本
                this.updateMaterialCostUpdateAssts(fixlist,newVO.getPk_org(), pk_material);
                //生产信息
                this.updateMaterialProdAssts(fixlist,newVO.getPk_org(), pk_material);
                //更新计划信息
                this.updateMaterialPlanAssts(fixlist,newVO.getPk_org(), pk_material);
//            }

        }

    }



    private List<String> getmarasstframe(String materialVID) throws BusinessException {
        String sql = "select pk_marasstframe from " + MaterialVO.getDefaultTableName() + " where pk_material = ? ";
        SQLParameter param = new SQLParameter();
        param.addParam(materialVID);
        return (List)this.getBaseDAO().executeQuery(sql, param, new ColumnListProcessor());
    }

    private BaseDAO getBaseDAO() {
        if (this.baseDAO == null) {
            this.baseDAO = new BaseDAO();
        }

        return this.baseDAO;
    }
    private void updateMaterialCostUpdateAssts(List<String> fixlist,String pk_org, String pk_material) throws BusinessException {
        //查询成本信息
        String  sql = "SELECT " +
                " pk_materialcost " +
                "FROM " +
                " bd_materialcost " +
                "WHERE " +
                " (pk_org = (SELECT PK_COSTREGION FROM org_cr_stockorg WHERE pk_stockorg= ?) or pk_org in (SELECT PK_COSTREGION FROM org_cr_stockstore WHERE pk_stockorg= ? ) )  " +
                " AND " +
                " pk_material = ? ";
        SQLParameter param = new SQLParameter();
        param.addParam(pk_org);
        param.addParam(pk_org);
        param.addParam(pk_material);
        List list = ((List) this.getBaseDAO().executeQuery(sql, param, new ColumnListProcessor()));
        if(list.isEmpty()){
            return;
        }

// 构建用于 IN 子句的参数列表
        StringBuilder inClause = new StringBuilder();
        List<String> paramValues = new ArrayList<>();

        for (Object obj : list) {
            String pk_materialcost = obj.toString();
            if (pk_materialcost == null) {
                continue; // 如果 pk_materialcost 为空，则跳过当前循环
            }
            inClause.append("?, ");
            paramValues.add(pk_materialcost);
        }

        if (paramValues.isEmpty()) {
            return; // 如果没有有效的 pk_materialcost，则直接返回
        }

// 去掉最后一个多余的逗号
        inClause = new StringBuilder(inClause.substring(0, inClause.length() - 2));

// 更新计价方式
        String update24 = "UPDATE bd_materialcostmod SET marasst2 = ?, marasst4 = ? WHERE pk_materialcost IN (" + inClause + ")";
        SQLParameter param1 = new SQLParameter();

// 添加更新参数
        param1.addParam(fixlist.get(0));  // 假设 fixlist 已经正确初始化且包含至少两个元素
        param1.addParam(fixlist.get(1));

// 添加 IN 子句的参数
        for (String value : paramValues) {
            param1.addParam(value);
        }

        this.getBaseDAO().executeUpdate(update24, param1);

    }

    private void updateMaterialProdAssts(List<String> fixlist,String pk_org, String pk_material) throws DAOException {
        //查询成本信息
        String  sql = "SELECT " +
                " pk_materialprod " +
                "FROM " +
                " bd_materialprod " +
                "WHERE " +
                "    pk_org = ? and " +
                " pk_material = ?  ";
        SQLParameter param = new SQLParameter();
        param.addParam(pk_org);
        param.addParam(pk_material);
        List list =( (List) this.getBaseDAO().executeQuery(sql, param, new ColumnListProcessor()));
        if(list.isEmpty()){
            return;
        }
        String pk_materialcost = list.get(0).toString();
        if(pk_materialcost == null){
            return;
        }
        //更新计价方式
        String update2 = "update bd_materialprod set costvalutasst2 = ? where pk_materialprod = ? ";
        String update4 = "update bd_materialprod set costvalutasst4 = ? where pk_materialprod = ? ";
        String update24 = "update bd_materialprod set costvalutasst2 = ?,costvalutasst4 = ? where pk_materialprod = ? ";
        SQLParameter param1 = new SQLParameter();
        param1.addParam(fixlist.get(0));
        param1.addParam(fixlist.get(1));
        param1.addParam(pk_materialcost);
        this.getBaseDAO().executeUpdate(update24, param1);

    }
    private void updateMaterialPlanAssts(List<String> fixlist,String pk_org, String pk_material) throws DAOException {
        //查询成本信息
        String  sql = "SELECT " +
                " pk_materialplan " +
                "FROM " +
                " bd_materialplan " +
                "WHERE " +
                "    pk_org = ? and " +
                " pk_material = ?  ";
        SQLParameter param = new SQLParameter();
        param.addParam(pk_org);
        param.addParam(pk_material);
        List list =( (List) this.getBaseDAO().executeQuery(sql, param, new ColumnListProcessor()));
        if(list.isEmpty()){
            return;
        }
        String pk_materialcost = list.get(0).toString();
        if(pk_materialcost == null){
            return;
        }
        //更新计价方式
        String update2 = "update bd_materialplan set marasst2 = ? where pk_materialplan = ? ";
        String update4 = "update bd_materialplan set marasst4 = ? where pk_materialplan = ? ";
        String update24 = "update bd_materialplan set marasst2 = ?,marasst4 = ? where pk_materialplan = ? ";
        SQLParameter param1 = new SQLParameter();
        param1.addParam(fixlist.get(0));
        param1.addParam(fixlist.get(1));
        param1.addParam(pk_materialcost);
        this.getBaseDAO().executeUpdate(update24, param1);

    }
}
