AdjustLotWafersAction.java

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

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.exception.SystemIllegalArgumentException;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.BeanUtils;
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.consts.CarrierType;
import com.mycim.valueobject.ems.Carrier;
import com.mycim.valueobject.sorter.FilterParam;
import com.mycim.valueobject.sorter.SorterDetailBean;
import com.mycim.valueobject.wip.*;
import com.mycim.webapp.actions.lot.ManualAndSortCheckAction;
import com.mycim.webapp.forms.lot.LotInfoForm;
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.*;

/**
 * @Author: yibing.liu
 * @Date: 2021/11/16 9:34
 */
public class AdjustLotWafersAction extends ManualAndSortCheckAction {

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

    public ActionForward adjustWafers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        LotInfoForm theform = getForm(form);
        List<SorterDetailBean> sourceUnitList = parseSorterDetailBeanList(theform.getSourceListValues());
        List<SorterDetailBean> targetUnitList = parseSorterDetailBeanList(theform.getTargetListValues());
        List<Map> targetListValues = buildCarrierMapping(targetUnitList);
        Assert.isFalse(targetUnitList.size() != sourceUnitList.size(), Errors.create().key(MessageIdList.UNIT_DATA_ERROR).content("").build());

        String lotId = theform.getLotId();
        Assert.isFalse(StringUtils.isBlank(lotId), Errors.create().key(MessageIdList.LOT_LOTID_OR_CASSETTEID_EMPTY)
                                                         .content("Lot Id and Cassette Id can't be Empty!</br>").build());

        Lot lot = lotQueryService.getLot(lotId, LocalContext.getFacilityRrn());
        Assert.isFalse(lot == null || lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("批次不存在!").build());

        List<Map> oldUnitList = wipQueryService.getUnitListByCarrier(lot.getCarrierRrn());

        String userName = LocalContext.getUserId();
        String reason = theform.getReasonCode() + " " + theform.getDeptExt().trim().toUpperCase() + " " + userName.toUpperCase() + " " + theform.getReason();
        lot.setTransPerformedby(userName);

        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("lot", lot);
        valueMap.put("transPerformedBy", userName);
        valueMap.put("newUnits", targetListValues);

        List<String> oldUnitIdList = new ArrayList<>();
        List<String> newUnitIdList = new ArrayList<>();
        for (Map sourceMap : oldUnitList) {
            String unitId = MapUtils.getStringCheckNull(sourceMap, "unitId");
            if (StringUtils.isNotEmptyTrim(unitId)) {
                oldUnitIdList.add(unitId);
            }
        }

        for (Map sourceMap : targetListValues) {
            String unitId = MapUtils.getStringCheckNull(sourceMap, "unitId");
            if (StringUtils.isNotEmptyTrim(unitId)) {
                newUnitIdList.add(unitId);
            }
        }

        Set<String> oldSet = new HashSet<>(oldUnitIdList);
        Set<String> newSet = new HashSet<>(newUnitIdList);
        Assert.isFalse(oldSet.size() != newSet.size(),
                       Errors.create().key(MessageIdList.WAFER_INVAILD_REFRESH).content("Wafer is invaild, please adjust after refresh !").build());
        Assert.isFalse(!newSet.containsAll(oldSet),
                       Errors.create().key(MessageIdList.WAFER_INVAILD_REFRESH).content("Wafer is invaild, please adjust after refresh !").build());


        TransReason transReason = new TransReason();
        transReason.setReasonCode(theform.getReasonCode());
        transReason.setReason(reason);
        transReason.setTransQty1(lot.getQty1());
        transReason.setTransQty2(lot.getQty2());
        transReason.setResponsibility(userName);
        valueMap.put("transReason", transReason);

        //暂时保留
        // if (carrierService.isDivideLocation(lot, lot.getFacilityRrn(), false)) {
        //     for (Object sourceListValue : sourceListValues) {
        //         Map sourceMap = (Map) sourceListValue;
        //         int position = MapUtils.getIntValue(sourceMap, "position");
        //         if (position % 2 == 0) {
        //             Assert.isFalse(position % 2 == 0, Errors.create().key("").content(
        //                     "Lot location is divide, unit position must be uneven number!").build());
        //         }
        //     }
        // }

        Carrier carrier = carrierService.getCarrier(lot.getCarrierRrn());
        checkInvalidLot4AdjustWafers(lot, carrier);
        lotBeforeIntoServiceCheck(lot, "OPQRSV");
        lotService.adjustLotWafers(valueMap);
        BeanUtils.copyProperties(theform, lot);
        theform.setReason("");
        theform.setReasonCode("");
        theform.setDeptExt("");
        theform.setLotId(lot.getLotId());

        request.setAttribute("success", "1");
        return initLotWafer(mapping, form, request, response);
    }


    public ActionForward initLotWafer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String lotId = request.getParameter("lotId");
        LotInfoForm theform = getForm(form);
        Assert.isFalse(StringUtils.isBlank(lotId), Errors.create().key(MessageIdList.LOT_ID_EMPTY).content("Lot Id can't be Empty!").build());

        Lot lot = lotQueryService.getLot(lotId, LocalContext.getFacilityRrn());
        Assert.isFalse(lot == null || lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("批次不存在!").build());

        Carrier carrier = carrierService.getCarrier(lot.getCarrierRrn());

        checkWaitJobs(carrier.getInstanceRrn(), carrier.getInstanceRrn(), 0L, null);
        checkInvalidLot4AdjustWafers(lot, carrier);

        List<Map> sourceUnits = wipQueryService.getUnitListByCarrier(carrier.getInstanceRrn());
        String sourceListValues = sorterQueryService.parseToJsonT(sourceUnits, new FilterParam<>(String.class));
        lot.setRecipePhysicalId(getRecipePhysicalId(lot));
        if (this.isTrackUnitFlag(lot)) {
            lot.setTrackUnitFlag("1");
        } else {
            lot.setTrackUnitFlag("0");
        }
        BeanUtils.copyProperties(theform, lot);
        theform.setCarrierId(carrier.getInstanceId());
        theform.setQty1(lot.getInt_qty1().doubleValue());
        theform.setSourceListValues(sourceListValues);
        theform.setTargetListValues(sourceListValues);
        request.setAttribute("lotInfo", form);
        request.setAttribute("success", Objects.isNull(request.getAttribute("success"))?"0":request.getAttribute("success"));

        return mapping.findForward("modify");
    }

    public ActionForward getViewAdjustWaferHistory(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String oldMappingRrn = request.getParameter("oldMappingRrn");
        String newMappingRrn = request.getParameter("newMappingRrn");
        LotInfoForm theform = getForm(form);

        Assert.isFalse(StringUtils.isBlankCheckNull(oldMappingRrn) && StringUtils.isBlankCheckNull(newMappingRrn),
                       Errors.create().key(MessageIdList.UNIT_DATA_ERROR).build());
        List<Unit> oldCarrierMapList = wipQueryService.getUnitHistoryListByCarrierMapRrn(NumberUtils.toLong(oldMappingRrn,0L));
        List<Unit> newCarrierMapList = wipQueryService.getUnitHistoryListByCarrierMapRrn(NumberUtils.toLong(newMappingRrn,0L));

        String sourceListValues = sorterQueryService.parseToJsonT(parseUnits(oldCarrierMapList), new FilterParam<>(String.class));
        String targetListValues = sorterQueryService.parseToJsonT(parseUnits(newCarrierMapList), new FilterParam<>(String.class));

        theform.setSourceListValues(sourceListValues);
        theform.setTargetListValues(targetListValues);
        request.setAttribute("lotInfo", form);

        return mapping.findForward("viewHistory");
    }

    private void checkInvalidLot4AdjustWafers(Lot lot, Carrier carrier) {
        Assert.isFalse(!(lot != null && carrier != null && lot.getLotRrn() > 0 && carrier.getInstanceRrn() > 0),
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("批次不存在!").build());

        if (StringUtils.equals(lot.getLotStatus(), LotStatus.BONDED)){
            int bondLotQty = wipQueryService.getLotWafeCount(lot.getLotRrn());
            Assert.isFalse(bondLotQty <= 0,
                           Errors.create().key(MessageIdList.QTY_IS_INVALID).content("The lot of QTY is invalid!").build());
        } else if (StringUtils.equals(lot.getLotStatus(), LotStatus.HOLD)){
            Assert.isFalse(lot.getQty1() + lot.getQty2() <= 0,
                           Errors.create().key(MessageIdList.QTY_IS_INVALID).content("The lot of QTY is invalid!").build());
            Assert.isFalse(StringUtils.isBlank(carrier.getInstanceId())
                                   || StringUtils.equals(CarrierType.DUMMY, carrier.getObjectSubtype()),
                           Errors.create().key("").content("The lot of cassette is invalid!").build());
        } else {
            throw new SystemIllegalArgumentException(
                    Errors.create().key(MessageIdList.CURRENT_STATUS_NOT_ALLOW).content("The current status of lot does not allow operation!").build());
        }

    }

    private void initPageInfo(HttpServletRequest request, LotInfoForm theform, Long facilityRrn, String language) {
        // String lotLocation = request.getParameter("lotLocation");
        // theform.setLotLocation(lotLocation);
        String lotId = request.getParameter("lotId");
        if (StringUtils.isBlank(lotId)) {
            theform.setSourceListValues(StringUtils.EMPTY);
            request.setAttribute("slotCount", 25);
            request.setAttribute("maxPosition", 0);
            Lot lot = new Lot();
            BeanUtils.copyProperties(theform, lot);
            request.setAttribute("lotInfo", theform);
        }
    }

    private long getSlotByBondLot (long sourceLotRrn){
        long slot = 25;

        List<Bonding> bondTargetLotList = lotQueryService.getBondingsByLotRrn(sourceLotRrn, StringUtils.EMPTY);
        if (CollectionUtils.isNotEmpty(bondTargetLotList)){
            Bonding bondTargetLot = bondTargetLotList.get(0);
            Lot targetLot = lotQueryService.getLot(bondTargetLot.getTargetLotRrn());
            Carrier targetCarrier = carrierService.getCarrier(targetLot.getCarrierRrn());
            slot = targetCarrier.getSlotCount();
        }

        return slot;
    }


}