SortJobQueryAction.java

package com.mycim.webapp.actions.sort;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.fa.sesa.threadlocal.LocalContextNames;
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.valueobject.MessageIdList;
import com.mycim.valueobject.consts.HoldCodeNames;
import com.mycim.valueobject.consts.SorterEnum;
import com.mycim.valueobject.sorter.SortJobBean;
import com.mycim.valueobject.sorter.SorterBean;
import com.mycim.valueobject.sorter.SorterDetailBean;
import com.mycim.valueobject.sorter.StatusBean;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.webapp.WebUtils;
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.*;

/**
 * @author yibing.liu
 * @version 1.0
 * @date 2021/06/29
 */
public class SortJobQueryAction extends SortAbsAction {


    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        if (request.getParameter("mainJobRrn") != null) {
            return sortJobDetail(mapping, form, request);
        }
        return mapping.findForward("init");
    }

    public Page querySortJobList(ActionMapping mapping, Map paramMap, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        Long start = MapUtils.getLong(paramMap, "start");
        Long limit = MapUtils.getLong(paramMap, "limit");
        String sourceCarrierId = MapUtils.getString(paramMap, "sourceCarrierId");
        String targetCarrierId = MapUtils.getString(paramMap, "targetCarrierId");
        String jobType = MapUtils.getString(paramMap, "jobType");
        String jobStatus = MapUtils.getString(paramMap, "status");

        Page page = new Page();
        long pageNo = 1;
        if (start > 0) {
            pageNo = start / limit + 1;
        }
        page.setPageNo(pageNo);
        page.setPageSize(limit);
        page = sorterQueryService.querySorterList(page,
                                                  new SorterBean(sourceCarrierId, targetCarrierId, jobStatus, jobType));
        return page;
    }

    public ActionForward cancelSortJob(ActionMapping mapping, ActionForm form,
                                       HttpServletRequest request) throws Exception {
        long mainJobRrn = WebUtils.getParameterLong("mainJobRrn", request);
        if (mainJobRrn > 0) {
            sorterService.deleteSorter(new SorterBean(mainJobRrn));
        }
        request.setAttribute("success", 1);
        request.setAttribute("status", SorterEnum.Constant.CANCEL);
        return sortJobDetail(mapping, form, request);
    }

    public ActionForward manualSortJob(ActionMapping mapping, ActionForm form,
                                       HttpServletRequest request) throws Exception {
        long mainJobRrn = WebUtils.getParameterLong("mainJobRrn", request);
        if (mainJobRrn > 0) {
            sorterService.manualSortJob(new SorterBean(mainJobRrn));
        }
        request.setAttribute("success", 1);
        request.setAttribute("status", SorterEnum.Constant.FINISH);
        return sortJobDetail(mapping, form, request);
    }

    public ActionForward manualAutoMonitorSorter(ActionMapping mapping, ActionForm form,
                                                 HttpServletRequest request) throws Exception {
        long mainJobRrn = WebUtils.getParameterLong("mainJobRrn", request);
        long subJobRrn = WebUtils.getParameterLong("subJobRrn", request);
        String status = request.getAttribute("status") == null ? request.getParameter("status") : (String) request
                .getAttribute("status");

        SortJobBean sortJobBean = sorterQueryService.getSortJobByJobRrnAndStatus(mainJobRrn,
                                                                                 subJobRrn != 0 ? subJobRrn : null,
                                                                                 new StatusBean(status));

        if (Objects.nonNull(sortJobBean)) {
            List<Long> lotRrnList = new ArrayList<>();
            for (SorterBean sorterBean : sortJobBean.getSorterBeans()) {
                for (SorterDetailBean detailBean : sorterBean.getSorterDetailBeanList()) {
                    if (!lotRrnList.contains(detailBean.getLotRrn()) && detailBean.getLotRrn() > 0) {
                        lotRrnList.add(detailBean.getLotRrn());
                    }
                }
            }

            List<String> notHoldLotList = new ArrayList<>();
            for(Long lotRrn :lotRrnList) {
                boolean notHold = true;
                String lotStatus = lotQueryService.getLotStatus(lotRrn);
                if(LotStatus.isHold(lotStatus)) {
                    List<Map> holdReasons = wipQueryService.getHoldReasons(lotRrn);
                    for(Map holdReason:holdReasons) {
                        String holdCode = MapUtils.getString(holdReason,"reasonCode");
                        if(StringUtils.equals(holdCode, HoldCodeNames.SORTER_AUTOMONITOR_MANUAL)) {
                            notHold = false;
                        }
                    }
                } else if (LotStatus.isAutoMonitorMerge(lotStatus)) {
                    notHold = false;
                }
                if(notHold) {
                    notHoldLotList.add(lotInqService.getLotId(lotRrn));
                }
            }

            Assert.state(CollectionUtils.isEmpty(notHoldLotList),
                         Errors.create().key(MessageIdList.SORT_AUTO_MONITOR_NOT_HOLD_BY)
                               .args(notHoldLotList.toString(), HoldCodeNames.SORTER_AUTOMONITOR_MANUAL).build());

            sorterService.manualAutoMonitorSorterJob(new SorterBean(mainJobRrn));
        }

        request.setAttribute("success", 1);
        request.setAttribute("status", SorterEnum.Constant.FINISH);
        return sortJobDetail(mapping, form, request);
    }

    public ActionForward sortJobDetail(ActionMapping mapping, ActionForm form,
                                       HttpServletRequest request) throws Exception {

        Long mainJobRrn = WebUtils.getParameterLong("mainJobRrn", request);
        long subJobRrn = WebUtils.getParameterLong("subJobRrn", request);
        String status =
                request.getAttribute("status") == null ? request.getParameter("status") : (String) request.getAttribute(
                        "status");

        SortJobBean sortJobBean = sorterQueryService.getSortJobByJobRrnAndStatus(mainJobRrn,
                                                                                 subJobRrn != 0 ? subJobRrn : null,
                                                                                 new StatusBean(status));
        String allowCancel = (
                StringUtils.equalsIgnoreCase(sortJobBean.getStatus(), SorterEnum.Status.CANCEL.getStatus()) ||
                        StringUtils.equalsIgnoreCase(sortJobBean.getStatus(), SorterEnum.Status.FINISH.getStatus()) ||
                        StringUtils.equalsIgnoreCase(sortJobBean.getJobType(),
                                                     SorterEnum.JobType.RUN_CARD.getJobType())) ? null : MARK;
        String allowManual = (StringUtils.equalsIgnoreCase(sortJobBean.getStatus(),
                                                           SorterEnum.Status.DISPATCH.getStatus())) ? MARK : null;
        String allowAutoMonitor = SorterEnum.Check.isAutoMonitorType(sortJobBean.getJobType()) && StringUtils
                .equalsIgnoreCase(sortJobBean.getStatus(), SorterEnum.Status.CREATE.getStatus()) ? MARK : null;

        Map<String, Object> lotInfo = buildSortJobDetail(sortJobBean);
        lotInfo.put("allowCancel", allowCancel);
        lotInfo.put("allowManual", allowManual);
        lotInfo.put("allowAutoMonitor", allowAutoMonitor);
        lotInfo.put("allowCloseWin", MARK);
        request.setAttribute("lotInfo", lotInfo);
        return mapping.findForward("detail");
    }

    public List<Map> getJobTypesOrStatus(Map paramMap) {
        String queryType = MapUtils.getString(paramMap, "type");
        List<SorterBean> jobTypes = sorterQueryService.getJobTypeOrStatusList(queryType, LocalContext.get(
                LocalContextNames.LANGUAGE_KEY));
        List<Map> result = new ArrayList<>();
        for (SorterBean sb : jobTypes) {
            Map map = new HashMap(3);
            if ("jobType".equalsIgnoreCase(queryType)) {
                map.put("key", sb.getJobTypeParse());
                map.put("value", sb.getJobType());
            } else {
                map.put("key", sb.getStatusParse());
                map.put("value", sb.getStatus());
            }
            result.add(map);
        }
        return result;
    }

}