ErpShipLotAction.java

package com.mycim.webapp.actions.unit.erpShipLot;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.BeanUtils;
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.consts.TransactionNames;
import com.mycim.valueobject.prp.Operation;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotExt;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.TransReason;
import com.mycim.valueobject.wip.dto.ShipQueryConditionDto;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.unit.ShipLotForm;
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;

/**
 * @author yibing.liu
 * @version 1.0
 * @date 2021/5/27
 */
public class ErpShipLotAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        ShipLotForm shipLotForm = getForm(form);
        ShipQueryConditionDto sqcd = new ShipQueryConditionDto();

        boolean isQuery = request.getParameter("query") != null;

        if (StringUtils.isNotBlank(shipLotForm.getProductId())){
            long productRrn = getInstanceRrn(shipLotForm.getProductId(), LocalContext.getFacilityRrn(), ObjectList.PRODUCT_KEY);
            sqcd.setProductRrn(productRrn);
        }
        if (StringUtils.isNotBlank(shipLotForm.getLotType())){
            sqcd.setLotType(shipLotForm.getLotType());
        }
        if (StringUtils.isNotBlank(shipLotForm.getLotId())){
            sqcd.setLotId(shipLotForm.getLotId());
        }
        if (isQuery){
            sqcd.setLotStatus(LotStatus.FINISH);
        } else {
            sqcd = new ShipQueryConditionDto();
        }

        List<Map> finishLotList = sqcd.isEmpty()? new ArrayList<>():lotQueryService.getLotList4Ship(sqcd);

        request.setAttribute("lotList", finishLotList);
        return mapping.getInputForward();
    }


    public Map<String, Object> addMemberLot(Map map) {
        Map<String, Object> result = new HashMap<>();
        long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = MapUtils.getString(map, "lotId");
        List<Map<String, Object>> lots = new ArrayList<Map<String, Object>>();
        Lot lot;
        Assert.isTrue(StringUtils.isNotBlank(lotId),
                      Errors.create().key(MessageIdList.SHIPLOT_ENTER_LOTID).content("Please enter lot Id!").build());
        lot = lotQueryService.getLot(lotId, facilityRrn);
        Assert.isFalse((lot == null) || (lot.getLotRrn() <= 0),
                       Errors.create().key(MessageIdList.SHIPLOT_LOT_NOT_EXISTS).content("This lot does not exist!")
                             .build());
        if (StringUtils.isNotBlank(lot.getLotStatus())) {
            Assert.isTrue((LotStatus.FINISH.equalsIgnoreCase(lot.getLotStatus())) &&
                                  (!"S".equalsIgnoreCase(lot.getLastScheduleFlag())),
                          Errors.create().key(MessageIdList.SHIPLOT_STATUS_ERROR)
                                .content("Lot: {} can't do ship! " + "Please check lot status!").args(lotId).build());
            // Validate the lot can do the ship
            Long operationRrn = lot.getOperationRrn();
            Operation myoperation = prpService.getOperation(operationRrn);
            Assert.nonNull(myoperation, Errors.create().key(MessageIdList.SHIPLOT_OPERATION_ERROR)
                                              .content("Lot: {} can't do ship! " + "Please check operation!")
                                              .args(lotId).build());
        }
        LotExt lotExt = new LotExt();
        lotExt.setLotRrn(lot.getLotRrn());
        lotExt = lotQueryService.getLotExt(lotExt);
        Assert.isFalse(
                StringUtils.isNotBlank(lotExt.getAttributeData5()),
                Errors.create().content("Lot {} has applied for shipment to ERP!")
                      .args(lot.getLotId()).build());
        long recipeRrn = NumberUtils.toLong(lot.getRecipeId());
        if (recipeRrn > 0) {
            lot.setRecipeId(getInstanceId(recipeRrn));
        }
        Map<String, Object> resultMap = BeanUtils.copyBeanToMap(lot);
        String completedssType = MapUtils.getString(map, "completedssType");
        resultMap.put("storageChoose", MapUtils.getString(map, "storageChoose"));
        resultMap.put("storage", MapUtils.getString(map, "storage"));
        resultMap.put("completedssType", completedssType);
        resultMap.put("completedssLevel", MapUtils.getString(map, "completedssLevel"));
        resultMap.put("dieNums", map.get("dieNums"));
        lots.add(resultMap);
        result.put("lots", lots);
        return result;
    }

    public void checkValidShip(Map map) {
        Map<String, Object> result = new HashMap<>();
        long lotRrn = MapUtils.getLong(map, "lotRrn", 0L);
        Assert.isFalse(lotRrn <= 0, Errors.create().key(MessageIdList.SHIPLOT_ENTER_LOTID).content("Please enter lot Id!").build());
        checkLot(null, lotRrn, 0);
    }

    public ActionForward shipLot(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        String lotArr = request.getParameter("instanceId");
        long facilityRrn = LocalContext.getFacilityRrn();

        List<Map<String, String>> paramMap = new ArrayList<>();
        List<String> lotIds = StringUtils.splitAsList(lotArr, ",");
        Assert.isFalse(lotIds.isEmpty(), Errors.create().key(MessageIdList.PLEASE_SELECT_DATA).content("Please select the data to be manipulated!").build());
        for (String lotId: lotIds){
            checkLot(lotId, 0, facilityRrn);
            Map<String,String> lotMap = new HashMap<>();
            lotMap.put("lotId", lotId);
            lotMap.put("warehouseId", "WAFER_BANK");
            paramMap.add(lotMap);
        }
        erpService.shipLotRequest(paramMap);
        WebUtils.setSuccessMsg(request);
        request.setAttribute("query", 1);
        return init(mapping, form, request, response);
    }

    private void checkLot(String lotId, long lotRrn, long facilityRrn) {
        Lot lot = lotRrn > 0? lotQueryService.getLot(lotRrn):lotQueryService.getLot(lotId, facilityRrn);
        Assert.isFalse(lot == null || lot.getLotRrn() <= 0, Errors.create().key(MessageIdList.UNFINISHLOT_NOT_FIND_LOT)
                             .content("Cannot Find Lot!").args(lotId).build());

        Assert.isFalse(lot.getLotStatus() == null || !StringUtils.equalsIgnoreCase(lot.getLotStatus(), LotStatus.FINISH),
                       Errors.create().key(MessageIdList.UNFINISHLOT_NOT_STATUS).content("The lot must be Finish!").build());

        Assert.isFalse(StringUtils.isBlank(lot.getInnerOrderNO()), Errors.create().key(MessageIdList.WORKORDER_NOT_FOUND).content("WorkOrder {} not found").args(lot.getInnerOrderNO()).build());
        LotExt lotExt = new LotExt();
        lotExt.setLotRrn(lot.getLotRrn());
        lotExt = lotQueryService.getLotExt(lotExt);
        Assert.isFalse( StringUtils.isNotBlank(lotExt.getAttributeData5()),
                Errors.create().key(MessageIdList.LOT_HAVE_SHIP_REQUEST).content("Lot {} has applied for shipment to ERP!").args(lot.getLotId()).build());
    }


}