package com.yeshi.fanli.controller.admin.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.yeshi.utils.DateUtil; import com.google.gson.Gson; import com.yeshi.fanli.util.StringUtil; public class AdminUtils { /** * 验证数据 * @param dateType * @param startTime * @param endTime * @return */ public static String validateParams(Integer dateType, String startTime, String endTime) { String validateMsg = null; if (dateType == null) { validateMsg = "请选择视图类型"; } if (dateType == 1 && (StringUtil.isNullOrEmpty(startTime) || StringUtil.isNullOrEmpty(endTime))) { validateMsg = "请选择时间区间"; } return validateMsg; } /** * 获取时间列表 * @param dateType * @param startTime * @param endTime * @param year * @return */ public static List getDateList(Integer dateType, String startTime, String endTime, String year) { List dateList = new ArrayList(); if (dateType == 1) { try { dateList = DateUtil.dayFactory(startTime, endTime); } catch (Exception e) { e.printStackTrace(); } } else if (dateType == 2) { for (int i = 1; i <= 12; i++) { dateList.add(i + ""); // 12个月 } } return dateList; } public static Map yearsDataFactory(List> list) { List resultList = new ArrayList(); List listDate = new ArrayList(); if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { Map map = list.get(i); Object showValue = map.get("showValue"); Object showDate = map.get("showDate"); if (showValue == null) { showValue = 0; } resultList.add(showValue); listDate.add(showDate); } } Map map = new HashMap(); map.put("date", listDate); map.put("value", resultList); return map; } public static List dayOrMonthDataFactory(Integer dateType, List listDate , List> list) throws Exception { List resultList = new ArrayList(); if (list != null && list.size() > 0) { for (int i = 0; i < listDate.size(); i++) { Object showValue = null; if (list != null && list.size() > 0) { // 日期匹配赋值 for (int j = 0; j < list.size(); j++) { Map map = list.get(j); String showDate = listDate.get(i); if (dateType == 2 && Integer.parseInt(showDate) < 10 ) { showDate = "0" + showDate; } Object reslutDate = map.get("showDate"); String reslutTime = reslutDate.toString(); if (showDate.equalsIgnoreCase(reslutTime)) { showValue = map.get("showValue"); break; } } } if (showValue == null) { showValue = 0; } resultList.add(showValue); } } return resultList; } /** * 图表数据组织 * @param dateType * @param year * @param startTime * @param endTime * @param list * @throws Exception */ public static JSONObject chartDataFactory(Integer dateType, String year, String startTime, String endTime, List> list) throws Exception{ Object objectDate = null; List dateList = getDateList(dateType, startTime, endTime, year); Gson gson = new Gson(); JSONArray line_list = new JSONArray(); JSONObject innerList = new JSONObject(); innerList.put("name", "总计"); if (dateType != 3) { innerList.put("data", gson.toJson(AdminUtils.dayOrMonthDataFactory(dateType, dateList, list))); } else { // 年视图 Map map = yearsDataFactory(list); if (objectDate == null) { objectDate = map.get("date"); } innerList.put("data", gson.toJson(map.get("value"))); } line_list.add(innerList); JSONObject data = new JSONObject(); if (objectDate != null) { data.put("xAxis_list", gson.toJson(objectDate)); } else { data.put("xAxis_list", gson.toJson(dateList)); } data.put("line_list", line_list); return data; } }