DeleteLotAction.java

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

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.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.wip.Lot;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.ScrapLotInfoForm;
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.List;

/**
 * @author liuji.li
 * @version 6.0.0
 * @date 2019/9/26
 **/
public class DeleteLotAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        initLotBaseInfoForJsp(request, false, "DELETELOT", "DELETELOT_CARRIER");
        return mapping.getInputForward();
    }

    public ActionForward query(ActionMapping mapping, HttpServletRequest request) {
        checkLotInfo(request);
        initLotBaseInfoForJsp(request, false, "DELETELOT", "DELETELOT_CARRIER");
        return mapping.getInputForward();
    }

    public ActionForward deleteLot(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                   HttpServletResponse response) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        ScrapLotInfoForm theform = (ScrapLotInfoForm) form;
        Lot lot = this.checkLotInfo(request);
        String requestLotId = WebUtils.getParameter("requestLotId", request);
        String requestCarrierId = WebUtils.getParameter("requestCarrierId", request);
        Assert.isFalse(StringUtils.equalsIgnoreCase(requestLotId, theform.getLotId()) != true ||
                               StringUtils.equalsIgnoreCase(requestCarrierId, theform.getCarrierId()) != true,
                       Errors.create().key(MessageIdList.DELETELOT_INVALID_ID).content("Invalid ID!").build());
        String lotComments = theform.getLotComments();
        lot.setTransPerformedby(user);
        lot.setFacilityRrn(facilityRrn);
        // List<Lot> lockLots = new ArrayList<>();
        // lockLots.add(lot);
        // checkAndCreateLotsTransLock(LocalContext.getUserRrn(), TransactionNames.LOCK_DEL_LOT, lockLots,
        //                             "Delete lot in DeleteLotAction by: " + user);
        try {
            lotService.deleteLot(lot, lotComments, user);
        } catch (Exception e) {
            // removeLotLockWhenTransError(LocalContext.getUserRrn(), lockLots, TransactionNames.LOCK_DEL_LOT,
            //                             "Delete lot fail, in DeleteLotAction by: " + user);
            throw new SystemIllegalArgumentException(Errors.create().content("Delete lot fail!").build());
        }
        request.setAttribute("deleteLotFlag", true);
        request.setAttribute(LOT_SEARCH_TYPE_KEY, "DELETELOT");
        request.setAttribute(CARRIER_SEARCH_TYPE_KEY, "DELETELOT_CARRIER");
        request.setAttribute(LOT_READONLY_KEY, false);
        return mapping.findForward(Constants.DELETE_KEY);
    }

    private Lot checkLotInfo(HttpServletRequest request) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = WebUtils.getParameter("lotId", request);
        Lot lot = null;
        if (WebUtils.getParameter("byCarrierId", request) != null) {
            String carrierId = WebUtils.getParameter("carrierId", request);
            List<String> lotIds = carrierService
                    .getInUseCarrierLotIdList(getInstanceRrn(carrierId, facilityRrn, ObjectList.ENTITY_KEY));
            Assert.isFalse(lotIds == null || lotIds.size() != 1,
                           Errors.create().key(MessageIdList.DELETELOT_CASSETTE_SIZE_LOT)
                                 .content("The Cassette have {} lots!").args(lotIds == null ? 0 : lotIds.size())
                                 .build());
            lotId = lotIds.get(0);
        }
        Assert.isTrue(StringUtils.isNotBlank(lotId),
                      Errors.create().key(MessageIdList.DELETE_WRITE_LOTID).content("Please input lotId or carrierId!")
                            .build());
        lot = lotQueryService.getLot(lotId, facilityRrn);
        if ((lot == null) || (lot.getLotRrn() <= 0)) {
            lot = wipQueryService.getLotPlan(lotId);
        }

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

        Assert.isFalse(lot == null || lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.DELETELOT_NOT_FOUND).content("Lot not found!").build());

        Assert.isTrue("WAITING".equals(lot.getLotStatus()),
                      Errors.create().key(MessageIdList.DELETELOT_NOT_DELETE).content("lot {} Can't delete!")
                            .args(lot.getLotId()).build());

        Assert.isFalse(lotQueryService.isLotHaveMovein(lot.getLotRrn()),
                       Errors.create().key(MessageIdList.DELETE_PROCESS_LOT)
                             .content("The lot of processing operations cannot be deleted!").build());

        return lot;
    }

}