CancelReworkLotAction.java

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

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.framework.utils.lang.collections.MapUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.TransReason;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.ReworkLotInfoForm;
import org.apache.commons.compress.utils.Sets;
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.Map;

/**
 * @author shijie.deng
 * @version 6.0.0
 * @date 2019/10/13
 **/
public class CancelReworkLotAction extends WipSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        ReworkLotInfoForm theform = (ReworkLotInfoForm) form;
        Lot lot = initLotBaseInfoForJsp(request, true, null, null);
        super.checkCstRemoved(Sets.newHashSet(lot.getLotRrn()));
        Map reworkMap = lotQueryService.getReworkInfo(lot.getReworkTransRrn(), new Long(lot.getLotRrn()));
        Assert.isFalse(reworkMap == null || reworkMap.size() <= 0,
                       Errors.create().key(MessageIdList.REWORK_CANNOT_CANCEL_REWORK).content(
                               "Cannot cancel rework, please " + "check whether the lot is " +
                                       "still in the rework process!").build());
        Assert.isTrue(LotStatus.HOLD.equals(lot.getLotStatus()),
                      Errors.create().key(MessageIdList.SCRAPLOT_STATUS_NOT_ALLOW).content("Lot status must be Hold!")
                            .build());
        BeanUtils.copyProperties(lot, theform);
        return mapping.findForward(Constants.INIT_KEY);
    }


    public ActionForward cancelRework(ActionMapping mapping, HttpServletRequest request) {

        String userId = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = request.getParameter("lotId");
        String userName = securityService.getUserName(LocalContext.getUserRrn());
        String reason =
                request.getParameter("reasonCode") + " " + request.getParameter("deptExt").trim().toUpperCase() + " " +
                        userName + " " + request.getParameter("reason");

        Lot lot = lotQueryService.getLot(lotId, facilityRrn);

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

        Assert.isFalse(!LotStatus.HOLD.equalsIgnoreCase(lot.getLotStatus()) &&
                               !LotStatus.WAITING.equalsIgnoreCase(lot.getLotStatus()),
                       Errors.create().content("The state must be {} or {}").args(LotStatus.HOLD, LotStatus.WAITING)
                             .build());

        TransReason transReason = new TransReason();
        transReason.setReasonCode(request.getParameter("reasonCode"));
        transReason.setReason(reason);
        transReason.setResponsibility(userId);
        transReason.setTransQty1(lot.getQty1());
        transReason.setTransQty2(lot.getQty2());
        Map reworkMap = lotQueryService.getReworkInfo(lot.getReworkTransRrn(), new Long(lot.getLotRrn()));
        String reworkWflStepPath = MapUtils.getString(reworkMap, "reworkWflStepPath");
        // List<Lot> lockLots = new ArrayList<>();
        // lockLots.add(lot);
        // long userRrn = LocalContext.getUserRrn();
        if (reworkMap != null && StringUtils.isNotEmptyTrim(reworkWflStepPath)) {
            // checkAndCreateLotsTransLock(userRrn, TransactionNames.LOCK_CANCEL_REWORK, lockLots,
            //                             "CancelRework lot in CancelReworkLotAction by: " + userId);
            lotService.cancelRework(lot, transReason);
        } else {
            // removeLotLockWhenTransError(userRrn, lockLots, TransactionNames.LOCK_CANCEL_REWORK,
            //                             "CancelRework fail, in CancelReworkLotAction by:" + userId);

            throw new SystemIllegalArgumentException(Errors.create().key(MessageIdList.REWORK_CANNOT_CANCEL_REWORK)
                                                           .content("Cannot cancel rework, please check whether the " +
                                                                            "lot is still in the rework process!")
                                                           .build());
        }
        return new ActionForward("/lotLocation4CSECAction.do?action=lotLocation&lotId=" + lot.getLotId());
    }

}