|  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Calendar; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.LinkedHashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.apache.commons.collections.CollectionUtils; | 
|---|
|  |  |  | import org.apache.commons.lang.StringUtils; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | 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.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.moral.common.bean.ResultBean; | 
|---|
|  |  |  | import com.moral.common.util.ParameterUtils; | 
|---|
|  |  |  | import com.moral.common.util.WebUtils; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.moral.service.HistoryDailyService; | 
|---|
|  |  |  | import com.moral.service.HistoryMinutelyService; | 
|---|
|  |  |  | import com.moral.service.MonitorPointService; | 
|---|
|  |  |  | import com.moral.service.SensorService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import cn.hutool.core.io.IoUtil; | 
|---|
|  |  |  | import cn.hutool.poi.excel.ExcelWriter; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private MonitorPointService monitorPointService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private SensorService sensorService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @GetMapping("compare") | 
|---|
|  |  |  | public ResultBean<Map<String, List>> getCompareReport(HttpServletRequest request) throws Exception { | 
|---|
|  |  |  | 
|---|
|  |  |  | public ResultBean<Boolean> getNExcelReport(HttpServletRequest request, HttpServletResponse response) throws Exception { | 
|---|
|  |  |  | Map<String, Object> parameters = getParametersStartingWith(request, null); | 
|---|
|  |  |  | List<Map<String, Object>> list = historyMinutelyService.getMonitorPointOrDeviceAvgData(parameters); | 
|---|
|  |  |  | ArrayList<String> sensorKeys = (ArrayList<String>)parameters.get("sensorKeys"); | 
|---|
|  |  |  | ArrayList<String> sensorKeys = (ArrayList<String>) parameters.get("sensorKeys"); | 
|---|
|  |  |  | for (Map<String, Object> map : list) { | 
|---|
|  |  |  | for (String sensorKey : sensorKeys) { | 
|---|
|  |  |  | if(map.get(sensorKey)==null){ | 
|---|
|  |  |  | map.put(sensorKey,""); | 
|---|
|  |  |  | if (map.get(sensorKey) == null) { | 
|---|
|  |  |  | map.put(sensorKey, ""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | beginTime = time + " 00"; | 
|---|
|  |  |  | endTime = timeb + " 23"; | 
|---|
|  |  |  | i = Calendar.HOUR_OF_DAY; | 
|---|
|  |  |  | }else if(type.equals("hour")){ | 
|---|
|  |  |  | dateFormat="yyyy-MM-dd HH:mm"; | 
|---|
|  |  |  | beginTime=time+":00:00"; | 
|---|
|  |  |  | endTime=timeb+":59:00"; | 
|---|
|  |  |  | i=Calendar.MINUTE; | 
|---|
|  |  |  | } else if (type.equals("hour")) { | 
|---|
|  |  |  | dateFormat = "yyyy-MM-dd HH:mm"; | 
|---|
|  |  |  | beginTime = time + ":00:00"; | 
|---|
|  |  |  | endTime = timeb + ":59:00"; | 
|---|
|  |  |  | i = Calendar.MINUTE; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); | 
|---|
|  |  |  | 
|---|
|  |  |  | Map<String, Object> parameters = getParametersStartingWith(request, null); | 
|---|
|  |  |  | ParameterUtils.getTimeType4Time(parameters); | 
|---|
|  |  |  | String sensorsInfo = parameters.get("sensors").toString(); | 
|---|
|  |  |  | String[] sensorsStr = sensorsInfo.split(","); | 
|---|
|  |  |  | List<String> sensorKeys = new ArrayList<>(); | 
|---|
|  |  |  | List<String> sensorKeysNames = new ArrayList<>(); | 
|---|
|  |  |  | List<String> sensorKeysUnits = new ArrayList<>(); | 
|---|
|  |  |  | for (int index = 0; index < sensorsStr.length; index++) { | 
|---|
|  |  |  | String[] split = sensorsStr[index].split("-"); | 
|---|
|  |  |  | String key; | 
|---|
|  |  |  | if (index == 0) { | 
|---|
|  |  |  | key = split[0].replace("\"", "").substring(1); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | key = split[0].replace("\"", ""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String name = split[1].replace("\"", ""); | 
|---|
|  |  |  | String Unit = split[2].replace("\"", ""); | 
|---|
|  |  |  | sensorKeys.add(key); | 
|---|
|  |  |  | sensorKeysNames.add(name); | 
|---|
|  |  |  | sensorKeysUnits.add(Unit); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | parameters.put("sensorKeys", sensorKeys); | 
|---|
|  |  |  | String sensorsInfoNew = StringUtils.strip(sensorsInfo, "[]"); | 
|---|
|  |  |  | String[] sensorsStr = sensorsInfoNew.split(","); | 
|---|
|  |  |  | List<List<String>> sensorInfos = sensorService.listSensorInfos(sensorsStr); | 
|---|
|  |  |  | parameters.put("sensorKeys", sensorInfos.get(0)); | 
|---|
|  |  |  | List<String> macList = new ArrayList<>(); | 
|---|
|  |  |  | List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsAndDevicesByRegion(parameters); | 
|---|
|  |  |  | for (MonitorPoint m : monitorPoints) { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (!CollectionUtils.isEmpty(macList)) { | 
|---|
|  |  |  | list = historyMinutelyService.getDevicesSensorsAvgDataToExcel(parameters); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<List<Map<String, Object>>> sheets = new ArrayList<>(); | 
|---|
|  |  |  | for (int i = 0; i < sensorKeys.size(); i++) { | 
|---|
|  |  |  | List<Map<String, Object>> sheet = new ArrayList<>(); | 
|---|
|  |  |  | for (int j = 0; j < list.size(); j++) { | 
|---|
|  |  |  | Map<String, Object> data = new LinkedHashMap<>(); | 
|---|
|  |  |  | for (Map.Entry<String, Object> kv : list.get(j).entrySet()) { | 
|---|
|  |  |  | if ("monitorPointName".equals(kv.getKey())) { | 
|---|
|  |  |  | data.put("站点名称(单位:" + sensorKeysUnits.get(i) + ")", kv.getValue()); | 
|---|
|  |  |  | } else if ("name".equals(kv.getKey())) { | 
|---|
|  |  |  | data.put("设备名称", kv.getValue()); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | String sensorsValue = kv.getValue().toString(); | 
|---|
|  |  |  | JSONObject jsonObject = JSONObject.parseObject(sensorsValue); | 
|---|
|  |  |  | if (jsonObject != null) { | 
|---|
|  |  |  | List<Object> sensorsValueList = (List<Object>) jsonObject.get(sensorKeys.get(i)); | 
|---|
|  |  |  | if (sensorsValueList != null) { | 
|---|
|  |  |  | data.put(kv.getKey(), sensorsValueList.get(0)); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | data.put(kv.getKey(), ""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | data.put(kv.getKey(), ""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | sheet.add(data); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | sheets.add(sheet); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<List<Map<String, Object>>> sheets = sensorService.listExcelDatas(sensorInfos, list); | 
|---|
|  |  |  | if (!CollectionUtils.isEmpty(sheets)) { | 
|---|
|  |  |  | ExcelWriter writer = new ExcelWriter(false, sensorKeysNames.get(0)); | 
|---|
|  |  |  | ExcelWriter writer = new ExcelWriter(false, sensorInfos.get(1).get(0)); | 
|---|
|  |  |  | writer.write(sheets.get(0), true); | 
|---|
|  |  |  | writer.autoSizeColumnAll(); | 
|---|
|  |  |  | writer.setColumnWidth(0, 25); | 
|---|
|  |  |  | writer.setColumnWidth(1, 25); | 
|---|
|  |  |  | if (sheets.size() >= 2) { | 
|---|
|  |  |  | for (int i = 1; i < sheets.size(); i++) { | 
|---|
|  |  |  | writer.setSheet(sensorKeysNames.get(i)); | 
|---|
|  |  |  | writer.setSheet(sensorInfos.get(1).get(i)); | 
|---|
|  |  |  | writer.write(sheets.get(i), true); | 
|---|
|  |  |  | writer.autoSizeColumnAll(); | 
|---|
|  |  |  | writer.setColumnWidth(0, 25); | 
|---|