ViewLotSplitHistoryEditAction.java

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

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.wip.Lot;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
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.ArrayList;
import java.util.List;

/**
 * 批次分批信息
 *
 * @author weike.li
 * @version 6.0.0
 * @date 2019/10/8
 **/
public class ViewLotSplitHistoryEditAction extends WipSetupAction {

    private static final String PARENT_OK = "parentok";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();

        LotInfoForm theForm = (LotInfoForm) form;

        if (WebUtils.getParameterBoolean(Constants.NAV_KEY, request)) {
            theForm.setPriority(10);
            request.setAttribute("lotInfo", theForm);
            request.setAttribute(SessionNames.COLLECTION_KEY, new ArrayList());
            return new ActionForward(mapping.getInput());
        }

        String id = "";
        if (request.getParameter(Constants.OK_KEY) != null) {
            id = StringUtils.trimToUpperCase(request.getParameter("lotId"));
        } else if (request.getParameter(PARENT_OK) != null) {
            String lotId = StringUtils.trimToUpperCase(request.getParameter("lotId"));
            Lot lot = lotQueryService.getLot(lotId, facilityRrn);

            Assert.isFalse(lot.getLotRrn() == lot.getBasedLotRrn().longValue(),
                           Errors.create().key(MessageIdList.PARENTLOT_MISSING).content("Invalid ParentLot Id!")
                                 .build());
            id = lotQueryService.getLot(lot.getBasedLotRrn()).getLotId();
        }

        Lot lot = lotQueryService.getLot(id, facilityRrn);
        theForm.setLotId(lot.getLotId());
        theForm.setCarrierId(lot.getCarrierId());
        theForm.setQty1(lot.getQty1());

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

        request.setAttribute(SessionNames.LOT_KEY, lot);
        String allPriority = wipQueryService
                .getHotflagSplicingPriority(Integer.parseInt(theForm.getHotFlag()), theForm.getPriority(), facilityRrn);
        theForm.setPriorityText(allPriority);
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, theForm);
        List splitLotInfo = lotQueryService.getSplitLotInfo(lot.getLotRrn(), facilityRrn);

        request.setAttribute(SessionNames.COLLECTION_KEY, splitLotInfo);

        return new ActionForward(mapping.getInput());
    }

    public ActionForward query(ActionMapping mapping, ActionForm form, HttpServletRequest request) {
        Long facilityRrn = LocalContext.getFacilityRrn();

        LotInfoForm theForm = (LotInfoForm) form;

        String id = "";
        String carrierId = theForm.getCarrierId();
        Long carrierRrn = 0L;

        if (StringUtils.isNotEmpty(carrierId)) {
            carrierRrn = this.getInstanceRrn(carrierId, this.getNamedSpace(ObjectList.ENTITY_KEY, facilityRrn),
                                             ObjectList.ENTITY_KEY);
            Assert.isFalse(carrierRrn <= 0,
                           Errors.create().key(MessageIdList.CASSETTE_MISSING_ID).content("No such Carrier exist!")
                                 .build());
        }
        if (request.getParameter(Constants.OK_KEY) != null) {
            id = StringUtils.trimToUpperCase(request.getParameter("lotId"));
        } else if (request.getParameter(PARENT_OK) != null) {
            String lotId = StringUtils.trimToUpperCase(request.getParameter("lotId"));
            Lot lot = lotQueryService.getLot(lotId, facilityRrn);

            Assert.isFalse(lot.getLotRrn() == lot.getBasedLotRrn().longValue(),
                           Errors.create().key(MessageIdList.PARENTLOT_MISSING).content("Invalid ParentLot Id!")
                                 .build());
            id = lotQueryService.getLot(lot.getBasedLotRrn()).getLotId();
        }

        Lot lot = lotQueryService.getLot(id, facilityRrn);
        Assert.isFalse((lot == null) || (lot.getLotRrn() <= 0),
                       Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("对象{}未找到").args("Lot")
                             .build());

        theForm.setLotId(lot.getLotId());
        theForm.setCarrierId(lot.getCarrierId());
        theForm.setQty1(lot.getQty1());

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

        request.setAttribute(SessionNames.LOT_KEY, lot);
        String allPriority = wipQueryService
                .getHotflagSplicingPriority(Integer.parseInt(theForm.getHotFlag()), theForm.getPriority(), facilityRrn);
        theForm.setPriorityText(allPriority);
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, theForm);
        List splitLotInfo = lotQueryService.getSplitLotInfo(lot.getLotRrn(), facilityRrn);

        request.setAttribute(SessionNames.COLLECTION_KEY, splitLotInfo);

        return new ActionForward(mapping.getInput());
    }

    public ActionForward reset(ActionMapping mapping, HttpServletRequest request) {
        LotInfoForm theForm = new LotInfoForm();

        theForm.setPriority(10);
        request.setAttribute("lotInfo", theForm);
        request.setAttribute(SessionNames.COLLECTION_KEY, new ArrayList());
        return new ActionForward(mapping.getInput());
    }

}