LotStatusAction.java

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

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.math.NumberUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.prp.ProcessPlanning;
import com.mycim.valueobject.prp.ProcessVersion;
import com.mycim.webapp.Constants;
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.*;

/**
 * @author Luopeng.Wang
 * @version 6.0.0
 * @date 2019/9/22
 **/
public class LotStatusAction extends WipSetupAction {
    private static final String THISPAGE_KEY = "thisPage";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {

        Map<String, Object> result = new HashMap<String, Object>();

        result.put("sortCondition", "lotId");

        WebUtils.setDefaultPageInfo(request);
        request.setAttribute("lotStatusInfos", new ArrayList<Map<String, String>>());
        request.setAttribute("pageInfo", result);
        request.setAttribute("sequence", "Ascending");

        return mapping.findForward(Constants.INIT_KEY);
    }

    public ActionForward query(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        String sequence = "Desc".equalsIgnoreCase(request.getParameter("sequence")) ? "Desc" : "Ascending";
        String sortCondition = setSortCondition(theform, request);
        Map condition = setQueryCondition(theform, LocalContext.getFacilityRrn(), sortCondition, sequence);

        int current = 1;
        if (StringUtils.isNotEmpty(WebUtils.getParameter(THISPAGE_KEY, request)) &&
                StringUtils.isEmpty(WebUtils.getParameter("page", request))) {
            current = (int) NumberUtils.toDouble(WebUtils.getParameter(THISPAGE_KEY, request));
        }

        int pageSize = WebUtils.getParameterInt("pageSize", request);

        if (pageSize <= 0) {
            pageSize = 20;
        }

        Page page = new Page();
        page.setPageSize(pageSize);
        page.setPageNo(current);

        page = lotQueryService.qryLotStatus(page, condition);

        request.setAttribute("lotStatusInfos", page.getResults());
        request.setAttribute("sequence", sequence);
        request.setAttribute("thisPage", current);
        request.setAttribute("pageSize", pageSize);
        request.setAttribute("maxPage", page.getTotalPages());
        request.setAttribute("returnProcessId",theform.getProcessId());
        request.setAttribute("returnProcessVersion",theform.getProcessVersion());
        request.setAttribute("returnRouteId",theform.get_routeId());

        return mapping.findForward(Constants.INIT_KEY);
    }

    private String setSortCondition(LotInfoForm theform, HttpServletRequest request) {
        String sortCondition = theform.getSortCondition();

        if ("sort".equals(request.getParameter("sortType")) &&
                ("LOT_STATUS".equals(sortCondition) || "PRODUCT_ID".equals(sortCondition))) {
            sortCondition = theform.getSortCondition();
        } else if ("changePage".equals(request.getParameter("sortType"))) {
            sortCondition = theform.getSortCondition();
        } else {
            sortCondition = "LOT_ID";
        }

        return sortCondition;
    }

    private HashMap<String, Object> setQueryCondition(LotInfoForm theform, Long facilityRrn, String sortCondition,
                                                      String sequence) throws Exception {
        HashMap<String, Object> queryCondition = new HashMap<String, Object>();

        String objectType = theform.getObjectType();
        String productId = theform.getProductId();
        String processId = theform.getProcessId();
        String operationId = theform.getOperationId();
        String lotStatus = theform.getLotStatus();

        long productRrn = StringUtils.isNotEmpty(productId) ? baseService
                .getNamedObjectRrn(productId.trim().toUpperCase(),
                                   baseService.getNamedSpace(facilityRrn, ObjectList.PRODUCT_KEY),
                                   ObjectList.PRODUCT_KEY) : -1;
        long processRrn = StringUtils.isNotEmpty(processId) ? baseService
                .getNamedObjectRrn(processId.trim().toUpperCase(),
                                   baseService.getNamedSpace(facilityRrn, ObjectList.WFL_KEY), ObjectList.WFL_KEY) : -1;
        long operationRrn = StringUtils.isNotEmpty(operationId) ? baseService
                .getNamedObjectRrn(operationId.trim().toUpperCase(),
                                   baseService.getNamedSpace(facilityRrn, ObjectList.OPERATION_KEY),
                                   ObjectList.OPERATION_KEY) : -1;

        Assert.isFalse(productRrn == 0 || processRrn == 0 || operationRrn == 0,
                       Errors.create().content("No such object exist!").build());

        queryCondition.put("sortCondition", sortCondition);
        queryCondition.put("sequence", sequence);
        queryCondition.put("productId", StringUtils.isNotEmpty(productId) ? productId : null);
        queryCondition.put("processId", StringUtils.isNotEmpty(processId) ? processId : null);
        queryCondition.put("operationId", StringUtils.isNotEmpty(operationId) ? operationId : null);
        queryCondition.put("lotStatus", StringUtils.isNotEmpty(lotStatus) ? lotStatus : null);

        queryCondition.put("productRrn", productRrn != -1 ? new Long(productRrn) : null);
        queryCondition.put("processRrn", processRrn != -1 ? new Long(processRrn) : null);
        queryCondition.put("operationRrn", operationRrn != -1 ? new Long(operationRrn) : null);
        queryCondition.put("facilityRrn", facilityRrn);
        queryCondition.put("objectType", objectType);

        return queryCondition;
    }

    private Map getPageInfo(int current, int fixSize, int resultCount, Map queryCondition) throws Exception {
        HashMap<String, Object> result = new HashMap<String, Object>();

        int thisPage = current;
        int lastPageSize = (int) resultCount % fixSize;
        int pageSize = fixSize;
        int maxPage = (int) (resultCount / fixSize + 1);

        result.put("currentPage", thisPage);
        result.put("lastPageSize", lastPageSize);
        result.put("pageSize", pageSize);
        result.put("maxPage", maxPage);
        result.put("sequence", queryCondition.get("sequence"));

        return result;
    }

    public List<Map<String, String>> getProcessVersionByProcess(Map map) throws Exception {
        List<Map<String, String>> result = new ArrayList<>();
        String processId = MapUtils.getString(map, "processId");
        long processRrn = getInstanceRrn(processId, LocalContext.getFacilityRrn(), ObjectList.WFL_KEY);
        if (processRrn > 0) {
            ProcessPlanning processPlanning = new ProcessPlanning(processRrn);
            List<ProcessVersion> processVersions = prpService.getProcessVersions(processPlanning);
            Assert.isFalse(CollectionUtils.isEmpty(processVersions),
                           Errors.create().key(MessageIdList.PROCESS_NO_VERSION)
                                 .content("There is no version under the process!").build());
            processVersions.forEach(processVersion -> {
                String key = String.valueOf(processVersion.getInstanceVersion());
                String text = key + "(" + processVersion.getVersionStatus() + ")";
                Map<String, String> m = new LinkedHashMap<>(2);
                m.put("key", key);
                m.put("text", text);
                result.add(m);
            });
        }
        return result;
    }

    public Map<String, Object> queryProcess(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String productId = StringUtils.trimToUpperCase(MapUtils.getString(map, "productId"));
        Assert.isFalse(StringUtils.isBlank(productId),
                       Errors.create().key(MessageIdList.BATCHFUTUREHOLDLOT_SELECT_PRODUCTID)
                             .content("The Part Have Not Select Or KeyIn!").build());

        long productRrn = getInstanceRrn(productId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn),
                                         ObjectList.PRODUCT_KEY);
        Assert.isFalse(productRrn <= 0,
                       Errors.create().key(MessageIdList.PRODUCT_PRODUCT_MISSING).content("产品不存在!").build());
        List<Map> processIdList = new ArrayList<>();
        List<String> processIds = prpService.getProcessIdsByProductRrn(productRrn);
        for (String processId : processIds) {
            Map processIdMap = new HashMap(processIds.size());
            processIdMap.put("processId", processId);
            processIdMap.put("processRrn", getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY));
            processIdList.add(processIdMap);
        }
        Map<String, Object> result = new HashMap();
        result.put("processIds", processIdList);
        result.put("productId", productId);
        result.put("productRrn", productRrn);
        return result;
    }
}