LotPlanPortalAction.java

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

import com.fa.sesa.i18n.I18nUtils;
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.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.framework.utils.msg.JsonUtils;
import com.mycim.valueobject.InvConst;
import com.mycim.webapp.Constants;
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.*;

public class LotPlanPortalAction extends WipSetupAction {

    private static final String CN = "cn";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        int tabMaxCount = Integer.MAX_VALUE;
        String tabMaxCountStr = sysService.getRefFileValue("$PORTAL_TAB_MAX_COUNT", "LOTPORTAL", "DATA_1_VALUE");
        if (StringUtils.isNotBlank(tabMaxCountStr) && NumberUtils.isNumber(tabMaxCountStr)) {
            tabMaxCount = NumberUtils.toInt(tabMaxCountStr, Integer.MAX_VALUE);
        }
        request.setAttribute("totalTabCount", tabMaxCount);
        String[] types = new String[]{};
        registerOptionType(LocalContext.getFacilityRrn(), Arrays.asList(types), request);
        return mapping.getInputForward();
    }


    public List<Map> queryCondition(Map<String, Object> params) {
        String type = MapUtils.getString(params, Constants.TYPE_KEY);
        String likeValue = MapUtils.getString(params, "likeValue");

        if (StringUtils.isNotEmpty(likeValue) && StringUtils.equalsIgnoreCase(likeValue, "null")) {
            likeValue = null;
        }

        return lotQueryService.getComboItems(LocalContext.getFacilityRrn(), type, likeValue);
    }

    public Map<String, Object> getLotPlansList(Map<String, Object> params) {
        Map<String, Object> data = new HashMap<>(2);
        int pageSize = MapUtils.getIntValue(params, "limit");
        int pageNo = MapUtils.getIntValue(params, "page");
        params.put("waferStartFlag", "0");
        processParams(params);

        Page page = lotQueryService.getLotPlanPage(LocalContext.getFacilityRrn(), pageSize, pageNo, params);
        if (page.getResults() != null) {
            for (Object object : page.getResults()) {
                Map<String, Object> map = (Map<String, Object>) object;
                if (StringUtils.isBlank(MapUtils.getString(map, "materialNO"))) {
                    Long productRrn = MapUtils.getLong(map, "productRrn");
                    StringBuffer materials = new StringBuffer("");
                    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
                    list = prpService.getProductAndMaterialRelationForShowByProductRrn(productRrn);
                    if (list != null && list.size() > 0) {
                        for (Map<String, String> ignored : list) {
                            materials.append(ignored.get(InvConst.MATERIAL_ID)).append(",");
                        }
                    }
                    map.put("materialNO", StringUtils.substringBeforeLast(materials.toString(), ","));
                }
            }
        }
        data.put("totalItems", page.getTotalItems());
        data.put("items", page.getResults());
        return data;
    }

    public ActionForward exportAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileName = "lotPlanInfo_" + DateUtils.getNowTime(DateUtils.DATE_FORMAT4NOSPLICING) + ".xlsx";
        Map<String, Object> titles = WebUtils.getExportTitles(request);
        List<Map<String, Object>> data = (List<Map<String, Object>>) WebUtils.getExportDatas(request);
        titles.put("title", "Lot Plan Info");
        if (StringUtils.equalsIgnoreCase(CN, I18nUtils.getCurrentLanguage().toString())) {
            titles.put("priority", "优先级");
        } else {
            titles.put("priority", "Pri.");
        }

        int i = 0;
        for (Map<String, Object> m : data) {
            i++;
            m.put("seq", i);
            String hotFlag = MapUtils.getString(m, "hotFlag");
            String priority = MapUtils.getString(m, "priority");
            m.put("hotFlag&priority", hotFlag + "-" + priority);
        }
        WebUtils.exportExcel(fileName, titles, data, TemplateLocation.LOT_PLAN_LIST, response);
        return WebUtils.NULLActionForward;
    }

    private String buildOrderBy(String sortJsonString) {
        StringBuilder orderBy = new StringBuilder();

        if (StringUtils.isNotEmpty(sortJsonString)) {
            List sort = JsonUtils.toObject(sortJsonString, List.class);
            for (Iterator iterator = sort.iterator(); iterator.hasNext(); ) {
                Map<String, Object> map = (Map<String, Object>) iterator.next();
                String property = MapUtils.getString(map, "property");
                String direction = MapUtils.getString(map, "direction");
                orderBy.append(property).append(" ").append(direction);
                if (iterator.hasNext()) {
                    orderBy.append(", ");
                }
            }
        }

        return orderBy.toString();
    }

    private void processParams(Map<String, Object> params) {
        String sortJsonString = MapUtils.getString(params, "sort");
        params.put("orderBy", buildOrderBy(sortJsonString));

        for (Map.Entry<String, Object> m : params.entrySet()) {
            String value = m.getValue().toString();

            if (StringUtils.equals("productId", m.getKey()) || StringUtils.equals("lotId", m.getKey()) ||
                    StringUtils.equals("processId", m.getKey()) || StringUtils.equals("materialinfo", m.getKey())) {
                value = StringUtils.trimToUpperCase(value);
            }

            if (StringUtils.equals("endDueDate", m.getKey()) || StringUtils.equals("lotOwner", m.getKey()) ||
                    StringUtils.equals("waferStartFlag", m.getKey()) || StringUtils.equals("lotStatus", m.getKey())) {
                value = StringUtils.trim(value);
            }

            m.setValue(value);
        }
    }

}