SeasonAction.java

package com.mycim.webapp.actions.season;

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.ObjectUtils;
import com.mycim.framework.utils.lang.StringUtils;
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.ems.Entity;
import com.mycim.valueobject.ems.Season;
import com.mycim.valueobject.ems.SeasonHistory;
import com.mycim.valueobject.ems.SeasonRecipeGroup;
import com.mycim.valueobject.prp.RecipeVersion;
import com.mycim.valueobject.wip.Lot;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
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.Collection;
import java.util.List;
import java.util.Map;


public class SeasonAction extends EmsSetupAction {
    public static final String RESULTMESSAGE = "operate success";

    /**
     * portal界面season查询
     *
     * @param season
     * @return
     */

    public Page querySeason(Season season) {
        String eqpId = StringUtils.replace(StringUtils.trimToUpperCase(season.getEqptId()), "*", "%");
        String seasonId = StringUtils.replace(StringUtils.trimToUpperCase(season.getSeasonId()), "*", "%");
        season.setEqptId(eqpId);
        season.setSeasonId(seasonId);
        return emsService.getSeasonListForPage(season);
    }

    public Page querySeasonHistory(SeasonHistory seasonHistory) {
        String eqpId = StringUtils.replace(StringUtils.trimToUpperCase(seasonHistory.getEqptId()), "*", "%");
        String seasonId = StringUtils.replace(StringUtils.trimToUpperCase(seasonHistory.getSeasonId()), "*", "%");
        seasonHistory.setEqptId(eqpId);
        seasonHistory.setSeasonId(seasonId);
        return emsService.getSeasonListHistory(seasonHistory);
    }

    public ActionForward addSeason(ActionMapping mapping, HttpServletRequest request) {
        String seasonId = WebUtils.getParameterUpperCase("seasonId", request);
        String completeFlag = WebUtils.getParameterUpperCase("completeFlag", request);
        request.setAttribute("seasonId", seasonId);
        request.setAttribute("completeFlag", completeFlag);
        return mapping.findForward("addSeason");
    }

    public Map saveSeason(Map dataMap) {
        Season seasonInfo = buildMapPara(dataMap);
        String seasonId = MapUtils.getString(dataMap, "seasonId");
        seasonId = StringUtils.trimToUpperCase(seasonId);
        Season seasonInfoTemp = new Season();
        seasonInfoTemp.setEqptId(seasonInfo.getEqptId());
        seasonInfoTemp.setSeasonType(seasonInfo.getSeasonType());
        if (StringUtils.equalsIgnoreCase(seasonInfo.getSeasonType(), Season.SEASONTYPE_BYRC)) {
            seasonInfoTemp.setToRecipeGroupId(seasonInfo.getToRecipeGroupId());
            seasonInfoTemp.setFromRecipeGroupId(seasonInfo.getFromRecipeGroupId());
        }
        String seasonIdInDB = emsService.getSeasonId(seasonInfoTemp);

        if (StringUtils.isBlank(seasonId)) {
            Assert.isTrue(StringUtils.isBlank(seasonIdInDB),
                          Errors.create().key(MessageIdList.SEASON_SEASON_SETUP_EXIST)
                                .content("There is already a season of this type!").build());
            seasonInfo.setOperationType(Season.OPERATIONTYPE_ADD);
            seasonInfo.setConstraintRecipeId(StringUtils.trimAllWhitespace(seasonInfo.getConstraintRecipeId()));
            emsService.insertSeasonInfo(seasonInfo);
        } else {
            Assert.isFalse(StringUtils.isNotBlank(seasonIdInDB)
                          && !StringUtils.equalsIgnoreCase(seasonIdInDB, seasonId)
                          && StringUtils.equalsIgnoreCase(seasonInfoTemp.getSeasonType(),Season.SEASONTYPE_BYRC),
                          Errors.create().key(MessageIdList.SEASON_SEASON_SETUP_EXIST)
                                .content("There is already a season of this type!").build());
            long seasonRrn = MapUtils.getLong(dataMap, "seasonRrn");
            seasonInfo.setSeasonRrn(seasonRrn);
            seasonInfo.setSeasonId(seasonId);
            emsService.updateSeasonInfo(seasonInfo);
        }
        dataMap.put("success", true);
        dataMap.put("msg", RESULTMESSAGE);
        return dataMap;
    }

    /**
     * modify season
     *
     * @param dataMap
     * @return
     */
    public Season modifySeason(Map dataMap) {
        Season seasonInfo = new Season();
        String seasonId = MapUtils.getString(dataMap, "seasonId");
        String completeFlag = MapUtils.getString(dataMap, "completeFlag");
        if (StringUtils.isNotBlank(seasonId)) {
            List<Season> seasonInfos = emsService.getSeasonList(StringUtils.upperCase(seasonId), null, null, null);
            if (seasonInfos.size() > 0) {
                seasonInfo = seasonInfos.get(0);
                List<Map<String, Object>> stepInfo = emsService.getSeasonStepInfo(seasonInfo.getSeasonRrn());
                /*if (StringUtils.equalsIgnoreCase(seasonInfo.getSeasonType(), Season.SEASONTYPE_BYIDLETIME)) {
                    String lastRunTime = emsService
                            .getEqpLastRunTime(seasonInfo.getEqptId(), seasonInfo.getConstraintRecipeId(),
                                               seasonInfo.getLastUpdateTime());
                    lastRunTime = StringUtils.isBlank(lastRunTime) ? seasonInfo.getLastUpdateTime() : lastRunTime;
                    seasonInfo.setLastRunTime(lastRunTime);
                }*/
                seasonInfo.setSeasonSteps(stepInfo);
                seasonInfo.setCompleteFlag(StringUtils.upperCase(completeFlag));
            }
        }
        return seasonInfo;
    }

    public Map deleteSeason(Map dataMap) {
        Season seasonInfo = buildMapPara(dataMap);
        String seasonId = MapUtils.getString(dataMap, "seasonId");
        long seasonRrn = MapUtils.getLong(dataMap, "seasonRrn");
        seasonInfo.setSeasonId(seasonId);
        seasonInfo.setSeasonRrn(seasonRrn);
        emsService.deleteSeason(seasonInfo);
        dataMap.put("success", true);
        dataMap.put("msg", RESULTMESSAGE);
        return dataMap;
    }

    public Map seasonComplete(Map<String, Object> dataMap) {
        String seasonId = MapUtils.getString(dataMap, "seasonId");
        List<Season> seasonInfos = emsService.getSeasonList(seasonId, null, null, null);
        Season seasonInfo = seasonInfos.iterator().next();
        seasonInfo.setOperationType(Season.OPERATIONTYPE_SEASONCOMPLETE);
        String resetComment = MapUtils.getString(dataMap, "resetComment");
        seasonInfo.setResetComment(resetComment);
        if (StringUtils.equalsIgnoreCase(seasonInfo.getSeasonType(), Season.SEASONTYPE_BYIDLETIME) ||
                StringUtils.equalsIgnoreCase(seasonInfo.getSeasonType(), Season.SEASONTYPE_BYIDLETFETCH)) {
            emsService.seasonComplete4ByIdleTime(seasonInfo);
        } else if (StringUtils.equalsIgnoreCase(seasonInfo.getSeasonType(), Season.SEASONTYPE_BYPM)) {
            long stepRrn = seasonInfo.getStepRrn();
            List<Map<String, Object>> settingStepInfo = emsService.getSeasonStepInfo(stepRrn);
            List<Map<String, Object>> seasonSteps = buildSeasonStepInfo(dataMap);
            emsService.checkSeasonStepInfo(seasonSteps, settingStepInfo);
            emsService.insertSeasonCompleteInfo(seasonInfo);
        } else if (StringUtils.equalsIgnoreCase(seasonInfo.getSeasonType(), Season.SEASONTYPE_BYRC)) {
            emsService.insertSeasonCompleteInfo(seasonInfo);
        }
        dataMap.put("success", true);
        dataMap.put("msg", RESULTMESSAGE);
        return dataMap;
    }

    public Map createRecipeGroup(Map map) {
        String recipeGrpId = StringUtils.trimToUpperCase(MapUtils.getString(map, "recipeGrpId"));
        Assert.isFalse(StringUtils.isBlank(recipeGrpId), Errors.create().key(MessageIdList.SEASON_RECIPE_GROUP_EMPTY)
                                                               .content("Recipe group id can not be empty!").build());
        Long recipeGrpRrn = emsService.getSeasonRecipeGrp(recipeGrpId);
        Assert.isFalse(recipeGrpRrn.longValue() > 0, Errors.create().key(MessageIdList.SEASON_RECIPE_GROUP_EXIST)
                                                               .content("Recipe group id already exists!").build());
        emsService.insertSeasonRecipeGroupInfo(recipeGrpId);
        map.put("success", true);
        map.put("msg", RESULTMESSAGE);
        return map;
    }

    public Map deleteSeasonRecipeGroup(Map map) {

        String recipeGrpId = StringUtils.upperCase(MapUtils.getString(map, "recipeGrpId"));
        Assert.isFalse(StringUtils.isBlank(recipeGrpId), Errors.create().key(MessageIdList.SEASON_RECIPE_GROUP_EMPTY)
                                                               .content("Recipe group id can not be empty!").build());
        Long recipeGrpRrn = emsService.getSeasonRecipeGrp(recipeGrpId);
        Assert.isFalse(recipeGrpRrn == null || recipeGrpRrn <= 0,
                       Errors.create().key(MessageIdList.SEASON_RECIPE_GROUP_ID_NOEXIST)
                             .content("Recipe group id does not exist!").build());
        String seasonId = emsService.checkRecipeGrpIsUsed(recipeGrpId);
        Assert.isFalse(StringUtils.isNotBlank(seasonId), Errors.create().key(MessageIdList.SEASON_RECIPE_GROUP_USED)
                                                               .content("Recipe group is user by season").args(seasonId)
                                                               .build());
        emsService.deleteSeasonRecipeGroup(recipeGrpRrn, recipeGrpId);
        map.put("success", true);
        map.put("msg", RESULTMESSAGE);
        return map;
    }

    public Map getRecipeGrpInfo(Map map) {

        String recipeGrpId = MapUtils.getString(map, "recipeGrpId");
        recipeGrpId = StringUtils.trimToUpperCase(recipeGrpId);
        Assert.isFalse(StringUtils.isBlank(recipeGrpId), Errors.create().key(MessageIdList.SEASON_RECIPE_GROUP_EMPTY)
                                                               .content("Recipe group id can not be empty!").build());
        Long recipeGrpRrn = emsService.getSeasonRecipeGrp(recipeGrpId);
        Assert.isFalse(recipeGrpRrn == null || recipeGrpRrn <= 0,
                       Errors.create().key(MessageIdList.SEASON_RECIPE_GROUP_ID_NOEXIST)
                             .content("Recipe group id does not exist!").build());
        List<Map<String, Object>> list = emsService.getSeasonRecipeInGroup(recipeGrpId);
        map.put("list", list);
        map.put("success", true);
        return map;
    }

    public Map addRecipToGroup(Map map) {

        String recipeGrpId = MapUtils.getString(map, "recipeGrpId");
        String recipeId = MapUtils.getString(map, "recipeId");
        recipeGrpId = StringUtils.trimToUpperCase(recipeGrpId);
        recipeId = StringUtils.trimToUpperCase(recipeId);
        Assert.isFalse(StringUtils.isBlank(recipeId),
                       Errors.create().key(MessageIdList.RECIPE_INVALID_ID).content("Recipe cannot be empty!").build());
        long recipeRrn = getInstanceRrn(recipeId, LocalContext.getFacilityRrn(), ObjectList.RECIPE_KEY);
        Assert.isFalse(recipeRrn <= 0,
                       Errors.create().key(MessageIdList.RECIPE_NOT_EXISTS).content("Recipe Id:{} does not exist!")
                             .args(recipeId).build());
        Long recipeGrpRrn = emsService.getSeasonRecipeGrp(recipeGrpId);
        String mainRecipeId = recipeService.getMainRecipeId(recipeId);
        Assert.state(StringUtils.equalsIgnoreCase(mainRecipeId, recipeId),
                       Errors.create().key(MessageIdList.SEASON_ADD_SUB_RECIPE_ERROR).content("Cannot add sub Recipe Id with a '-'!")
                             .build());
        emsService.addSeasonRecipeToGroup(recipeId, recipeGrpId, recipeGrpRrn);
        map.put("success", true);
        map.put("msg", RESULTMESSAGE);
        return map;
    }

    public Map deleteRecipeFromRecipeGrp(Map map) {
        String recipeGrpId = MapUtils.getString(map, "recipeGrpId");
        String recipeId = MapUtils.getString(map, "recipeId");
        recipeGrpId = StringUtils.upperCase(recipeGrpId);
        recipeId = StringUtils.upperCase(recipeId);
        Long recipeGrpRrn = emsService.getSeasonRecipeGrp(recipeGrpId);
        emsService.deleteRecipeFromRecipeGrp(recipeId, recipeGrpId, recipeGrpRrn);
        map.put("success", true);
        map.put("msg", RESULTMESSAGE);
        return map;
    }

    public ActionForward seasonPortal(ActionMapping mapping, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward("seasonPortal");
    }

    public ActionForward seasonRecipeGroup(ActionMapping mapping, HttpServletRequest request,
                                           HttpServletResponse response) {
        return mapping.findForward("seasonRecipeGroup");
    }

    private Season buildMapPara(Map<String, Object> dataMap) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        Season seasonInfo = new Season();

        String eqpId = StringUtils.upperCase(StringUtils.trim(MapUtils.getString(dataMap, "objectId")));
        String constraintRecipeId = StringUtils.upperCase(StringUtils.trim(MapUtils.getString(dataMap, "constraintRecipeId")));
        String status = StringUtils.upperCase(MapUtils.getString(dataMap, "status"));
        String fromRecipeGroup = StringUtils.upperCase(MapUtils.getString(dataMap, "fromRecipeGroupId"));
        String toRecipeGroup = StringUtils.upperCase(MapUtils.getString(dataMap, "toRecipeGroupId"));
        String seasonType = MapUtils.getString(dataMap, "seasonType");
        Double idleTime = MapUtils.getDouble(dataMap, "idleTime");
        Double maxRunTime = MapUtils.getDouble(dataMap, "maxRunTime");
        Long fromRecipeGrpRrn = emsService.getSeasonRecipeGrp(fromRecipeGroup);
        Long toRecipeGrpRrn = emsService.getSeasonRecipeGrp(toRecipeGroup);
        String subType = StringUtils.upperCase(MapUtils.getString(dataMap, "subType"));
        Assert.state(StringUtils.isNotBlank(eqpId), Errors.create().key(MessageIdList.EQUIPMENT_ID_CANNOT_EMPTY)
                                                           .content("Equipment ID can not be empty!").build());
        Entity entity = new Entity(eqpId, getNamedSpace(ObjectList.ENTITY_KEY, facilityRrn), ObjectList.ENTITY_KEY);
        entity = emsService.getEntity(entity);
        Assert.state(ObjectUtils.isNotEmpty(entity), Errors.create().key(MessageIdList.EQUIPMENT_ID_NOT_EXIST)
                                                               .content("Equipment ID does not exist!").build());
        boolean flag = entity.getParentEntityRrn() != null && entity.getParentEntityRrn() > 0;
        Assert.isFalse(flag && StringUtils.equalsIgnoreCase(subType, Season.SUBTYPE_LOTLEVEL),
                       Errors.create().key(MessageIdList.SEASON_CHAMBER_EQUIPMENT_WAFER_LEVEL)
                             .content("Chamber equipment must be wafer level.").build());
        Assert.isFalse(!flag && StringUtils.equalsIgnoreCase(subType, Season.SUBTYPE_WAFERLEVEL),
                       Errors.create().key(MessageIdList.SEASON_MAIN_EQUIPMENT_LOT_LEVEL)
                             .content("Main equipment must be lot level.").build());
        Assert.isTrue(StringUtils.isNotBlank(eqpId), Errors.create().key(MessageIdList.EQUIPMENT_ID_CANNOT_EMPTY)
                                                           .content("EquipmentId can not be empty!").build());

        long equipmentRrn = this
                .getInstanceRrn(eqpId, getNamedSpace(ObjectList.ENTITY_KEY, facilityRrn), ObjectList.ENTITY_KEY);
        Assert.isFalse(equipmentRrn <= 0,
                       Errors.create().key(MessageIdList.EQUIPMENT_ID_NOT_EXIST).content("Equipment ID does not exist!")
                             .build());
        Assert.isFalse(StringUtils.isBlank(status),
                       Errors.create().key(MessageIdList.SEASON_STATUS_EMPTY).content("Season status can not be empty.")
                             .build());
        if (StringUtils.equalsIgnoreCase(seasonType, Season.SEASONTYPE_BYRC)) {

            Assert.isFalse(StringUtils.isBlank(fromRecipeGroup),
                           Errors.create().key(MessageIdList.SEASON_FROM_RECIPE_GROUP_EMPTY)
                                 .content("From recipe group can not be empty!").build());
            Assert.isFalse(StringUtils.isBlank(toRecipeGroup),
                           Errors.create().key(MessageIdList.SEASON_TO_RECIPE_GROUP_EMPTY)
                                 .content("To recipe group can not be empty!").build());
            Assert.isFalse(fromRecipeGrpRrn <= 0, Errors.create().key(MessageIdList.SEASON_FROM_RECIPE_GROUP_ID_NOEXIST)
                                                        .content("From recipe group id does not exist!").build());
            Assert.isFalse(toRecipeGrpRrn <= 0, Errors.create().key(MessageIdList.SEASON_TO_RECIPE_GROUP_ID_NOEXIST)
                                                      .content("To recipe group id does not exist!").build());
        } else if (StringUtils.equalsIgnoreCase(seasonType, Season.SEASONTYPE_BYIDLETIME)) {
            Assert.isFalse(ObjectUtils.isEmpty(idleTime) || idleTime <= 0, Errors.create().key(MessageIdList.SEASON_IDLETIME_CHECK)
                                                            .content("Idletime must be greater than 0!").build());
            Assert.isFalse(ObjectUtils.isEmpty(maxRunTime) || maxRunTime <= 0, Errors.create().key(MessageIdList.SEASON_MAXRUNTIME_CHECK)
                                                  .content("MaxRunTime must be greater than 0!").build());
            Assert.isFalse(StringUtils.isEmpty(constraintRecipeId), Errors.create().key(MessageIdList.SEASON_CONSTRAINT_RECIPE_EMPTY)
                                                   .content("Constraint Recipe cannot empty!").build());
             Assert.isFalse(constraintRecipeId.length()>64, Errors.create().key(MessageIdList.SEASON_CONSTRAINT_RECIPE_MORE_64)
                                                                  .content("Constraint Recipe length cannot more than 64!").build());
        }

        seasonInfo.setEqptId(eqpId);
        seasonInfo.setUserId(user);

        seasonInfo.setSeasonType(seasonType);
        seasonInfo.setSubType(subType);
        seasonInfo.setMaxRunTime(maxRunTime);
        seasonInfo.setStatus(status);
        seasonInfo.setConstraintRecipeId(constraintRecipeId);
        seasonInfo.setIdleTime(idleTime);
        seasonInfo.setFromRecipeGroupId(fromRecipeGroup);
        seasonInfo.setToRecipeGroupId(toRecipeGroup);
        seasonInfo.setFromRecipeGroupRrn(fromRecipeGrpRrn);
        seasonInfo.setToRecipeGroupRrn(toRecipeGrpRrn);
        return seasonInfo;
    }

    private List<Map<String, Object>> buildSeasonStepInfo(Map<String, Object> dataMap) {
        long facilityRrn = LocalContext.getFacilityRrn();
        List<Map<String, Object>> steps = new ArrayList<>();
        return steps;

    }

    private List<String> getSubParamByKeyList(Map<String, Object> params, String key) {
        List<String> ret = new ArrayList<>();
        if (params.get(key) == null) {
            return ret;
        } else if ((params.get(key)) instanceof ArrayList) {
            ret.addAll((Collection<? extends String>) params.get(key));
        } else {
            String kVal = MapUtils.getString(params, key);
            ret.add(StringUtils.trim(kVal));
        }
        return ret;
    }

    private void buildRecipeId(Lot lot) {
        RecipeVersion recipeVersion = recipeService.getLotRecipe(lot);
        if (recipeVersion != null) {
            String physicalId = recipeVersion.getPpid();
            lot.setRecipePhysicalId(physicalId);
        }
    }

    /**
     * Season Recipe Group History界面
     * @param mapping
     * @param request
     * @param response
     * @return
     */
    public ActionForward seasonRecipeGroupHistory(ActionMapping mapping, HttpServletRequest request,
                                           HttpServletResponse response) {
        return mapping.findForward("seasonRecipeGroupHistory");
    }

    /**
     * 查询Season Recipe Group 操作历史
     * @param seasonRecipeGroup
     * @return
     */
    public Page querySeasonRecipeGroupHistory(SeasonRecipeGroup seasonRecipeGroup) {
        String recipeGrpId = StringUtils.replace(StringUtils.trimToUpperCase(seasonRecipeGroup.getRecipeGrpId()), "*", "%");
        String transUserId = StringUtils.replace(StringUtils.trimToUpperCase(seasonRecipeGroup.getTransUserId()), "*", "%");
        seasonRecipeGroup.setRecipeGrpId(recipeGrpId);
        seasonRecipeGroup.setTransUserId(transUserId);
        return emsService.getSeasonRecipeGroupHistory(seasonRecipeGroup);
    }
}