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

package nccloud.web.mmpac.issue.action;

import java.util.ArrayList;
import java.util.List;
import nc.itf.mmpac.pickm.IPickmBusinessService;
import nc.util.mmf.framework.base.MMArrayUtil;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpac.pickm.entity.PickmItemVO;
import nc.vo.mmpac.pickm.entity.PickmViewVO;
import nccloud.dto.mmpac.pickm.pub.entity.PickmDeliverDTO;
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.framework.web.ui.pattern.grid.Grid;
import nccloud.framework.web.ui.pattern.grid.GridOperator;
import nccloud.web.mmpac.issue.scale.IssueScaleForPickm;
import nccloud.web.mmpac.issue.util.IssueInitDataUtil;
import nccloud.web.mmpac.issue.util.IssueMapCatch;
import nccloud.web.mmpac.issue.view.IssueValueHandle;
import nccloud.web.mmpac.pwr.util.PwrUtil;

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

    public Object doAction(IRequest request) {
        try {
            IJson json = JsonFactory.create();
            PickmDeliverDTO dto = (PickmDeliverDTO)json.fromJson(request.read(), PickmDeliverDTO.class);
            IPickmBusinessService service = (IPickmBusinessService)ServiceLocator.find(IPickmBusinessService.class);
            AggPickmVO[] aggPickVO = null;
            if (dto.getDeliverType() == 1) {
                aggPickVO = service.getDeliverAggVOByIDs((String[])null, dto.getCpickmbids());
            } else {
                aggPickVO = service.getSetDeliverAggVOByParams(dto.getSetParams(), dto.getDeliverType());
            }

            List<PickmViewVO> list = new ArrayList();
            if(null!=aggPickVO){
                for(AggPickmVO aggVO : aggPickVO) {
                    PickmItemVO[] itemVOs = (PickmItemVO[])aggVO.getChildren(PickmItemVO.class);
                    if (!MMArrayUtil.isEmpty(itemVOs)) {
                        for(PickmItemVO itemVO : itemVOs) {
                            PickmViewVO pickmViewVO = new PickmViewVO();
                            pickmViewVO.setItem(itemVO);
                            pickmViewVO.setHead(aggVO.getParentVO());
                            pickmViewVO.setPrimaryKey(aggVO.getPrimaryKey());
                            list.add(pickmViewVO);
                        }
                    }
                }
            }else{
            }
            PickmViewVO[] vos = new PickmViewVO[0];
            if(list.size() > 0){
                vos = (PickmViewVO[])list.toArray(new PickmViewVO[0]);
            }else{
                PickmViewVO pickmViewVO = new PickmViewVO();
                return new Grid();
            }
            String pageCode = "5008010501";
            if (dto.getIsDispersed() != null && dto.getIsDispersed()) {
                pageCode = "5009010501";
            }

            if (dto.getPageCode() != null) {
                pageCode = dto.getPageCode();
            }

            GridOperator operator = new GridOperator(pageCode);
            Grid grid = operator.toGrid(vos);
            IssueMapCatch mc = IssueInitDataUtil.fillCatchInitDataForPickm(vos);
            IssueValueHandle.setIssuePickmValue(grid, vos, mc);
            IssueMapCatch returnMc = new IssueMapCatch();
            returnMc.setIsWWMap(mc.getIsWWMap());
            returnMc.setDeliveryOrgMap(mc.getDeliveryOrgMap());
            String issuePickmCache = json.toJson(returnMc);
            grid.setUserjson(issuePickmCache);
            this.afterProcess(grid);
            PwrUtil pwrUtil = new PwrUtil();
            pwrUtil.dealGridFlow(grid);
            return grid;
        } catch (Exception e) {
            ExceptionUtils.wrapException(e);
            return null;
        }
    }

    public void afterProcess(Grid grid) {
        IssueScaleForPickm scaleUtil = new IssueScaleForPickm();
        scaleUtil.processGrid(grid);
    }
}
