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

package nc.bs.mmpac.pmo.pac0002.rule.fill;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import nc.bs.framework.common.NCLocator;
import nc.bs.mmpac.pmo.pac0002.bp.service.PMOBPService;
import nc.bs.trade.business.HYPubBO;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.pub.billcode.itf.IBillcodeManage;
import nc.uif.pub.exception.UifException;
import nc.util.mmf.busi.service.MaterialPubService;
import nc.util.mmf.framework.base.*;
import nc.vo.bd.material.prod.MaterialProdVO;
import nc.vo.mmpac.pickm.param.PickmTransParamForMO;
import nc.vo.mmpac.pmo.pac0002.constant.PMOConstLang;
import nc.vo.mmpac.pmo.pac0002.entity.PMOAggVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOHeadVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOItemVO;
import nc.vo.mmpac.pmo.pac0002.enumeration.PMOFItemStatusEnum;
import nc.vo.mmpac.pmo.pac0002.util.PMOTransferUtil;
import nc.vo.org.OrgVO;
import nc.vo.pd.pd0332.entity.PbEventParamVO;
import nc.vo.pd.pd0332.entity.PbParamVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.MapList;

public class PMOFillBatchCodeRule implements IRule<PMOAggVO> {
    public PMOFillBatchCodeRule() {
    }

    public void process(PMOAggVO[] aggvos) {
        try {
            this.fillBatchCode(aggvos);
        } catch (UifException e) {
            throw new RuntimeException(e);
        }
    }

    private void fillBatchCode(PMOAggVO[] aggvos) throws UifException {
        PMOItemVO[] filteritems = this.getItemByBatchCode(aggvos);
        if (!MMArrayUtil.isEmpty(filteritems)) {
            Map<String, PMOHeadVO> headMap = new HashMap();

            for(PMOAggVO aggvo : aggvos) {
                PMOHeadVO headvo = aggvo.getParentVO();
                headMap.put(headvo.getCpmohid(), headvo);
            }

            MapList<String, PMOItemVO> orgMapList = new MapList();

            for(PMOItemVO item : filteritems) {
                orgMapList.put(item.getPk_org(), item);
            }

            Set<String> orgids = orgMapList.keySet();
            List<PMOItemVO> needbatcodeList = new ArrayList();

            for(String orgid : orgids) {
                List<PMOItemVO> itemList = orgMapList.get(orgid);
                MapList<String, PMOItemVO> marMapList = new MapList();

                for(PMOItemVO vo : (PMOItemVO[])itemList.toArray(new PMOItemVO[itemList.size()])) {
                    marMapList.put(vo.getCmaterialvid(), vo);
                }

                Set<String> marSet = marMapList.keySet();
                Map<String, MaterialProdVO> resultMap = MaterialPubService.queryMaterialProduceInfoByPks((String[])marSet.toArray(new String[0]), orgid, new String[]{"pchscscd"});
                needbatcodeList.clear();
                if (MMMapUtil.isNotEmpty(resultMap)) {
                    for(String marid : marSet) {
                        MaterialProdVO provo = (MaterialProdVO)resultMap.get(marid);
                        List<PMOItemVO> itemvoList = marMapList.get(marid);
                        if (!MMValueCheck.isEmpty(provo.getPchscscd()) && !MMCollectionUtil.isEmpty(itemvoList)) {
                            for(PMOItemVO itemVO : itemvoList) {
                                if (PMOFItemStatusEnum.PUT.equalsValue(itemVO.getFitemstatus()) && provo.getPchscscd() == 0) {
                                    needbatcodeList.add(itemVO);
                                } else if (PMOFItemStatusEnum.PLAN.equalsValue(itemVO.getFitemstatus()) && provo.getPchscscd() == 3) {
                                    needbatcodeList.add(itemVO);
                                } else if (PMOFItemStatusEnum.PLAN.equalsValue(itemVO.getFitemstatus()) && provo.getPchscscd() == 0) {
                                    needbatcodeList.add(itemVO);
                                }
                            }
                        }
                    }
                }

                if (!needbatcodeList.isEmpty()) {
                    this.genBatchCode((PMOItemVO[])needbatcodeList.toArray(new PMOItemVO[needbatcodeList.size()]), headMap);
                }
            }

        }
    }

    private PMOItemVO[] getItemByBatchCode(PMOAggVO[] aggvos) {
        if (MMArrayUtil.isEmpty(aggvos)) {
            return null;
        } else {
            List<PMOItemVO> itemList = new ArrayList();

            for(PMOAggVO aggvo : aggvos) {
                PMOItemVO[] items = aggvo.getChildrenVO();
                if (!MMArrayUtil.isEmpty(items)) {
                    for(PMOItemVO item : items) {
                        if (MMValueCheck.isEmpty(item.getVbatchcode())) {
                            itemList.add(item);
                        }
                    }
                }
            }

            return (PMOItemVO[])itemList.toArray(new PMOItemVO[itemList.size()]);
        }
    }

    private void genBatchCode(PMOItemVO[] paravos, Map<String, PMOHeadVO> headMap) throws UifException {
        if (!MMArrayUtil.isEmpty(paravos)) {
            List<PMOItemVO> itemList = new ArrayList();


            List<PMOItemVO> xbitemList = new ArrayList();
            HYPubBO hybo = new HYPubBO();
            String org = hybo.findColValue("org_adminorg", "pk_adminorg", " code = 'C030'  ") + "";
            for(PMOItemVO vo : paravos) {
                if(vo.getPk_org().equals(org)) {
                    xbitemList.add(vo);
                }else{
                    itemList.add(vo);
                }

            }


            if (itemList.size() > 0) {
                PMOItemVO[] vos = (PMOItemVO[])itemList.toArray(new PMOItemVO[itemList.size()]);
                IBillcodeManage billcodeManage = (IBillcodeManage)NCLocator.getInstance().lookup(IBillcodeManage.class);
                String[] billcodes = null;

                try {
                    billcodes = billcodeManage.getBatchBillCodes_RequiresNew("55A2-2", vos[0].getPk_group(), vos[0].getPk_org(), vos[0], vos.length);
                } catch (BusinessException ex) {
                    ExceptionUtils.wrappException(ex);
                }

                if (MMArrayUtil.isEmpty(billcodes)) {
                    ExceptionUtils.wrappBusinessException(PMOConstLang.getMSG_RULE_PUT_BATCHCODECREATE_CHECK());
                }

                int i = 0;
                List<PbEventParamVO> eparaList = new ArrayList();

                for(PMOItemVO vo : vos) {
                    vo.setVbatchcode(billcodes[i++]);
                    if (!MMValueCheck.isEmpty(vo.getVbatchcode())) {
                        PbParamVO paravo = PMOTransferUtil.getBatchParamVO((PMOHeadVO)headMap.get(vo.getCpmohid()), vo, false);
                        PbEventParamVO eparavo = new PbEventParamVO(paravo, (PbParamVO)null, (PbParamVO)null, (PbParamVO)null);
                        eparaList.add(eparavo);
                    }
                }

                Map<String, PbParamVO> ResultParamVOMap = PMOBPService.getIPbMaintainServiceForPac().batchSavePdParamVO((PbEventParamVO[])eparaList.toArray(new PbEventParamVO[eparaList.size()]));
                if (MMMapUtil.isEmpty(ResultParamVOMap)) {
                    return;
                }

                for(PMOItemVO item : vos) {
                    PMOHeadVO headvo = (PMOHeadVO)headMap.get(item.getCpmohid());
                    if (!MMValueCheck.isEmpty(item.getVbatchcode())) {
                        String var10001 = headvo.getPk_org();
                        PbParamVO paravo = (PbParamVO)ResultParamVOMap.get(var10001 + headvo.getVbillcode() + item.getVrowno());
                        if (!MMValueCheck.isEmpty(paravo)) {
                            item.setVbatchcode(paravo.getVprodbatchcode());
                            item.setCbatchid(paravo.getPk_batchcode());
                        }
                    }
                }

                try {
                    List<PickmTransParamForMO> paramList = new ArrayList();

                    for(PMOItemVO vo : vos) {
                        PickmTransParamForMO param = new PickmTransParamForMO();
                        param.setSourceBillID(vo.getCpmohid());
                        param.setSourceBillRowID(vo.getCmoid());
                        param.setBatchCode(vo.getVbatchcode());
                        param.setBatchid(vo.getCbatchid());
                        paramList.add(param);
                    }

                    if (paramList.size() > 0) {
                        PMOBPService.getIPpickmBusinessServiceForPmo().updatePickmBatchCode((PickmTransParamForMO[])paramList.toArray(new PickmTransParamForMO[0]));
                    }
                } catch (Exception e) {
                    ExceptionUtils.wrappException(e);
                }
            }

            if (xbitemList.size() > 0) {
                PMOItemVO[] vos = (PMOItemVO[])xbitemList.toArray(new PMOItemVO[itemList.size()]);
                IBillcodeManage billcodeManage = (IBillcodeManage)NCLocator.getInstance().lookup(IBillcodeManage.class);
                String[] billcodes = null;

                try {
                    billcodes = billcodeManage.getBatchBillCodes_RequiresNew("55A2-2", vos[0].getPk_group(), vos[0].getPk_org(), vos[0],1);
                } catch (BusinessException ex) {
                    ExceptionUtils.wrappException(ex);
                }

                if (MMStringUtil.isEmpty(billcodes[0])) {
                    ExceptionUtils.wrappBusinessException(PMOConstLang.getMSG_RULE_PUT_BATCHCODECREATE_CHECK());
                }

                int i = 0;
                List<PbEventParamVO> eparaList = new ArrayList();

                for(PMOItemVO vo : vos) {
                    vo.setVbatchcode(billcodes[0]);
                    if (!MMValueCheck.isEmpty(vo.getVbatchcode())) {
                        PbParamVO paravo = PMOTransferUtil.getBatchParamVO((PMOHeadVO)headMap.get(vo.getCpmohid()), vo, false);
                        PbEventParamVO eparavo = new PbEventParamVO(paravo, (PbParamVO)null, (PbParamVO)null, (PbParamVO)null);
                        eparaList.add(eparavo);
                    }
                }

                Map<String, PbParamVO> ResultParamVOMap = PMOBPService.getIPbMaintainServiceForPac().batchSavePdParamVO((PbEventParamVO[])eparaList.toArray(new PbEventParamVO[eparaList.size()]));
                if (MMMapUtil.isEmpty(ResultParamVOMap)) {
                    return;
                }

                for(PMOItemVO item : vos) {
                    PMOHeadVO headvo = (PMOHeadVO)headMap.get(item.getCpmohid());
                    if (!MMValueCheck.isEmpty(item.getVbatchcode())) {
                        String var10001 = headvo.getPk_org();
                        PbParamVO paravo = (PbParamVO)ResultParamVOMap.get(var10001 + headvo.getVbillcode() + item.getVrowno());
                        if (!MMValueCheck.isEmpty(paravo)) {
                            item.setVbatchcode(paravo.getVprodbatchcode());
                            item.setCbatchid(paravo.getPk_batchcode());
                        }
                    }
                }

                try {
                    List<PickmTransParamForMO> paramList = new ArrayList();

                    for(PMOItemVO vo : vos) {
                        PickmTransParamForMO param = new PickmTransParamForMO();
                        param.setSourceBillID(vo.getCpmohid());
                        param.setSourceBillRowID(vo.getCmoid());
                        param.setBatchCode(vo.getVbatchcode());
                        param.setBatchid(vo.getCbatchid());
                        paramList.add(param);
                    }

                    if (paramList.size() > 0) {
                        PMOBPService.getIPpickmBusinessServiceForPmo().updatePickmBatchCode((PickmTransParamForMO[])paramList.toArray(new PickmTransParamForMO[0]));
                    }
                } catch (Exception e) {
                    ExceptionUtils.wrappException(e);
                }
            }

        }
    }
}
