TestController2.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package com.yanfan.web.controller.tool;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.yanfan.common.annotation.Anonymous;
  4. import com.yanfan.common.core.domain.ImportExcelVO;
  5. import com.yanfan.common.core.domain.OutputExcelVO;
  6. import com.yanfan.common.utils.StringUtils;
  7. import com.yanfan.common.utils.poi.ExcelUtil;
  8. import com.yanfan.iot.domain.AlertLog;
  9. import com.yanfan.iot.domain.DeviceJob;
  10. import com.yanfan.iot.domain.DeviceLog;
  11. import com.yanfan.iot.mapper.AlertLogMapper;
  12. import com.yanfan.iot.mapper.DeviceJobMapper;
  13. import com.yanfan.iot.mapper.DeviceLogMapper;
  14. import com.yanfan.iot.mapper.DeviceMapper;
  15. import com.yanfan.iot.model.DeviceRelateAlertLogVO;
  16. import org.apache.commons.collections4.CollectionUtils;
  17. import org.springframework.web.bind.annotation.*;
  18. import org.springframework.web.multipart.MultipartFile;
  19. import javax.annotation.Resource;
  20. import java.util.*;
  21. import java.util.function.Function;
  22. import java.util.stream.Collectors;
  23. /**
  24. * 测试类
  25. *
  26. * @author fastb
  27. * @date 2023-09-13 11:42
  28. */
  29. @Anonymous
  30. @RestController
  31. @RequestMapping("/test2")
  32. public class TestController2 {
  33. @Resource
  34. private AlertLogMapper alertLogMapper;
  35. @Resource
  36. private DeviceMapper deviceMapper;
  37. @Resource
  38. private DeviceJobMapper deviceJobMapper;
  39. @Resource
  40. private DeviceLogMapper deviceLogMapper;
  41. @GetMapping("/add")
  42. public void add() {
  43. Set<String> deviceNumbers = new HashSet<>();
  44. deviceNumbers.add("D1PGLPG58K88");
  45. deviceNumbers.add("D1F0L7P84D8Z");
  46. deviceNumbers.add("D1F0L7P84D8Z_2");
  47. List<DeviceRelateAlertLogVO> deviceRelateAlertLogVOList = deviceMapper.selectDeviceBySerialNumbers(deviceNumbers);
  48. Map<String, DeviceRelateAlertLogVO> deviceRelateAlertLogVOMap = deviceRelateAlertLogVOList
  49. .stream()
  50. .collect(Collectors.toMap(DeviceRelateAlertLogVO::getSerialNumber, Function.identity()));
  51. ArrayList<AlertLog> alertLogList = new ArrayList<>();
  52. for (String deviceNumber : deviceNumbers) {
  53. AlertLog alertLog = new AlertLog();
  54. alertLog.setSerialNumber(deviceNumber);
  55. alertLog.setAlertName("温度告警测试");
  56. alertLog.setAlertLevel(1L);
  57. alertLog.setStatus(1);
  58. alertLog.setProductId(1L);
  59. alertLog.setDetail("111");
  60. alertLog.setCreateTime(new Date());
  61. // 添加设备关联信息
  62. if (deviceRelateAlertLogVOMap.containsKey(deviceNumber)) {
  63. DeviceRelateAlertLogVO deviceRelateAlertLogVO = deviceRelateAlertLogVOMap.get(deviceNumber);
  64. alertLog.setDeviceName(deviceRelateAlertLogVO.getDeviceName());
  65. alertLog.setUserId(deviceRelateAlertLogVO.getUserId());
  66. }
  67. alertLogList.add(alertLog);
  68. }
  69. // 批量插入告警日志
  70. alertLogMapper.insertAlertLogBatch(alertLogList);
  71. }
  72. @PostMapping("/handleExcel")
  73. public String handleExcel(@RequestParam("file") MultipartFile file) throws Exception {
  74. if (null == file) {
  75. return "导入失败,请先上传文件!";
  76. }
  77. ExcelUtil<ImportExcelVO> util = new ExcelUtil<>(ImportExcelVO.class);
  78. List<ImportExcelVO> importExcelVOList = util.importExcel(file.getInputStream());
  79. if (CollectionUtils.isEmpty(importExcelVOList)) {
  80. return "导入失败,模板数据不能为空!";
  81. }
  82. System.out.println(importExcelVOList.size());
  83. List<String> oneCityName = new ArrayList<>();
  84. List<String> twoCityName = new ArrayList<>();
  85. List<String> threeCityName = new ArrayList<>();
  86. List<OutputExcelVO> oneCityList = new ArrayList<>();
  87. Map<String, List<OutputExcelVO>> twoCityMap = new HashMap<>(2);
  88. Map<String, List<OutputExcelVO>> threeCityMap = new HashMap<>(2);
  89. for (ImportExcelVO vo : importExcelVOList) {
  90. String city = vo.getCity();
  91. List<String> cityNameList = StringUtils.str2List(city, "/", true, true);
  92. int size = cityNameList.size();
  93. if (1 == size) {
  94. String name = cityNameList.get(0);
  95. if (!oneCityName.contains(name)) {
  96. oneCityName.add(name);
  97. OutputExcelVO outputExcelVO = new OutputExcelVO();
  98. outputExcelVO.setName(name);
  99. outputExcelVO.setCode(vo.getCode());
  100. outputExcelVO.setLat(vo.getLat());
  101. outputExcelVO.setLon(vo.getLon());
  102. oneCityList.add(outputExcelVO);
  103. twoCityMap.put(name, new ArrayList<>());
  104. }
  105. } else if (2 == size) {
  106. String oneName = cityNameList.get(0);
  107. if (!oneCityName.contains(oneName)) {
  108. oneCityName.add(oneName);
  109. OutputExcelVO outputExcelVO = new OutputExcelVO();
  110. outputExcelVO.setName(oneName);
  111. oneCityList.add(outputExcelVO);
  112. twoCityMap.put(oneName, new ArrayList<>());
  113. }
  114. String twoName = cityNameList.get(1);
  115. OutputExcelVO outputExcelVO = new OutputExcelVO();
  116. outputExcelVO.setName(twoName);
  117. outputExcelVO.setCode(vo.getCode());
  118. outputExcelVO.setLat(vo.getLat());
  119. outputExcelVO.setLon(vo.getLon());
  120. if (!twoCityName.contains(twoName)) {
  121. twoCityName.add(twoName);
  122. List<OutputExcelVO> twoList = twoCityMap.get(oneName);
  123. twoList.add(outputExcelVO);
  124. twoCityMap.put(oneName, twoList);
  125. threeCityMap.put(twoName, new ArrayList<>());
  126. }
  127. } else if (3 == size) {
  128. String oneName = cityNameList.get(0);
  129. if (!oneCityName.contains(oneName)) {
  130. oneCityName.add(oneName);
  131. OutputExcelVO outputExcelVO = new OutputExcelVO();
  132. outputExcelVO.setName(oneName);
  133. oneCityList.add(outputExcelVO);
  134. twoCityMap.put(oneName, new ArrayList<>());
  135. }
  136. String twoName = cityNameList.get(1);
  137. if (!twoCityName.contains(twoName)) {
  138. twoCityName.add(twoName);
  139. OutputExcelVO outputExcelVO = new OutputExcelVO();
  140. outputExcelVO.setName(twoName);
  141. List<OutputExcelVO> twoList = twoCityMap.get(oneName);
  142. twoList.add(outputExcelVO);
  143. twoCityMap.put(oneName, twoList);
  144. threeCityMap.put(twoName, new ArrayList<>());
  145. }
  146. String threeName = cityNameList.get(2);
  147. if (!threeCityName.contains(threeName)) {
  148. threeCityName.add(threeName);
  149. OutputExcelVO outputExcelVO = new OutputExcelVO();
  150. outputExcelVO.setName(threeName);
  151. outputExcelVO.setCode(vo.getCode());
  152. outputExcelVO.setLat(vo.getLat());
  153. outputExcelVO.setLon(vo.getLon());
  154. List<OutputExcelVO> twoList = threeCityMap.get(twoName);
  155. twoList.add(outputExcelVO);
  156. threeCityMap.put(twoName, twoList);
  157. }
  158. }
  159. }
  160. int total = 0;
  161. total = total + oneCityList.size();
  162. for (OutputExcelVO outputExcelVO : oneCityList) {
  163. List<OutputExcelVO> voList = twoCityMap.get(outputExcelVO.getName());
  164. if (CollectionUtils.isNotEmpty(voList)) {
  165. total = total + voList.size();
  166. outputExcelVO.setChildren(voList);
  167. for (OutputExcelVO excelVO : voList) {
  168. List<OutputExcelVO> voList1 = threeCityMap.get(excelVO.getName());
  169. if (CollectionUtils.isNotEmpty(voList1)) {
  170. total = total + voList1.size();
  171. excelVO.setChildren(voList1);
  172. }
  173. }
  174. }
  175. }
  176. System.out.println(total);
  177. return JSON.toJSONString(oneCityList);
  178. }
  179. /**
  180. * 计算场景运算型变量的值
  181. *
  182. * @return java.lang.String
  183. */
  184. @GetMapping("/getJobList")
  185. public String getJobList() {
  186. Long[] ids = {1L, 2L};
  187. List<DeviceJob> deviceJobList = deviceJobMapper.selectListByJobTypeAndDatasourceIds(ids, 3);
  188. return JSON.toJSONString(deviceJobList);
  189. }
  190. @GetMapping("/getStatsValue")
  191. public String getStatsValue() {
  192. DeviceLog deviceLog = new DeviceLog();
  193. deviceLog.setLogType(1);
  194. deviceLog.setIdentity("k1");
  195. deviceLog.setSerialNumber("D10I741UL2P3_1");
  196. deviceLog.setBeginTime("2024-05-17 00:00:00");
  197. deviceLog.setEndTime("2024-05-17 23:59:59");
  198. // deviceLog.setOperation(5);
  199. List<String> list = deviceLogMapper.selectStatsValue(deviceLog);
  200. return JSON.toJSONString(list);
  201. }
  202. }