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;
            }
            //如果库存信息生产厂商和项目被选中则 更新计划信息，生产信息和成本信息
            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= ?) " +
                " 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()));
        String pk_materialcost = list.get(0).toString();
        if(pk_materialcost == null){
            return;
        }
        //更新计价方式
//        String update2 = "update bd_materialcostmod set marasst2 = ? where pk_materialcost = ? ";
//        String update4 = "update bd_materialcostmod set marasst4 = ? where pk_materialcost = ? ";
        String update24 = "update bd_materialcostmod set marasst2 = ?,marasst4 = ? where pk_materialcost = ? ";
//        if(fixlist.contains("2") && fixlist.contains("4")){
            SQLParameter param1 = new SQLParameter();
            param1.addParam(fixlist.get(0));
            param1.addParam(fixlist.get(1));
            param1.addParam(pk_materialcost);
            this.getBaseDAO().executeUpdate(update24, param1);
//        }else if(fixlist.contains("2")){
//            SQLParameter param1 = new SQLParameter();
//            param1.addParam("Y");
//            param1.addParam(pk_materialcost);
//            this.getBaseDAO().executeUpdate(update2, param1);
//        }else if(fixlist.contains("4")){
//            SQLParameter param1 = new SQLParameter();
//            param1.addParam("Y");
//            param1.addParam(pk_materialcost);
//            this.getBaseDAO().executeUpdate(update4, 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()));
        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()));
        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);

    }
}
