SubconOutForLotAction.java

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

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.ems.Carrier;
import com.mycim.valueobject.security.User;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.TransReason;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
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 liuji.li
 * @version 6.0.0
 * @date 2019/10/17
 **/
public class SubconOutForLotAction extends WipSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = WebUtils.getParameter("lotId", request);
        Assert.isFalse(StringUtils.isEmpty(lotId), Errors.create().key(MessageIdList.LOT_ENTER_CORRECT_LOTID)
                                                         .content("Please enter the correct lot Id!").build());
        Lot lot = lotQueryService.getLot(lotId, facilityRrn);
        Carrier carrier = carrierService.getCarrier(lot.getCarrierRrn());

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

        Assert.isFalse(carrier == null || carrier.getInstanceRrn() <= 0,
                       Errors.create().content("Can not find this cassette of" + " Lot!").build());
        boolean allowInFlag = lotService
                .allowSubconOut(lot.getProductRrn(), lot.getProcessRrn(), lot.getRouteRrn(), lot.getOperationRrn(),
                                lot.getLotStatus(), lot.getBeforeStatus());
        Assert.isTrue(allowInFlag, Errors.create().key(MessageIdList.SUBCONOUT_NOT_ALLOWED)
                                         .content("Subcon out is not currently allowed!").build());
        initLotBaseInfoForJsp(request, true, "", "");
        return mapping.getInputForward();
    }
/*

    public ActionForward subconOutInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                       HttpServletResponse response) {
        long   facilityRrn = LocalContext.getFacilityRrn();
        String lotId       = WebUtils.getParameter("lotId", request);
        if (StringUtils.isEmpty(lotId)) {
            throw new WebException("lot.enter_correct_lotId", "Please enter the correct lot Id!");
        }
        Lot     lot     = lotQueryService.getLot(lotId, facilityRrn);
        Carrier carrier = carrierService.getCarrier(lot.getCarrierRrn());
        if (carrier == null || carrier.getInstanceRrn() <= 0) {
            throw new WebException("Can not find this cassette of Lot!");
        }
        boolean allowInFlag = lotService.allowSubconOut(lot.getProductRrn(), lot.getProcessRrn(),
                                                        lot.getRouteRrn(), lot.getOperationRrn(),
                                                        lot.getLotStatus(), lot.getBeforeStatus());
        if (!allowInFlag) {
            throw new WebException("subconOut.not_allowed", "Subcon out is not currently allowed!");
        }
        initLotBaseInfoForJsp(request, true, "", "");
        return mapping.getInputForward();
    }
*/

    public void subconOut(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String userId = LocalContext.getUserId();
        String lotId = MapUtils.getString(map, "lotId");
        Lot lot = lotQueryService.getLot(lotId, facilityRrn);
        boolean allowOutFlag = lotService
                .allowSubconOut(lot.getProductRrn(), lot.getProcessRrn(), lot.getRouteRrn(), lot.getOperationRrn(),
                                lot.getLotStatus(), lot.getBeforeStatus());

        Assert.isTrue(allowOutFlag, Errors.create().key(MessageIdList.SUBCONOUT_NOT_ALLOWED)
                                          .content("Subcon out is not currently allowed!").build());
        String department = MapUtils.getString(map, "department");
        String responsibilityUserId = MapUtils.getString(map, "responsibilityUserId").toUpperCase();
        Long responsibilityUserRrn = MapUtils.getLong(map, "responsibilityUserRrn");
        String deptExt = MapUtils.getString(map, "deptExt").toUpperCase();
        String reason = MapUtils.getString(map, "reason").toUpperCase();

        checkParameters(department, responsibilityUserId, responsibilityUserRrn, reason);

        User responsibilityUser = securityService.getUser(responsibilityUserRrn);
        String reasonDetail = department + " " + deptExt + " " + responsibilityUser.getUserName() + " " + reason;
        TransReason transReason = new TransReason();
        transReason.setReasonCategory(TransactionNames.SUBCON_OUT_KEY);
        transReason.setReasonCode(department);
        transReason.setReason(reasonDetail);
        transReason.setResponsibility(responsibilityUser.getInstanceId());
        transReason.setTransQty1(lot.getQty1());
        transReason.setTransQty2(lot.getQty2());

        Map<String, Object> transInfo = new HashMap<>();
        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_SUBCON_OUT, lockLots,
        //                             "Subcon out for lot in SubconOutForLotAction by: " + userId);
        lotService.subconOutForLot(lot, transInfo);
    }

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

}