//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.impl.bd.material.baseinfo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import nc.bs.bd.cache.CacheProxy;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.businessevent.bd.BDCommonEvent;
import nc.bs.businessevent.bd.BDCommonEventUtil;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.logging.Logger;
import nc.impl.bd.material.prod.MaterialProdAsstFrameChangedListner;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.md.persist.framework.MDPersistenceService;
import nc.vo.bd.material.MaterialVO;
import nc.vo.bd.material.cost.MaterialCostVO;
import nc.vo.bd.material.cost.MaterialCostmodeVO;
import nc.vo.bd.material.marassistant.MarAssistantVO;
import nc.vo.bd.material.plan.MaterialPlanVO;
import nc.vo.bd.material.prod.MaterialProdVO;
import nc.vo.bd.material.stock.MaterialStockVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.trade.sqlutil.IInSqlBatchCallBack;
import nc.vo.trade.sqlutil.InSqlBatchCaller;
import nc.vo.util.BDPKLockUtil;
import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;

public class MaterialAsstFrame1ChangedListener implements IBusinessListener {
    private BaseDAO baseDAO = new BaseDAO();
    private static final String FIXPRECODE = "fixasst";
    private static final String FREEPRECODE = "freeasst";
    private IMDPersistenceQueryService mdPersistenceQueryService = null;


    public MaterialAsstFrame1ChangedListener() {
    }

    public void doAction(IBusinessEvent e) throws BusinessException {
        if (e instanceof BDCommonEvent event) {
            if ("c7dc0ccd-8872-4eee-8882-160e8f49dfad".equals(event.getSourceID())) {
                if ("1004".equals(event.getEventType()) ) {
                    this.dealMaterialStockAssts(event, false);//库存信息分配后
                    this.dealMaterialPlanUpdateAssts(event, false);//计划信息分配后
//                    this.dealMaterialProdUpdateAssts(event, false);//生产信息
//                    this.dealMaterialCostAssts(event, false);//成本信息分配后

                } else if ("1061".equals(event.getEventType())) {
                    this.dealMaterialStockAssts(event, true);
                    this.dealMaterialPlanUpdateAssts(event, true);
                }
            }else if("8b7d49ab-7a65-4885-bcf1-7df89b84df4b".equals(event.getSourceID())){
                //计划信息分配后
                if ("1009".equals(event.getEventType()) ) {
                    this.dealMaterialPlanUpdateAssts1(event, false);
                }
            }else if("13636fc1-0215-4102-a11e-af4be73a05ab".equals(event.getSourceID())){
                //库存信息分配后
                if ("1009".equals(event.getEventType()) ) {
                    this.dealMaterialStockAssts1(event, false);
                }
            }else if("a3133495-8c94-47b4-83d4-0d32bd08ac82".equals(event.getSourceID())){
                //生产信息分配后
                if ("1009".equals(event.getEventType()) ) {
                    this.dealMaterialProdUpdateAssts1(event, false);
                }
            }else if("d0ff92c6-1059-46b4-97fd-ed8b3d0a85d9".equals(event.getSourceID())){
                //成本信息分配后
                if ("1009".equals(event.getEventType()) ) {
                   this.dealMaterialCostAssts1(event, false);
                }
            }
        }

    }

    private void dealMaterialStockAssts(BDCommonEvent event, boolean isBatchUp) throws BusinessException {
        for(int i = 0; i < event.getOldObjs().length; ++i) {
            MaterialVO oldVO = (MaterialVO)event.getOldObjs()[i];
            MaterialVO newVO = (MaterialVO)event.getNewObjs()[i];
            if (!StringUtils.equals(oldVO.getPk_marasstframe(), newVO.getPk_marasstframe())) {
                String pk_material = newVO.getPk_material();
                String pk_marasstframe = newVO.getPk_marasstframe();
                if (!isBatchUp) {
                    BDPKLockUtil.lockString(new String[]{pk_material});
                }
                if(pk_marasstframe == null){
                    continue;
                }
               //库存
                this.updateMaterialStockUpdateAssts(pk_marasstframe, pk_material);
                //成本
                this.updateMaterialCostUpdateAssts(pk_marasstframe, pk_material);
                //生产信息
                this.updateMaterialProdAssts(pk_marasstframe, pk_material);
            }
        }

    }

    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;
            }

            this.updateMaterialStockUpdateAssts(pk_marasstframe, pk_material);
        }

    }
    private void dealMaterialProdUpdateAssts1(BDCommonEvent event, boolean isBatchUp) throws BusinessException {
        for(int i = 0; i < event.getNewObjs().length; ++i) {
            MaterialProdVO newVO = (MaterialProdVO)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;
            }
            this.updateMaterialProdAssts(pk_marasstframe, pk_material);
        }

    }
    private void dealMaterialCostAssts1(BDCommonEvent event, boolean isBatchUp) throws BusinessException {
        for(int i = 0; i < event.getNewObjs().length; ++i) {
            MaterialCostVO newVO = (MaterialCostVO)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;
            }
            //生产信息
            this.updateMaterialCostUpdateAssts(pk_marasstframe, pk_material);

        }

    }




    private void updateMaterialStockUpdateAssts(String pk_marasstframe, String materialVID) throws BusinessException {
        List<String> pks = this.queryToUpdateStockPks(materialVID);
        if (!pks.isEmpty()) {
            this.updateMaterialStockAsstByPks1(this.getField(pk_marasstframe), pks);
        }
    }

    private List<String> queryToUpdateStockPks(String materialVID) throws BusinessException {
        String sql = "select pk_materialstock from " + MaterialStockVO.getDefaultTableName() + " where pk_material = ? ";
        SQLParameter param = new SQLParameter();
        param.addParam(materialVID);
        return (List)this.getBaseDAO().executeQuery(sql, param, new ColumnListProcessor());
    }
    private void updateMaterialStockAsstByPks1(Set<String> notAllowFields, List<String> pks) throws BusinessException {
        Map<String, MaterialStockVO> pk_stock_map = this.queryToUpdateVOs(pks);
        List<MaterialStockVO> newVOs = this.getUpdateStockVOs1(notAllowFields, pk_stock_map);
        if (!newVOs.isEmpty()) {
            MaterialStockVO[] vos = (MaterialStockVO[])newVOs.toArray(new MaterialStockVO[0]);
            MaterialStockVO[] oldVOs = this.getOldVOs(vos, pk_stock_map);
            BDCommonEventUtil eventUtil = new BDCommonEventUtil("13636fc1-0215-4102-a11e-af4be73a05ab");
            eventUtil.setOldObjs(oldVOs);
            eventUtil.dispatchUpdateBeforeEvent((Object[])vos);
            this.getBaseDAO().updateVOArray(vos, (String[])notAllowFields.toArray(new String[0]));
            CacheProxy.fireDataUpdated(MaterialStockVO.getDefaultTableName());
            eventUtil.dispatchUpdateAfterEvent((Object[])vos);
        }

    }

    private Map<String, MaterialStockVO> queryToUpdateVOs(List<String> pks) throws BusinessException {
        InSqlBatchCaller caller = new InSqlBatchCaller((String[])pks.toArray(new String[0]));

        try {
            return (Map)caller.execute(new IInSqlBatchCallBack() {
                Map<String, MaterialStockVO> pk_stock_map = new HashMap();

                public Object doWithInSql(String inSql) throws BusinessException, SQLException {
                    Collection<MaterialStockVO> col = MaterialAsstFrame1ChangedListener.this.getBaseDAO().retrieveByClause(MaterialStockVO.class, "pk_materialstock in " + inSql);
                    if (col != null) {
                        for(MaterialStockVO vo : col) {
                            this.pk_stock_map.put(vo.getPrimaryKey(), vo);
                        }
                    }

                    return this.pk_stock_map;
                }
            });
        } catch (SQLException e) {
            Logger.error(e.getMessage(), e);
            throw new BusinessException(e.getMessage(), e);
        }
    }

    private List<MaterialStockVO> getUpdateStockVOs1(Set<String> notAllowFields, Map<String, MaterialStockVO> pk_stock_map) {
        List<MaterialStockVO> newVOs = new ArrayList();
        if (pk_stock_map != null && !pk_stock_map.isEmpty()) {
            for(MaterialStockVO oldVO : pk_stock_map.values()) {
                boolean valueChanged = false;
                MaterialStockVO newVO = (MaterialStockVO)oldVO.clone();

                for(String field : notAllowFields) {
                    valueChanged = true;
                    newVO.setAttributeValue(field, UFBoolean.TRUE);
                }

                if (valueChanged) {
                    newVOs.add(newVO);
                }
            }

            return newVOs;
        } else {
            return newVOs;
        }
    }


    private MaterialStockVO[] getOldVOs(MaterialStockVO[] vos, Map<String, MaterialStockVO> pk_stock_map) {
        MaterialStockVO[] oldVOs = new MaterialStockVO[vos.length];

        for(int i = 0; i < vos.length; ++i) {
            oldVOs[i] = (MaterialStockVO)pk_stock_map.get(vos[i].getPrimaryKey());
        }

        return oldVOs;
    }
    private Set<String> getField(String pk_marasstframe) throws DAOException {
        List<String> codes = this.queryMarasstCodesByFrameID(pk_marasstframe);
        Set<String> allowFields = new HashSet();
        //如果项目被更改项目需要被默认选中
        for(int i = 2; i <= 16; ++i) {
            if (codes.contains(Integer.toString(i)) ) {
                allowFields.add(this.getAsstCodeByIndex(i));
            }
        }

        return allowFields;
    }

    public String getAsstCodeByIndex(int index) {
        if (Integer.parseInt("16") == index) {
            return "fixasst100";
        } else {
            return index >= 6 && index <= 15 ? "freeasst" + (index - 5) : "fixasst" + index;
        }
    }

    private List<String> queryMarasstCodesByFrameID(String pk_marasstframe) throws DAOException {
        String sql = "select code from " + MarAssistantVO.getDefaultTableName() + " where pk_marasstframe = ?";
        SQLParameter param = new SQLParameter();
        param.addParam(pk_marasstframe);
        List<String> codes = (List)this.getBaseDAO().executeQuery(sql, param, new ColumnListProcessor());
        return codes;
    }

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

        return this.baseDAO;
    }






















    private void dealMaterialPlanUpdateAssts(BDCommonEvent event, boolean isBatchUp) throws BusinessException {
        for(int i = 0; i < event.getOldObjs().length; ++i) {
            MaterialVO oldVO = (MaterialVO)event.getOldObjs()[i];
            MaterialVO newVO = (MaterialVO)event.getNewObjs()[i];
            if (!StringUtils.equals(oldVO.getPk_marasstframe(), newVO.getPk_marasstframe())) {
                String pk_material = newVO.getPk_material();
                String pk_marasstframe = newVO.getPk_marasstframe();
                if (!isBatchUp) {
                    BDPKLockUtil.lockString(new String[]{pk_material});
                }
                if(pk_marasstframe == null){
                    continue;
                }
                this.updateMaterialPlanAssts(pk_marasstframe, pk_material);
            }
        }

    }
    private void dealMaterialPlanUpdateAssts1(BDCommonEvent event, boolean isBatchUp) throws BusinessException {
        for(int i = 0; i < event.getNewObjs().length; ++i) {
            MaterialPlanVO newVO = (MaterialPlanVO)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;
            }
            this.updateMaterialPlanAssts(pk_marasstframe, pk_material);
        }

    }

    private void updateMaterialPlanAssts(String pk_marasstframe, String materialVID) throws BusinessException {
        List<String> pks = this.queryToUpdatePlanPks(materialVID);
        if (!pks.isEmpty()) {
            this.updateMaterialPlanAsstByPks(this.getField1(pk_marasstframe),  pks);
        }
    }

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

    private void updateMaterialPlanAsstByPks(Set<String> notAllowFields, List<String> pks) throws BusinessException {
        Map<String, MaterialPlanVO> pk_plan_map = this.queryToUpdateVOs1(pks);
        List<MaterialPlanVO> newVOs = this.getUpdatePlanVOs2(notAllowFields, pk_plan_map);
        if (!newVOs.isEmpty()) {
            MaterialPlanVO[] vos = (MaterialPlanVO[])newVOs.toArray(new MaterialPlanVO[0]);
            MaterialPlanVO[] oldVOs = this.getOldVOs1(vos, pk_plan_map);
            BDCommonEventUtil eventUtil = new BDCommonEventUtil("8b7d49ab-7a65-4885-bcf1-7df89b84df4b");
            eventUtil.setOldObjs(oldVOs);
            eventUtil.dispatchUpdateBeforeEvent((Object[])vos);
            this.getBaseDAO().updateVOArray(vos, (String[])notAllowFields.toArray(new String[0]));
            CacheProxy.fireDataUpdated(MaterialPlanVO.getDefaultTableName());
            eventUtil.dispatchUpdateAfterEvent((Object[])vos);
        }
    }
    private Map<String, MaterialPlanVO> queryToUpdateVOs1(List<String> pks) throws BusinessException {
        InSqlBatchCaller caller = new InSqlBatchCaller((String[])pks.toArray(new String[0]));

        try {
            return (Map)caller.execute(new IInSqlBatchCallBack() {
                Map<String, MaterialPlanVO> pk_plan_map = new HashMap();

                public Object doWithInSql(String inSql) throws BusinessException, SQLException {
                    Collection<MaterialPlanVO> col = MaterialAsstFrame1ChangedListener.this.getBaseDAO().retrieveByClause(MaterialPlanVO.class, "pk_materialplan in " + inSql);
                    if (col != null) {
                        for(MaterialPlanVO vo : col) {
                            this.pk_plan_map.put(vo.getPrimaryKey(), vo);
                        }
                    }

                    return this.pk_plan_map;
                }
            });
        } catch (SQLException e) {
            Logger.error(e.getMessage(), e);
            throw new BusinessException(e.getMessage(), e);
        }
    }
    private List<MaterialPlanVO> getUpdatePlanVOs2(Set<String> notAllowFields, Map<String, MaterialPlanVO> pk_plan_map) {
        List<MaterialPlanVO> newVOs = new ArrayList();
        if (pk_plan_map != null && !pk_plan_map.isEmpty()) {
            for(MaterialPlanVO oldVO : pk_plan_map.values()) {
                boolean valueChanged = false;
                MaterialPlanVO newVO = (MaterialPlanVO)oldVO.clone();

                for(String field : notAllowFields) {
                    valueChanged = true;
                    newVO.setAttributeValue(field, UFBoolean.TRUE);
                }

                if (valueChanged) {
                    newVOs.add(newVO);
                }
            }

            return newVOs;
        } else {
            return newVOs;
        }
    }
    private MaterialPlanVO[] getOldVOs1(MaterialPlanVO[] vos, Map<String, MaterialPlanVO> pk_plan_map) {
        MaterialPlanVO[] oldVOs = new MaterialPlanVO[vos.length];

        for(int i = 0; i < vos.length; ++i) {
            oldVOs[i] = (MaterialPlanVO)pk_plan_map.get(vos[i].getPrimaryKey());
        }

        return oldVOs;
    }
    private Set<String> getField1(String pk_marasstframe) throws DAOException {
        List<String> codes = this.queryMarasstCodesByFrameID(pk_marasstframe);
        Set<String> allowFields = new HashSet();
        //如果项目被更改项目需要被默认选中
        for(int i = 2; i <= 16; ++i) {
            String code = this.getNewCodeOfMarasst(i);
            if (codes.contains(code) ) {
                allowFields.add("marasst" + code);
            }
        }


        return allowFields;
    }
    private String getNewCodeOfMarasst(int i) {
        return i == 16 ? "100" : Integer.toString(i);
    }






    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 void updateMaterialCostUpdateAssts(String pk_marasstframe, String materialVID) throws BusinessException {
        List<MaterialCostVO> materialCostVOs = this.queryMarCostVOsByMaterialPK(materialVID);
        if (materialCostVOs != null && materialCostVOs.size() != 0) {
            Set<String> allowFields = this.getCostAsstField(pk_marasstframe);
            Set<String> notallowFields = this.getnotCostAsstField(pk_marasstframe);

            this.updateMaterialCostAsstByPks(notallowFields,allowFields, materialCostVOs);
        }
    }
    private List<MaterialCostVO> queryMarCostVOsByMaterialPK(String pk_material) throws BusinessException {
        List<MaterialCostVO> voList = new ArrayList();
        String wherePart = "pk_material = '" + pk_material + "'";
        Collection<MaterialCostVO> col = this.getMdPersistenceQueryService().queryBillOfVOByCondWithOrder(MaterialCostVO.class, wherePart, false, new String[]{"materialcostmode"}, (String[])null);
        if (col != null && col.size() > 0) {
            voList.addAll(col);
        }

        return voList;
    }
    public IMDPersistenceQueryService getMdPersistenceQueryService() {
        if (this.mdPersistenceQueryService == null) {
            this.mdPersistenceQueryService = MDPersistenceService.lookupPersistenceQueryService();
        }

        return this.mdPersistenceQueryService;
    }
    private Set<String> getCostAsstField(String pk_marasstframe) throws BusinessException {
        List<String> codes = this.queryMarasstCodesByFrameID(pk_marasstframe);
        Set<String> notAllowFields = new HashSet();

        for(int i = 2; i <= 16; ++i) {
            String code = this.getNewCodeOfMarasst(i);
            if (codes.contains(code)) {
                notAllowFields.add("marasst" + code);
            }
        }

        return notAllowFields;
    }
    private Set<String> getnotCostAsstField(String pk_marasstframe) throws BusinessException {
        List<String> codes = this.queryMarasstCodesByFrameID(pk_marasstframe);
        Set<String> notAllowFields = new HashSet();

        for(int i = 2; i <= 16; ++i) {
            String code = this.getNewCodeOfMarasst(i);
            if (!codes.contains(code)) {
                notAllowFields.add("marasst" + code);
            }
        }

        return notAllowFields;
    }
    private void updateMaterialCostAsstByPks(Set<String> notAllowFields,Set<String> allowFields,  List<MaterialCostVO> materialCostVOs) throws BusinessException {
        Map<String, MaterialCostVO> pk_cost_map = new HashMap();

        for(MaterialCostVO materialCostVO : materialCostVOs) {
            pk_cost_map.put(materialCostVO.getPrimaryKey(), materialCostVO);
        }

        String pk_group = InvocationInfoProxy.getInstance().getGroupId();
        List<MaterialCostVO> oldMaterialCostVOs = new ArrayList();
        List<MaterialCostmodeVO> oldMaterialCostmodeVOs = new ArrayList();

        for(MaterialCostVO oldCostVO : pk_cost_map.values()) {
            oldMaterialCostVOs.add(oldCostVO);
            MaterialCostmodeVO[] costmodeVOs = oldCostVO.getMaterialcostmode();
            if (!ArrayUtils.isEmpty(costmodeVOs)) {
                for(MaterialCostmodeVO oldCostmodeVO : costmodeVOs) {
//                    if (!oldCostmodeVO.getPk_costregion().equals(pk_group)) {
                        oldMaterialCostmodeVOs.add(oldCostmodeVO);
//                    }
                }
            }
        }

        List<MaterialCostmodeVO> newCostmodeVOs = this.valueChangedCostModeVOs(notAllowFields,allowFields, oldMaterialCostmodeVOs);
        if (!newCostmodeVOs.isEmpty()) {
            Map<String, List<MaterialCostmodeVO>> changedCostmodeMap = new HashMap();

            for(MaterialCostmodeVO maCostmodeVO : newCostmodeVOs) {
                List<MaterialCostmodeVO> list = (List)changedCostmodeMap.get(maCostmodeVO.getPk_materialcost());
                if (list != null && list.size() != 0) {
                    list.add(maCostmodeVO);
                    changedCostmodeMap.put(maCostmodeVO.getPk_materialcost(), list);
                } else {
                    list = new ArrayList();
                    list.add(maCostmodeVO);
                    changedCostmodeMap.put(maCostmodeVO.getPk_materialcost(), list);
                }
            }

            Set<String> pk_materialcosts = changedCostmodeMap.keySet();
            List<MaterialCostVO> newMaterialCostVOs = new ArrayList();
            List<MaterialCostmodeVO> newMaterialCostmodeVOs = new ArrayList();

            for(String pk_materialcost : pk_materialcosts) {
                MaterialCostVO newCostVO = (MaterialCostVO)pk_cost_map.get(pk_materialcost);
                newMaterialCostVOs.add(newCostVO);
            }

            for(List<MaterialCostmodeVO> list : changedCostmodeMap.values()) {
                newMaterialCostmodeVOs.addAll(list);
            }

            this.dispatchUpdateUpdateEvent(oldMaterialCostVOs, oldMaterialCostmodeVOs, newMaterialCostVOs, newMaterialCostmodeVOs, notAllowFields,allowFields);
        }
    }
    private List<MaterialCostmodeVO> valueChangedCostModeVOs(Set<String> notAllowFields,Set<String> allowFields, List<MaterialCostmodeVO> costmodeVOs) {
        List<MaterialCostmodeVO> newCostmodeVOs = new ArrayList();

        for(MaterialCostmodeVO oldcCostmodeVO : costmodeVOs) {
            boolean valueChanged = false;
            MaterialCostmodeVO newcCostmodeVO = (MaterialCostmodeVO)oldcCostmodeVO.clone();

            for(String field : allowFields) {
                valueChanged = true;
                newcCostmodeVO.setAttributeValue(field, UFBoolean.TRUE);
            }
            for(String field : notAllowFields) {
                valueChanged = true;
                newcCostmodeVO.setAttributeValue(field, UFBoolean.FALSE);
            }

            if (valueChanged) {
                newCostmodeVOs.add(newcCostmodeVO);
            }
        }

        return newCostmodeVOs;
    }
    private void dispatchUpdateUpdateEvent(List<MaterialCostVO> oldMaterialCostVOs, List<MaterialCostmodeVO> oldMaterialCostmodeVOs, List<MaterialCostVO> newMaterialCostVOs, List<MaterialCostmodeVO> newMaterialCostmodeVOs, Set<String> notAllowFields, Set<String> allowFields) throws BusinessException {
        BDCommonEventUtil costeventUtil = new BDCommonEventUtil("d0ff92c6-1059-46b4-97fd-ed8b3d0a85d9");
        BDCommonEventUtil costmodeeventUtil = new BDCommonEventUtil("bc230e0a-68c1-4cf9-bd78-58e55953596b");
        costeventUtil.setOldObjs(oldMaterialCostVOs.toArray(new MaterialCostVO[0]));
        costeventUtil.dispatchUpdateBeforeEvent(oldMaterialCostVOs.toArray(new MaterialCostVO[0]));
        costmodeeventUtil.setOldObjs(oldMaterialCostmodeVOs.toArray(new MaterialCostmodeVO[0]));
        costmodeeventUtil.dispatchUpdateBeforeEvent(oldMaterialCostmodeVOs.toArray(new MaterialCostmodeVO[0]));
        this.getBaseDAO().updateVOArray((SuperVO[])newMaterialCostVOs.toArray(new MaterialCostVO[0]));
        CacheProxy.fireDataUpdated(MaterialCostVO.getDefaultTableName());
        this.getBaseDAO().updateVOArray((SuperVO[])newMaterialCostmodeVOs.toArray(new MaterialCostmodeVO[0]), (String[])notAllowFields.toArray(new String[0]));
        this.getBaseDAO().updateVOArray((SuperVO[])newMaterialCostmodeVOs.toArray(new MaterialCostmodeVO[0]), (String[])allowFields.toArray(new String[0]));

        CacheProxy.fireDataUpdated(MaterialCostmodeVO.getDefaultTableName());
        costeventUtil.dispatchUpdateAfterEvent(newMaterialCostVOs.toArray(new MaterialCostVO[0]));
        costmodeeventUtil.dispatchUpdateAfterEvent(newMaterialCostmodeVOs.toArray(new MaterialCostmodeVO[0]));
    }






    private void updateMaterialProdAssts(String pk_marasstframe, String materialVID) throws BusinessException {
        List<String> pks = this.queryToUpdateProdPks(materialVID);
        if (!pks.isEmpty()) {
            Set<String> notAllowFields = this.getNotProdAsstField(pk_marasstframe);
            this.updateMaterialProdAsstByPks(notAllowFields, pks);
        }
    }

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

    private void updateMaterialProdAsstByPks(Set<String> notAllowFields, List<String> pks) throws BusinessException {
        Map<String, MaterialProdVO> pk_prod_map = this.queryToUpdateVOsProd(pks);
        List<MaterialProdVO> newVOs = this.getUpdateProdVOs(notAllowFields, pk_prod_map);
        if (!newVOs.isEmpty()) {
            MaterialProdVO[] vos = (MaterialProdVO[])newVOs.toArray(new MaterialProdVO[0]);
            MaterialProdVO[] oldVOs = this.getOldVOs(vos, pk_prod_map);
            BDCommonEventUtil eventUtil = new BDCommonEventUtil("a3133495-8c94-47b4-83d4-0d32bd08ac82");
            eventUtil.setOldObjs(oldVOs);
            eventUtil.dispatchUpdateBeforeEvent((Object[])vos);
            this.getBaseDAO().updateVOArray(vos, (String[])notAllowFields.toArray(new String[0]));
            CacheProxy.fireDataUpdated(MaterialProdVO.getDefaultTableName());
            eventUtil.dispatchUpdateAfterEvent((Object[])vos);
        }
    }

    private Map<String, MaterialProdVO> queryToUpdateVOsProd(List<String> pks) throws BusinessException {
        InSqlBatchCaller caller = new InSqlBatchCaller((String[])pks.toArray(new String[0]));

        try {
            return (Map)caller.execute(new IInSqlBatchCallBack() {
                Map<String, MaterialProdVO> pk_prod_map = new HashMap();

                public Object doWithInSql(String inSql) throws BusinessException, SQLException {
                    Collection<MaterialProdVO> col = MaterialAsstFrame1ChangedListener.this.getBaseDAO().retrieveByClause(MaterialProdVO.class, "pk_materialprod in " + inSql);
                    if (col != null) {
                        for(MaterialProdVO vo : col) {
                            this.pk_prod_map.put(vo.getPrimaryKey(), vo);
                        }
                    }

                    return this.pk_prod_map;
                }
            });
        } catch (SQLException e) {
            Logger.error(e.getMessage(), e);
            throw new BusinessException(e.getMessage(), e);
        }
    }

    private List<MaterialProdVO> getUpdateProdVOs(Set<String> notAllowFields, Map<String, MaterialProdVO> pk_prod_map) {
        List<MaterialProdVO> newVOs = new ArrayList();
        if (pk_prod_map != null && !pk_prod_map.isEmpty()) {
            for(MaterialProdVO oldVO : pk_prod_map.values()) {
                boolean valueChanged = false;
                MaterialProdVO newVO = (MaterialProdVO)oldVO.clone();

                for(String field : notAllowFields) {
//                    if (UFBoolean.TRUE.equals(oldVO.getAttributeValue(field))) {
                        valueChanged = true;
                        newVO.setAttributeValue(field, UFBoolean.TRUE);
//                    }
                }

                if (valueChanged) {
                    newVOs.add(newVO);
                }
            }

            return newVOs;
        } else {
            return newVOs;
        }
    }

    private MaterialProdVO[] getOldVOs(MaterialProdVO[] vos, Map<String, MaterialProdVO> pk_prod_map) {
        MaterialProdVO[] oldVOs = new MaterialProdVO[vos.length];

        for(int i = 0; i < vos.length; ++i) {
            oldVOs[i] = (MaterialProdVO)pk_prod_map.get(vos[i].getPrimaryKey());
        }

        return oldVOs;
    }

    private Set<String> getNotProdAsstField(String pk_marasstframe) throws DAOException {
        List<String> codes = this.queryMarasstCodesByFrameID(pk_marasstframe);
        Set<String> notAllowFields = new HashSet();

        for(int i = 2; i <= 16; ++i) {
            String code = this.getNewCodeOfMarasst(i);
            if (codes.contains(code)) {
                notAllowFields.add("costvalutasst" + code);
            }
        }

        return notAllowFields;
    }




}
