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

package nccloud.web.mmpac.pickm.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.itf.mmpac.pickm.IPickmBusinessService;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.pubitf.mmpac.pickm.mo.IPickmQueryServiceForMO;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpac.pickm.entity.PickmItemVO;
import nc.vo.mmpac.pickm.param.PickmTransParam;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.res.NCModule;
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;
import nccloud.web.mmpub.pub.busi.MMInitGroupQuery;

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

    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();
            String[] mopks = paramDTO.getMopks();
            String[] hmopks = paramDTO.getHmopks();
            if (MMValueCheck.isEmpty(cpickmids) && (null == cpickmbids || cpickmbids.length == 0) && MMValueCheck.isEmpty(mopks) && MMValueCheck.isEmpty(hmopks)) {
                return null;
            } else {
                AggPickmVO[] aggVOs = null;
                if (!MMValueCheck.isNotEmpty(cpickmids) && !MMValueCheck.isNotEmpty(cpickmbids)) {
                    if (MMValueCheck.isNotEmpty(mopks)) {
                        IPickmQueryServiceForMO service = (IPickmQueryServiceForMO)ServiceLocator.find(IPickmQueryServiceForMO.class);
                        List<PickmTransParam> params = new ArrayList();

                        for(String mopk : mopks) {
                            PickmTransParam param = new PickmTransParam();
                            if (MMValueCheck.isNotEmpty(paramDTO.getAppcode()) && paramDTO.getAppcode().startsWith("5009")) {
                                param.setSourceBillID(mopk);
                            } else {
                                param.setSourceBillRowID(mopk);
                            }

                            params.add(param);
                        }

                        aggVOs = service.queryPickmByMO((PickmTransParam[])params.toArray(new PickmTransParam[0]));
                    }
                } else {
                    IPickmBusinessService service = (IPickmBusinessService)ServiceLocator.find(IPickmBusinessService.class);
                    aggVOs = service.getDeliverAggVOByIDs(cpickmids, cpickmbids);
                }

                if (MMValueCheck.isNotEmpty(hmopks)) {
                    IPickmQueryServiceForMO service = (IPickmQueryServiceForMO)ServiceLocator.find(IPickmQueryServiceForMO.class);
                    List<PickmTransParam> params = new ArrayList();

                    for(String hmopk : hmopks) {
                        PickmTransParam param = new PickmTransParam();
                        param.setSourceBillID(hmopk);
                        params.add(param);
                    }

                    aggVOs = service.queryPickmByMO((PickmTransParam[])params.toArray(new PickmTransParam[0]));
                }

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

                List<String> pks = new ArrayList();

                for(AggPickmVO agg : aggVOs) {
                    for(PickmItemVO item : (PickmItemVO[])agg.getChildrenVO()) {
//                        流程生产订单明细表
                       String id=agg.getParentVO().getVfirstmorowid();
                       if(null != id &&!id.isEmpty()){
                           String sql = "SELECT m.tplanendtime from  mm_mo m "+
                                   "WHERE\n" +
                                   "\tm.cmoid = '"+id+"' ";
                           BaseDAO dao = new BaseDAO();
                           String data =(String) dao.executeQuery(sql, new ColumnProcessor());

                           UFDate now=new UFDate();
//                              领料校验 当前时间必须在计划结束时间前七天
                           if(null!=data && (now.compareTo(new UFDate(data)) >0 || now.getDateAfter(7).compareTo(new UFDate(data)) <0)){
                                   continue;
                           };

                       }
                        pks.add(item.getCpickm_bid());
                    }
                }

                String deliverAppcode = "50080105";
                String deliverPagecode = "5008010501";
                if (MMValueCheck.isNotEmpty(paramDTO.getAppcode()) && paramDTO.getAppcode().startsWith("5009")) {
                    deliverAppcode = "50090105";
                    deliverPagecode = "5009010501";
                } else if (!MMInitGroupQuery.isEnabled(InvocationInfoProxy.getInstance().getGroupId(), NCModule.MMPPAC)) {
                    deliverAppcode = "400802808";
                    deliverPagecode = "5008010501";
                }
                // if(pks.isEmpty()){
                //     pks.add("");
                // }
                Map<String, Object> returnMap = new HashMap();
                returnMap.put("pks", pks.toArray(new String[0]));
                returnMap.put("success", true);
                returnMap.put("deliverAppcode", deliverAppcode);
                returnMap.put("deliverPagecode", deliverPagecode);
                return returnMap;
            }
        } catch (Exception e) {
            ExceptionUtils.wrapException(e);
            return null;
        }
    }
}
