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

package nc.impl.mmpac.pmo.pac0002;

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.PMOAdjustBP;
import nc.bs.mmpac.pmo.pac0002.bp.PMOApproveBP;
import nc.bs.mmpac.pmo.pac0002.bp.PMOBatchEditBP;
import nc.bs.mmpac.pmo.pac0002.bp.PMODeleteBP;
import nc.bs.mmpac.pmo.pac0002.bp.PMOInsertBP;
import nc.bs.mmpac.pmo.pac0002.bp.PMOQueryBP;
import nc.bs.mmpac.pmo.pac0002.bp.PMOSaveCommitBP;
import nc.bs.mmpac.pmo.pac0002.bp.PMOUpdateBP;
import nc.bs.mmpac.pmo.pac0002.bp.query.PMOItemVOQueryBP;
import nc.bs.mmpac.pmo.pac0002.bp.service.PMOBPService;
import nc.bs.mmpac.pmo.pac0002.bp.util.PMOBPCalUtil;
import nc.bs.mmpac.pmo.pac0002.bp.util.PMOBPUtil;
import nc.bs.mmpac.pmo.pac0002.pluginpoint.PMOPluginPoint;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOCheckDeptNotNullRule;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOCheckItemNotNullRule;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOCheckItemParentProcedureRule;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOCheckParentProcedureNoRule;
import nc.impl.pubapp.bd.userdef.UserDefSaveRule;
import nc.impl.pubapp.pattern.rule.processer.AroundProcesser;
import nc.itf.mmpac.pmo.pac0002.IPMOMaintainService;
import nc.itf.uap.rbac.IUserManageQuery_C;
import nc.jdbc.framework.generator.SequenceGenerator;
import nc.util.mmf.busi.measure.MeasureHelper;
import nc.util.mmf.busi.service.OrgUnitPubService;
import nc.util.mmf.framework.base.MMArrayUtil;
import nc.util.mmf.framework.base.MMCollectionUtil;
import nc.util.mmf.framework.base.MMMapUtil;
import nc.util.mmf.framework.base.MMNumberUtil;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.util.mmf.framework.gc.GCBillTransferTool;
import nc.util.mmf.framework.gc.GCPseudoColUtil;
import nc.vo.bd.feature.ffile.entity.AggFFileVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOAggVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOHeadVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOItemMeasureParam;
import nc.vo.mmpac.pmo.pac0002.entity.PMOItemVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOMaterialRelationVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOMaterialViewVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOPlanOutputVO;
import nc.vo.mmpac.pmo.pac0002.enumeration.PMOFSrcMoOperTypeEnum;
import nc.vo.mmpac.pmo.pac0002.util.PMOFillTransTypeUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.AppContext;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.MapList;

public class PMOMaintainServiceImpl implements IPMOMaintainService {
    public PMOMaintainServiceImpl() {
    }

    public PMOAggVO[] insert(PMOAggVO[] vos) throws BusinessException {
        try {
            new GCBillTransferTool(vos);
            AroundProcesser<PMOAggVO> processer = new AroundProcesser(PMOPluginPoint.VIEWINSERT);
            processer.addBeforeRule(new PMOCheckItemNotNullRule());
            processer.addBeforeRule(new PMOCheckItemParentProcedureRule());
            processer.addBeforeRule(new PMOCheckParentProcedureNoRule());
            processer.addBeforeRule(new PMOCheckDeptNotNullRule());
            processer.before(vos);
            vos = (new PMOInsertBP()).insert(vos, false, false);
            return vos;
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public PMOAggVO[] insert_RequiresNew(PMOAggVO[] vos) throws BusinessException {
        try {
            PMOAggVO[] rtnVos = (new PMOInsertBP()).insert(vos, false, false);
            return rtnVos;
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public PMOAggVO[] update(PMOAggVO[] vos) throws BusinessException {
        try {
            GCBillTransferTool<PMOAggVO> transTool = new GCBillTransferTool(vos);
            PMOAggVO[] fullBills = (PMOAggVO[])transTool.getClientFullInfoBill();
            this.setAggFFileVO(vos, fullBills);
            PMOAggVO[] originBills = (PMOAggVO[])transTool.getOriginBills();
            this.setVfirstmocode(fullBills, originBills);
            PMOUpdateBP bp = new PMOUpdateBP();
            PMOAggVO[] retBills = bp.update(fullBills, originBills);
            this.setCheckInfoToReturnAggVO(retBills, bp.getCheckInfoList());
            return retBills;
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    private void setAggFFileVO(PMOAggVO[] vos, PMOAggVO[] fullBills) {
        Map<String, AggFFileVO> cffileVOMap = new HashMap();

        for(PMOAggVO vo : vos) {
            for(PMOItemVO itemvo : vo.getChildrenVO()) {
                String key = itemvo.getCmoid();
                if (MMValueCheck.isEmpty(key)) {
                    key = (String)itemvo.getAttributeValue("pseudocolumn");
                }

                cffileVOMap.put(itemvo.getCmoid(), itemvo.getFfvo());
            }
        }

        for(PMOAggVO vo : fullBills) {
            for(PMOItemVO itemvo : vo.getChildrenVO()) {
                String key = itemvo.getCmoid();
                if (MMValueCheck.isEmpty(key)) {
                    key = (String)itemvo.getAttributeValue("pseudocolumn");
                }

                AggFFileVO ffvo = (AggFFileVO)cffileVOMap.get(key);
                if (MMValueCheck.isNotEmpty(ffvo)) {
                    itemvo.setFfvo(ffvo);
                }
            }
        }

    }

    private void setVfirstmocode(PMOAggVO[] fullBills, PMOAggVO[] originBills) {
        if (!MMArrayUtil.isEmpty(fullBills)) {
            Map<String, PMOHeadVO> cacheBill = new HashMap();
            if (MMArrayUtil.isNotEmpty(originBills)) {
                for(PMOAggVO origin : originBills) {
                    cacheBill.put(origin.getPrimaryKey(), origin.getParentVO());
                }
            }

            for(PMOAggVO vo : fullBills) {
                PMOHeadVO headVo = vo.getParentVO();
                String vBillCode = headVo.getVbillcode();
                if (!MMValueCheck.isEmpty(vBillCode)) {
                    PMOHeadVO originHeadVo = (PMOHeadVO)cacheBill.get(headVo.getPrimaryKey());
                    String originBillCode = originHeadVo.getVbillcode();
                    PMOItemVO[] items = vo.getChildrenVO();
                    if (MMArrayUtil.isNotEmpty(items)) {
                        for(PMOItemVO item : items) {
                            if (MMValueCheck.isNotEmpty(originBillCode) && !vBillCode.equals(originBillCode) && originBillCode.equals(item.getVfirstmocode())) {
                                item.setVfirstmocode(vBillCode);
                            }
                        }
                    }
                }
            }

        }
    }

    private void setCheckInfoToReturnAggVO(PMOAggVO[] returnAggVOs, List<String> checkInfoList) {
        if (MMCollectionUtil.isNotEmpty(checkInfoList) && MMArrayUtil.isNotEmpty(returnAggVOs)) {
            String checkInfo = (String)checkInfoList.get(0);
            returnAggVOs[0].getChildrenVO()[0].setExtendmsg(checkInfo);
        }

    }

    public PMOAggVO[] adjustPMO(PMOAggVO[] vos) throws BusinessException {
        try {
            GCBillTransferTool<PMOAggVO> transTool = new GCBillTransferTool(vos);
            PMOAggVO[] fullBills = (PMOAggVO[])transTool.getClientFullInfoBill();
            this.setAggFFileVO(vos, fullBills);
            PMOAggVO[] originBills = (PMOAggVO[])transTool.getOriginBills();
            PMOAdjustBP bp = new PMOAdjustBP();
            PMOAggVO[] retBills = bp.adjust(fullBills, originBills);
            this.setCheckInfoToReturnAggVO(retBills, bp.getCheckInfoList());
            return retBills;
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public PMOAggVO[] batchUpdate(PMOAggVO[] vos) throws BusinessException {
        try {
            GCBillTransferTool<PMOAggVO> transTool = new GCBillTransferTool(vos);
            PMOAggVO[] fullBills = (PMOAggVO[])transTool.getClientFullInfoBill();
            PMOAggVO[] originBills = (PMOAggVO[])transTool.getOriginBills();
            PMOBatchEditBP bp = new PMOBatchEditBP();
            PMOAggVO[] updvos = bp.batchUpdate(fullBills, originBills);
            return (new PMOQueryBP()).fillPbInfo(updvos);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public void delete(PMOAggVO[] vos) throws BusinessException {
        try {
            GCBillTransferTool<PMOAggVO> transferTool = new GCBillTransferTool(vos);
            PMOAggVO[] fullBills = (PMOAggVO[])transferTool.getClientFullInfoBill();
            PMODeleteBP bp = new PMODeleteBP();
            bp.delete(fullBills);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
        }

    }

    public PMOAggVO[] approve(PMOAggVO[] vos) throws BusinessException {
        try {
            GCBillTransferTool<PMOAggVO> transTool = new GCBillTransferTool(vos);
            PMOAggVO[] fullBills = (PMOAggVO[])transTool.getClientFullInfoBill();
            PMOAggVO[] originBills = (PMOAggVO[])transTool.getOriginBills();
            PMOApproveBP bp = new PMOApproveBP();
            PMOAggVO[] updvos = bp.approve(fullBills, originBills);
            List<String> hidList = new ArrayList();
            if (MMArrayUtil.isEmpty(updvos)) {
                return null;
            } else {
                for(PMOAggVO returnvo : updvos) {
                    hidList.add(returnvo.getParentVO().getCpmohid());
                }

                return (new PMOQueryBP()).fillPbInfo(PMOBPService.getIPMOQueryService().queryByPks((String[])hidList.toArray(new String[hidList.size()])));
            }
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public PMOAggVO[] approve_RequiresNew(PMOAggVO[] vos) throws BusinessException {
        try {
            PMOApproveBP bp = new PMOApproveBP();
            List<PMOAggVO> aggvos = new ArrayList();

            for(PMOAggVO vo : vos) {
                GCPseudoColUtil.getInstance().setPseudoColInfo(vo);
                aggvos.add((PMOAggVO)vo.clone());
            }

            PMOAggVO[] updvos = bp.approve(vos, (PMOAggVO[])aggvos.toArray(new PMOAggVO[aggvos.size()]));
            return updvos;
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public PMOAggVO[] approveCancel(PMOAggVO[] vos) throws BusinessException {
        try {
            GCBillTransferTool<PMOAggVO> transTool = new GCBillTransferTool(vos);
            PMOAggVO[] fullBills = (PMOAggVO[])transTool.getClientFullInfoBill();
            PMOAggVO[] originBills = (PMOAggVO[])transTool.getOriginBills();
            PMOApproveBP bp = new PMOApproveBP();
            PMOAggVO[] updvos = bp.approveCancel(fullBills, originBills);
            List<String> hidList = new ArrayList();

            for(PMOAggVO returnvo : updvos) {
                hidList.add(returnvo.getParentVO().getCpmohid());
            }

            return (new PMOQueryBP()).fillPbInfo(PMOBPService.getIPMOQueryService().queryByPks((String[])hidList.toArray(new String[hidList.size()])));
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public PMOAggVO[] fillVOInfo4AID(PMOAggVO[] aggvos) throws BusinessException {
        return MMArrayUtil.isEmpty(aggvos) ? null : (new PMOBPUtil()).fillVOInfo4AID(aggvos);
    }

    public PMOAggVO[] fillVOInfo4KF(PMOAggVO[] aggvos) throws BusinessException {
        if (MMArrayUtil.isEmpty(aggvos)) {
            return null;
        } else {
            (new PMOFillTransTypeUtil()).fillTransTypeValues(aggvos);

            try {
                (new UserDefSaveRule(new Class[]{PMOItemVO.class, PMOPlanOutputVO.class})).process(aggvos);
                List<PMOItemVO> itemList = new ArrayList();
                MapList<String, PMOAggVO> orgMapList = new MapList();

                for(PMOAggVO aggvo : aggvos) {
                    String pk_org = aggvo.getParentVO().getPk_org();
                    orgMapList.put(pk_org, aggvo);
                    PMOItemVO[] items = aggvo.getChildrenVO();
                    Map<String, UFDouble> waterRateMap = PMOBPCalUtil.getWasterrate(aggvo);
                    if (!MMArrayUtil.isEmpty(items)) {
                        PMOItemMeasureParam param = new PMOItemMeasureParam();

                        for(PMOItemVO item : items) {
                            if (MMNumberUtil.isNotNullAndNotZero(item.getNplanputastnum())) {
                                MeasureHelper.fillAccNumber(item, param, "nplanputnum", "nplanputastnum");
                            }

                            UFDouble wastrate = (UFDouble)waterRateMap.get(item.getCmaterialvid());
                            UFDouble nNum = item.getNplanputnum().multiply((new UFDouble(1)).sub(wastrate));
                            item.setNnum(nNum);
                            item.setNrwxis(wastrate);
                            itemList.add(item);
                        }

                        MeasureHelper.fillAssNumber((SuperVO[])itemList.toArray(new PMOItemVO[itemList.size()]), param, "nnum", "nastnum");
                    }
                }

                for(PMOAggVO aggvo : aggvos) {
                    PMOItemVO[] items = aggvo.getChildrenVO();
                    if (!MMArrayUtil.isEmpty(items)) {
                        for(PMOItemVO item : items) {
                            item.setNmmastnum(item.getNastnum());
                            item.setNmmnum(item.getNnum());
                        }
                    }
                }

                Set<String> orgids = orgMapList.keySet();
                Map<String, String> orgoid2Vid = new HashMap();
                if (orgids.size() > 0) {
                    orgoid2Vid = OrgUnitPubService.getNewVIDSByOrgIDS((String[])orgids.toArray(new String[orgids.size()]));
                }

                if (MMMapUtil.isNotEmpty(orgoid2Vid)) {
                    for(PMOAggVO aggvo : aggvos) {
                        PMOHeadVO head = aggvo.getParentVO();
                        if (orgoid2Vid.containsKey(head.getPk_org())) {
                            head.setPk_org_v((String)orgoid2Vid.get(head.getPk_org()));
                        }

                        if (MMValueCheck.isEmpty(head.getCplanfactoryid())) {
                            head.setCplanfactoryid(head.getPk_org());
                            head.setCplanfactoryvid(head.getPk_org_v());
                        }

                        PMOItemVO[] items = aggvo.getChildrenVO();
                        if (!MMArrayUtil.isEmpty(items)) {
                            for(PMOItemVO item : items) {
                                item.setPk_group(head.getPk_group());
                                item.setPk_org(head.getPk_org());
                                item.setPk_org_v(head.getPk_org_v());
                            }
                        }
                    }
                }

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

                    for(PMOAggVO aggvo : moList) {
                        PMOItemVO[] items = aggvo.getChildrenVO();
                        SequenceGenerator sgkey = new SequenceGenerator();
                        String[] cmoids = sgkey.generate(items.length);

                        for(int i = 0; i < items.length; ++i) {
                            if (null == items[i].getCmoid()) {
                                items[i].setCmoid(cmoids[i]);
                            }

                            marMapList.put(items[i].getCmaterialvid(), items[i]);
                        }
                    }

                    Set<String> marSet = marMapList.keySet();
                    String[] marPlanFields = new String[]{"prevahead", "endahead", "fixedahead", "aheadbatch", "aheadcoff", "planprop"};
                    String[] marProdFields = new String[]{"pk_prodeptdoc", "pk_propsndoc"};
                    Map<String, PMOMaterialRelationVO> infoMap = PMOBPService.getIPMOQueryService().queryMaterialRelationArray(marMapList, marPlanFields, marProdFields, orgid, ((PMOAggVO)orgMapList.get(orgid).get(0)).getParentVO().getPk_group());
                    if (!MMMapUtil.isEmpty(infoMap)) {
                        for(String key : marSet) {
                            PMOItemVO[] hvos = (PMOItemVO[])marMapList.get(key).toArray(new PMOItemVO[0]);
                            if (!MMArrayUtil.isEmpty(hvos)) {
                                for(PMOItemVO hvo : hvos) {
                                    PMOMaterialRelationVO infovo = (PMOMaterialRelationVO)infoMap.get(key + hvo.getCmoid());
                                    if (MMValueCheck.isEmpty(hvo.getCbomversionid())) {
                                        hvo.setCbomversionid(infovo.getCbomversionid());
                                        hvo.setVbomversion(infovo.getVbomversion());
                                    }

                                    if (MMValueCheck.isEmpty(hvo.getCpackbomid())) {
                                        hvo.setCpackbomid(infovo.getCpackbomid());
                                        hvo.setVpackbomversion(infovo.getVpackbomversion());
                                    }

                                    if (MMValueCheck.isEmpty(hvo.getCdeptvid())) {
                                        hvo.setCdeptvid(infovo.getCdeptvid());
                                        hvo.setCdeptid(infovo.getCdeptid());
                                    }

                                    if (MMValueCheck.isEmpty(hvo.getCemployeeid())) {
                                        hvo.setCemployeeid(infovo.getCemployeeid());
                                    }

                                    if (MMValueCheck.isEmpty(hvo.getCrtversionid())) {
                                        hvo.setCrtversionid(infovo.getCrtversionid());
                                        hvo.setVrtversion(infovo.getVrtversion());
                                    }

                                    if (MMValueCheck.isEmpty(hvo.getCwkid())) {
                                        hvo.setCwkid(infovo.getCwkid());
                                    }
                                }
                            }
                        }
                    }
                }

                return aggvos;
            } catch (BusinessException e) {
                ExceptionUtils.marsh(e);
                return null;
            }
        }
    }

    public PMOAggVO[] fillVOInfo4TRAN(PMOAggVO[] aggvos) throws BusinessException {
        try {
            return this.fillVOInfo4AID(aggvos);
        } catch (BusinessException e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public PMOAggVO[] fillVOInfo4SO(PMOAggVO[] aggvos) throws BusinessException {
        try {
            return this.fillVOInfo4AID(aggvos);
        } catch (BusinessException e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public PMOAggVO[] fillVOInfo4WR(PMOAggVO[] aggvos) throws BusinessException {
        if (MMArrayUtil.isEmpty(aggvos)) {
            return null;
        } else {
            try {
                (new UserDefSaveRule(new Class[]{PMOItemVO.class, PMOPlanOutputVO.class})).process(aggvos);
                List<PMOItemVO> itemList = new ArrayList();
                List<String> fitemidList = new ArrayList();
                MapList<String, PMOAggVO> orgMapList = new MapList();

                for(PMOAggVO aggvo : aggvos) {
                    String pk_org = aggvo.getParentVO().getPk_org();
                    PMOItemVO[] items = aggvo.getChildrenVO();
                    orgMapList.put(pk_org, aggvo);
                    Map<String, UFDouble> waterRateMap = PMOBPCalUtil.getWasterrate(aggvo);
                    if (!MMArrayUtil.isEmpty(items)) {
                        for(PMOItemVO item : items) {
                            if (!MMValueCheck.isEmpty(item.getCsrcmobid())) {
                                fitemidList.add(item.getCsrcmobid());
                            }

                            UFDouble wastrate = (UFDouble)waterRateMap.get(item.getCmaterialvid());
                            UFDouble nNum = item.getNmmnum().div((new UFDouble(1)).sub(wastrate));
                            item.setNplanputnum(nNum);
                            item.setNrwxis(wastrate);
                            if (PMOFSrcMoOperTypeEnum.RE.equalsValue(item.getFsrcmooper()) || PMOFSrcMoOperTypeEnum.PROCRE.equalsValue(item.getFsrcmooper())) {
                                item.setNplanputastnum(item.getNmmastnum());
                                item.setNplanputnum(item.getNmmnum());
                                item.setNrwxis(UFDouble.ZERO_DBL);
                            }

                            itemList.add(item);
                        }

                        PMOItemMeasureParam param = new PMOItemMeasureParam();
                        MeasureHelper.fillAssNumber((SuperVO[])itemList.toArray(new PMOItemVO[itemList.size()]), param, "nplanputnum", "nplanputastnum");
                    }
                }

                if (fitemidList.size() > 0) {
                    Map<String, PMOItemVO> fitemsMap = PMOItemVOQueryBP.getPMOItemVOMap((String[])fitemidList.toArray(new String[fitemidList.size()]));

                    for(PMOAggVO aggvo : aggvos) {
                        PMOItemVO[] items = aggvo.getChildrenVO();
                        if (!MMArrayUtil.isEmpty(items)) {
                            for(PMOItemVO item : items) {
                                PMOItemVO fitem = (PMOItemVO)fitemsMap.get(item.getCsrcmobid());
                                if (!MMValueCheck.isEmpty(fitem)) {
                                    if (PMOFSrcMoOperTypeEnum.RW.equalsValue(item.getFsrcmooper())) {
                                        item.setCbomversionid(fitem.getCbomversionid());
                                        item.setVbomversion(fitem.getVbomversion());
                                        item.setCpackbomid(fitem.getCpackbomid());
                                        item.setVpackbomversion(fitem.getVpackbomversion());
                                        item.setCrtversionid(fitem.getCrtversionid());
                                        item.setVrtversion(fitem.getVrtversion());
                                    }

                                    item.setCvendorid(fitem.getCvendorid());
                                    item.setCproductorid(fitem.getCproductorid());
                                    item.setCprojectid(fitem.getCprojectid());
                                    item.setCcustomerid(fitem.getCcustomerid());
                                    item.setVfree1(fitem.getVfree1());
                                    item.setVfree2(fitem.getVfree2());
                                    item.setVfree3(fitem.getVfree3());
                                    item.setVfree4(fitem.getVfree4());
                                    item.setVfree5(fitem.getVfree5());
                                    item.setVfree6(fitem.getVfree6());
                                    item.setVfree7(fitem.getVfree7());
                                    item.setVfree8(fitem.getVfree8());
                                    item.setVfree9(fitem.getVfree9());
                                    item.setVfree10(fitem.getVfree10());
                                    item.setCrequireorg(fitem.getCrequireorg());
                                    item.setCrequireorgvid(fitem.getCrequireorgvid());
                                    item.setTrequiredate(fitem.getTrequiredate());
                                    item.setCcustmaterialid(fitem.getCcustmaterialid());

                                    for(int i = 1; i <= 20; ++i) {
                                        if (MMValueCheck.isEmpty(item.getAttributeValue("vdef" + i))) {
                                            item.setAttributeValue("vdef" + i, fitem.getAttributeValue("vdef" + i));
                                        }
                                    }

                                    item.setVparentbillid(fitem.getVparentbillid());
                                    item.setVparentbillcode(fitem.getVparentbillcode());
                                    item.setCparentmobid(fitem.getCparentmobid());
                                    item.setVparentmorowno(fitem.getVparentmorowno());
                                }
                            }
                        }
                    }
                }

                String pk_user = AppContext.getInstance().getPkUser();
                String cemployeeid = null;
                if (!MMValueCheck.isEmpty(pk_user)) {
                    try {
                        cemployeeid = ((IUserManageQuery_C)NCLocator.getInstance().lookup(IUserManageQuery_C.class)).queryPsndocByUserid(pk_user);
                    } catch (BusinessException ex) {
                        ExceptionUtils.wrappException(ex);
                    }
                }

                if (!MMValueCheck.isEmpty(cemployeeid)) {
                    for(PMOAggVO aggvo : aggvos) {
                        PMOItemVO[] items = aggvo.getChildrenVO();
                        if (!MMArrayUtil.isEmpty(items)) {
                            for(PMOItemVO item : items) {
                                if (MMValueCheck.isEmpty(item.getCemployeeid())) {
                                    item.setCemployeeid(cemployeeid);
                                }
                            }
                        }
                    }
                }

                Set<String> orgids = orgMapList.keySet();
                Map<String, String> orgoid2Vid = new HashMap();
                if (orgids.size() > 0) {
                    orgoid2Vid = OrgUnitPubService.getNewVIDSByOrgIDS((String[])orgids.toArray(new String[orgids.size()]));
                }

                if (MMMapUtil.isNotEmpty(orgoid2Vid)) {
                    for(PMOAggVO aggvo : aggvos) {
                        PMOHeadVO head = aggvo.getParentVO();
                        if (orgoid2Vid.containsKey(head.getPk_org())) {
                            head.setPk_org_v((String)orgoid2Vid.get(head.getPk_org()));
                        }

                        if (MMValueCheck.isEmpty(head.getCplanfactoryid())) {
                            head.setCplanfactoryid(head.getPk_org());
                            head.setCplanfactoryvid(head.getPk_org_v());
                        }

                        PMOItemVO[] items = aggvo.getChildrenVO();
                        if (!MMArrayUtil.isEmpty(items)) {
                            for(PMOItemVO item : items) {
                                item.setPk_group(head.getPk_group());
                                item.setPk_org(head.getPk_org());
                                item.setPk_org_v(head.getPk_org_v());
                            }
                        }
                    }
                }

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

                    for(PMOAggVO aggvo : moList) {
                        PMOItemVO[] items = aggvo.getChildrenVO();
                        SequenceGenerator sgkey = new SequenceGenerator();
                        String[] cmoids = sgkey.generate(items.length);

                        for(int i = 0; i < items.length; ++i) {
                            if (null == items[i].getCmoid()) {
                                items[i].setCmoid(cmoids[i]);
                            }

                            marMapList.put(items[i].getCmaterialvid(), items[i]);
                        }
                    }

                    Set<String> marSet = marMapList.keySet();
                    String[] marPlanFields = new String[]{"prevahead", "endahead", "fixedahead", "aheadbatch", "aheadcoff", "planprop"};
                    String[] marProdFields = new String[]{"pk_prodeptdoc", "pk_propsndoc"};
                    Map<String, PMOMaterialRelationVO> infoMap = PMOBPService.getIPMOQueryService().queryMaterialRelationArray(marMapList, marPlanFields, marProdFields, orgid, ((PMOAggVO)orgMapList.get(orgid).get(0)).getParentVO().getPk_group());
                    if (!MMMapUtil.isEmpty(infoMap)) {
                        for(String key : marSet) {
                            PMOItemVO[] hvos = (PMOItemVO[])marMapList.get(key).toArray(new PMOItemVO[0]);
                            if (!MMArrayUtil.isEmpty(hvos)) {
                                for(PMOItemVO hvo : hvos) {
                                    PMOMaterialRelationVO infovo = (PMOMaterialRelationVO)infoMap.get(key + hvo.getCmoid());
                                    if (MMValueCheck.isEmpty(hvo.getCdeptvid())) {
                                        hvo.setCdeptvid(infovo.getCdeptvid());
                                        hvo.setCdeptid(infovo.getCdeptid());
                                    }

                                    if (MMValueCheck.isEmpty(hvo.getCemployeeid())) {
                                        hvo.setCemployeeid(infovo.getCemployeeid());
                                    }

                                    if (MMValueCheck.isEmpty(hvo.getCwkid())) {
                                        hvo.setCwkid(infovo.getCwkid());
                                    }

                                    PMOMaterialViewVO materialvo = PMOBPCalUtil.getMaterialView(hvo.getPk_org(), infovo);
                                    PMOBPCalUtil.fillTReqTimeSupplyTime(materialvo, hvo);
                                }
                            }
                        }
                    }
                }

                return aggvos;
            } catch (BusinessException e) {
                ExceptionUtils.marsh(e);
                return null;
            }
        }
    }

    public PMOAggVO[] insertCommit(PMOAggVO[] vos) throws BusinessException {
        try {
            return (new PMOSaveCommitBP()).insertCommit(vos);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public PMOAggVO[] updateCommit(PMOAggVO[] vos) throws BusinessException {
        try {
            return (new PMOSaveCommitBP()).updateCommit(vos);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }
}
