PodPortalAction.java

package com.mycim.webapp.actions.pod.podportal;

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.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.SystemConstant;
import com.mycim.valueobject.consts.EventName;
import com.mycim.valueobject.consts.PODConts;
import com.mycim.valueobject.consts.PcdStatus;
import com.mycim.valueobject.ems.POD;
import com.mycim.valueobject.ems.PcdClean;
import com.mycim.valueobject.sys.ReferenceFileDetail;
import com.mycim.webapp.actions.CarrierSetupAction;
import org.apache.commons.collections.MapUtils;
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;
import java.util.regex.Pattern;

/**
 * Pod Portal
 *
 * @author yanbing.chen
 * @date 2019/8/31
 * @since 1.8
 **/
public class PodPortalAction extends CarrierSetupAction {

    public final static int PCD_ID_LENGTH = 7;

    public final static int CLEAN_CYCLE_NUM_SIX = 6;

    public final static String POD_ID = "podId";

    public final static String POD_ALLOWABLE_EVENT = "podAllowableEvent";

    public final static String POD_CLEAN_CYLE = "podCleanCycle";


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

    public ActionForward podInfoTab(HttpServletRequest request, ActionMapping mapping) {
        request.setAttribute("pcdId", request.getParameter("podId"));
        request.setAttribute("transId", "pod");
        return mapping.findForward("pcdInfoTab");
    }

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

    public Map getPodInfoDetail(Map map) {
        String podId = map.get("podId").toString();
        long facilityRrn = LocalContext.getFacilityRrn();
        Map<String, Object> dataMap = new HashMap<String, Object>(10);
        if (StringUtils.isNotBlank(podId)) {
            String namedSpace = baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY);
            dataMap = carrierService.getPodInfoDetailById(podId, namedSpace);
            PcdClean cleanInfo = carrierService.getPcdCleanInfo(MapUtils.getLong(dataMap, "podRrn"));
            if (cleanInfo != null && cleanInfo.getInstanceRrn() > 0) {
                dataMap.put("cleanCycle", cleanInfo.getCleanCycle());
                dataMap.put("cleanEqpt", cleanInfo.getCleanEqptId());
            }
            // check是否拥有从当前状态切换为目标状态的权限
            Map<String, Object> btnMap = this.carrierService
                    .checkPcdChangeStatusButton(facilityRrn, MapUtils.getLong(dataMap, "podRrn"),
                                                MapUtils.getString(dataMap, "podStatus"));
            dataMap.put("btnPermissionByEvent", btnMap);
        }
        return dataMap;
    }

    public Map queryPodByPage(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        Map<String, Object> argMap = new HashMap<String, Object>(10);
        argMap.put("startRow", map.get("start"));
        argMap.put("limit", map.get("limit"));
        argMap.put("podId", StringUtils.trim(map.get("POD").toString()));
        argMap.put("status", StringUtils.trim(map.get("formPodStatus").toString()));
        argMap.put("type", StringUtils.trim(map.get("formPodType").toString()));
        argMap.put("category", StringUtils.trim(map.get("formPodCategory").toString()));
        argMap.put("facility", facilityRrn);
        Map<String, Object> dataMap = new HashMap<String, Object>(20);
        dataMap = carrierService.qryPodListByPage(argMap);
        return dataMap;
    }

    public POD queryPodDetail(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String podId = map.get("podId").toString();
        Assert.isFalse(StringUtils.isBlank(podId),
                       Errors.create().key(MessageIdList.POD_ID_CANNOT_EMPTY).content("Pod号不能为空!").build());
        long podRrn = baseService
                .getNamedObjectRrn(podId, this.getNamedSpace(ObjectList.ENTITY_KEY, facilityRrn), ObjectList.ENTITY_KEY,
                                   ObjectList.POD_KEY);
        Assert.isFalse(podRrn < 1, Errors.create().key(MessageIdList.POD_POD_ID_MISSING).content("Pod号不存在!").build());
        POD pod = carrierService.getPod(podRrn);
        PcdClean podCleanInfo = this.carrierService.getPcdCleanInfo(pod.getInstanceRrn());
        if (podCleanInfo != null && podCleanInfo.getInstanceRrn() > 0) {
            pod.setCleanCycle(podCleanInfo.getCleanCycle());
        }
        return pod;
    }


    public void delete(Map map) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        String podId = map.get("winPodId").toString();
        Assert.isFalse(StringUtils.isBlank(podId),
                       Errors.create().key(MessageIdList.POD_ID_CANNOT_EMPTY).content("Pod号不能为空!").build());
        carrierService.deletePodEntity(podId, facilityRrn, user);
    }

    public List<Map> combobxData(HttpServletRequest request) {
        List<Map> list = putDefaultData();
        String type = request.getParameter("type");
        List<ReferenceFileDetail> comboData = queryComboDataList(type);

        for (ReferenceFileDetail detail : comboData) {
            if (SystemConstant.Pcd.CARRIER_TYPE.equalsIgnoreCase(type)) {
                if (StringUtils.isBlank(detail.getData2Value())) {
                    continue;
                }
            }
            Map jo = new HashMap(10);
            if (SystemConstant.Pcd.POD_COLOR.equalsIgnoreCase(type)) {
                jo.put("value", detail.getData4Value()); // 实际传递的值
            } else {
                jo.put("value", detail.getKey1Value()); // 实际传递的值
            }
            jo.put("key", detail.getData1Value()); // 显示在页面的值

            list.add(jo);
        }
        return list;
    }

    /**
     * 检查输入的podid是否已经存在
     *
     * @return
     */
    public Map checkPod(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        Map result = new HashMap(20);
        String podId = map.get("winPodId").toString();
        Assert.isTrue(StringUtils.isNotBlank(podId),
                      Errors.create().key(MessageIdList.POD_ID_CANNOT_EMPTY).content("Pod号不能为空!").build());
        podId = StringUtils.trimToUpperCase(podId);

        checkNamedObjectIdRule(podId, "Pod");

        long podRrn = baseService.getNamedObjectRrn(podId, baseService.getNamedSpace(facilityRrn, ObjectList.FACILITY_KEY),
                                   ObjectList.ENTITY_KEY, ObjectList.POD_KEY);


        Assert.isTrue(podRrn < 1, Errors.create().key(MessageIdList.POD_POD_ID_EXISTS).content("Pod号已存在!").build());

        long namedObjectRrn = baseService.getNamedObjectRrn(podId, baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY),
                                   ObjectList.ENTITY_KEY);
        Assert.isTrue(namedObjectRrn < 1,
                      Errors.create().key(MessageIdList.CARRIER_ID_EXISTS).content("系统中已存在此ID!").build());

        Map<String, Object> allowableMap = baseService
                .getAllowableEventSetFirst(baseService.getNamedSpace(facilityRrn, ObjectList.ALLOWABLEEVENTSET_KEY),
                                           ObjectList.POD_KEY);
        result.put("podId", podId);
        result.put("allowableEvent", allowableMap.get("allwoableEventId"));
        return result;
    }

    public void modify(Map map) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        Map<String, Object> attrMap = getRequestParameterMap(map);
        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, POD_ID)),
                       Errors.create().key(MessageIdList.POD_ID_CANNOT_EMPTY).content("Pod号不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, SystemConstant.Pcd.POD_STATUS)),
                       Errors.create().key(MessageIdList.POD_POD_STATUS_EMPTY).content("Pod状态不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, SystemConstant.Pcd.POD_TYPE)),
                       Errors.create().key(MessageIdList.POD_POD_TYPE_EMPTY).content("Pod类型不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, POD_ALLOWABLE_EVENT)),
                       Errors.create().key(MessageIdList.CARRIER_ALLOWABLE_EVENT_EMPTY).content("允许事件集号不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, SystemConstant.Pcd.POD_CATEGORY)),
                       Errors.create().key(MessageIdList.POD_POD_CATEGORY_EMPTY).content("Pod类别不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, POD_CLEAN_CYLE)),
                       Errors.create().key(MessageIdList.CARRIER_CLEAN_CYCLE_EMPTY).content("清洗周期不能为空!").build());


        String podCleanCycle = MapUtils.getString(attrMap, POD_CLEAN_CYLE);
        Assert.isFalse(StringUtils.length(podCleanCycle) > CLEAN_CYCLE_NUM_SIX,
                       Errors.create().key(MessageIdList.CARRIER_CLEAN_LENGTH_EXCEED_SIX).content("清洗周期不能超过6位数!")
                             .build());

        // 修改操作
        long podRrn = baseService.getNamedObjectRrn(MapUtils.getString(attrMap, "podId"),
                                                    baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY),
                                                    ObjectList.ENTITY_KEY, ObjectList.POD_KEY);

        Assert.isTrue(podRrn > 0, Errors.create().key(MessageIdList.POD_POD_ID_MISSING).content("Pod号不存在!").build());
        // 只有FREE状态才能允许修改数据
        POD pod = carrierService.getPod(podRrn);
        Assert.isTrue(StringUtils.isNotBlank(pod.getPodStatus()) &&
                              StringUtils.equalsIgnoreCase(pod.getPodStatus(), PcdStatus.FREE_KEY),
                      Errors.create().key(MessageIdList.POD_FREE_STATUS_CAN_MODIFY).content("只有FREE状态的Pod才能修改!!")
                            .build());
        attrMap.put("podRrn", podRrn);
        carrierService.updatePodEntity(attrMap, facilityRrn, user);
    }

    private Map<String, Object> getRequestParameterMap(Map paramMap) {
        Map<String, Object> map = new HashMap<String, Object>(20);
        map.put("podId", paramMap.get("winPodId"));
        map.put("podDesc", paramMap.get("winPodDesc"));
        map.put("podStatus", paramMap.get("winPodStatus"));
        map.put("podType", paramMap.get("winPodType"));
        map.put("podColor", paramMap.get("winPodColor"));
        map.put("podPosition", paramMap.get("winPodLocation"));
        map.put("podSlotCount", paramMap.get("winSlotCount"));
        map.put("podPollutionLevel", paramMap.get("winPollutionLevel"));
        map.put("podAllowableEvent", paramMap.get("winPodAllowableEvent"));
        map.put("podEngineerGroupID", paramMap.get("winPodEngineerGroupID"));
        map.put("podCategory", paramMap.get("winPodCategory"));
        map.put("podCleanCycle", paramMap.get("winPodCleanCycle"));
        return map;
    }

    public void save(Map map) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        Map<String, Object> attrMap = getRequestParameterMap(map);
        String podId;
        Assert.isFalse(StringUtils.isBlank(podId = MapUtils.getString(attrMap, POD_ID)),
                       Errors.create().key(MessageIdList.POD_ID_CANNOT_EMPTY).content("Pod号不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, SystemConstant.Pcd.POD_STATUS)),
                       Errors.create().key(MessageIdList.POD_POD_STATUS_EMPTY).content("Pod状态不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, SystemConstant.Pcd.POD_TYPE)),
                       Errors.create().key(MessageIdList.POD_POD_TYPE_EMPTY).content("Pod类型不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, POD_ALLOWABLE_EVENT)),
                       Errors.create().key(MessageIdList.CARRIER_ALLOWABLE_EVENT_EMPTY).content("允许事件集号不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, SystemConstant.Pcd.POD_CATEGORY)),
                       Errors.create().key(MessageIdList.POD_POD_CATEGORY_EMPTY).content("Pod类别不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(MapUtils.getString(attrMap, POD_CLEAN_CYLE)),
                       Errors.create().key(MessageIdList.CARRIER_CLEAN_CYCLE_EMPTY).content("清洗周期不能为空!").build());

        String podCleanCycle = MapUtils.getString(attrMap, POD_CLEAN_CYLE);
        Assert.isFalse(StringUtils.length(podCleanCycle) > CLEAN_CYCLE_NUM_SIX,
                       Errors.create().key(MessageIdList.CARRIER_CLEAN_LENGTH_EXCEED_SIX).content("清洗周期不能超过6位数!")
                             .build());

        checkNamedObjectIdRule(podId, "Pod");

        long podRrn = baseService.getNamedObjectRrn(MapUtils.getString(attrMap, "podId"),
                                                    baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY),
                                                    ObjectList.ENTITY_KEY, ObjectList.POD_KEY);

        Assert.isTrue(podRrn < 1, Errors.create().key(MessageIdList.POD_POD_ID_EXISTS).content("Pod号已存在!").build());
        long namedObjectRrn = baseService.getNamedObjectRrn(MapUtils.getString(attrMap, "podId"), baseService
                .getNamedSpace(facilityRrn, ObjectList.FACILITY_KEY), ObjectList.ENTITY_KEY);
        Assert.isTrue(namedObjectRrn < 1,
                      Errors.create().key(MessageIdList.CARRIER_ID_EXISTS).content("系统中已存在此ID!").build());
        carrierService.addPodEntity(attrMap, facilityRrn, user);
        initStatusByEvent(facilityRrn, user, MapUtils.getString(attrMap, "podId"), ObjectList.POD_KEY,
                          EventName.INIT_POD);
    }


    public Map<String,Object> generatePodId(Map<String,Object> param){
        String type = MapUtils.getString(param,"type");
        String podId = generatePCDId(SystemConstant.Pcd.POD, type);
        return new HashMap<String,Object>(){{
            put("podId",podId);
        }};
    }

}