FinishLotAction.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.valueobject.MessageIdList;
import com.mycim.valueobject.consts.CarrierType;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.ems.Carrier;
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.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.DummyForm;
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.HashMap;
import java.util.Map;

/**
 * @author liuji.li
 * @version 6.0.0
 * @date 2019/9/27
 **/
public class FinishLotAction 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.FINISHLOT_NOT_FIND_LOT).content("Cannot Find Lot!")
                                 .build());
            //卡控post future hold被hold住的lot
            checkPostFutureHold(lot.getLotRrn());
            super.checkCstRemoved(Sets.newHashSet(lot.getLotRrn()));
            Assert.isFalse(
                    lot.getLotStatus() == null || !StringUtils.equalsIgnoreCase(lot.getLotStatus(), LotStatus.HOLD),
                    Errors.create().key(MessageIdList.FINISHLOT_NOT_STATUS).content("The lot must be hold!").build());

            Assert.isFalse(diffBatchQueryService.checkLotInBatch(lot.getLotRrn()),
                           Errors.create().key(MessageIdList.FINISHLOT_LOT_BATCH).content("Lot in batch!").build());
            wipQueryService.checkActiveInlineOcapId(lot.getLotRrn());
            //check sortJob
            checkWaitJobs(lot.getCarrierRrn(), 0L, 0L, null);
        }
        theform.setReasonCode("");
        theform.setDeptExt("");
        initLotBaseInfoForJsp(request, true, "", "");
        return mapping.getInputForward();
    }

    public ActionForward completeLot(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                     HttpServletResponse response) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        long userRrn = LocalContext.getUserRrn();
        DummyForm theform = (DummyForm) form;
        Lot lot = lotQueryService.getLot(theform.getLotId(), facilityRrn);
        Carrier carrier = carrierService.getCarrier(lot.getCarrierRrn());
        Assert.isFalse(carrier == null || carrier.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.FINISHLOT_CARRIER_NOT_FIND)
                             .content("Can not find this carrier of " + "Lot!").build());

        Assert.isFalse(StringUtils.equalsIgnoreCase(carrier.getObjectSubtype(), CarrierType.DUMMY),
                       Errors.create().key(MessageIdList.FINISHLOT_CARRIER_REMOVED)
                             .content("This carrier of Lot is already removed!").build());
        //check sortJob
        checkWaitJobs(carrier.getInstanceRrn(), 0L, 0L, null);

        String hiddenUserId = request.getParameter("hiddenUserId");
        String reason = theform.getReasonCode() + " " + theform.getDeptExt().trim().toUpperCase() + " " +
                hiddenUserId.trim().toUpperCase() + " " + request.getParameter("reason");
        TransReason transReason = new TransReason();
        transReason.setReasonCode(theform.getReasonCode());
        transReason.setReasonCategory(TransactionNames.TERMINATELOT_KEY);
        transReason.setReason(reason);
        String userName = securityService.getUser(userRrn).getUserName();
        if (userName != null) {
            transReason.setResponsibility(user + " " + userName);
        } else {
            transReason.setResponsibility(user);
        }
        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.COMPLETE_LOT);
        transInfo.put("comments", theform.getReason());
        transInfo.put("facilityRrn", facilityRrn);

        //check lot lock
        // List<Lot> lockLots = new ArrayList<>();
        // lockLots.add(lot);
        // checkAndCreateLotsTransLock(userRrn, TransactionNames.LOCK_FINISH_LOT, lockLots,
        //                             "Finish lot in FinishLotAction by: " + user);
        //============== MES CALL SAP  begin =============
        //  mes2SAPLotStatusChange(lot, user, getInstanceId(facilityRrn), "Finished");
        //============== MES CALL SAP  end   =============
        //erpService.lotInfoForShip(lot.getLotId(), user, facilityRrn, LotStatus.FINISH);

        lotService.completeLotFinish(transInfo);
        //   removeEqptlotCache(lot, facilityRrn, userRrn);

          /*  List<LotRunCard> runcardList = getLotManager().getEffectiveLotRuncardByLot(lot.getLotRrn());
            for (LotRunCard runcard : runcardList) {
                lotRuncardService.forceWithdrawRunCard(facilityRrn, user, userRrn, runcard.getRunCardRrn());
            }*/
        return mapping.findForward(Constants.LOTLOCATION_KEY);
    }

    /**
     *private void mes2SAPLotStatusChange(Lot lot, String user, String facilityId, String status) {
     //增加开关控制是否调用sap
     String callFlag = sysService.getRefFileValue("$MES_CALL_SAP_FLAG", "CALLFLAG", "DATA_1_VALUE");

     if ("1".equals(callFlag)) {
     //1.封装参数
     Map inmap = new HashMap();
     inmap.put("OperatorID", user);
     inmap.put("FabName", facilityId);
     inmap.put("LotId", lot.getLotId());
     inmap.put("Status", status);
     inmap.put("WaferCount", lot.getQty1());
     if (lot.getLotId().contains(".")) {
     inmap.put("StartWaferCount", lot.getQty1());
     inmap.put("PARENT", lot.getLotId().substring(0, lot.getLotId().indexOf(".")));
     } else {
     // inmap.put("StartWaferCount", lotService.getInitUnitsCountWithLotID(lot.getLotId()));
     inmap.put("PARENT", lot.getLotId());
     }
     List<Unit>    unitList  = wipQueryService.getUnitList(lot.getLotRrn());
     StringBuilder WaferList = new StringBuilder();
     for (Unit unit : unitList) {
     WaferList.append(unit.getUnitId().split("#")[1] + ",");
     }
     inmap.put("WaferList", WaferList.toString().substring(0, WaferList.lastIndexOf(",")));
     inmap.put("ProductId", lot.getProductId());
     inmap.put("TechId", lot.getProcessId().substring(1, 6));
     inmap.put("STARTDATE", DateUtils.formatDate(lot.getStartedTimestamp(), "yyyyMMdd"));
     inmap.put("LASTTRANSACTIONTIME", DateUtils.formatDate(new Date(), "yyyyMMdd HHmmss"));
     inmap.put("DUEDATE", lot.getDueDate());
     inmap.put("PLANNAME", lot.getProcessId());
     inmap.put("LOTTYPE", lot.getLotType());
     inmap.put("LOTOWNER", lot.getLotOwner());
     inmap.put("VENDORID", lot.getCustomerId());
     inmap.put("VENDORLOTID", lot.getCustomerLotId());
     inmap.put("SHIPREQUIREMENT", lot.getShippingCode());
     inmap.put("WAFERLOTID", lot.getCustomerLotId());

     SAPInterfaceTarget sapInterfaceTarget = SAPImplFactory.getSAPTarget();
     Map                rmap               = sapInterfaceTarget.mes2SAPLotStatusChange(inmap);

     MesSapData mesSapData = new MesSapData("mes2SAPLotStatusChange", user, facilityId,
     lot.getLotId());
     mesSapData.setReceiveInfo(inmap.toString());
     mesSapData.setReturnInfo(org.apache.commons.collections.MapUtils.getString(rmap, "returnInfo"));
     mesSapData.setReturnCode(org.apache.commons.collections.MapUtils.getString(rmap, "result"));

     //记录日志。
     baseService.insertMes2SapLog(mesSapData);
     if (0 != org.apache.commons.collections.MapUtils.getLong(rmap, "result")) {
     //提示错误信息
     throw new WebException("Finish lot fail because :" + rmap.get("desc"));
     }
     }*/
}