沈斌
2017-12-20 e1fd5f7d07a50c8682397936000f236d53f1b526
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
package com.moral.controller;
 
import static com.moral.common.util.ExportExcelUtils.exportData;
import static com.moral.common.util.WebUtils.getParametersStartingWith;
import static org.springframework.util.ObjectUtils.isEmpty;
 
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.moral.common.exception.BusinessException;
import com.moral.common.util.ValidateUtil;
import com.moral.service.DeviceService;
import com.moral.service.HistoryService;
 
@RestController
@RequestMapping("report")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ReportController {
 
    @Resource
    private HistoryService historyService;
    
    @Resource
    private DeviceService deviceService;
 
    @GetMapping("sensors-average")
    public Map<String, Object> getSensorsAverageByDevice(HttpServletRequest request,HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Map<String, Object> parameters = getParametersStartingWith(request, null);
            Object mac = parameters.get("mac");
            Object time = parameters.get("time");
            Object type = parameters.get("type");
            ValidateUtil.notNull(mac, "param.is.null");
            ValidateUtil.notNull(time, "param.is.null");
            ValidateUtil.notNull(type, "param.is.null");
            List<Map<String, Object>> sensors = deviceService.getSensorsByDevice(mac.toString());
            List<Map<String, Object>> sensorsAverage = historyService.getSensorsAverageByDevice4Report(parameters,sensors);
            if (isEmpty(sensorsAverage)) {
                result.put("msg", "无有效数据");
            } else {
                String[][] exportColumn = new String[sensors.size() + 1][];
                exportColumn[0] = new String[] { "时间", "20", "time" };
                for (int i = 0; i < sensors.size(); i++) {
                    String name = (String) sensors.get(i).get("name");
                    String key = (String) sensors.get(i).get("key");
                    exportColumn[i + 1] = new String[] { name, "10", key };
                }
                
                OutputStream outputStream = exportData(response, time + "_" + mac + "_" + type, sensorsAverage, exportColumn);
                outputStream.flush();
                outputStream.close();
            }
        } catch (BusinessException be) {
            be.printStackTrace();
            result.put("msg", be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("msg", "系统错误,请联系管理员!原因如下:"+e.getMessage());
        }
        return result;
    }
 
}