OutFactoryHistoryAction.java

package com.mycim.webapp.actions.outfactory.outfactoryhistory;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.exception.SystemIllegalArgumentException;
import com.fa.sesa.i18n.I18nUtils;
import com.mycim.framework.jdbc.Page;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.OutFactory;
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.outfactory.OutFactoryHistoryForm;
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.text.SimpleDateFormat;
import java.util.*;

/**
 * @author liuji.li
 * @version 6.0.0
 * @date 2019/10/31
 **/
public class OutFactoryHistoryAction extends WipSetupAction {

    private static final String THISPAGE_KEY = "thisPage";

    private static final String PAGE_KEY = "page";

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

    public ActionForward queryOutFactoryHistory(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                                HttpServletResponse response) {
        OutFactoryHistoryForm ofohForm = (OutFactoryHistoryForm) form;
        Map<String, Object> condition = verifyParamsAndReturnQueryParams(ofohForm, request);

        int current = 1;
        if (StringUtils.isNotEmpty(WebUtils.getParameter(THISPAGE_KEY, request)) &&
                StringUtils.isEmpty(WebUtils.getParameter(PAGE_KEY, request))) {
            current = WebUtils.getParameterInt(THISPAGE_KEY, request);
        }
        int pageSize = WebUtils.getPageSize(request);

        Page page = new Page();
        page.setPageSize(pageSize);
        page.setPageNo(current);
        page = wipService.queryOutFactoryHistory(page, condition);

        request.setAttribute("outFactoryHistorys", page.getResults());
        WebUtils.setPageInfo(current, (int) page.getTotalPages(), pageSize, request);
        return mapping.findForward(Constants.VIEW_KEY);
    }

    public ActionForward export(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        OutFactoryHistoryForm theForm = (OutFactoryHistoryForm) form;
        Map<String, Object> titles = WebUtils.getExportTitles(request);
        Map<String, Object> condition = verifyParamsAndReturnQueryParams(theForm, request);

        int pageSize = WebUtils.getPageSize(request);
        int maxPage = WebUtils.getMaxPage(request);

        Page page = new Page();
        page.setPageSize(pageSize * maxPage);
        page.setPageNo(1);
        page = wipService.queryOutFactoryHistory(page, condition);
        // 导出
        String exportDateTime = DateUtils.getNowTime(DateUtils.DATE_FORMAT4NOSPLICING);
        List<Map> data = (List<Map>) page.getResults();
        if (StringUtils.equals("EN", I18nUtils.getCurrentLanguage().toString())) {
            titles.put("title", "Lot Outsourcing History Info");
        } else {
            titles.put("title", "批次外包历史信息");
        }
        String fileName = "History of lot outsourcing_" + exportDateTime + ".xlsx";
        WebUtils.exportExcel(fileName, titles, data, TemplateLocation.LOT_OUTSOURCING_HISTORY, response);
        return WebUtils.NULLActionForward;
    }

    private Map<String, Object> verifyParamsAndReturnQueryParams(OutFactoryHistoryForm theForm,
                                                                 HttpServletRequest request) {
        String lotId = theForm.getLotId();
        String outFactoryId = theForm.getOutFactoryId();
        String fsendTimeStart = theForm.getFsendTimeStart();
        String fsendTimeEnd = theForm.getFsendTimeEnd();
        String lotStatus = theForm.getLotStatus();

        Long outFactoryRrn = null;
        Long lotRrn = null;
        if (StringUtils.isNotBlank(outFactoryId)) {
            OutFactory outFactoryById = wipService.getOutFactoryById(outFactoryId.trim().toUpperCase());
            Assert.notNull(outFactoryById,
                           Errors.create().key(MessageIdList.LOTOUTFACTORY_NAME_ERROR).content("outFactoryId error!")
                                 .build());
            outFactoryRrn = outFactoryById.getOutFactoryRrn();
            theForm.setOutFactoryRrn(outFactoryRrn);
        }
        if (StringUtils.isNotBlank(lotId)) {
            Lot lot = lotQueryService.getLot(lotId.trim().toUpperCase());
            Assert.isFalse(lot.getLotRrn() < 1,
                           Errors.create().key(MessageIdList.DELETELOT_INVALID_ID).content("lotId error!").build());
            lotRrn = lot.getLotRrn();
            theForm.setLotRrn(lotRrn);
        }
        Map<String, Object> condtion = new HashMap<String, Object>();
        analyzeTimes(fsendTimeStart, fsendTimeEnd, condtion, request);
        condtion.put("outFactoryRrn", outFactoryRrn);
        condtion.put("lotRrn", lotRrn);
        condtion.put("lotStatus", lotStatus);
        return condtion;
    }

    /**
     * 针对时间条件做处理
     */
    private void analyzeTimes(String fsendTimeStart, String fsendTimeEnd, Map<String, Object> condtion,
                              HttpServletRequest request) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT4DAY);
            Calendar calendar = Calendar.getInstance();
            Date fsendTimeStartDate = null;
            Date fsendTimeEndDate = null;
            if (StringUtils.isNotBlank(fsendTimeStart) && StringUtils.isBlank(fsendTimeEnd)) {
                fsendTimeStartDate = format.parse(fsendTimeStart);
                calendar.setTime(fsendTimeStartDate);
                calendar.add(Calendar.MONTH, 1);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                fsendTimeEndDate = calendar.getTime();
            } else if (StringUtils.isNotBlank(fsendTimeStart) && StringUtils.isNotBlank(fsendTimeEnd)) {
                fsendTimeStartDate = format.parse(fsendTimeStart);
                fsendTimeEndDate = format.parse(fsendTimeEnd);
                calendar.setTime(fsendTimeEndDate);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                fsendTimeEndDate = calendar.getTime();
            } else if (StringUtils.isBlank(fsendTimeStart) && StringUtils.isNotBlank(fsendTimeEnd)) {
                fsendTimeEndDate = format.parse(fsendTimeEnd);
                calendar.setTime(fsendTimeEndDate);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                fsendTimeEndDate = calendar.getTime();
            }
            condtion.put("fsendTimeStart", fsendTimeStartDate);
            condtion.put("fsendTimeEnd", fsendTimeEndDate);
        } catch (Exception e) {
            throw new SystemIllegalArgumentException(
                    Errors.create().key(MessageIdList.LOTOUTFACTORY_DATA_WRONG).content("Date format is wrong!")
                          .build());
        }
    }

}