EquipmentPortalAction.java

package com.mycim.webapp.actions.equipmentportal;

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.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.SystemConstant;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.valueobject.ems.Entity;
import com.mycim.webapp.TemplateLocation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
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;

public class EquipmentPortalAction extends EmsSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm theform, HttpServletRequest request,
                              HttpServletResponse response) {
        return mapping.findForward("eqptPortal");
    }

    /**
     * 获取页面下拉框数据(从所有lot中group by获取)
     *
     * @return 返回下拉框数据
     */
    public List<Map> getConditionCombo(HttpServletRequest request) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String type = request.getParameter("type");
        String cascadeSource = request.getParameter("cascadeSource");
        List list = new ArrayList();
        List<Long> userGroupRrnList = null;
        if (isSpecialRole()){
            userGroupRrnList = securityService.getUserGroupRrnsByUserRrn(LocalContext.getUserRrn());
            if (userGroupRrnList.isEmpty()){//没有用户组的用户没有权限查看任何设备
                userGroupRrnList.add(-9999L);
            }
        }
        List<String[]> comboList = emsService.qryConditionCombo(facilityRrn, type, cascadeSource, userGroupRrnList);
        if (CollectionUtils.isNotEmpty(comboList)) {
            for (String[] strs : comboList) {
                Map map = new HashMap();
                map.put("value", strs[0]);
                map.put("key", strs[1]);
                list.add(map);
            }
        }
        return list;
    }

    public Map likeQuery(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        Map<String, Object> argMap = getConditionMapByMap(map);
        argMap.put("facility", facilityRrn);
        Map<String, Object> dataMap;
        dataMap = emsService.qryEqptInfo(argMap);
        return dataMap;
    }

    public List<Map> comboData(HttpServletRequest request) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        String type = request.getParameter("type");
        List list = new ArrayList();
        List<String[]> comboData = emsService.qryCombodata(type, facilityRrn, user);
        for (String[] next : comboData) {
            Map map = new HashMap();
            map.put("key", next[0]);
            map.put("value", next[1]);
            list.add(map);
        }
        return list;
    }

    public ActionForward eqptInfoTab(ActionMapping mapping, HttpServletRequest request) {
        String entityId = request.getParameter("entityId");
        request.setAttribute("eqptId", StringUtils.trim(entityId));
        return mapping.findForward("eqptInfoTab");
    }

    public Map getEqptInfo(Map dataMap) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        String entityId = dataMap.get("eqptId").toString();
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isBlank(entityId)) {
            return null;
        } else {
            String namedSpace = baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY);
            NamedObject no = new NamedObject();
            no.setInstanceId(entityId);
            no.setNamedSpace(namedSpace);
            no.setObject(ObjectList.ENTITY_KEY);
            Long eqptRrn = baseService.getNamedObjectRrn(no);

            if (eqptRrn > 0) {
                List<Entity> chamberList = emsService.getChildChamberEquip(eqptRrn);
                Map<String, Object> entity = emsService.getEntityInfo(facilityRrn, entityId, eqptRrn, namedSpace);
                map.put("chamberList", chamberList);
                map.put("entity", entity);
                map.put("changeStatusFlag", 'N');
                map.put("ocapIds", wipQueryService.getActiveOfflineOcapIds(eqptRrn));

                // change eqpt status auth
                if (emsService.checkUserAndEqptStationJurisdiction(user, entityId, facilityRrn)) {
                    map.put("changeStatusFlag", 'Y');
                }
                return map;
            }
        }
        return null;
    }

    public ActionForward addComment(HttpServletRequest request, ActionMapping mapping) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        String transAction = WebUtils.getParameter("transAction", request);
        return getCommentHistory(request, mapping, facilityRrn, user, transAction, "getCommentHistory", true);
    }

    public ActionForward getCommentHistory(HttpServletRequest request, ActionMapping mapping) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        return getCommentHistory(request, mapping, facilityRrn, user, "eqptPortal", "getCommentHistory", false);
    }

    public ActionForward exportEqptInfo(ActionMapping mapping, ActionForm theform, HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        Map<String, Object> argMap = getConditionMapByRequest(request);
        Map dataMap = likeQuery(argMap);
        List<Map> data = (List<Map>) dataMap.get("data");
        Assert.isFalse(CollectionUtils.isEmpty(data), Errors.create().content("No Data to Export!").build());
        // 导出
        String exportDateTime = DateUtils.getNowTime(DateUtils.DATE_FORMAT4NOSPLICING);
        String fileName = "EqptInfoHist" + exportDateTime + ".xlsx";
        Map<String, Object> titles = WebUtils.getExportTitles(request);
        titles.put("title", "设备列表信息");
        WebUtils.exportExcel(fileName, titles, data, TemplateLocation.EQPT_INFO_HIST, response);
        return WebUtils.NULLActionForward;
    }

    private ActionForward getCommentHistory(HttpServletRequest request, ActionMapping mapping, Long facilityRrn,
                                            String user, String transAction, String actionOp, boolean isAdd) {
        String entity = request.getParameter("eqptId");
        String comment = request.getParameter("reason");
        int pageNo = WebUtils.getParameterInt("pageNo", request);
        int pageSize = WebUtils.getParameterInt("pageSize", request);

        if (StringUtils.isNotBlank(entity)) {
            String namedSpace = baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY);
            NamedObject no = new NamedObject();
            no.setInstanceId(entity);
            no.setNamedSpace(namedSpace);
            no.setObject(ObjectList.ENTITY_KEY);
            Long eqptRrn = baseService.getNamedObjectRrn(no);

            if (eqptRrn > 0) {
                if (isAdd) {
                    emsService.addEqptComment(eqptRrn, comment, user);
                    pageNo = 1;
                }
                Map<String, Object> map = emsService.getEqptCommentHistoryByPage(eqptRrn, pageNo, pageSize);
                int totalCount = MapUtils.getIntValue(map, "totalCount");
                List<Map<String, Object>> commentList = (List<Map<String, Object>>) MapUtils
                        .getObject(map, "commentList");

                request.setAttribute("entityId", entity);
                request.setAttribute("pageNo", pageNo);
                request.setAttribute("reason", "");
                request.setAttribute("comments", commentList);
                if (commentList != null && commentList.size() > 0) {
                    request.setAttribute("totalCount", totalCount);
                } else {
                    request.setAttribute("totalCount", -1);
                }
            }
        }

        request.setAttribute("transAction", transAction);
        request.setAttribute("actionOp", actionOp);
        return mapping.findForward("eqptCommentHistory");
    }

    private Map<String, Object> getConditionMapByMap(Map map) {
        Map<String, Object> argMap = new HashMap<>();
        argMap.put("startRow", map.get("start"));
        argMap.put("limit", map.get("limit"));
        argMap.put("eqptGroup", map.get("eqptGroupId"));
        String eqptId = StringUtils.trim(map.get("formEqptId").toString());
        if (eqptId.contains(",")) {
            eqptId = spilt(eqptId);
        }
        argMap.put("eqptId", eqptId);
        argMap.put("eqptStatus", map.get("formEqptStatus"));
        argMap.put("eqptArea", map.get("formEqptWorkArea"));
        argMap.put("eqptLocation", map.get("fromEqptLocation"));
        if (isSpecialRole()){
            List<Long> userGroupRrnList = securityService.getUserGroupRrnsByUserRrn(LocalContext.getUserRrn());
            if (userGroupRrnList.isEmpty()) {
                userGroupRrnList.add(-9999L);
            }
            argMap.put("engineerGroupRrns", userGroupRrnList);
            argMap.put(SystemConstant.Str.SPECIAL_ROLE, true);
        }
        argMap.put("engineerGroup", map.get("engineerGroup"));
        argMap.put("sort", map.get("formSorting"));
        argMap.put("showAll", map.get("showAll"));
        return argMap;
    }

    private Map<String, Object> getConditionMapByRequest(HttpServletRequest request) {
        Map<String, Object> argMap = new HashMap<String, Object>();
        argMap.put("start", request.getParameter("start"));
        argMap.put("limit", request.getParameter("limit"));
        argMap.put("eqptGroupId", StringUtils.trim(request.getParameter("eqptGroupId")));
        argMap.put("formEqptId", StringUtils.trim(request.getParameter("formEqptId")));
        argMap.put("engineerGroup", request.getParameter("engineerGroup"));
        argMap.put("formSorting", request.getParameter("formSorting"));
        argMap.put("showAll", request.getParameter("showAll")); // showAll

        //需要转换的字段( 字段eqptGroupId在dao做的处理,此处不处理了):
        List<String> formEqptStatus = convertToStringList(request.getParameter("formEqptStatus"));
        List<String> formEqptWorkArea = convertToStringList(request.getParameter("formEqptWorkArea"));
        List<String> fromEqptLocation = convertToStringList(request.getParameter("fromEqptLocation"));
        argMap.put("formEqptStatus", CollectionUtils.isNotEmpty(formEqptStatus) ? formEqptStatus : "");
        argMap.put("formEqptWorkArea", CollectionUtils.isNotEmpty(formEqptWorkArea) ? formEqptWorkArea : "");
        argMap.put("fromEqptLocation", CollectionUtils.isNotEmpty(fromEqptLocation) ? fromEqptLocation : "");
        return argMap;
    }

    /**
     * 页面extjs下拉框多选结果转换为list
     **/
    private List<String> convertToStringList(String str) {
        List<String> list = new ArrayList<>();
        if (StringUtils.isNotBlank(str)) {
            //以英文字符“,”做分割
            String[] split = StringUtils.split(str, StringUtils.COMMA_SIGN);
            for (String s : split) {
                if (StringUtils.isNotBlank(s)) {
                    list.add(StringUtils.trim(s));
                }
            }
        }
        return list;
    }

    private String spilt(String str) {
        StringBuffer sb = new StringBuffer();
        String[] temp = str.split(",");
        for (int i = 0; i < temp.length; i++) {
            if (!"".equals(temp[i]) && temp[i] != null) {
                sb.append("'" + temp[i] + "',");
            }
        }
        String result = sb.toString();
        String tp = result.substring(result.length() - 1, result.length());
        if (",".equals(tp)) {
            return result.substring(0, result.length() - 1);
        } else {
            return result;
        }
    }

}