EquipmentBomListAction.java

package com.mycim.webapp.actions.equipment;

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.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.EquipmentBomInfo;
import com.mycim.valueobject.ems.Entity;
import com.mycim.valueobject.inv.MaterialDO;
import com.mycim.valueobject.prp.Recipe;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.forms.EntityInfoForm;
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: can.yang
 * @Date: 2021/10/22 14:48
 */
public class EquipmentBomListAction extends EquipmentSaveAction {

    private static final String MEASURABLE_TYPE = "MeasurableType";

    private static final String COUNTING_TYPE = "CountingType";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        EntityInfoForm theForm = getForm(form);
        Entity theEntity = getEntityInfo(theForm);
        Assert.isFalse(theEntity == null || theEntity.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.EQUIPMENT_MISSING_ID).build());
        if (StringUtils.isNotBlank(WebUtils.getParameter("equipmentReadOnlyFlag", request))) {
            request.setAttribute("equipmentReadOnlyFlag", 1);
        }

        if (StringUtils.isNotEmpty(theEntity.getChamberType())) {
            Assert.isFalse((theEntity.getParentEntityRrn() != null && theEntity.getParentEntityRrn() != 0),
                           Errors.create().key(MessageIdList.BOM_CHAMBER_CANNOT_SET)
                                 .content("Sub chamber equipment cannot set available BOM!").build());
        }

        List<EquipmentBomInfo> bomList = emsService.getFullEquipmentBom(theEntity.getInstanceRrn());

        request.setAttribute("bomList", bomList);
        request.setAttribute("eqptRrn", theEntity.getInstanceRrn());
        theForm.setTransId("modify");
        return mapping.getInputForward();
    }

    public ActionForward addCountingTypeBomBind(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                                HttpServletResponse response) throws Exception {
        EntityInfoForm theForm = (EntityInfoForm) form;
        Entity theEntity = getEntityInfo(theForm);

        String bomId = StringUtils.trimToUpperCase(theForm.getBomId());
        String consumeMode = theForm.getConsumeMode();
        Assert.isFalse(theEntity == null || theEntity.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.EQUIPMENT_MISSING_ID).build());
        Assert.isFalse(StringUtils.isBlank(bomId), Errors.create().key(MessageIdList.BOM_ID_NOT_EMPTY).build());
        Assert.isFalse(StringUtils.isBlank(consumeMode),
                       Errors.create().key(MessageIdList.BOM_CONSUME_MODE_NOT_EMPTY).build());
        MaterialDO bom = asmService.getMaterial(LocalContext.getFacilityRrn(), bomId);
        Assert.isFalse(bom == null || bom.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.BOM_ID_NOT_EXIST_2).args(bomId).build());

        List<EquipmentBomInfo> bomList = emsService.getFullEquipmentBom(theEntity.getInstanceRrn());
        if (CollectionUtils.isNotEmpty(bomList)) {
            for (EquipmentBomInfo r : bomList) {
                Assert.isFalse(r.getMaterialRrn() == bom.getInstanceRrn(),
                               Errors.create().key(MessageIdList.BOM_ID_HAS_EXIST).args(bomId).build());
            }
        }
        EquipmentBomInfo equipmentBomInfo = new EquipmentBomInfo();
        equipmentBomInfo.setEquipmentRrn(theEntity.getInstanceRrn());
        equipmentBomInfo.setMaterialRrn(bom.getInstanceRrn());
        equipmentBomInfo.setConsumeType(COUNTING_TYPE);
        equipmentBomInfo.setConsumeMode(consumeMode);
        equipmentBomInfo.setRecipeId(StringUtils.EMPTY);
        equipmentBomInfo.setLotTypeFlag(StringUtils.EMPTY);
        equipmentBomInfo.setTransId(Constants.CREATE_KEY);
        equipmentBomInfo.setCreatedUserId(LocalContext.getUserId());
        equipmentBomInfo.setCreateUserRrn(LocalContext.getUserRrn());
        equipmentBomInfo.setTransPerformedby(LocalContext.getUserId());
        equipmentBomInfo.setCreateTime(DateUtils.formatDate(new Date()));

        emsService.insertEquipmentBom(equipmentBomInfo);

        theForm.setBomId("");
        WebUtils.setSuccessMsg(request);
        return init(mapping, theForm, request, response);
    }

    public ActionForward addMeasurableTypeBomBind(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        EntityInfoForm theForm = (EntityInfoForm) form;
        Entity theEntity = getEntityInfo(theForm);

        String bomId = StringUtils.trimToUpperCase(theForm.getBomId());
        String consumeMode = theForm.getConsumeMode();
        String recipeId = theForm.getRecipeId();
        String lotTypeFlag = theForm.getLotTypeFlag();
        Assert.isFalse(StringUtils.isBlank(lotTypeFlag),
                       Errors.create().key(MessageIdList.BOM_LOTTYPEFLAG_NOT_EMPTY).build());
        Assert.isFalse(theEntity == null || theEntity.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.EQUIPMENT_MISSING_ID).build());
        Assert.isFalse(StringUtils.isBlank(bomId), Errors.create().key(MessageIdList.BOM_ID_NOT_EMPTY).build());
        Assert.isFalse(StringUtils.isBlank(consumeMode),
                       Errors.create().key(MessageIdList.BOM_CONSUME_MODE_NOT_EMPTY).build());
        Assert.isFalse(StringUtils.isBlank(recipeId), Errors.create().key(MessageIdList.RECIPE_INVALID_ID).build());

        List<Map<String, Object>> recipeList4Equipment = recipeService
                .getRecipeList4Equipment(theEntity.getInstanceRrn());
        boolean recipeFlag = false;
        for (Map<String, Object> map : recipeList4Equipment) {
            String instance_id = MapUtils.getString(map, "recipeId");
            recipeFlag = recipeId.equals(instance_id) ? true : false;
            if (recipeFlag) {
                break;
            }
        }
        Assert.isTrue(recipeFlag, Errors.create().key(MessageIdList.RECIPE_NOT_BELONG_EQUIPMENT).build());
        Recipe recipe = recipeService.getRecipe(recipeId, LocalContext.getFacilityRrn());

        long mainRecipeRrn = recipeService.isMainRecipe(recipe.getInstanceRrn());
        Assert.isFalse(mainRecipeRrn != recipe.getInstanceRrn(),
                       Errors.create().key(MessageIdList.RECIPE_NO_MAIN_RECIPE).args(recipeId).build());

        MaterialDO bom = asmService.getMaterial(LocalContext.getFacilityRrn(), bomId);
        Assert.isFalse(bom == null || bom.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.BOM_ID_NOT_EXIST_2).args(bomId).build());

        List<EquipmentBomInfo> bomList = emsService.getFullEquipmentBom(theEntity.getInstanceRrn());
        if (CollectionUtils.isNotEmpty(bomList)) {
            for (EquipmentBomInfo r : bomList) {
                Assert.isFalse(r.getMaterialRrn() == bom.getInstanceRrn(),
                               Errors.create().key(MessageIdList.BOM_ID_HAS_EXIST).args(bomId).build());
            }
        }

        EquipmentBomInfo equipmentBomInfo = new EquipmentBomInfo();
        equipmentBomInfo.setEquipmentRrn(theEntity.getInstanceRrn());
        equipmentBomInfo.setMaterialRrn(bom.getInstanceRrn());
        equipmentBomInfo.setConsumeType(MEASURABLE_TYPE);
        equipmentBomInfo.setConsumeMode(consumeMode);
        equipmentBomInfo.setRecipeId(recipeId);
        equipmentBomInfo.setLotTypeFlag(StringUtils.EMPTY);
        equipmentBomInfo.setTransId(Constants.CREATE_KEY);
        equipmentBomInfo.setCreatedUserId(LocalContext.getUserId());
        equipmentBomInfo.setCreateUserRrn(LocalContext.getUserRrn());
        equipmentBomInfo.setTransPerformedby(LocalContext.getUserId());
        equipmentBomInfo.setCreateTime(DateUtils.formatDate(new Date()));
        equipmentBomInfo.setLotTypeFlag(lotTypeFlag);

        emsService.insertEquipmentBom(equipmentBomInfo);
        theForm.setBomId("");
        theForm.setRecipeId("");
        WebUtils.setSuccessMsg(request);
        return init(mapping, theForm, request, response);
    }

    public ActionForward deleteBomBind(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
        EntityInfoForm theForm = getForm(form);
        Entity theEntity = getEntityInfo(theForm);
        Assert.isFalse(theEntity == null || theEntity.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.EQUIPMENT_MISSING_ID).build());
        Long bomRrn = new Long(request.getParameter(Constants.ITEM_KEY));
        EquipmentBomInfo equipmentBom = emsService.getEquipmentBom(theEntity.getInstanceRrn(), bomRrn);
        if (equipmentBom != null) {
            equipmentBom.setEquipmentRrn(theEntity.getInstanceRrn());
            equipmentBom.setMaterialRrn(bomRrn);
            equipmentBom.setTransPerformedby(LocalContext.getUserId());
            equipmentBom.setTransId(Constants.DELETE_KEY);
            emsService.deleteEquipmentBom(equipmentBom);
        }
        WebUtils.setSuccessMsg(request);
        return init(mapping, theForm, request, response);
    }

    public ActionForward bomHistory(ActionMapping mapping, ActionForm form, HttpServletRequest request) {
        EntityInfoForm theForm = getForm(form);
        if (StringUtils.isNotBlank(request.getParameter("flag"))) {
            request.setAttribute("flag", "flag");
        }
        return mapping.findForward(Constants.HISTORY_KEY);
    }

    public Map<String, Object> query(Map<String, Object> params) {
        String entityId = StringUtils.trimToUpperCase(MapUtils.getString(params, "instanceId"));
        Entity theEntity = emsService.getEntity(
                new Entity(entityId, getNamedSpace(ObjectList.ENTITY_KEY, LocalContext.getFacilityRrn()),
                           ObjectList.ENTITY_KEY));

        int pageNo = MapUtils.getInteger(params, "currentPage");
        if (pageNo <= 0) {
            pageNo = 1;
        }

        int pageSize = 10;

        Page page = new Page(pageNo, pageSize);
        page = emsService.getEquipmentBomHistory(theEntity.getInstanceRrn(), page);
        Map<String, Object> result = new HashMap<>();

        result.put("pageSize", page.getNextPage());
        result.put("pageNum", page.getPageNo());
        result.put("maxPage", page.getTotalPages());
        result.put("startRowNum", page.getStartRow());

        result.put("isFirstPage", page.isFirstPage());
        result.put("hasPreviousPage", page.isHasPrePage());
        result.put("isLastPage", page.getTotalPages() == 0 || page.isLastPage());
        result.put("hasNextPage", page.isHasNextPage());

        result.put("list", page.getResults());


        return result;
    }

}