LotSplitMergeHistoryAction.java

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

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.collections.MapUtils;
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/21
 **/
public class LotSplitMergeHistoryAction extends WipSetupAction {

    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();
        lotInfo.put("lotId", "");
        lotInfo.put("qty1", "");
        lotInfo.put("productId", "");
        request.setAttribute("lotInfo", lotInfo);
        request.setAttribute("list", new ArrayList<>());
        request.setAttribute("current", 1);
        request.setAttribute("lastPageSize", 1);
        request.setAttribute("pageSize", 10);
        request.setAttribute("maxPage", 1);
        return mapping.findForward("init");
    }

    public ActionForward query(ActionMapping mapping, HttpServletRequest request) {
        String lotId = StringUtils.isNotEmpty(request.getParameter("lotId")) ? StringUtils
                .trim(request.getParameter("lotId")) : StringUtils.trim(request.getParameter("lotIdForExport"));
        String startDate = WebUtils.getParameter("startDate", request);
        String endDate = WebUtils.getParameter("endDate", request);
        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),
                       Errors.create().key(MessageIdList.LOT_MISSING_LOTID_AND_CASSETTEID)
                             .content("lotId and carrierId miss").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 lotMapInfo = BeanUtils.copyBeanToMap(lot);

        long parentLotRrn = lotQueryService.getParentLotRrn(lot.getLotRrn());
        String sourceLotId = null;
        long lotRrn = lot.getLotRrn();
        if (parentLotRrn != lot.getLotRrn()) {
            sourceLotId = lotId;
            lotRrn = parentLotRrn;
        }

        Map lotInfo = BeanUtils.copyBeanToMap(lot);
        lotInfo.put("qty1", lot.getQty1().intValue());

        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 > 0 ? pageSize : 10);
        page = lotQueryService.qryLotSplitMergeHistory(page, lotRrn, startDate, endDate, sourceLotId);

        for (Object obj : page.getResults()) {
            Map temp = (Map) obj;
            temp.put("operationDesc", ctxExecService.getOperationDescByProcessInfo(lotMapInfo));
            temp.put("recipeId", getRecipePhysicalId(lot));
            temp.put("stageId", lot.getStageId());
        }
        request.setAttribute("list", page.getResults());
        request.setAttribute("lotInfo", lotInfo);
        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 export(HttpServletRequest request, ActionMapping mapping,
                                HttpServletResponse response) throws Exception {
        String lotId = StringUtils.trim(request.getParameter("lotIdForExport"));
        Map<String, Object> titles = WebUtils.getExportTitles(request);

        String exportDateTime = DateUtils.getNowTime(DateUtils.DATE_FORMAT4NOSPLICING);

        Lot lot = lotQueryService.getLot(lotId, LocalContext.getFacilityRrn());
        long parentLotRrn = lotQueryService.getParentLotRrn(lot.getLotRrn());
        String sourceLotId = null;
        if (parentLotRrn != lot.getLotRrn()) {
            sourceLotId = lotId;
            lot.setLotRrn(parentLotRrn);
        }
        Page page = new Page(1, 99999999);//导出excel查询所有
        page = lotQueryService.qryLotSplitMergeHistory(page, lot.getLotRrn(), null, null, sourceLotId);
        List data = (List) page.getResults();
        Map lotMapInfo = BeanUtils.copyBeanToMap(lot);
        for (Object obj : data) {
            Map tempMap = (Map) obj;
            Map<String, String> m = MapUtils.convertToMapOfString(tempMap);
            String unitIds = m.get("unitIds");
            unitIds = unitIds.substring(unitIds.indexOf("(") + 1, unitIds.indexOf(")"));
            String[] unitIdsArray = unitIds.split(",");
            unitIds = "";
            for (String uid : unitIdsArray) {
                uid = uid.substring(uid.indexOf("_") + 1);
                if (uid.length() < 2) {
                    uid = "0" + uid;
                }
                unitIds = unitIds + uid + ",";
            }
            unitIds = unitIds.substring(0, unitIds.length() - 1);
            tempMap.put("unitIds", unitIds);
            tempMap.put("operationDesc", ctxExecService.getOperationDescByProcessInfo(lotMapInfo));
            tempMap.put("recipeId", getRecipePhysicalId(lot));
            tempMap.put("stageId", lot.getStageId());
        }
        String fileName = "LotSplitMergeHistory_" + exportDateTime + ".xlsx";

        query(mapping, request);
        Map lotInfo = (Map) request.getAttribute("lotInfo");
        if (StringUtils.equalsIgnoreCase(CN, I18nUtils.getCurrentLanguage().toString())) {
            titles.put("title", "Lot Split/Merge History");
            titles.put("titleLotId", "批次号:" + lotInfo.get("lotId"));
            titles.put("titleQty", "数量:" + lotInfo.get("qty1"));
            titles.put("titleProductId", "产品名:" + lotInfo.get("productId"));
            titles.put("recipeId", "工艺菜单号");
        } else {
            titles.put("title", "Lot Split/Merge 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, data, TemplateLocation.LOT_SPLIT_MERGE_HIST, response);
        return WebUtils.NULLActionForward;
    }

}