SplitRunCardPortalAction.java

package com.mycim.webapp.actions.splitruncardportal;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.jdbc.Page;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.automonitor.dto.LotAutoMonitorQueryDTO;
import com.mycim.valueobject.bas.Relation;
import com.mycim.valueobject.runcard.dto.RunCardPortalQueryDTO;
import com.mycim.valueobject.runcard.util.RunCardConstants;
import com.mycim.valueobject.runcard.util.RunCardStoreSubStatus;
import com.mycim.valueobject.runcard.util.RunCardUtils;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotRecoveryRunCard;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.SRCLotSpecialStep;
import com.mycim.webapp.TemplateLocation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.RunCardAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author pinyan.song
 * @version 6.0.0
 * @date 2020-1-3 14:04
 **/
public class SplitRunCardPortalAction extends RunCardAction {
    /**
     * Action 方法:init
     **/
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        request.setAttribute("portalType", request.getParameter("portalType"));
        return mapping.findForward("lotPortal");
    }

    /**
     * Action 方法:exportLotQuery
     **/
    public ActionForward exportLotQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileName = "lotListInfo_" + DateUtils.getNowTime(DateUtils.DATE_FORMAT4DAYD) + ".xlsx";
        Map<String, Object> titles = WebUtils.getExportTitles(request);
        List<Map<String, Object>> data = (List<Map<String, Object>>) WebUtils.getExportDatas(request);
        Assert.isFalse(CollectionUtils.isEmpty(data), Errors.create().content("No Data to Export!").build());
        data.forEach(m -> {
            if (StringUtils.equals(MapUtils.getString(m, "timeLimit"), "-10000")) {
                m.put("timeLimit", "");
            }
        });
        titles.put("title", "lotListInfo");
        int i = 0;
        for (Map<String, Object> m : data) {
            i++;
            m.put("seq", i);
        }

        WebUtils.exportExcel(fileName, titles, data, TemplateLocation.RC_LOT_LIST, response);
        return WebUtils.NULLActionForward;
    }

    public Map<String, Object> queryNew(Map<String, Object> params) {
        Long pageSize = MapUtils.getLong(params, "limit");
        Long pageNo = MapUtils.getLong(params, "page");
        Page page = new Page(pageNo, pageSize);

        RunCardPortalQueryDTO queryDTO = buildRunCardQueryDto(params);

        page = runCardLotInqService.getRunCardPortalPage(page, queryDTO);

        Map<String, Object> map = MapUtils.newHashMap();
        map.put("data", page.getResults());
        map.put("totalCount", page.getTotalItems());
        return map;
    }

    /**
     * Action 方法:query
     **/
    public Map<String, Object> query(Map<String, Object> params) throws Exception {
        String rowStart = MapUtils.getString(params, "start");
        String pageSize = MapUtils.getString(params, "limit");
        String portalType = MapUtils.getString(params, "portalType");

        Map<String, Object> lotPortalFormMap = getConditionMapByRequest(params);
        List<Map<String, Object>> datas = lotRunCardQueryService
                .qrySplitRunCardLotInfo(lotPortalFormMap, rowStart, pageSize);

        // List<Map<String, Object>> datas = lotRunCardQueryService.querySplitRunCardLotInfo(buildRunCardQueryDto
        // (params, rowStart, pageSize));

        int totalCount = 0;
        int qtyTotal = 0;

        for (Map<String, Object> lot : datas) {
            if (totalCount == 0 || qtyTotal == 0) {
                totalCount = MapUtils.getIntValue(lot, "lotCount");
                qtyTotal = MapUtils.getIntValue(lot, "qtyCount");
            }

            if (StringUtils.equals(LotStatus.BONDED, MapUtils.getString(lot, "lotStatus", StringUtils.EMPTY))) {
                int qty = wipQueryService.getLotWafeCount(MapUtils.getLongValue(lot, "lotRRN"));
                lot.put("qty1", qty);
            }

            if (StringUtils.equals(MapUtils.getString(lot, "lotStatus"), LotStatus.WAITING) ||
                    StringUtils.equals(MapUtils.getString(lot, "lotStatus"), LotStatus.HOLD)) {
                Lot constraintLot = new Lot();
                constraintLot.setLotRrn(MapUtils.getLongValue(lot, "lotRRN"));
                constraintLot.setProductRrn(MapUtils.getLongValue(lot, "productRrn"));
                constraintLot.setProcessRrn(MapUtils.getLongValue(lot, "processRrn"));
                constraintLot.setProcessStepVersion("|" + MapUtils.getStringCheckNull(lot, "routeRrn") + ",|");
                constraintLot.setOperationRrn(MapUtils.getLongValue(lot, "stepRrn"));
                constraintLot.setRouteSeq(MapUtils.getStringCheckNull(lot, "routeSeq"));
                constraintLot.setOperationSeq(MapUtils.getStringCheckNull(lot, "stepSeq"));
                constraintLot.setFacilityRrn(LocalContext.getFacilityRrn());

                // 判断批次的机限设置 并组装设备列表--->开始
                List<Relation> constrainPairEquipments = getConstrainPairEquipments(constraintLot);
                if (constrainPairEquipments.size() > 0) {
                    // eqptRelations = constrainPairEquipments;
                    StringBuilder eqptIds = new StringBuilder(StringUtils.EMPTY);
                    for (Relation eqpt : constrainPairEquipments) {
                        if (eqpt != null && StringUtils.isNotBlank(eqpt.getInstanceId())) {
                            eqptIds.append(eqpt.getInstanceId()).append(",");
                        }
                    }
                    eqptIds = new StringBuilder(StringUtils.removeLastChar(eqptIds.toString(), ","));
                    lot.put("eqptId", eqptIds.toString());
                    lot.put("eqptList", eqptIds.toString());
                }
                // 判断批次的机限设置 并组装设备列表--->结束
            }
            String contextPath = WebUtils.getContextPath();
            String splitRuncardUrl =
                    contextPath + "/splitRunCard.do?action=viewRunCardDetail&isReview=false&isPrint=true" +
                            "&runCardRrn=";
            String recoveryUrl =
                    contextPath + "/recoveryRunCard.do?action=runCardDetail&isView=true&isPrint=true" + "&runCardRrn=";
            if (StringUtils.isBlank(MapUtils.getString(lot, "runcardId"))) {
                String[] runcardIds = StringUtils.split(MapUtils.getString(lot, "runcardIdHistory"), ",");
                String[] runcardRrns = StringUtils.split(MapUtils.getString(lot, "runcardRrnHistory"), ",");

                StringBuilder runcardId = new StringBuilder();
                if (runcardIds != null) {
                    for (int i = 0; i < runcardIds.length; i++) {
                        if (runcardId.length() > 0) {
                            runcardId.append(",");
                        }
                        if (StringUtils.startsWithIgnoreCase(runcardIds[i], "SRC")) {
                            runcardId.append("<a href='javascript:return false' onclick=doOpenFree3('")
                                     .append(splitRuncardUrl).append(runcardRrns[i]).append("','").append(runcardIds[i])
                                     .append("');>").append(runcardIds[i]).append("</a>");
                        } else if (StringUtils.startsWithIgnoreCase(runcardIds[i], "RRC")) {
                            runcardId.append("<a href='javascript:return false' onclick=doOpenFree3('")
                                     .append(recoveryUrl).append(runcardRrns[i]).append("','").append(runcardIds[i])
                                     .append("');>").append(runcardIds[i]).append("</a>");
                        }
                    }
                    lot.put("runcardIdHistory", runcardId.toString());
                } else {
                    lot.put("runcardIdHistory", "");
                }
            } else {
                String runcardId = MapUtils.getString(lot, "runcardId");
                if (StringUtils.startsWithIgnoreCase(runcardId, "SRC")) {
                    lot.put("runcardIdHistory",
                            "<a href='javascript:return false' onclick=doOpenFree3('" + splitRuncardUrl +
                                    MapUtils.getLong(lot, "runcardRrn") + "','" + runcardId + "');>" + runcardId +
                                    "</a>");
                } else if (StringUtils.startsWithIgnoreCase(runcardId, "RRC")) {
                    lot.put("runcardIdHistory",
                            "<a href='javascript:return false' onclick=doOpenFree3('" + recoveryUrl +
                                    MapUtils.getLong(lot, "runcardRrn") + "','" + runcardId + "');>" + runcardId +
                                    "</a>");
                }
            }

            if (StringUtils.contains(MapUtils.getString(lot, "lotID"), ".MAIN")) {
                String subStatus = MapUtils.getString(lot, "subStatus");
                if (StringUtils.equalsIgnoreCase(RunCardStoreSubStatus.BANKIN.toString(), subStatus) &&
                        StringUtils.equals(LotStatus.WAITING, MapUtils.getString(lot, "lotStatus"))) {
                    lot.put("recipeID", "");
                    lot.put("lotStatus", RunCardConstants.SHOW_RUNCARD_MAIN_LOT_STATUS_WAITING_SPLIT);

                } else if (StringUtils.equalsIgnoreCase(RunCardStoreSubStatus.AUTO.toString(), subStatus) &&
                        StringUtils.equals(LotStatus.WAITING, MapUtils.getString(lot, "lotStatus"))) {
                    lot.put("recipeID", "");
                    lot.put("lotStatus", RunCardConstants.SHOW_RUNCARD_MAIN_LOT_STATUS_WAITING_REPOS);
                } else if (StringUtils.equals(LotStatus.RUNCARD_FINISH, MapUtils.getString(lot, "lotStatus"))) {
                    lot.put("recipeID", "");
                } else if (StringUtils.equalsIgnoreCase(RunCardStoreSubStatus.AUTO.toString(), subStatus) &&
                        StringUtils.equals(LotStatus.RUNNING, MapUtils.getString(lot, "lotStatus"))) {

                    LotRecoveryRunCard recoveryRunCard = lotRunCardQueryService
                            .getLotRecoveryRunCard(MapUtils.getLong(lot, "runcardRrn"));
                    if (recoveryRunCard != null &&
                            !StringUtils.equalsIgnoreCase(RunCardStoreSubStatus.BANKOUT.toString(), subStatus)) {
                        String runcardAction = recoveryRunCard.getRunCardAction();
                        if (StringUtils.equalsIgnoreCase("CANCELTRACKIN", runcardAction)) {
                            lot.put("lotStatus", RunCardConstants.SHOW_RUNCARD_MAIN_LOT_STATUS_WAITING_CANCEL);
                        } else if (StringUtils.equalsIgnoreCase("TRACKOUT", runcardAction)) {
                            lot.put("lotStatus", RunCardConstants.SHOW_RUNCARD_MAIN_LOT_STATUS_WAITING_DISPATCH);
                        }
                    }
                }
            }
        }
        // 返回数据
        Map<String, Object> map = new HashMap<>(4);
        map.put("data", datas);
        map.put("totalCount", totalCount);
        map.put("qtyTotal", qtyTotal);
        map.put("portalType", MapUtils.getString(params, "portalType"));
        return map;
    }

    public ActionForward jumpToRC(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        String lotId = WebUtils.getParameter("lotId", request);
        put("lotId", lotId, request);
        return mapping.findForward("lotPortal");
    }

    /**
     * 封装页面条件数据
     */
    private Map<String, Object> getConditionMapByRequest(Map<String, Object> params) throws Exception {
        Map<String, Object> lotPortalFormMap = new HashMap<String, Object>();

        String lotId = MapUtils.getString(params, "lotId").trim();
        if (StringUtils.isNotBlank(lotId) && (!StringUtils.endsWithIgnoreCase(lotId, "*")) &&
                (!RunCardUtils.checkLotIdIsRunCardLot(lotId))) {
            lotId = lotId + ".MAIN*";
        }

        lotPortalFormMap.put("lotId", lotId);
        lotPortalFormMap.put("lotType", MapUtils.getString(params, "lotType1").trim());

        if (MapUtils.getObject(params, "lotStatus1") != null && MapUtils.getObject(params, "lotStatus1") != "") {
            // lotStatus 支持多选
            List<String> lotStatus = (List<String>) MapUtils.getObject(params, "lotStatus1");
            lotPortalFormMap.put("lotStatus", lotStatus.toArray(new String[lotStatus.size()]));
        }

        if (MapUtils.getObject(params, "lotCategory1") != null && MapUtils.getObject(params, "lotCategory1") != "") {
            // lot category 支持多选
            List<String> lotCategoryArray = (List<String>) MapUtils.getObject(params, "lotCategory1");
            if (!lotCategoryArray.contains("ALL")) {
                lotPortalFormMap.put("lotCategory", lotCategoryArray.toArray(new String[lotCategoryArray.size()]));
            }
        }

        lotPortalFormMap.put("priority", MapUtils.getString(params, "priority").trim());
        lotPortalFormMap.put("productId", MapUtils.getString(params, "productId").trim());
        lotPortalFormMap.put("routeId", MapUtils.getString(params, "stageId").trim());
        lotPortalFormMap.put("processId", MapUtils.getString(params, "processId1").trim());
        lotPortalFormMap.put("carrierId", MapUtils.getString(params, "cassete").trim());
        lotPortalFormMap.put("productType", MapUtils.getString(params, "productType").trim());

        lotPortalFormMap.put("stationId", MapUtils.getString(params, "stationId").trim());
        lotPortalFormMap.put("eqptGroupId", MapUtils.getString(params, "eqptGroupId1").trim());
        lotPortalFormMap.put("workArea", MapUtils.getString(params, "workArea1").trim());
        lotPortalFormMap.put("holdDep", MapUtils.getString(params, "holdDep").trim());
        if (StringUtils.isNotEmptyTrim(MapUtils.getString(lotPortalFormMap, "holdDep"))) {
            lotPortalFormMap.put("lotStatus", "HOLD");
        }
        lotPortalFormMap.put("showAll", MapUtils.getString(params, "showAll").trim());
        lotPortalFormMap.put("portalType", MapUtils.getString(params, "portalType").trim());

        return lotPortalFormMap;
    }

    private RunCardPortalQueryDTO buildRunCardQueryDto(Map<String, Object> params) {
        RunCardPortalQueryDTO runCardQueryDto = new RunCardPortalQueryDTO();

        runCardQueryDto.setPortalType(MapUtils.getString(params, "portalType"));
        runCardQueryDto.setLotId(MapUtils.getString(params, "lotId"));

        List<String> queryStatus = new ArrayList<>();
        Object lotStatus1 = MapUtils.getObject(params, "lotStatus1");
        if(lotStatus1 instanceof List) {
            List<String> lotStatusList = (List<String>) lotStatus1;
            for (String status : lotStatusList) {
                if (StringUtils.equals(LotStatus.ACTIVE, status)) {
                    for (String activeStatus : LotStatus.getActiveStatus()) {
                        if (!lotStatusList.contains(activeStatus)) {
                            queryStatus.add(activeStatus);
                        }
                    }
                }
            }
            queryStatus.addAll(lotStatusList);
        } else if(lotStatus1 instanceof String) {
            queryStatus.add((String)lotStatus1);
        }
        runCardQueryDto.setLotStatus(queryStatus);

        // lot category 支持多选
        List<String> lotCategoryArray = (List<String>) MapUtils.getObject(params, "lotCategory1");
        runCardQueryDto.setLotCategory(lotCategoryArray);

        runCardQueryDto.setLotType(MapUtils.getString(params, "lotType1"));

        runCardQueryDto.setProductType(MapUtils.getString(params, "productType"));
        runCardQueryDto.setProductId(MapUtils.getString(params, "productId"));
        runCardQueryDto.setCarrierId(MapUtils.getString(params, "cassete"));
        runCardQueryDto.setWorkArea(MapUtils.getString(params, "workArea1"));
        runCardQueryDto.setStationId(MapUtils.getString(params, "stationId"));
        runCardQueryDto.setPriority(MapUtils.getString(params, "priority"));
        runCardQueryDto.setEqpGroupId(MapUtils.getString(params, "eqptGroupId1"));
        runCardQueryDto.setProcessId(MapUtils.getString(params, "processId1"));
        return runCardQueryDto;
    }

}