ContextvalueHistoryAction.java

package com.mycim.webapp.actions.process;

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.math.NumberUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.ContextNames;
import com.mycim.valueobject.prp.Context;
import com.mycim.valueobject.prp.ContextValueHistory;
import com.mycim.valueobject.prp.ProcessVersion;
import com.mycim.webapp.TemplateLocation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
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 weike.li
 * @version 6.0.0
 * @date 2019/9/3
 **/
public class ContextvalueHistoryAction extends PrpSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        String from = request.getParameter("from");
        String productId = request.getParameter("productId");
        String processId = request.getParameter("processId");
        String processVersionId = request.getParameter("processVersionId");
        request.setAttribute("from", from);
        request.setAttribute("productId", productId);
        request.setAttribute("processId", processId);
        request.setAttribute("processVersionId", processVersionId);
        return mapping.findForward("setup");
    }

    public Map queryHistoryList(Map requestMap) {

        Long facilityRrn = LocalContext.getFacilityRrn();
        String productId = StringUtils.trimToUpperCase((String) requestMap.get("productId"));
        String processId = StringUtils.trimToUpperCase((String) requestMap.get("processId"));

        Long pageNo = NumberUtils.toLong(requestMap.get("pageNo").toString());
        Long pageSize = NumberUtils.toLong(requestMap.get("pageSize").toString());

        Page page = new Page(pageNo, pageSize);

        Assert.isFalse(StringUtils.isBlank(productId),
                       Errors.create().key(MessageIdList.PRODUCT_EMPTY_ID).content("Please select product id!")
                             .build());
        long productRrn = this.getInstanceRrn(productId, facilityRrn, ObjectList.PRODUCT_KEY);
        Assert.isFalse(productRrn <= 0, Errors.create().key(MessageIdList.PRODUCT_PRODUCT_MISSING)
                                              .content("The product id does not exist!").build());

        Assert.isFalse(StringUtils.isBlank(processId),
                       Errors.create().key(MessageIdList.PROCESS_EMPTY_ID).content("Please select the process id!")
                             .build());
        long processRrn = this.getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY);
        Assert.isFalse(processRrn <= 0, Errors.create().key(MessageIdList.PROCESS_PROCESS_MISSING)
                                              .content("The process id does not exist!").build());
        List<Long> processRrnsByProductRrn = prpService.getProcessRrnsByProductRrn(productRrn);
        Assert.isFalse(processRrnsByProductRrn == null || (!processRrnsByProductRrn.contains(processRrn)),
                       Errors.create().key(MessageIdList.PRODUCT_PRODUCT_PROCESS_NOT_BOUND)
                             .content("Process isn't bonded to the product!").build());
        Integer processVersionId = parseProcessVersion(StringUtils.trim((String) requestMap.get("processVersionId")));
        ProcessVersion processVersion = prpService.getProcessVersion(processRrn, processVersionId);
        Assert.notNull(processVersion, Errors.create().key(MessageIdList.PROCESS_MISSING_VERSION)
                                             .content("The processVersion isn't existed!").build());

        Map baseInfo = new HashMap(4);
        baseInfo.put("productRrn", productRrn);
        baseInfo.put("processRrn", processRrn);
        baseInfo.put("processVersion", processVersionId);

        String namedSpace = getNamedSpace(ObjectList.CONTEXT_KEY, facilityRrn);

        Context recipeContext = new Context(ContextNames.RECIPE_CONTEXT, namedSpace, ObjectList.CONTEXT_KEY);
        List<Context> contexts = new ArrayList<>();
        recipeContext = ctxService.getContext(recipeContext);
        if (recipeContext != null) {
            contexts.add(recipeContext);
        }
        Context edcContext = new Context(ContextNames.EDC_CONTEXT_LOT, namedSpace, ObjectList.CONTEXT_KEY);
        edcContext = ctxService.getContext(edcContext);
        if (edcContext != null) {
            contexts.add(edcContext);
        }
        Context reticleFamilyContext = new Context(ContextNames.RETICLEFAMILY_CONTEXT, namedSpace,
                                                   ObjectList.CONTEXT_KEY);
        reticleFamilyContext = ctxService.getContext(reticleFamilyContext);
        if (reticleFamilyContext != null) {
            contexts.add(reticleFamilyContext);
        }
        Context stageContext = new Context(ContextNames.STAGE_CONTEXT, namedSpace, ObjectList.CONTEXT_KEY);
        stageContext = ctxService.getContext(stageContext);
        if (stageContext != null) {
            contexts.add(stageContext);
        }

        Context processLocationContext = new Context(ContextNames.PROCESS_LOCATION_CONTEXT, namedSpace,
                                                     ObjectList.CONTEXT_KEY);
        processLocationContext = ctxService.getContext(processLocationContext);
        if (processLocationContext != null) {
            contexts.add(processLocationContext);
        }

        Context operationDescContext = new Context(ContextNames.PROCESS_OPERATION_DESC_CONTEXT, namedSpace,
                                                   ObjectList.CONTEXT_KEY);
        operationDescContext = ctxService.getContext(operationDescContext);
        if (operationDescContext != null) {
            contexts.add(operationDescContext);
        }

        Context flowSeqContext = new Context(ContextNames.RESEQUENCE_CONTEXT, namedSpace, ObjectList.CONTEXT_KEY);
        flowSeqContext = ctxService.getContext(flowSeqContext);
        if (flowSeqContext != null) {
            contexts.add(flowSeqContext);
        }
        Context operationCsecContext = new Context(ContextNames.OPERATION_CSEC_CONTEXT, namedSpace,
                                                   ObjectList.CONTEXT_KEY);
        operationCsecContext = ctxService.getContext(operationCsecContext);
        if (operationCsecContext != null) {
            contexts.add(operationCsecContext);
        }

        page.setBaseInfo(baseInfo);
        page = ctxService.getFullContextValueHistory(page);
        Collection results = page.getResults();
        List array = new ArrayList();
        for (Object obj : results) {
            ContextValueHistory contextValueHistory = (ContextValueHistory) obj;
            Map map = new HashMap();
            map.put("transId", contextValueHistory.getTransId());
            for (Context context : contexts) {
                if (contextValueHistory.getContextRrn() == context.getInstanceRrn()) {
                    String routeId = "";
                    String operationId = "";
                    String resultValue = "";
                    String newResultValue = "";
                    String transType = "";
                    String transUser = "";
                    if (StringUtils.equals(ContextNames.RECIPE_CONTEXT, context.getInstanceId())) {
                        routeId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey2()));
                        operationId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey1()));
                        resultValue = getInstanceId(NumberUtils.toLong(contextValueHistory.getOldResultValue1()));
                        if (StringUtils.isNotBlank(contextValueHistory.getResultValue1())) {
                            newResultValue = getInstanceId(NumberUtils.toLong(contextValueHistory.getResultValue1()));
                        }
                        transUser = contextValueHistory.getTransUser();
                        transType = "RECIPE";
                    } else if (StringUtils.equals(ContextNames.EDC_CONTEXT_LOT, context.getInstanceId())) {
                        routeId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey2()));
                        operationId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey1()));
                        resultValue = getInstanceId(NumberUtils.toLong(contextValueHistory.getOldResultValue1()));
                        newResultValue = getInstanceId(NumberUtils.toLong(contextValueHistory.getResultValue1()));
                        transUser = contextValueHistory.getTransUser();
                        transType = "EDC";
                    } else if (StringUtils.equals(ContextNames.RETICLEFAMILY_CONTEXT, context.getInstanceId())) {
                        routeId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey3()));
                        operationId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey4()));
                        resultValue = getInstanceId(NumberUtils.toLong(contextValueHistory.getOldResultValue1()));
                        newResultValue = getInstanceId(NumberUtils.toLong(contextValueHistory.getResultValue1()));
                        transUser = contextValueHistory.getTransUser();
                        transType = "RETICLE";
                    } else if (StringUtils.equals(ContextNames.STAGE_CONTEXT, context.getInstanceId())) {
                        routeId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey4()));
                        operationId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey6()));
                        resultValue = contextValueHistory.getOldResultValue1();
                        newResultValue = contextValueHistory.getResultValue1();
                        transUser = contextValueHistory.getTransUser();
                        transType = "STAGE";
                    } else if (StringUtils.equals(ContextNames.PROCESS_LOCATION_CONTEXT, context.getInstanceId())) {
                        routeId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey4()));
                        operationId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey5()));
                        resultValue = contextValueHistory.getOldResultValue1();
                        newResultValue = contextValueHistory.getResultValue1();
                        transUser = contextValueHistory.getTransUser();
                        transType = "PROCESSLOCATION";
                    } else if (StringUtils
                            .equals(ContextNames.PROCESS_OPERATION_DESC_CONTEXT, context.getInstanceId())) {
                        routeId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey4()));
                        operationId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey5()));
                        resultValue = contextValueHistory.getOldResultValue1();
                        newResultValue = contextValueHistory.getResultValue1();
                        transUser = contextValueHistory.getTransUser();
                        transType = "OPERATIONDESC";
                    } else if (StringUtils.equals(ContextNames.RESEQUENCE_CONTEXT, context.getInstanceId())) {
                        routeId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey3()));
                        operationId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey4()));
                        resultValue = contextValueHistory.getOldResultValue1();
                        if (StringUtils.isNotBlank(contextValueHistory.getResultValue1())) {
                            newResultValue = contextValueHistory.getResultValue1();
                        }
                        transUser = contextValueHistory.getTransUser();
                        transType = "FLOWSEQ";
                    } else if (StringUtils.equals(ContextNames.OPERATION_CSEC_CONTEXT, context.getInstanceId())) {
                        routeId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey2()));
                        operationId = getInstanceId(NumberUtils.toLong(contextValueHistory.getContextKey1()));
                        resultValue = contextValueHistory.getOldResultValue3();
                        newResultValue = contextValueHistory.getResultValue3();
                        transUser = contextValueHistory.getTransUser();
                        transType = "CONTAMINATION";
                    }

                    if (StringUtils.equals("add", contextValueHistory.getTransId())) {
                        newResultValue = resultValue;
                        resultValue = "";
                    }

                    map.put("routeId", routeId == null ? "N/A" : routeId);
                    map.put("operationId", operationId);
                    map.put("resultValue",
                            resultValue == null || StringUtils.equalsIgnoreCase("N/A", resultValue) ? "" : resultValue);
                    map.put("newResultValue", newResultValue == null ||
                            StringUtils.equalsIgnoreCase("N/A", newResultValue) ? "" : newResultValue);
                    map.put("transType", transType);
                    map.put("transTime", DateUtils.formatDate(contextValueHistory.getTransTime()));
                    map.put("transUser", transUser);
                }
            }
            array.add(map);
        }

        Map msg = new HashMap();
        msg.put("resultInfo", array);
        msg.put("itemCount", page.getTotalItems());
        if (page.isFirstPage()) {
            msg.put("isFirst", "true");
        }
        if (page.isLastPage()) {
            msg.put("isLast", "true");
        }
        return msg;
    }

    public ActionForward export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> args = new HashMap<>();
        args.put("pageNo", 1);
        args.put("pageSize", Integer.MAX_VALUE);
        args.put("productId", request.getParameter("productId"));
        args.put("processId", request.getParameter("processId"));
        args.put("processVersionId", request.getParameter("processVersionId"));
        Map historyList = queryHistoryList(args);
        List<Map> resultInfo = (List<Map>) historyList.get("resultInfo");

        int i = 1;
        for (Map map : resultInfo) {
            map.put("rowNum", i++);
        }
        Map<String, Object> titles = WebUtils.getExportTitles(request);
        titles.put("title", "Context Value History Info");
        titles.put("titleProductId", "Product Id: " + request.getParameter("productId"));
        titles.put("titleProcessId", "ProcessId: " + request.getParameter("processId"));
        titles.put("titleProcessVersionId", "Process Ver.: " + request.getParameter("processVersionId"));

        String exportDateTime = DateUtils.getNowTime(DateUtils.DATE_FORMAT4NOSPLICING);
        String fileName = "ContextValueHistoryInfo_" + exportDateTime + ".xlsx";
        WebUtils.exportExcel(fileName, titles, resultInfo, TemplateLocation.CONTEXT_VALUE_HISTORY, response);
        return WebUtils.NULLActionForward;
    }

}