PostFutureHoldQueryAction.java

package com.mycim.webapp.actions.lot.postfuturehold;

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.valueobject.MessageIdList;
import com.mycim.valueobject.consts.ContextNames;
import com.mycim.valueobject.security.User;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.LotInfoForm;
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 Sola
 * @date 2021/6/30
 **/
public class PostFutureHoldQueryAction extends WipSetupAction {
    private static final String NUM_ONE = "1";

    private static final String CURRENT_FOR_COMPARE = "currentForCompare";

    private static final String THIS_PAGE = "thisPage";

    private static final String PAGE_SIZE = "pageSize";

    public ActionForward queryForLot(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        long facilityRrn = LocalContext.getFacilityRrn();

        Lot lot = initLotBaseInfoForJsp(request, true, null, null);

        Assert.isFalse(lot == null || lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("批次不存在!").build());
        String lotStatus = lot.getLotStatus();
        Assert.isFalse(
                StringUtils.equals(lotStatus, LotStatus.TERMINATED) || StringUtils.equals(lotStatus, LotStatus.FINISH),
                Errors.create().content("Lot status can not be terminated or finish!").build());

        List allFutureHoldForLot = getAllFutureHoldForLot(facilityRrn, lot);

        List<Map> futureReassignList = wipService.getLotFutureReassignInfoList(lot.getLotRrn());

        request.setAttribute("futureReassignList", futureReassignList);
        request.setAttribute("allfutureinfo", allFutureHoldForLot);
        request.setAttribute("lotId", lot.getLotId());

        return mapping.findForward("bylot");

    }

    public ActionForward query(ActionMapping mapping, HttpServletRequest request) {

        long facilityRrn = LocalContext.getFacilityRrn();
        String userId = WebUtils.getParameterUpperCase("instanceId", request);
        String userName = WebUtils.getParameter("userName", request);
        String holdType = ContextNames.EEN_CONTEXT_POST_HOLDLOT + "','" + ContextNames.EEN_CONTEXT_POST_HOLDPRODUCT;
        Map<String, Object> condition = new HashMap();
        condition.put("userId", StringUtils.trimToUpperCase(userId));
        condition.put("userName", userName);
        condition.put("holdType", holdType);
        int fixSize = 20;

        int pageSize = fixSize;
        int current = 1;

        if (request.getParameter(THIS_PAGE) != null) {
            current = new Integer(request.getParameter(THIS_PAGE)).intValue();
        }

        if (request.getParameter(PAGE_SIZE) != null && (request.getParameter(PAGE_SIZE).length()) != 0) {
            pageSize = new Integer(request.getParameter(PAGE_SIZE)).intValue();
        }

        if (NUM_ONE.equals(request.getParameter(CURRENT_FOR_COMPARE))) {
            current = 1;
        }
        List<Map<String, Object>> list = new ArrayList<>();
        Page page = new Page(current, fixSize);
        if (StringUtils.isNotBlank(userId) || StringUtils.isNotBlank(userName)) {
            page = ctxExecService.getFutureHoldPage(page, condition);
            list = (List) page.getResults();
            if (CollectionUtils.isNotEmpty(list)) {
                int i = 0;
                for (Map<String, Object> map : list) {
                    i++;
                    if (current > 1) {
                        map.put("rowNum", i + fixSize * (current - 1));
                    } else {
                        map.put("rowNum", i);
                    }
                    long lotRrn = MapUtils.getLongValue(map, "lotRrn", 0L);
                    if (lotRrn > 0) {
                        Lot lot = lotQueryService.getLot(lotRrn);
                        map.put("processVersion", lot.getProcessVersion());
                        map.put("processRrn", lot.getProcessRrn());
                        map.put("productRrn", lot.getProductRrn());
                    } else {
                        Long productRrn = MapUtils.getLongValue(map, "productRrn");
                        Long processRrn = MapUtils.getLongValue(map, "processRrn");
                        long processVer = prpService.getLastAvaliableProcessVersion(productRrn, processRrn);
                        map.put("processVersion", processVer);
                        map.put("processRrn", processRrn);
                        map.put("productRrn", productRrn);
                    }
                    map.put("facilityRrn", facilityRrn);
                    map.put("flowSeq", ctxExecService.getFlowSeqByProcessInfo(map));
                    map.put("stepDesc", ctxExecService.getOperationDescByProcessInfo(map));
                    map.put("stageId", ctxExecService.getStageByProcessInfo(map));
                    long processRrn = MapUtils.getLongValue(map, "processRrn");
                    if (processRrn > 0) {
                        map.put("processId", baseService.getNamedObjectId(processRrn));
                    }
                    long routeRrn = MapUtils.getLongValue(map, "routeRrn");
                    if (routeRrn > 0) {
                        map.put("routeId", baseService.getNamedObjectId(routeRrn));
                    }
                    long operationRrn = MapUtils.getLongValue(map, "operationRrn");
                    if (operationRrn > 0) {
                        map.put("operationId", baseService.getNamedObjectId(operationRrn));
                    }
                }
                //sortFutureHoldInfosByFlowSeq(list);
            }
        }

        if(StringUtils.isBlank(userId)){
            request.setAttribute("name",userName);
        }else {
            User user = securityService.getUser(userId, facilityRrn);
            request.setAttribute("name",user.getUserName());
        }
        request.setAttribute("list", list);
        request.setAttribute("current", current);
        request.setAttribute("lastPageSize", (page.getTotalItems()) % fixSize);
        request.setAttribute("pageSize", pageSize);
        request.setAttribute("maxPage", page.getTotalPages() > 0 ? page.getTotalPages() : 1);
        request.setAttribute("instanceId", userId);

        return mapping.findForward("postFutureHoldPortal");
    }
}