AdjustLotIdAction.java

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

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.MiscUtils;
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.framework.utils.lang.time.DateUtils;
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.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.LotInfoForm;
import org.apache.commons.beanutils.PropertyUtils;
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;
import java.util.Map;
import java.util.Objects;

/**
 * 修改批次号
 *
 * @author weike.li
 * @version 6.0.0
 * @date 2019/10/29
 **/
public class AdjustLotIdAction extends WipSetupAction {

    private final String CHANGE_ID = "changeid";

    private final String TRANSVERSE_LINE = "-";

    private final String UNDERLINE = "_";

    private final String COMMA = ",";

    private final String NEW_LOT_ID = "newLotId";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {

        String transId = StringUtils.EMPTY;
        if (WebUtils.getParameter(CHANGE_ID, request) != null) {
            transId = CHANGE_ID;
        }
        request.setAttribute("transId", transId);
        request.setAttribute("lotInfoForm", new LotInfoForm());
        return mapping.findForward("modify");
    }


    public ActionForward query(ActionMapping mapping, LotInfoForm theForm,
                               HttpServletRequest request) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = StringUtils.trim(request.getParameter("lotId"));
        Lot lot = new Lot();
        if (StringUtils.isNotEmpty(lotId)) {
            lot = lotInqService.getLot(lotId);
            Assert.state(Objects.nonNull(lot),
                         Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND)
                               .content("Cannot Find Lot!").build());
        }
        /*
         * 当carrierId为空时,视图渲染为N/A ;
         */
        lot.setCarrierId(lot.getCarrierId() == null ? "N/A" : lot.getCarrierId());

        PropertyUtils.copyProperties(theForm, lot);
        theForm.setRecipePhysicalId(getRecipePhysicalId(lot));

        theForm.setDueDateS(DateUtils.formatDate(lot.getDueDate(), DateUtils.DATE_FORMAT4DAY));

        theForm.setWaitTime(lot.getwaitTime());

        String carrierId2 = getInstanceId(MiscUtils.parseSQL(lot.getCarrierRrn()));
        String productId = getInstanceId(MiscUtils.parseSQL(lot.getProductRrn()));
        theForm.setCarrierId(StringUtils.isEmpty(carrierId2) ? "N/A" : carrierId2);
        theForm.setProductId(productId);
        theForm.setLotComments(lot.getLotComments());
        theForm.setQty1toInt(theForm.getQty1().intValue());
        Map conditionMap = BeanUtils.copyBeanToMap(lot);
        conditionMap = ctxExecService.buildContextValueForReticleFamily(LocalContext.getFacilityRrn(), conditionMap);
        theForm.setReticleGroupId(MapUtils.getString(conditionMap, "reticleFamilyId", StringUtils.EMPTY));

        String createType;
        createType = lot.getCreateCategory().toUpperCase();
        request.setAttribute("createType", createType);
        request.setAttribute("currentLotType", StringUtils.upperCase(lot.getLotType()));

        if (request.getParameter(CHANGE_ID) != null) {
            request.setAttribute("transId", "changeid");
            String hotFlagSplicingPriority = wipQueryService
                    .getHotflagSplicingPriority(Integer.parseInt(lot.getHotFlag()), lot.getPriority(), facilityRrn);
            request.setAttribute("hotflagSplicingPriority", hotFlagSplicingPriority);
        }

        return mapping.findForward("modify");
    }

    public ActionForward modify(ActionMapping mapping, LotInfoForm theForm, HttpServletRequest request) {

        long facilityRrn = LocalContext.getFacilityRrn();

        Lot lot = new Lot();

        String userId = LocalContext.getUserId();
        lot.setTransPerformedby(userId);

        String lotId = StringUtils.trim(theForm.getLotId());
        String newLotId = StringUtils.trimToUpperCase(request.getParameter(NEW_LOT_ID));

        Assert.isFalse(newLotId.contains(TRANSVERSE_LINE) || newLotId.contains(UNDERLINE),
                       Errors.create().key(MessageIdList.LOT_CONTAIN_LINE)
                             .content("New Lot ID can't contain '-' or '_'!").build());

        Assert.isTrue(lotQueryService.getLot(newLotId, facilityRrn).equals(new Lot()),
                      Errors.create().key(MessageIdList.LOT_NEW_ID_EXIST).content("新批次号已存在").build());

        Assert.isFalse(newLotId.indexOf(COMMA) > 0,
                       Errors.create().key(MessageIdList.LOT_CONTAIN_COMMA).content("新批次号不能包含逗号").build());
        // List<Lot> lockLots = new ArrayList<>();
        Lot oldLot = lotQueryService.getLot(lotId, facilityRrn);

        //卡控post future hold被hold住的lot
        checkPostFutureHold(lot.getLotRrn());
        
        // lockLots.add(oldLot);
        // checkAndCreateLotsTransLock(LocalContext.getUserRrn(), TransactionNames.LOCK_ADJUST_LOT, lockLots,
        //                             "Adjust lotId in AdjustLotIdAction by " + userId);
        lotService.changeLotId(lotId, newLotId);

        request.setAttribute("transId", CHANGE_ID);
        request.setAttribute("lotInfoForm", new LotInfoForm());

        WebUtils.setSuccessMsg(request);

        return mapping.findForward("changeId");
    }

    public ActionForward reset(ActionMapping mapping, HttpServletRequest request) {

        request.setAttribute("transId", CHANGE_ID);
        request.setAttribute("lotInfoForm", new LotInfoForm());
        return mapping.findForward("modify");
    }

}