AutoMonitorFinishAction.java

package com.mycim.webapp.actions;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.automonitor.dto.LotAutoMonitorQueryDTO;
import com.mycim.valueobject.automonitor.entity.LotAutoMonitorInfo;
import com.mycim.valueobject.sys.Message;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.webapp.WebUtils;
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;
import java.util.stream.Collectors;

/**
 * @author finatice.yang
 * @date 2021/9/21
 **/
public class AutoMonitorFinishAction extends AutoMonitorAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        return finishAutoMonitorPage(mapping, form, request, response);
    }

    public ActionForward finishAutoMonitorPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                               HttpServletResponse response) {
        String lotId = WebUtils.getParameter("lotId", request);
        Long lotRrn = lotQueryService.getLotRrn(lotId);
        LotAutoMonitorInfo monitorLotInfo = lotAutoMonitorInqService.getLotActiveAutoMonitorInfo(lotRrn);
        Assert.state(Objects.nonNull(monitorLotInfo), Errors.create().content("Monitor lot not exist!").build());

        LotAutoMonitorQueryDTO queryDTO = new LotAutoMonitorQueryDTO();
        queryDTO.setBaseLotRrn(monitorLotInfo.getBaseLotRrn());
        List<LotAutoMonitorInfo> monitorLotInfos = lotAutoMonitorInqService.getLotMonitorLotList(queryDTO);

        request.setAttribute("baseLotRrn", monitorLotInfo.getBaseLotRrn());
        request.setAttribute("lotList", monitorLotInfos);
        request.setAttribute("lotId", lotId);
        return mapping.findForward("finishAutoMonitorPage");
    }

    public void finishAutoMonitorJob(Map params) {
        Long baseLotRrn = MapUtils.getLong(params, "baseLotRrn");
        String reason = MapUtils.getString(params, "reason");

        LotAutoMonitorQueryDTO queryDTO = new LotAutoMonitorQueryDTO();
        queryDTO.setBaseLotRrn(baseLotRrn);
        List<LotAutoMonitorInfo> monitorLotInfos = lotAutoMonitorInqService.getLotMonitorLotList(queryDTO);
        if (CollectionUtils.isNotEmpty(monitorLotInfos)) {
            Lot parentLot = lotInqService.getLot(baseLotRrn);
            sorterService.autoCancelAutoMonitorSortJob(parentLot.getCarrierRrn());

            List<Lot> childLotList = new ArrayList<>();
            for (LotAutoMonitorInfo lotMonitorInfo : monitorLotInfos) {
                Lot childLot = lotInqService.getLot(lotMonitorInfo.getLotRrn());
                Assert.state(!LotStatus.isProcessingStatus(childLot.getLotStatus()),
                             Errors.create().key(MessageIdList.LOT_STATUS_NOT_ALLOW).content("Lot status not allow")
                                   .build());

                Assert.state(lotMonitorInfo.getMonitorCarrierRrn() == null ||
                                     lotMonitorInfo.getMonitorCarrierRrn().longValue() !=
                                             lotMonitorInfo.getCarrierRrn().longValue(),
                             Errors.create().key(MessageIdList.AUTOMONITOR_LOT_NEED_BACK).args(childLot.getLotId())
                                   .build());

                childLotList.add(childLot);
            }

            // lock
            List<Long> lockLotRrns = new ArrayList<>();
            lockLotRrns.add(parentLot.getLotRrn());
            lockLotRrns.addAll(childLotList.stream().map(Lot::getLotRrn).collect(Collectors.toList()));

            lotAutoMonitorReqService.finishMonitorJob(parentLot, childLotList, reason, lockLotRrns);
        }
    }

    public ActionForward finishSingleAutoMonitorLotPage(ActionMapping mapping, HttpServletRequest request,
                                                        HttpServletResponse response) {
        String lotId = WebUtils.getParameter("lotId", request);
        Lot lot = lotInqService.getLot(lotId);
        LotAutoMonitorInfo monitorLotInfo = lotAutoMonitorInqService.getLotActiveAutoMonitorInfo(lot.getLotRrn());
        Assert.state(Objects.nonNull(monitorLotInfo), Errors.create().content("Monitor lot not exist!").build());

        List<LotAutoMonitorInfo> monitorLotInfos = new ArrayList<>();
        monitorLotInfos.add(monitorLotInfo);

        request.setAttribute("baseLotRrn", monitorLotInfo.getBaseLotRrn());
        request.setAttribute("lotList", monitorLotInfos);
        request.setAttribute("lotRrn", monitorLotInfo.getLotRrn());
        request.setAttribute("lotId", lotId);
        return mapping.findForward("finishAutoMonitorLotPage");
    }

    public Map showSingleMergeToInfo(Map params){
        Long baseLotRrn = MapUtils.getLong(params, "baseLotRrn");
        Long lotRrn = MapUtils.getLong(params, "lotRrn");

        Lot parentLot = null;
        Lot childLot = null;

        Lot lot = lotInqService.getLot(lotRrn);
        Assert.state(!LotStatus.isProcessingStatus(lot.getLotStatus()),
                     Errors.create().key(MessageIdList.LOT_STATUS_NOT_ALLOW).build());

        String parentLotId = lot.getLotId();
        List<String> finishLotIdList = lotAutoMonitorInqService.getFinishLotIdList(baseLotRrn);
        for (String lotId : finishLotIdList) {
            if (NumberUtils.toInt(StringUtils.substringAfter(lotId, StringUtils.POINT_SIGN)) <
                    NumberUtils.toInt(StringUtils.substringAfter(parentLotId, StringUtils.POINT_SIGN))) {
                parentLotId = lotId;
            }
        }

        Assert.state(!StringUtils.equals(parentLotId,lot.getLotId()),
                     Errors.create().key(MessageIdList.AUTOMONITOR_LOT_IS_PARENT_LOT).args(lot.getLotId()).build());

        Map<String, Object> resultMap = MapUtils.newHashMap();
        resultMap.put("msg", I18nUtils
                .getMessage(MessageIdList.AUTOMONITOR_LOT_MERGE_TO, new Object[]{lot.getLotId(), parentLotId}));
        resultMap.put("parentLotId", parentLotId);
        resultMap.put("childLotId", lot.getLotId());
        return resultMap;
    }

    public void finishSingleAutoMonitorLot(Map params) {
        String reason = MapUtils.getString(params, "reason");
        String parentLotId = MapUtils.getString(params, "parentLotId");
        String childLotId = MapUtils.getString(params, "childLotId");

        Lot parentLot = lotInqService.getLot(parentLotId);
        Lot childLot = lotInqService.getLot(childLotId);

        // lock
        List<Long> lockLotRrns = new ArrayList<>();
        lockLotRrns.add(parentLot.getLotRrn());
        lockLotRrns.add(childLot.getLotRrn());

        lotAutoMonitorReqService.finishMonitorJob(parentLot, childLot, reason, lockLotRrns);
    }

}