RemoveCSTAction.java

package com.mycim.webapp.actions.lot.modifycassette;


import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.exception.SystemIllegalArgumentException;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.BeanUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.CarrierType;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.ems.Carrier;
import com.mycim.valueobject.security.User;
import com.mycim.valueobject.wip.BatchLotStore;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.TransReason;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.LotInfoForm;
import org.apache.commons.beanutils.PropertyUtils;
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 RemoveCSTAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        Lot lot = initLotBaseInfoForJsp(request, true, null, null);
        String lotId = lot.getLotId();
        Assert.isTrue(LotStatus.isWaitingOrHold(lot.getLotStatus()),
                      Errors.create().content("The lot status must be in " + "WAITING、HOLD. ").build());

        Assert.isFalse(diffBatchQueryService.checkLotInBatch(lot.getLotRrn()),
                       Errors.create().key(MessageIdList.LOT_IN_BATCH).content("Lot in batch!").build());
        LotInfoForm lotInfoForm = new LotInfoForm();

        //卡控post future hold被hold住的lot
        checkPostFutureHold(lot.getLotRrn());

        PropertyUtils.copyProperties(lotInfoForm, lot);
        lotInfoForm.setRecipePhysicalId(getRecipePhysicalId(lot));
        Map<String, Object> conditionMap = BeanUtils.copyBeanToMap(lot);
        lotInfoForm.setFlowSeq(ctxExecService.getFlowSeqByProcessInfo(conditionMap));
        lotInfoForm.setOperationDesc(ctxExecService.getOperationDescByProcessInfo(conditionMap));

        Carrier carrier = carrierService.getCarrier(lot.getCarrierRrn());
        Assert.isFalse(carrier == null || carrier.getInstanceRrn() <= 0,
                       Errors.create().content("Can not find this cassette of" + " Lot.").build());
        wipQueryService.checkActiveInlineOcapId(lot.getLotRrn());

        //check sortJob
        checkWaitJobs(lot.getCarrierRrn(), 0L, 0L, null);

        request.setAttribute("lotId", lotId);
        request.setAttribute("carrierType", carrier.getDmmType());
        request.setAttribute("carrierCategory", carrier.getFlagType());
        request.setAttribute("lotLocation", StringUtils.trimToEmpty(request.getParameter("lotLocation")));
        request.setAttribute(SessionNames.LOT_KEY, lot);
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, lotInfoForm);

        Assert.isFalse(StringUtils.equalsIgnoreCase(carrier.getObjectSubtype(), CarrierType.DUMMY),
                       Errors.create().content("This cassette of Lot is already removed.").build());
        return mapping.findForward("remove");
    }

    public void removeCSTForLot(Map map) {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String userId = LocalContext.getUserId();
        String department = map.get("department").toString().toUpperCase();
        String responsibilityUserId = map.get("responsibilityUserId").toString().toUpperCase();
        Long responsibilityUserRrn = new Long(map.get("responsibilityUserRrn").toString());
        String deptExt = map.get("deptExt").toString().toUpperCase();
        String reason = map.get("reason").toString().toUpperCase();
        String lotId = map.get("lotId").toString().toUpperCase();

        checkParameters(department, responsibilityUserId, responsibilityUserRrn, reason);

        Map<String, Object> operatorTransInfo = new HashMap<>();

        operatorTransInfo.put("department", department);
        operatorTransInfo.put("deptExt", deptExt);
        operatorTransInfo.put("responsibilityUserId", responsibilityUserId);
        operatorTransInfo.put("responsibilityUserRrn", responsibilityUserRrn);
        operatorTransInfo.put("reason", reason);
        Lot lot = lotQueryService.getLot(lotQueryService.getLotRrn(lotId));
        Assert.isTrue(LotStatus.isWaitingOrHold(lot.getLotStatus()),
                      Errors.create().content("The lot status must be in " + "WAITING、HOLD. ").build());

        //check sortJob
        checkWaitJobs(lot.getCarrierRrn(), 0L, 0L, null);

        User responsibilityUser = getResponsibilityUser(responsibilityUserRrn);

        TransReason transReason = new TransReason();
        transReason.setReasonCategory(TransactionNames.REMOVE_CST_KEY);
        transReason.setReasonCode(department);
        transReason.setReason(buildReasonDetail(department, deptExt, reason, responsibilityUser.getUserName()));
        transReason.setResponsibility(responsibilityUser.getInstanceId());
        transReason.setTransQty1(lot.getQty1());
        transReason.setTransQty2(lot.getQty2());

        Map<String, Object> transInfo = new HashMap<>();

        transInfo.put("facilityRrn", facilityRrn);
        transInfo.put("transReason", transReason);
        transInfo.put("transPerformedBy", userId);
        transInfo.put("transComments", reason);
        // List<Lot> lockLots = new ArrayList<>();
        // lockLots.add(lot);
        // checkAndCreateLotsTransLock(LocalContext.getUserRrn(), TransactionNames.LOCK_REMOVE_CST, lockLots,
        //                             "Remove CST in RemoveCSTAction by: " + userId);
        try {
            lotService.removeCSTForLot(lot, transInfo);
        } catch (Exception e) {
            // removeLotLockWhenTransError(LocalContext.getUserRrn(), lockLots, TransactionNames.LOCK_REMOVE_CST,
            //                             "Remove CST fail, in RemoveCSTAction by: " + userId);
            throw new SystemIllegalArgumentException(Errors.create().content(e.getMessage()).build());
        }
    }

    private void checkParameters(String department, String responsibilityUserId, Long responsibilityUserRrn,
                                 String reason) {
        Assert.isFalse(StringUtils.isEmpty(department),
                       Errors.create().content("Department can not be empty!").build());
        Assert.isFalse(StringUtils.isEmpty(responsibilityUserId) || responsibilityUserRrn <= 0,
                       Errors.create().content("User Id can not be empty!").build());
        Assert.isFalse(StringUtils.isEmpty(reason), Errors.create().content("Reason can not be empty!").build());
    }

    private User getResponsibilityUser(long responsibilityUserRrn) {
        User responsibilityUser = securityService.getUser(responsibilityUserRrn);
        Assert.isFalse(responsibilityUser == null || responsibilityUser.getInstanceRrn() <= 0,
                       Errors.create().content("User not exist, Please check!").build());
        return responsibilityUser;
    }

    private String buildReasonDetail(String department, String deptExt, String reason, String userName) {
        return department + " " + deptExt + " " + userName + " " + reason;
    }

}