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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package web;
 
import com.alibaba.druid.support.json.JSONUtils;
import common.util.TemplateUtil;
import common.util.WebUtil;
import model.authority.User;
import model.chart.ChartBuilderParams;
import model.myPanel.MyCharts;
import model.myPanel.MyPanel;
import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import service.chart.ChartOption;
import service.chart.TableData;
import service.chart.bar.echarts.Bar;
import service.chart.line.echarts.Line;
import service.chart.pie.echarts.Pie;
import service.myPanel.MyChartsService;
import service.myPanel.MyPanelService;
 
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * Created by ct on 2016/8/18.
 */
@Controller
public class DefaultController {
 
    @Resource
    private MyPanelService myPanelService;
 
    @Resource
    private MyChartsService myChartsService;
 
    @Resource
    private TableData tableData;
 
    @RequestMapping("/login")
    public String login(User user,  RedirectAttributes redirectAttributes){
        final Subject subject = SecurityUtils.getSubject();
        final UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(user.getUserName(), user.getPassword());
        String redirect = "redirect:index.page";
        try {
            subject.login(usernamePasswordToken);
            redirect = "redirect:query.page";
        }catch (final UnknownAccountException uae){
            redirectAttributes.addFlashAttribute("error_message", "身份认证失败,请确认用户名和密码是否正确!");
        }catch (final IncorrectCredentialsException ice) {
            redirectAttributes.addFlashAttribute("error_message", "身份认证失败,请确认用户名和密码是否正确!");
        }catch (final AuthenticationException ae) {
            redirectAttributes.addFlashAttribute("error_message", "身份认证失败,请确认用户名和密码是否正确!");
            usernamePasswordToken.clear();
        }
        return redirect;
    }
 
    /**
     * 添加设计面板
     *
     * @param myPanel
     * @throws Exception
     */
    @RequestMapping("/addPanel")
    @ResponseBody
    public Object addPanel(MyPanel myPanel) throws Exception {
        return myPanelService.insert(myPanel);
    }
 
    /**
     * 显示设计面板
     *
     * @param exportId
     * @param map
     * @throws Exception
     */
    @RequestMapping("/showPanel.page")
    public Object showPanel(String exportId, ModelMap map) throws Exception {
        MyPanel myPanel = myPanelService.queryAsObject(exportId);
        map.addAttribute("exportId", myPanel.getExportId());
        map.addAttribute("htmlCode", myPanel.getHtmlCode());
        map.addAttribute("jsCode", myPanel.getJsCode());
        return "panel/designPanel";
    }
 
    /**
     * 保存当前设计面板并截取非实时预览图保存
     *
     * @param myPanel
     * @throws Exception
     */
    @RequestMapping("/export")
    @ResponseBody
    public Object export(MyPanel myPanel) throws Exception {
        myPanelService.update(myPanel);
        String snapshotImg = WebUtil.snapshotHtmlImageBase64Format(myPanel.getExtraMsg(), myPanel.getExportId());
        MyPanel panel = new MyPanel();
        panel.setExportId(myPanel.getExportId());
        panel.setImg(snapshotImg);
        return myPanelService.update(panel);
    }
 
    /**
     * 预览
     *
     * @param exportId
     * @param map
     * @throws Exception
     */
    @RequestMapping("/share.page")
    public Object share(String exportId, ModelMap map) throws Exception {
        MyPanel myPanel = myPanelService.queryAsObject(exportId);
        map.addAttribute("htmlCode", myPanel.getHtmlCode());
        map.addAttribute("exportId",exportId);
        return "export/exportTemplate";
    }
 
    /**
     * 获取预览图表配置
     *
     * @param cids
     * @throws Exception
     */
    @RequestMapping("/getShareOptions")
    @ResponseBody
    public Object getShareOptions(int[] cids,MyCharts myCharts) throws Exception{
        List<MyCharts> list = new ArrayList<>();
        for(int i=0;i<cids.length;i++){
            myCharts.setId(String.valueOf(cids[i]));
            list.add(myChartsService.selectOneChartInfo(myCharts));
        }
        return list;
    }
 
    /**
     * 跳转至主页面
     *
     * @throws Exception
     */
    @RequestMapping("/query.page")
    public Object query() throws Exception {
        return "panel/myPanel";
    }
 
    @RequestMapping("/dataAnalysis.page")
    public Object dataAnalysis() throws Exception {
        return "panel/dataAnalysis";
    }
 
    @RequestMapping("/sqlClient.page")
    public Object sqlClient() throws Exception {
        return "panel/sqlClient";
    }
 
    @RequestMapping("/index.page")
    public Object index() throws  Exception {
        return "panel/index";
    }
 
    @RequestMapping("/dashboard.page")
    public Object dashboard() throws  Exception {
        return "panel/dashboard";
    }
 
    /**
     * 分页查询panel
     *
     * @param myPanel
     * @throws Exception
     */
    @RequestMapping("/selectList")
    @ResponseBody
    public Object selectList(MyPanel myPanel) throws Exception {
        myPanel.setPageSize(myPanel.getPageSize());
        myPanel.setPage(myPanel.getPage());
        Map<String, Object> respMap = new HashMap<>();
        respMap.put("data", myPanelService.queryAsList(myPanel));
        respMap.put("totalPage", myPanel.getTotalPage());
        return respMap;
    }
 
    /**
     * 删除一块设计面板
     *
     * @param exportId
     * @throws Exception
     */
    @RequestMapping("/deleteOne")
    @ResponseBody
    public Object deleteOne(String exportId) throws Exception {
        return myPanelService.delete(exportId);
    }
 
    /**
     * 添加我的图表
     *
     * @param myCharts
     * @throws Exception
     */
    @RequestMapping("/addCharts")
    @ResponseBody
    public Object addCharts(MyCharts myCharts) throws  Exception{
        return myChartsService.insert(myCharts);
    }
 
    /**
     * 更新图表信息
     *
     * @param myCharts
     * @throws Exception
     */
    @RequestMapping("/updateChartInfo")
    @ResponseBody
    public Object updateChartInfo(@RequestHeader(required = false) String imgBase64, MyCharts myCharts) throws Exception {
        Map map = TemplateUtil.genObjFormJson(myCharts.getJsCode(), Map.class);
        map.put("image", imgBase64);
        myCharts.setJsCode(TemplateUtil.genJsonStr4Obj(map));
        return myChartsService.update(myCharts);
    }
 
    /**
     * 批量更新图表信息
     * @param list
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateChartsInfo")
    @ResponseBody
    public Object updateChartsInfo(@RequestBody List<MyCharts> list) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        for(MyCharts myCharts : list){
            myChartsService.update(myCharts);
        }
        resMap.put("success", true);
        return resMap;
    }
 
    @RequestMapping("/selectOneChartInfo")
    @ResponseBody
    public Object selectOneChartInfo(MyCharts myCharts) throws Exception {
        myCharts =  myChartsService.selectOneChartInfo(myCharts);
        return myCharts;
    }
 
    /**
     * 查询所有图表信息
     *
     * @param myCharts
     * @throws Exception
     */
    @RequestMapping("/selectChartInfo")
    @ResponseBody
    public Object selectList(MyCharts myCharts) throws  Exception {
        myCharts.setPaging(false);
        Map<String, Object> respMap = new HashMap<>();
        respMap.put("data",myChartsService.selectChartInfo(myCharts));
        return respMap;
    }
 
    /**
     *
     * @param imgFile
     * @return
     * @throws Exception
     */
    @RequestMapping("/imgToBase64")
    @ResponseBody
    public Object imgToBase64(@RequestParam MultipartFile[] imgFile) throws Exception {
        Map<String, Object> respMap = new HashMap<>();
        String imgBase64 = "";
        if(imgFile.length > 0){
            if(ImageIO.read(imgFile[0].getInputStream()) != null){
                byte[] content = imgFile[0].getBytes();
                imgBase64 = Base64.encodeBase64String(content);
                respMap.put("imgBase64",imgBase64);
            }else{
                respMap.put("imgBase64","noImage");
            }
        }else{
            respMap.put("imgBase64","noFile");
        }
        return respMap;
    }
 
    /**
     * 动态渲染图形
     *
     * @param chartBuilderParams
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/render", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public Object render(@RequestBody ChartBuilderParams chartBuilderParams, HttpServletRequest request) throws Exception {
 
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
 
        ChartOption chartOption = null;
 
        if (chartBuilderParams.getChartType() == ChartBuilderParams.ChartType.pie) {
            chartOption = context.getBean(Pie.class);
        } else if (chartBuilderParams.getChartType() == ChartBuilderParams.ChartType.line) {
            chartOption = context.getBean(Line.class);
        } else if (chartBuilderParams.getChartType() == ChartBuilderParams.ChartType.bar) {
            chartOption = context.getBean(Bar.class);
        } else if (chartBuilderParams.getChartType() == ChartBuilderParams.ChartType.table) {
               return tableData.getTableData(chartBuilderParams);
        }
 
        return TemplateUtil.genJsonStr4Obj(chartOption.transform(chartBuilderParams), true);
    }
}