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.service.DeviceService; import com.moral.service.HistoryService; @RestController @RequestMapping(value = "report") @CrossOrigin(origins = "*", maxAge = 3600) public class ReportController { @Resource private HistoryService historyService; @Resource private DeviceService deviceService; @GetMapping("sensors-average") public Map getSensorsAverageByDevice(HttpServletRequest request,HttpServletResponse response) { Map result = new HashMap(); try { Map parameters = getParametersStartingWith(request, null); Object mac = parameters.get("mac"); Object time = parameters.get("time"); Object type = parameters.get("type"); if (isEmpty(mac) || isEmpty(time) || isEmpty(type)) { result.put("msg", "参数不能为空!"); } else { List> sensors = deviceService.getSensorsByDevice(mac.toString()); List> 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; } }