admin
2019-01-15 233b0814bbb73b5c791ebe3643f4bd91ba2fa959
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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<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个月
            }
        }
        
        return dateList;                   
    }
    
    
    public static Map<String, Object> yearsDataFactory(List<Map<String, Object>> list) {
 
        List<Object> resultList = new ArrayList<Object>();
        List<Object> listDate = new ArrayList<Object>();
        
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> 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<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<Map<String, Object>> list) throws Exception {
 
        List<Object> resultList = new ArrayList<Object>();
        
        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<String, Object> 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<Map<String, Object>> 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;
    }
    
}