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

package nccloud.web.mmpac.pickm.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.itf.mmpac.pickm.IPickmQueryService;
import nc.itf.mmpac.pickm.IPickmTransTypeService;
import nc.itf.uap.busibean.ISysInitGetValue;
import nc.pubitf.mmpac.mo.pickm.IMoPubService4Pickm;
import nc.util.mmf.framework.base.MMMapUtil;
import nc.util.mmf.framework.base.MMNumberUtil;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.bd.material.prod.MaterialProdModeEnum;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.mmpac.mo.param.MoQueryParam4Pickm;
import nc.vo.mmpac.pickm.consts.PickmLangConsts;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpac.pickm.entity.PickmHeadVO;
import nc.vo.mmpac.pickm.entity.PickmItemVO;
import nc.vo.mmpac.pickm.enumeration.FbillstatusEnum;
import nc.vo.mmpac.pickm.enumeration.FitemsourceEnum;
import nc.vo.mmpac.pickm.enumeration.FsourcetypeEnum;
import nc.vo.pub.BusinessException;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.pub.MapList;
import nccloud.dto.mmpac.pickm.pub.entity.PickmQueryInfoDTO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;

public class PickmToReqpickmAction implements ICommonAction {
    public PickmToReqpickmAction() {
    }

    public Object doAction(IRequest request) {
        String read = request.read();
        IJson json = JsonFactory.create();

        try {
            PickmQueryInfoDTO paramDTO = (PickmQueryInfoDTO)json.fromJson(read, PickmQueryInfoDTO.class);
            String[] cpickmids = paramDTO.getCpickmids();
            String[] cpickmbids = paramDTO.getCpickmbids();
            if (!MMValueCheck.isEmpty(cpickmids) || null != cpickmbids && cpickmbids.length != 0) {
                IPickmQueryService service = (IPickmQueryService)ServiceLocator.find(IPickmQueryService.class);
                AggPickmVO[] aggVOs = null;
                if (MMValueCheck.isNotEmpty(cpickmids)) {
                    aggVOs = service.queryBillsByPks(cpickmids);
                } else if (MMValueCheck.isNotEmpty(cpickmbids)) {
                    aggVOs = service.queryAggPickmVObyBid(cpickmbids);
                }

                if (MMValueCheck.isEmpty(aggVOs)) {
                    ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("5008100_0", "05008100-0009"));
                }

                AggPickmVO[] filterVOs = this.filterAggPickmVOForReqPickm(aggVOs);
                if (MMValueCheck.isEmpty(filterVOs)) {
                    ExceptionUtils.wrapBusinessException(PickmLangConsts.getErrNoDataToReqPickm());
                }

                List<String> pks = new ArrayList();

                for(AggPickmVO agg : filterVOs) {
                    for(PickmItemVO item : (PickmItemVO[])agg.getChildrenVO()) {
                        pks.add(item.getCpickm_bid());
                    }
                }

                Map<String, Object> returnMap = new HashMap();
                returnMap.put("pks", pks.toArray(new String[0]));
                returnMap.put("success", true);
                return returnMap;
            } else {
                return null;
            }
        } catch (Exception e) {
            ExceptionUtils.wrapException(e);
            return null;
        }
    }

    private AggPickmVO[] filterAggPickmVOForReqPickm(AggPickmVO[] aggvos) throws BusinessException {
        if (aggvos == null) {
            return null;
        } else {
            List<AggPickmVO> resultVO = new ArrayList();
            List<MoQueryParam4Pickm> moParams = new ArrayList();
            MapList<Integer, String> moidMap = new MapList();
            List<String> transTypeIds = new ArrayList();
            List<String> orgs = new ArrayList();

            for(AggPickmVO aggvo : aggvos) {
                PickmHeadVO head = aggvo.getParentVO();
                if (!FbillstatusEnum.FINISH_STATE.equalsValue(head.getFbillstatus()) && !MMValueCheck.isEmpty(aggvo.getChildren(PickmItemVO.class))) {
                    PickmItemVO[] items = (PickmItemVO[])aggvo.getChildren(PickmItemVO.class);
                    List<PickmItemVO> itemList = new ArrayList();

                    for(PickmItemVO item : items) {
                        if (MMValueCheck.isTrue(item.getBcandeliver()) && FitemsourceEnum.PREPARE.equalsValue(item.getFitemsource()) && MMNumberUtil.isGtZero(MMNumberUtil.sub(item.getNplanoutnum(), new UFDouble[]{item.getNaccoutnum(), item.getNatpnum(), item.getNpscnum()}))) {
                            itemList.add(item);
                        }
                    }

                    if (!MMValueCheck.isEmpty(itemList)) {
                        orgs.add(aggvo.getParentVO().getPk_org());
                        AggPickmVO vo = new AggPickmVO();
                        vo.setParent(head);
                        vo.setChildren(PickmItemVO.class, (ISuperVO[])itemList.toArray(new PickmItemVO[0]));
                        resultVO.add(vo);
                        transTypeIds.add(head.getVbusitypeid());
                        if (FsourcetypeEnum.MOBILL_STATE.equalsValue(head.getFsourcetype())) {
                            if (MaterialProdModeEnum.PRODMODE_PROD.equalsValue(head.getFprodmode())) {
                                moidMap.put(head.getFprodmode(), head.getCsourcebillrowid());
                            }

                            if (MaterialProdModeEnum.PRODMODE_SEPPROD.equalsValue(head.getFprodmode())) {
                                moidMap.put(head.getFprodmode(), head.getCsourcebillid());
                            }
                        }
                    }
                }
            }

            if (MMValueCheck.isEmpty(resultVO)) {
                return null;
            } else {
                IPickmTransTypeService wwService = (IPickmTransTypeService)ServiceLocator.find(IPickmTransTypeService.class);
                Map<String, Boolean> isWWPickm = wwService.isWWPickmTransType((String[])transTypeIds.toArray(new String[0]));
                Map<String, UFBoolean> ismoFinisOrClose = new HashMap();
                if (!MMMapUtil.isEmpty(moidMap)) {
                    for(Integer prodmode : moidMap.keySet()) {
                        MoQueryParam4Pickm param = new MoQueryParam4Pickm();
                        param.setProdMode(prodmode);
                        param.setIds((String[])moidMap.get(prodmode).toArray(new String[0]));
                        moParams.add(param);
                    }

                    IMoPubService4Pickm moService = (IMoPubService4Pickm)ServiceLocator.find(IMoPubService4Pickm.class);
                    ismoFinisOrClose = moService.getMOItemFinishOrCloseStatus((MoQueryParam4Pickm[])moParams.toArray(new MoQueryParam4Pickm[0]));
                }

                Map<String, UFBoolean> isOneReqMap = ((ISysInitGetValue)ServiceLocator.find(ISysInitGetValue.class)).getBatchParaBoolean((String[])orgs.toArray(new String[0]), "PAC045");
                List<AggPickmVO> resultMoVO = new ArrayList();

                for(AggPickmVO aggvo : resultVO) {
                    PickmHeadVO head = aggvo.getParentVO();
                    if (!isWWPickm.containsKey(head.getVbusitypeid()) || !MMValueCheck.isTrue((Boolean)isWWPickm.get(head.getVbusitypeid()))) {
                        if (!FsourcetypeEnum.MOBILL_STATE.equalsValue(head.getFsourcetype())) {
                            resultMoVO.add(aggvo);
                        } else {
                            String moid = "";
                            if (MaterialProdModeEnum.PRODMODE_PROD.equalsValue(head.getFprodmode())) {
                                moid = head.getCsourcebillrowid();
                            }

                            if (MaterialProdModeEnum.PRODMODE_SEPPROD.equalsValue(head.getFprodmode())) {
                                moid = head.getCsourcebillid();
                            }

                            if (!MMValueCheck.isNotEmpty(ismoFinisOrClose) || !ismoFinisOrClose.containsKey(moid) || !MMValueCheck.isTrue((UFBoolean)ismoFinisOrClose.get(moid))) {
                                if (MMValueCheck.isNotEmpty(isOneReqMap) && isOneReqMap.containsKey(head.getPk_org()) && MMValueCheck.isTrue((UFBoolean)isOneReqMap.get(head.getPk_org()))) {
                                    PickmItemVO[] items = (PickmItemVO[])aggvo.getChildren(PickmItemVO.class);
                                    List<PickmItemVO> itemList = new ArrayList();

                                    for(PickmItemVO item : items) {
                                        if (!MMValueCheck.isTrue(item.getBreqpickm())) {
                                            itemList.add(item);
                                        }
                                    }

                                    if (!MMValueCheck.isEmpty(itemList)) {
                                        AggPickmVO vo = new AggPickmVO();
                                        vo.setParent(head);
                                        vo.setChildren(PickmItemVO.class, (ISuperVO[])itemList.toArray(new PickmItemVO[0]));
                                        resultMoVO.add(vo);
                                    }
                                } else {
                                    resultMoVO.add(aggvo);
                                }
                            }
                        }
                    }
                }

                if (MMValueCheck.isEmpty(resultMoVO)) {
                    return null;
                } else {
                    return (AggPickmVO[])resultMoVO.toArray(new AggPickmVO[0]);
                }
            }
        }
    }
}
