IT-KIMI_SHI\SINOIT.KIMI
2018-12-07 50eb1d766c470dc6ff927199eaee934f972a8b70
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package web.myChart;
 
import job.UpdateChartOption;
import model.chart.ChartBuilderParams;
import model.connectionManage.SqlRecordingManage;
import model.myPanel.MyCharts;
import model.myPanel.PanelChartsWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.map.LinkedMap;
import org.quartz.*;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import service.chart.ChartsUtil;
import service.connectionManage.SqlRecordingManageService;
import service.myPanel.MyChartsService;
import service.myPanel.PanelChartsWrapperService;
import service.quartz.QuartzService;
import service.system.helper.DataSetProvider;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * Create time : 2016-09-09
 */
@Controller
@RequestMapping("/myChart")
public class MyChartController {
 
    @Resource
    private MyChartsService myChartsService;
 
    @Resource
    private PanelChartsWrapperService panelChartsWrapperService;
 
    @Resource
    private ChartsUtil chartsUtil;
 
    @Resource
    private QuartzService quartzService;
 
    @Resource
    private Scheduler quartzScheduler;
 
    @RequestMapping("/crud")
    @ResponseBody
    public Object crud(@RequestHeader(required = true) String oper, MyCharts myCharts) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        if ("insert".equals(oper)) {
            myChartsService.insert(myCharts);
        } else if ("update".equals(oper)) {
            myChartsService.update(myCharts);
        } else if ("delete".equals(oper)) {
            myChartsService.delete(myCharts);
            /**
             * 同时删除中间关联表信息
             */
            PanelChartsWrapper panelChartsWrapper = new PanelChartsWrapper();
            panelChartsWrapper.setChartId(myCharts.getId());
            panelChartsWrapperService.delete(panelChartsWrapper);
        }
        resMap.put("success", true);
        return resMap;
    }
 
    /**
     * 从我的图表中删除一条记录(需要先确认该图表未被使用, 同时删除对应的定时任务)
     * @param panelChartsWrapper
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteOneChart")
    @ResponseBody
    public Object deleteOneChart(PanelChartsWrapper panelChartsWrapper) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        panelChartsWrapper.setPaging(false);
        List<PanelChartsWrapper> panelChartsWrappers = panelChartsWrapperService.selectList(panelChartsWrapper);
        if(panelChartsWrappers.size() > 0){
            resMap.put("isDelete",false);
        }else{
            MyCharts myCharts = new MyCharts();
            myCharts.setId(panelChartsWrapper.getChartId());
            String group = myChartsService.selectOneChartInfo(myCharts).getChartType();
            myChartsService.delete(myCharts);
            quartzService.removeJob(myCharts.getId(), group);
            resMap.put("isDelete",true);
        }
        return resMap;
    }
 
    /**
     * 获取筛选字段的信息
     *
     * @param chartBuilderParams
     * @return
     * @throws Exception
     */
    @RequestMapping("/getFilterResult")
    @ResponseBody
    public Object getFilterResult(@RequestBody ChartBuilderParams chartBuilderParams) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("filterResult",chartsUtil.getFilterResult(chartBuilderParams));
        return map;
    }
 
    /**
     * 接受一组ChartBuilderParams,并返回筛选过滤信息
     * @param list
     * @return
     * @throws Exception
     */
    @RequestMapping("/getFilterResultOfList")
    @ResponseBody
    public Object getFilterResultOfList(@RequestBody List<ChartBuilderParams> list) throws Exception {
        List result = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            result.add(chartsUtil.getChartResult(list.get(i)));
        }
        return result;
    }
 
    /**
     * 多图表获取字段的信息
     * @param list
     * @return
     * @throws Exception
     */
    @RequestMapping("/getFilterResults")
    @ResponseBody
    public Object getFilterResults(@RequestBody List<ChartBuilderParams> list) throws Exception{
        Map<String, Object> map01 = new HashMap<>();
        for(int i=0;i<list.size();i++){
            map01.put(list.get(i).getDataRecordId(), chartsUtil.getChartResult(list.get(i)));
        }
        return map01;
    }
 
    /**
     * 添加quartz定時任務
     * @param chartId
     * @param startTime
     * @param period
     * @param triggerName
     * @param triggerGroup
     * @return
     * @throws Exception
     */
    @RequestMapping("/addSchedulerJob")
    @ResponseBody
    public Object addSchedulerJob(String chartId, String chartType, String startTime, String period, String triggerName, String triggerGroup) throws Exception{
        Map<String, Object> resMap = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
 
        param.put("chartId", chartId);
 
        Trigger trigger = quartzService.generateTrigger(startTime, period, triggerName, triggerGroup);
        quartzService.addJob(UpdateChartOption.class, chartId, chartType, trigger, param);
        quartzService.startJobs();
        resMap.put("success", true);
        return resMap;
    }
 
    /**
     * 获取当前定时任务信息
     * @param triggerName
     * @return
     */
    @RequestMapping("/getSchedulerInfo")
    @ResponseBody
    public Object getSchedulerInfo(String triggerName, String triggerGroup) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Trigger trigger = quartzScheduler.getTrigger(TriggerKey.triggerKey(triggerName, triggerGroup));
        if(trigger != null){
            Date startTime = trigger.getStartTime();
            Date nextTime = trigger.getNextFireTime();
            Date previousTime = trigger.getPreviousFireTime();
            String period = "";
 
            long timeDiff = 0;
            if(nextTime.getTime() > previousTime.getTime()){
                timeDiff = nextTime.getTime() - previousTime.getTime();
            }
            long days = timeDiff/(1000*60*60*24);
            if(days < 7){
                period = "day";
            }else if(days > 7 || days == 7 && days < 31){
                period = "week";
            }else if(days > 31 || days == 31){
                period = "month";
            }
 
            resMap.put("haveJob", true);
            resMap.put("startTime", sdf.format(startTime));
            resMap.put("period", period);
        }else {
            resMap.put("haveJob", false);
        }
 
        return resMap;
    }
 
    /**
     * 暂停指定任务
     * @param jobName
     * @param jobGroup
     * @return
     * @throws Exception
     */
    @RequestMapping("/pauseJob")
    @ResponseBody
    public Object pauseJob(String jobName, String jobGroup) throws Exception{
        Map<String, Object> resMap = new HashMap<>();
        if(quartzScheduler.getJobDetail(JobKey.jobKey(jobName, jobGroup)) != null){
            quartzService.pauseJob(JobKey.jobKey(jobName, jobGroup));
            resMap.put("success", true);
        }else {
            resMap.put("fail", "can not find the job!");
        }
        return resMap;
    }
 
    /**
     * 恢复指定任务
     * @param jobName
     * @param jobGroup
     * @return
     * @throws Exception
     */
    @RequestMapping("/resumeJob")
    @ResponseBody
    public Object resumeJob(String jobName, String jobGroup) throws Exception{
        Map<String, Object> resMap = new HashMap<>();
        if(quartzScheduler.getJobDetail(JobKey.jobKey(jobName, jobGroup)) != null){
            quartzService.resumeJob(JobKey.jobKey(jobName, jobGroup));
            resMap.put("success", true);
        }else {
            resMap.put("fail", "can not find the job!");
        }
        return resMap;
    }
 
    /**
     * 重新设置定时任务
     * @param startTime
     * @param period
     * @param triggerName
     * @param triggerGroup
     * @return
     * @throws Exception
     */
    @RequestMapping("/retScheduleJob")
    @ResponseBody
    public Object retScheduleJob(String startTime, String period, String triggerName, String triggerGroup) throws Exception{
        Map<String, Object> resMap = new HashMap<>();
        quartzService.retScheduleJob(TriggerKey.triggerKey(triggerName, triggerGroup), startTime, period);
        resMap.put("success", true);
        return resMap;
    }
 
    /**
     * 删除指定定时任务
     * @param name
     * @param group
     * @return
     */
    @RequestMapping("/removeJob")
    @ResponseBody
    public Object removeJob(String name, String group) throws Exception{
        Map<String, Object> resMap = new HashMap<>();
        quartzService.removeJob(name, group);
        resMap.put("success", true);
        return resMap;
    }
}