| | |
| | |
|
| | | import java.io.OutputStream;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.LinkedHashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | | import javax.servlet.ServletOutputStream;
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | |
|
| | |
| | | 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 cn.hutool.core.io.IoUtil;
|
| | | import cn.hutool.poi.excel.ExcelWriter;
|
| | |
|
| | | import static com.moral.common.util.ExportExcelUtils.exportData;
|
| | | import static com.moral.common.util.WebUtils.getParametersStartingWith;
|
| | |
| | | public ResultBean<Boolean> getCustomMadeExcelReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
| | | Map<String, Object> parameters = getParametersStartingWith(request, null);
|
| | | ParameterUtils.getTimeType4Time(parameters);
|
| | | Object sensorKey = parameters.get("sensorKey");
|
| | | String sensorInfo = parameters.get("sensorInfo").toString();
|
| | | parameters.put("sensorKeys", Arrays.asList(sensorKey));
|
| | | 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);
|
| | | List<String> macList = new ArrayList<>();
|
| | | List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsAndDevicesByRegion(parameters);
|
| | | for (MonitorPoint m : monitorPoints) {
|
| | |
| | | parameters.put("macs", macList);
|
| | | List<Map<String, Object>> list = new ArrayList<>();
|
| | | if (!CollectionUtils.isEmpty(macList)) {
|
| | | list = historyMinutelyService.getDevicesAvgDataToExcel(parameters);
|
| | | list = historyMinutelyService.getDevicesSensorsAvgDataToExcel(parameters);
|
| | | }
|
| | | Map<String, Object> map = list.get(0);
|
| | | Map<Integer, String> mapNew = new LinkedHashMap<>();
|
| | | int i = 0;
|
| | | for (String key : map.keySet()) {
|
| | | if (!("monitorPointName".equals(key) || "name".equals(key))) {
|
| | | mapNew.put(i, key);
|
| | | i++;
|
| | | 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(), "");
|
| | | }
|
| | | }
|
| | | String[][] exportColumn = new String[map.size()][];
|
| | | exportColumn[0] = new String[]{"站点名称-" + sensorInfo, "30", "monitorPointName"};
|
| | | exportColumn[1] = new String[]{"设备名称", "20", "name"};
|
| | | for (int index = 0; index < map.size() - 2; index++) {
|
| | | exportColumn[index + 2] = new String[]{mapNew.get(index), "20", mapNew.get(index)};
|
| | | }
|
| | | OutputStream outputStream = exportData(response, "Excel数据", list, exportColumn);
|
| | | outputStream.flush();
|
| | | outputStream.close();
|
| | | return new ResultBean<Boolean>(true);
|
| | | sheet.add(data);
|
| | | }
|
| | | sheets.add(sheet);
|
| | | }
|
| | | if (!CollectionUtils.isEmpty(sheets)) {
|
| | | ExcelWriter writer = new ExcelWriter(false, sensorKeysNames.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.write(sheets.get(i), true);
|
| | | writer.autoSizeColumnAll();
|
| | | writer.setColumnWidth(0, 25);
|
| | | writer.setColumnWidth(1, 25);
|
| | | }
|
| | | }
|
| | | response.setContentType("application/vnd.ms-excel;charset=utf-8");
|
| | | String codedFileName = java.net.URLEncoder.encode("Excel数据", "UTF-8");
|
| | | response.setHeader("Content-Disposition", "attachment;filename=" + codedFileName + ".xls");
|
| | | ServletOutputStream out = response.getOutputStream();
|
| | | writer.flush(out, true);
|
| | | // 关闭writer,释放内存
|
| | | writer.close();
|
| | | //此处记得关闭输出Servlet流
|
| | | IoUtil.close(out);
|
| | | }
|
| | | return new ResultBean<>(true);
|
| | | }
|
| | |
|
| | | }
|