LotHoldReleaseHistoryAction.java

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

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.jdbc.Page;
import com.mycim.framework.utils.beans.BeanUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.wip.Lot;
import com.mycim.webapp.TemplateLocation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
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 weike.li
 * @version 6.0.0
 * @date 2019/9/19
 **/
public class LotHoldReleaseHistoryAction extends WipSetupAction {
    private static final String NUM_ONE = "1";

    private static final String CURRENT_FOR_COMPARE = "currentForCompare";

    private static final String THIS_PAGE = "thisPage";

    private static final String PAGE_SIZE = "pageSize";

    private static final String CN = "CN";

    private static final String LAB_FLAG = "flag";

    private static final String LAB_FLAG_VALUE = "before";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        Map lotInfo = new HashMap(9);
        request.setAttribute("lotInfo", lotInfo);
        request.setAttribute("list", new ArrayList<>());
        request.setAttribute(LAB_FLAG, LAB_FLAG_VALUE);
        request.setAttribute("current", 1);
        request.setAttribute("lastPageSize", 0);
        request.setAttribute("pageSize", 10);
        request.setAttribute("maxPage", 1);

        return mapping.findForward("query");
    }

    public ActionForward query(ActionMapping mapping, HttpServletRequest request) {

        String lotId = WebUtils.getParameterBoolean("byCarrierId", request) ? null : WebUtils
                .getParameterUpperCase("lotId", request).trim();
        String carrierId = WebUtils.getParameterBoolean("byLotId", request) ? null : WebUtils
                .getParameterUpperCase("carrierId", request).trim();
        long facilityRrn = LocalContext.getFacilityRrn();

        Lot lot = new Lot();
        if (StringUtils.equals(WebUtils.getParameter(LAB_FLAG, request), LAB_FLAG_VALUE)) {
            request.setAttribute(LAB_FLAG, LAB_FLAG_VALUE);
        }
        Assert.isFalse(StringUtils.isEmpty(lotId) && StringUtils.isEmpty(carrierId),
                       Errors.create().key(MessageIdList.LOT_MISSING_LOTID_AND_CASSETTEID)
                             .content("LotId or " + "CassetteId " + "can't be " + "Empty!").build());
        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());
        }
        Map<String, Object> lotInfo = BeanUtils.copyBeanToMap(lot);
        lotInfo.put("qty1", lot.getQty1().intValue());
        lotInfo.put("lotTransId", "lotholdreleasehistory");

        int pageSize = 10;
        int current = 1;

        if (request.getParameter(THIS_PAGE) != null) {
            current = new Integer(request.getParameter(THIS_PAGE));
        }

        if (request.getParameter(PAGE_SIZE) != null && (request.getParameter(PAGE_SIZE).length()) != 0) {
            pageSize = new Integer(request.getParameter(PAGE_SIZE));
        }

        if (NUM_ONE.equals(request.getParameter(CURRENT_FOR_COMPARE))) {
            current = 1;
        }
        String lotTransId = "lotholdreleasehistory";

        Page page = new Page(current, pageSize);
        page = lotQueryService.qryLotHoldReleaseHistory(page, lot.getLotRrn(), lotTransId,"query");
        request.setAttribute("lotInfo", lotInfo);
        request.setAttribute("list", page.getResults());

        request.setAttribute("current", page.getPageNo());
        request.setAttribute("lastPageSize", page.getPageSize());
        request.setAttribute("pageSize", page.getPageSize());
        request.setAttribute("maxPage", page.getTotalPages());

        return mapping.findForward("query");
    }

    public ActionForward holdReleaseInfo(ActionMapping mapping, HttpServletRequest request) {
        String lotId = request.getParameter("lotId");
        long transRrn = NumberUtils.toLong(request.getParameter("transRrn"));
        long facilityRrn = LocalContext.getFacilityRrn();

        Lot lot = new Lot();
        if (StringUtils.isNotEmpty(lotId)) {
            lot = lotQueryService.getLot(lotId, facilityRrn);
            Assert.isFalse(lot.equals(new Lot()),
                           Errors.create().key(MessageIdList.LOT_MISSING_ID).content("No such lotId").build());
        }

        Map<String,Object> lotInfo = BeanUtils.copyBeanToMap(lot);
        lotInfo.put("qty1", lot.getQty1().intValue());
        lotInfo.put("lotTransId", "lotholdinfoforrelease");
        lotInfo.put("transRrn", transRrn);

        int pageSize = 10;
        int current = 1;

        if (request.getParameter(THIS_PAGE) != null) {
            current = new Integer(request.getParameter(THIS_PAGE));
        }

        if (request.getParameter(PAGE_SIZE) != null && (request.getParameter(PAGE_SIZE).length()) != 0) {
            pageSize = new Integer(request.getParameter(PAGE_SIZE));
        }
        Page page = new Page(current, pageSize);
        page = lotQueryService.qryLotHoldInfoForRelease(page, lot.getLotRrn(), transRrn);

        request.setAttribute("lotInfo", lotInfo);
        request.setAttribute("list", page.getResults());
        request.setAttribute("current", current);
        request.setAttribute("lastPageSize", page.getPageSize());
        request.setAttribute("pageSize", page.getPageSize());
        request.setAttribute("maxPage", page.getTotalPages());
        request.setAttribute("lotId", lotId);


        return mapping.findForward("holdReleaseInfo");
    }

    public ActionForward export(HttpServletRequest request, ActionMapping mapping,
                                HttpServletResponse response) throws Exception {
        String lotId = request.getParameter("lotId");
        long facilityRrn = LocalContext.getFacilityRrn();
        Assert.isFalse(StringUtils.isEmpty(lotId),
                       Errors.create().key(MessageIdList.LOT_MISSING_ID).content("No such lotId").build());
        if (StringUtils.isNotEmpty(lotId)) {
            Lot lot = lotQueryService.getLot(lotId, facilityRrn);
            Assert.isFalse(lot.equals(new Lot()),
                           Errors.create().key(MessageIdList.LOT_MISSING_ID).content("No such lotId").build());
        }
        String lotTransId = "lotholdreleasehistory";
        Map<String, Object> titles = WebUtils.getExportTitles(request);
        long lotRrn = lotQueryService.getLotRrn(lotId);

        String exportDateTime = DateUtils.getNowTime(DateUtils.DATE_FORMAT4NOSPLICING);
        Page page = new Page(1, 9999);
        page = lotQueryService.qryLotHoldReleaseHistory(page, lotRrn, lotTransId,"query");
        query(mapping, request);
        String fileName = "LotHoldReleaseHistory_" + exportDateTime + ".xlsx";
        Map lotInfo = (Map) request.getAttribute("lotInfo");
        if (StringUtils.equalsIgnoreCase(CN, I18nUtils.getCurrentLanguage().toString())) {
            titles.put("title", "Lot Hold/Release History");
            titles.put("titleLotId", "批次号:" + lotInfo.get("lotId"));
            titles.put("titleQty", "数量:" + lotInfo.get("qty1"));
            titles.put("titleProductId", "产品名:" + lotInfo.get("productId"));
        } else {
            titles.put("title", "Lot Hold/Release History");
            titles.put("titleLotId", "Lot ID:" + lotInfo.get("lotId"));
            titles.put("titleQty", "Qty:" + lotInfo.get("qty1"));
            titles.put("titleProductId", "Product ID:" + lotInfo.get("productId"));
        }
        WebUtils.exportExcel(fileName, titles, (List) page.getResults(), TemplateLocation.LOT_HOLD_RELEASE_HIST, response);
        return WebUtils.NULLActionForward;
    }

}