LotProcessStepHistoryAction.java

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

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.jdbc.Page;
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.valueobject.wip.Run;
import com.mycim.webapp.Constants;
import com.mycim.webapp.TemplateLocation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.LotStepHistoryForm;
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.List;
import java.util.Map;

/**
 * Lot Process Step History
 *
 * @author Luopeng.Wang
 * @version 6.0.0
 * @date 2019/9/5
 **/
public class LotProcessStepHistoryAction extends WipSetupAction {
    private static final String THISPAGE_KEY = "thisPage";

    private static final Integer PAGESIZE = 10;

    private static final String PAGE = "PAGE";

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

    public ActionForward query(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                               HttpServletResponse response) {

        LotStepHistoryForm theform = (LotStepHistoryForm) form;

        long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = StringUtils.trim(theform.getLotId());
        Assert.isFalse(StringUtils.isEmpty(lotId) ,
                       Errors.create().key(MessageIdList.LOT_LOTID_OR_CASSETTEID_EMPTY)
                             .content("Lot Id and Cassette Id can't be Empty!").build());

        Lot lot = null;
        if (StringUtils.isNotEmpty(lotId)) {
            lot = lotInqService.getLot(lotId);
        }

        Assert.isFalse(lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("Cannot Find Lot!").build());

        int current = 1;
        if (StringUtils.isNotEmpty(WebUtils.getParameter(THISPAGE_KEY, request)) &&
                StringUtils.isEmpty(WebUtils.getParameter("page", request))) {
            current = (int) NumberUtils.toDouble(WebUtils.getParameter(THISPAGE_KEY, request));
        }

        int pageSize = WebUtils.getParameterInt("pageSize", request);

        if (pageSize <= 0) {
            pageSize = PAGESIZE;
        }

        Page page = new Page();
        page.setPageSize(pageSize);
        page.setPageNo(current);

        page = lotQueryService.queryLotStepHistory(page, lot.getLotId());

        request.setAttribute("lotInfo", lot);
        request.setAttribute("lotStepHistory", page.getResults());
        request.setAttribute("currentPage", current);
        request.setAttribute("pageSize", pageSize);
        request.setAttribute("maxPage", page.getTotalPages());

        return mapping.findForward(Constants.HISTORY_SHOW_KEY);
    }

    public ActionForward runHistory(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        LotStepHistoryForm theform = (LotStepHistoryForm) form;

        Long runRrn = theform.getRunRrn();

        Run run = wipQueryService.getRun(runRrn);
        Assert.nonNull(run, Errors.create().key(MessageIdList.LOT_RUN_NOT_EXIST).content("No such Run exist!").build());

        int current = 1;
        if (StringUtils.isNotEmpty(WebUtils.getParameter(THISPAGE_KEY, request)) &&
                StringUtils.isEmpty(WebUtils.getParameter("page", request))) {
            current = (int) NumberUtils.toDouble(WebUtils.getParameter(THISPAGE_KEY, request));
        }

        int pageSize = WebUtils.getParameterInt("pageSize", request);

        if (pageSize <= 0) {
            pageSize = PAGESIZE;
        }

        Page page = new Page();
        page.setPageSize(pageSize);
        page.setPageNo(current);

        page = wipQueryService.queryRunHistory(page, run.getRunRrn());

        request.setAttribute("run", run);
        request.setAttribute("runHistory", page.getResults());
        request.setAttribute("currentPage", current);
        request.setAttribute("pageSize", pageSize);
        request.setAttribute("maxPage", page.getTotalPages());

        return mapping.findForward(Constants.RUNHISTORY_KEY);
    }


    public ActionForward export(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        Map<String, Object> titles = WebUtils.getExportTitles(request);
        titles.put("title", "Lot Process Step History");
        String exportType = WebUtils.getExportType(request);
        String lotId = WebUtils.getParameter("lotId", request);
        Lot lot = null;
        String qty1 = "";
        String productId = "";
        if (StringUtils.isNotEmpty(lotId)) {
            lot = lotInqService.getLot(lotId);
            qty1 = lot.getQty1() + "";
            productId = lot.getProductId();
        }

        titles.put("lot_value", "Lot Id:" + lotId);
        titles.put("qty_value", "Qty:" + qty1);
        titles.put("product_value", "Product Id:" + productId);

        Integer thisPage = WebUtils.getParameterInt("thisPage", request);
        Integer pageSize = WebUtils.getParameterInt("pageSize", request);
        Page page;
        if (StringUtils.equalsIgnoreCase(exportType, PAGE)) {
            page = getPage(false, pageSize, thisPage, PAGESIZE, 1);
        } else {
            page = getPage(true, pageSize, thisPage, PAGESIZE, 1);
        }


        page = lotQueryService.queryLotStepHistory(page, lot.getLotId());

        // 导出
        String exportDateTime = DateUtils.getNowTime(DateUtils.DATE_FORMAT4NOSPLICING);
        List<Map> data = (List<Map>) page.getResults();

        String fileName = "LotStepHist" + exportDateTime + ".xlsx";
        WebUtils.exportExcel(fileName, titles, data, TemplateLocation.LOT_STEP_HIST, response);
        return WebUtils.NULLActionForward;
    }

    private Page getPage(boolean isExportAll, Integer pageSize, Integer currentPage, Integer defaultpageSize,
                         Integer defaultcurrentPage) {
        Page page = new Page();
        currentPage = currentPage == 0 ? defaultcurrentPage : currentPage;
        pageSize = pageSize == 0 ? defaultpageSize : pageSize;
        if (isExportAll) {
            page.setPageNo(1);
            page.setPageSize(Integer.MAX_VALUE);
        } else {
            page.setPageNo(currentPage);
            page.setPageSize(pageSize);
        }
        return page;
    }

}