EntityHistoryViewAction.java

/*
 *        @ Copyright 2001 FA Software;
 *        All right reserved. No part of this program may be reproduced or
 *        transmitted in any form or by any means, electronic or
 *        mechanical, including photocopying, recording, or by any
 *        information storage or retrieval system without written
 *        permission from FA Software, except for inclusion of brief
 *        quotations in a review.
 */
package com.mycim.webapp.actions.entityhistory;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.ems.Entity;
import com.mycim.valueobject.ems.EntityStatus;
import com.mycim.webapp.Constants;
import com.mycim.webapp.TemplateLocation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.ObjectHistoryCommentInfoForm;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * 设备、光罩历史状态查询
 *
 * @author weike.li
 * @date 2019/8/26
 * @since 1.8
 **/
public class EntityHistoryViewAction extends EmsSetupAction {

    private static final String TRUE_Y_UPPER = "Y";

    private static final String CANCEL_MAIN = "cancelMain";

    private static final String TRANS_COMMENT = "transComment";

    private static final String SEARCH = "search";

    private static final String IN_FROM_EQPT_INFO = "inFromEqptInfo";

    private static final String IN_FROM_OPERATORPANEL = "inFromOperatorPanel";

    private static final String RETICLE = "reticle";

    private static final String ENTITY_ID = "entityId";

    private static final String MAIN_EQPT = "MAINEQPT";

    private static final String HISTORY = "HISTORY";

    private static final String NUMBER_1 = "1";

    private static final String NUMBER_5 = "5";

    private static final String SHOWFLAG = "showFlag";


    public ActionForward query(ActionMapping mapping, ObjectHistoryCommentInfoForm theform,
                               HttpServletRequest request) {
        Long facilityRrn = LocalContext.getFacilityRrn();
        request.setAttribute(SessionNames.DATE_FORMAT_KEY, DateUtils.DATE_FORMAT4D);
        request.setAttribute("showFlag", request.getParameter("showFlag"));

        if (TRUE_Y_UPPER.equals(request.getParameter(CANCEL_MAIN))) {
            request.setAttribute("cancelMain", "Y");
        }

        String printDate = DateUtils.formatDate(new Date(), DateUtils.DATE_FORMAT4DAY);

        if (request.getParameter(TRANS_COMMENT) != null) {
            theform.setPages((Integer) request.getAttribute("pagesnumber"));

            return mapping.findForward("eventhisory");
        }
        if (request.getParameter(SEARCH) != null) {
            if (mapping.getAttribute() != null) {
                request.removeAttribute(mapping.getAttribute());
            }

            theform.setDueDateS(printDate);
            theform.setDueDateE(printDate);
            request.setAttribute("dueDateE", printDate);
            request.setAttribute("dueDateS", printDate);

            return mapping.findForward("main");
        }
        if (StringUtils.isNotBlank(WebUtils.getParameter(IN_FROM_EQPT_INFO, request))) {
            request.setAttribute("showFlag", "1");
        }
        if (StringUtils.isNotBlank(WebUtils.getParameter(IN_FROM_OPERATORPANEL, request))) {
            request.setAttribute("showFlag", "1");
        }

        String id = "";

        if (request.getParameter(ENTITY_ID) != null) {
            id = request.getParameter("entityId").trim().toUpperCase();
        } else {
            id = theform.getInstanceId().trim().toUpperCase();
        }

        theform.setInstanceId(id);

        String namedSpace = baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY);
        NamedObject entityObject = baseService
                .getNamedObject(new NamedObject(theform.getInstanceId(), namedSpace, ObjectList.ENTITY_KEY));
        if (null == entityObject) {
            Assert.isFalse(NUMBER_1.equals(request.getParameter(SHOWFLAG)),
                           Errors.create().key(MessageIdList.EQUIPMENT_MISSING_ID).content("设备号为空或不存在").build());

            Assert.isFalse(NUMBER_5.equals(request.getParameter(SHOWFLAG)),
                           Errors.create().key(MessageIdList.RETICLE_RETICLEID_INVALID).content("光罩号不存在!").build());
        }
        Entity entity = emsService.getEntity(new Entity(entityObject.getInstanceRrn()));
        request.setAttribute(SessionNames.ENTITY_KEY, entity);

        Assert.isFalse(!MAIN_EQPT.equalsIgnoreCase(entity.getObjectType()) &&
                               !RETICLE.equalsIgnoreCase(entity.getObjectType()) &&
                               !"CARRIER".equalsIgnoreCase(entity.getObjectType()) &&
                               !"POD".equalsIgnoreCase(entity.getObjectType()) &&
                               !"DOOR".equalsIgnoreCase(entity.getObjectType()),
                       Errors.create().key(MessageIdList.ENTITY_MISSING).content("找不到entity").build());

        theform.setNamedSpace(entity.getNamedSpace());
        theform.setInstanceDesc(entity.getInstanceDesc());
        theform.setAttributeValue(entity.getObjectType());

        request.setAttribute("nameSpace_exp", entity.getNamedSpace());
        request.setAttribute("instanceDesc_exp", entity.getInstanceDesc());
        request.setAttribute("objectType_exp", entity.getObjectType());
        request.setAttribute("objectId_exp", entity.getInstanceId());

        List<Map> entityHistory = emsService
                .getEntityEventHistory(entity.getInstanceRrn(), theform.getDueDateS(), theform.getDueDateE());
        List<EntityStatus> entityStatus = emsService.getEntityStatuss(entity.getInstanceRrn());

        entityStatus = rebuildEntityStatus(entityStatus, entity.getObjectType());

        List<Map> commentsHistory = emsService.getEntityCommentsHistory(entity.getInstanceRrn());

        if ((request.getParameter(Constants.FIRST_KEY) != null) || (request.getParameter(Constants.PREV_KEY) != null) ||
                (request.getParameter(Constants.NEXT_KEY) != null) ||
                (request.getParameter(Constants.LAST_KEY) != null)) {

            processItemAction(request, theform);
            String dest = "commentshisory";

            String historytype = theform.getObjectType();
            if (historytype != null && HISTORY.equalsIgnoreCase(historytype)) {
                request.setAttribute("historyType", theform.getObjectType());
                request.setAttribute("entitystatus", entityStatus);
                request.setAttribute("entityhistory", entityHistory);
                dest = "eventhisory";
            }
            request.setAttribute("commentshistory", commentsHistory);
            return mapping.findForward(dest);
        }

        String dest = "eventhisory";
        if (theform.getObjectType() != null && HISTORY.equalsIgnoreCase(theform.getObjectType())) {
            Assert.isFalse(StringUtils.isBlank(theform.getDueDateS()) || StringUtils.isBlank(theform.getDueDateE()),
                           Errors.create().key(MessageIdList.EQUIPMENT_START_END_DATES_EMPTY).content("开始时间和结束时间不能为空")
                                 .build());

            request.setAttribute("entitystatus", entityStatus);
            request.setAttribute("entityhistory", entityHistory);
        } else {
            request.setAttribute("commentshistory", commentsHistory);
            dest = "commentshisory";
        }
        request.setAttribute("historyType", theform.getObjectType());
        theform.setPages(1);
        return mapping.findForward(dest);
    }

    public List<EntityStatus> rebuildEntityStatus(List<EntityStatus> entityStatus, String entityType) {
        for (EntityStatus object : entityStatus) {
            String ss = object.getStatusGroupId();
            String temp = "";

            temp = sysService.referenceDetailExchangeNull("$STATUS_GROUP", entityType, ss, "data_1_value");

            object.setStatusGroupId(temp);
        }
        return entityStatus;
    }

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        Long facilityRrn = LocalContext.getFacilityRrn();
        request.setAttribute(SessionNames.DATE_FORMAT_KEY, DateUtils.DATE_FORMAT4D);
        request.setAttribute("showFlag", request.getParameter("showFlag"));

        if (TRUE_Y_UPPER.equals(request.getParameter(CANCEL_MAIN))) {
            request.setAttribute("cancelMain", "Y");
        }

        ObjectHistoryCommentInfoForm theform = (ObjectHistoryCommentInfoForm) form;
        String printDate = DateUtils.formatDate(new Date(), DateUtils.DATE_FORMAT4DAY);

        if (request.getParameter(TRANS_COMMENT) != null) {
            theform.setPages((Integer) request.getAttribute("pagesnumber"));

            return mapping.findForward("eventhisory");
        }
        if (request.getParameter(SEARCH) != null) {
            if (mapping.getAttribute() != null) {
                request.removeAttribute(mapping.getAttribute());
            }

            theform.setDueDateS(printDate);
            theform.setDueDateE(printDate);
            request.setAttribute("dueDateE", printDate);
            request.setAttribute("dueDateS", printDate);

            return mapping.findForward("main");
        }

        if (request.getParameter(RETICLE) != null) {
            if (mapping.getAttribute() != null) {
                request.removeAttribute(mapping.getAttribute());
            }
            theform.setDueDateS(printDate);
            theform.setDueDateE(printDate);
            if (StringUtils.isNotBlank(request.getParameter(ENTITY_ID))) {
                request.setAttribute("reticleId", request.getParameter(ENTITY_ID));
            }
            return mapping.findForward("reticle");
        }

        String id = "";

        if (request.getParameter(ENTITY_ID) != null) {
            id = request.getParameter("entityId").trim().toUpperCase();
        } else if (null != theform.getInstanceId()) {
            id = theform.getInstanceId().trim().toUpperCase();
        }

        theform.setInstanceId(id);

        String namedSpace = baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY);
        NamedObject entityObject = baseService
                .getNamedObject(new NamedObject(theform.getInstanceId(), namedSpace, ObjectList.ENTITY_KEY));
        Assert.isFalse(null == entityObject,
                       Errors.create().key(MessageIdList.EQUIPMENT_MISSING_ID).content("设备号为空或不存在").build());

        Entity entity = emsService.getEntity(new Entity(entityObject.getInstanceRrn()));
        request.setAttribute(SessionNames.ENTITY_KEY, entity);

        Assert.isFalse(!MAIN_EQPT.equalsIgnoreCase(entity.getObjectType()) &&
                               !RETICLE.equalsIgnoreCase(entity.getObjectType()) &&
                               !"CARRIER".equalsIgnoreCase(entity.getObjectType()) &&
                               !"POD".equalsIgnoreCase(entity.getObjectType()) &&
                               !"DOOR".equalsIgnoreCase(entity.getObjectType()),
                       Errors.create().key(MessageIdList.ENTITY_MISSING).content("找不到entity").build());

        theform.setNamedSpace(entity.getNamedSpace());
        theform.setInstanceDesc(entity.getInstanceDesc());
        theform.setAttributeValue(entity.getObjectType());

        request.setAttribute("nameSpace_exp", entity.getNamedSpace());
        request.setAttribute("instanceDesc_exp", entity.getInstanceDesc());
        request.setAttribute("objectType_exp", entity.getObjectType());
        request.setAttribute("objectId_exp", entity.getInstanceId());


        List<Map> entityHistory = emsService
                .getEntityEventHistory(entity.getInstanceRrn(), theform.getDueDateS(), theform.getDueDateE());
        List<EntityStatus> entityStatus = emsService.getEntityStatuss(entity.getInstanceRrn());

        entityStatus = rebuildEntityStatus(entityStatus, entity.getObjectType());

        List<Map> commentsHistory = emsService.getEntityCommentsHistory(entity.getInstanceRrn());

        if ((request.getParameter(Constants.FIRST_KEY) != null) || (request.getParameter(Constants.PREV_KEY) != null) ||
                (request.getParameter(Constants.NEXT_KEY) != null) ||
                (request.getParameter(Constants.LAST_KEY) != null)) {

            processItemAction(request, theform);
            String dest = "commentshisory";

            String historytype = theform.getObjectType();
            if (historytype != null && HISTORY.equalsIgnoreCase(historytype)) {
                request.setAttribute("historyType", theform.getObjectType());
                request.setAttribute("entitystatus", entityStatus);
                request.setAttribute("entityhistory", entityHistory);
                dest = "eventhisory";
            }
            request.setAttribute("commentshistory", commentsHistory);
            return mapping.findForward(dest);
        }

        String dest = "eventhisory";

        if (theform.getObjectType() != null && HISTORY.equalsIgnoreCase(theform.getObjectType())) {
            Assert.isFalse(StringUtils.isBlank(theform.getDueDateS()) || StringUtils.isBlank(theform.getDueDateE()),
                           Errors.create().key(MessageIdList.EQUIPMENT_START_END_DATES_EMPTY).content("开始时间和结束时间不能为空")
                                 .build());

            request.setAttribute("entitystatus", entityStatus);
            request.setAttribute("entityhistory", entityHistory);

        } else {
            request.setAttribute("commentshistory", commentsHistory);
            dest = "commentshisory";
        }

        request.setAttribute("historyType", theform.getObjectType());

        theform.setPages(1);

        return mapping.findForward(dest);
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) {
        request.setAttribute(SessionNames.DATE_FORMAT_KEY, "YYYY/MM/DD");
        request.setAttribute("showFlag", request.getParameter("showFlag"));

        if (TRUE_Y_UPPER.equals(request.getParameter(CANCEL_MAIN))) {
            request.setAttribute("cancelMain", "Y");
        }

        if (mapping.getAttribute() != null) {
            request.removeAttribute(mapping.getAttribute());
        }
        String showFlag = request.getParameter("showFlag");

        if (NUMBER_1.equals(showFlag)) {
            return mapping.findForward("main");
        } else if (NUMBER_5.equals(showFlag)) {
            return mapping.findForward("reticle");
        }
        return WebUtils.NULLActionForward;
    }

    //add export function by yongyang.jiang 20191104
    public ActionForward export(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        ObjectHistoryCommentInfoForm theform = (ObjectHistoryCommentInfoForm) form;
        String id = "";
        if (request.getParameter(ENTITY_ID) != null) {
            id = request.getParameter("entityId").trim().toUpperCase();
        } else if (null != theform.getInstanceId()) {
            id = theform.getInstanceId().trim().toUpperCase();
        }
        theform.setInstanceId(id);
        String namedSpace = baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY);
        NamedObject entityObject = baseService
                .getNamedObject(new NamedObject(theform.getInstanceId(), namedSpace, ObjectList.ENTITY_KEY));
        Entity entity = emsService.getEntity(new Entity(entityObject.getInstanceRrn()));
        Map<String, Object> titles = WebUtils.getExportTitles(request);
        String exportDateTime = DateUtils.getNowTime(DateUtils.DATE_FORMAT4NOSPLICING);
        List<Map> data = emsService
                .getEntityEventHistory(entity.getInstanceRrn(), theform.getDueDateS(), theform.getDueDateE());
        List<EntityStatus> entityStatus = emsService.getEntityStatuss(entity.getInstanceRrn());
        entityStatus = rebuildEntityStatus(entityStatus, entity.getObjectType());
        for (Map datum : data) {
            List list = (ArrayList) datum.get("status");
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < entityStatus.size(); j++) {
                if (j < list.size()) {
                    Map e = (Map) list.get(j);
                    sb.append(e.get("currentstatus"));
                    sb.append(" ");
                }
            }
            datum.put("currentstatus", sb.toString());
        }
        titles.put("title", theform.getInstanceId().toUpperCase() + " Status History Info");
        String fileName = "EQPStatusHistoryInfo_" + exportDateTime + ".xlsx";
        WebUtils.exportExcel(fileName, titles, data, TemplateLocation.EQP_STATUS_HIST, response);
        return WebUtils.NULLActionForward;
    }

    //获取子设备信息
    public ActionForward getChamber(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        request.setAttribute(SessionNames.DATE_FORMAT_KEY, DateUtils.DATE_FORMAT4D);
        ObjectHistoryCommentInfoForm theform = (ObjectHistoryCommentInfoForm) form;
        String namedSpace = baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY);
        NamedObject entityObject = baseService
                .getNamedObject(new NamedObject(theform.getInstanceId(), namedSpace, ObjectList.ENTITY_KEY));
        Assert.isFalse(null == entityObject,
                       Errors.create().key(MessageIdList.EQUIPMENT_MISSING_ID).content("设备号为空或不存在").build());

        Entity entity = emsService.getEntity(new Entity(entityObject.getInstanceRrn()));
        List<Entity> chamberList = emsService.getChildChamberEquip(entity.getInstanceRrn());
        if (chamberList != null && chamberList.size() > 0) {
            request.setAttribute("chamberList", chamberList);
        }
        return mapping.findForward("main");
    }

}