UnFinishLotAction.java

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

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.CarrierType;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.ems.Carrier;
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.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.DummyForm;
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/9/29
 **/
public class UnFinishLotAction extends WipSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        DummyForm theform = (DummyForm) form;
        long facilityRrn = LocalContext.getFacilityRrn();
        Lot lot = null;
        String lotId = StringUtils.trimToUpperCase(WebUtils.getParameter("lotId", request));
        if (StringUtils.isNotBlank(lotId)) {
            lot = lotQueryService.getLot(lotId, facilityRrn);
            Assert.isFalse(lot == null || lot.getLotRrn() <= 0,
                           Errors.create().key(MessageIdList.UNFINISHLOT_NOT_FIND_LOT).content("Cannot Find Lot!")
                                 .build());
            //卡控post future hold被hold住的lot
            checkPostFutureHold(lot.getLotRrn());
            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());
            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());
            String lossTable = null;
            Operation operation = new Operation();
            Long operRrn = lot.getOperationRrn();

            Assert.isTrue(operRrn != null && operRrn.intValue() != 0,
                          Errors.create().key(MessageIdList.UNFINISHLOT_STEP_NOT_FIND)
                                .content("Step can't be find,the lot" + " " + "can't be unterminate!").build());
            operation = prpService.getOperation(operRrn);
            if (operation.getLossTableId() != null && operation.getLossTableId() != "") {
                lossTable = operation.getLossTableId();
            }

            Assert.isFalse(lot.getQty1().intValue() <= 0, Errors.create().key(MessageIdList.UNFINISHLOT_LOT_QTY)
                                                                .content("The lot qty must be greater than 0!")
                                                                .build());
            Carrier carrier = carrierService.getCarrier(lot.getCarrierRrn());
            Assert.isTrue(carrier != null && carrier.getInstanceRrn() > 0,
                          Errors.create().key(MessageIdList.UNFINISHLOT_NOT_FIND_CARRIER)
                                .content("Cannot Find carrier!").build());

            if (StringUtils.equalsIgnoreCase(carrier.getObjectSubtype(), CarrierType.DUMMY)) {
                request.setAttribute("carrierType", carrier.getDmmType());
                request.setAttribute("carrierCategory", carrier.getFlagType());
            } else {
                request.setAttribute("carrierType", carrier.getObjectSubtype());
                request.setAttribute("carrierCategory", carrier.getFlagType());
            }
            List<Map> holdReasons = wipQueryService.getHoldReasons(lot.getLotRrn());
            request.setAttribute("unCompleteCollection", holdReasons);
        }
        theform.setReasonCode("");
        theform.setTargetCarrierId("");
        initLotBaseInfoForJsp(request, true, "", "");
        return mapping.getInputForward();
    }

    public ActionForward uncompletelot(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                       HttpServletResponse response) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        DummyForm theform = (DummyForm) form;
        long userRRN = WebUtils.getParameterLong("userId", request);
        String userName = userRRN > 0 ? securityService.getUser(userRRN).getUserName() : "";

        Lot lot = lotQueryService.getLot(theform.getLotId(), facilityRrn);
        String reason = TransactionNames.UN_SCRAP_FINISH_TERMINATED + " " + theform.getReasonCode() + " " +
                StringUtils.trimToEmpty(WebUtils.getParameterUpperCase("deptExt", request)) + " " +
                StringUtils.trimToEmpty(userName) + " " + request.getParameter("reason");
        TransReason transReason = new TransReason();
        transReason.setReasonCode(theform.getReasonCode());
        transReason.setReasonCategory(TransactionNames.UNCOMPLETE_LOT);
        transReason.setReason(reason);
        transReason.setResponsibility(userName);
        transReason.setAccountCode(request.getParameter("accountCode"));
        transReason.setTransQty1(lot.getQty1());
        transReason.setTransQty2(lot.getQty2());

        Map transInfo = new HashMap();
        transInfo.put("transReason", transReason);
        transInfo.put("reasonCode", theform.getReasonCode());

        transInfo.put("lotRrn", new Long(lot.getLotRrn()));
        transInfo.put("lotId", lot.getLotId());
        transInfo.put("operationId", lot.getOperationId());
        transInfo.put("user", user);
        transInfo.put("transId", TransactionNames.UNCOMPLETE_LOT);
        transInfo.put("comments", request.getParameter("comments"));

        transInfo.put("routeId", request.getParameter("routeId"));
        transInfo.put("operationId", request.getParameter("operationId"));

        String targetCarrierId = StringUtils.trimToUpperCase(theform.getTargetCarrierId());
        Assert.isFalse(StringUtils.isEmpty(targetCarrierId),
                       Errors.create().key(MessageIdList.UNFINISHLOT_TARGET_CARRIER)
                             .content("Target carrier Not Specified!").build());
        carrierService.compareRealAndDummyCarrierAndCheckRealCarrierAvailabile(facilityRrn, lot.getCarrierId(),
                                                                               targetCarrierId);
        Long targetCarrierRrn = carrierService.getCarrierRrnForLot(facilityRrn, lot.getQty1(), targetCarrierId);

        transInfo.put("targetCarrierRrn", targetCarrierRrn);

       /* List<Lot> lockLots = new ArrayList<Lot>();
        erpService.lotInfoForShip(lot.getLotId(), user, facilityRrn, null);*/

        String stepNumber = request.getParameter("stepNumber");
        if (StringUtils.isBlank(stepNumber)) {
            List<Map<String, Object>> list = prpService
                    .getProcessAllStepInfo(lot.getProcessRrn(), lot.getProcessVersion(), null, null, new Long(99999),
                                           null, null, 0);
            for (Map<String, Object> map : list) {
                long routeRrn = MapUtils.getLong(map, "routeRrn");
                long operationRrn = MapUtils.getLong(map, "operationRrn");
                if (routeRrn == lot.getRouteRrn().longValue() && operationRrn == lot.getOperationRrn().longValue()) {
                    stepNumber = lot.getProcessRrn() + "." + lot.getProcessVersion() + "." +
                            MapUtils.getLongValue(map, "routeStepRrn") + "|" + lot.getRouteRrn() + "." +
                            MapUtils.getIntValue(map, "routeVersion") + "." + MapUtils.getLong(map, "operationStepRrn");
                }
            }
        }
        transInfo.put("stepNumber", stepNumber);
        // List<Lot> lockLots = new ArrayList<>();
        // lockLots.add(lot);
        // checkAndCreateLotsTransLock(LocalContext.getUserRrn(), TransactionNames.LOCK_UNFINISH_LOT, lockLots,
        //                             "UnFinish lot in UnFinishLotAction by: " + user);
        /* //============== MES CALL SAP  begin =============
        mes2SAPLotStatusChange(lot, user, session.getAttribute(SessionNames.FACILITY_ID_KEY).toString(),
                               "UnFinished");*/
        //============== MES CALL SAP  end   =============
        lotService.unCompleteLotFinish(transInfo);
        return mapping.findForward(Constants.LOTLOCATION_KEY);
    }

}