package com.yeshi.fanli.controller.admin.utils;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.yeshi.utils.DateUtil;
|
|
import com.google.gson.Gson;
|
import com.yeshi.fanli.dto.ChartTDO;
|
import com.yeshi.fanli.util.StringUtil;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
|
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<String> getDateList(Integer dateType, String startTime, String endTime, String year) {
|
List<String> dateList = new ArrayList<String>();
|
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个月
|
}
|
} else if (dateType == 3) {
|
for (int i = 2018; i <= 2030; i++) {
|
dateList.add(i + ""); // 10年
|
}
|
}
|
|
return dateList;
|
}
|
|
|
public static Map<String, Object> yearsDataFactory(List<ChartTDO> list) {
|
|
List<Object> resultList = new ArrayList<Object>();
|
List<Object> listDate = new ArrayList<Object>();
|
|
if (list != null && list.size() > 0) {
|
for (ChartTDO chart: list) {
|
String showDate = chart.getShowDate();
|
String showValue = chart.getShowValue();
|
if (showValue == null) {
|
showValue = "0";
|
}
|
resultList.add(showValue);
|
listDate.add(showDate);
|
}
|
}
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
map.put("date", listDate);
|
map.put("value", resultList);
|
|
return map;
|
}
|
|
|
|
public static List<Object> dayOrMonthDataFactory(Integer dateType, List<String> listDate ,
|
List<ChartTDO> list) throws Exception {
|
List<Object> resultList = new ArrayList<Object>();
|
if (list != null && list.size() > 0) {
|
for (int i = 0; i < listDate.size(); i++) {
|
String showValue = null;
|
// 日期匹配赋值
|
for (ChartTDO chart: list) {
|
String showDate = listDate.get(i);
|
if (dateType == 2 && Integer.parseInt(showDate) < 10 ) {
|
showDate = "0" + showDate;
|
}
|
String reslutTime = chart.getShowDate();
|
if (showDate.equalsIgnoreCase(reslutTime)) {
|
showValue = chart.getShowValue();
|
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<ChartTDO> list) throws Exception{
|
|
Object objectDate = null;
|
List<String> 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<String, Object> 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;
|
}
|
|
}
|