BerthingServiceImpl.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. package com.ruoyi.system.service.impl;
  2. import cn.hutool.json.JSONUtil;
  3. import com.alibaba.excel.EasyExcel;
  4. import com.alibaba.excel.ExcelWriter;
  5. import com.alibaba.excel.context.AnalysisContext;
  6. import com.alibaba.excel.read.listener.ReadListener;
  7. import com.alibaba.excel.write.metadata.WriteSheet;
  8. import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
  9. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  10. import com.ruoyi.common.exception.base.BaseException;
  11. import com.ruoyi.system.domain.*;
  12. import com.ruoyi.system.dto.*;
  13. import com.ruoyi.system.mapper.*;
  14. import com.ruoyi.system.paramet.BerthingPointQuery;
  15. import com.ruoyi.system.paramet.FloatPointQuery;
  16. import com.ruoyi.system.paramet.StopPointQuery;
  17. import com.ruoyi.system.paramet.TimePointQuery;
  18. import com.ruoyi.system.service.BerthingService;
  19. import com.ruoyi.system.service.MqttGateWayService;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import javax.annotation.Resource;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.text.SimpleDateFormat;
  26. import java.util.*;
  27. /**
  28. * classname: 断面信息
  29. * description 服务实现类
  30. */
  31. @Service
  32. @Slf4j
  33. public class BerthingServiceImpl implements BerthingService {
  34. @Resource
  35. private SiteInfoMapper siteInfoMapper;
  36. @Resource
  37. private BerthingPointMapper berthingPointMapper;
  38. @Resource
  39. private BerthingPointConfigMapper berthingPointConfigMapper;
  40. @Resource
  41. private BerthingTimeConfigMapper berthingTimeConfigMapper;
  42. @Resource
  43. private BerthingFloatConfigMapper berthingFloatConfigMapper;
  44. @Resource
  45. private MqttGateWayService mqttGateWayService;
  46. @Override
  47. public ArrayList<PrintExcelDTO> uploadFile(MultipartFile file) {
  48. ArrayList<PrintExcelDTO> excelDateList = new ArrayList<>();
  49. try {
  50. if (file.isEmpty()) {
  51. log.error("文件为空");
  52. throw new BaseException("文件为空");
  53. }
  54. EasyExcel.read(file.getInputStream(), PrintExcelDTO.class, new ReadListener<PrintExcelDTO>() {
  55. @Override
  56. public void invoke(PrintExcelDTO printExcelDto, AnalysisContext analysisContext) {
  57. excelDateList.add(printExcelDto);
  58. }
  59. @Override
  60. public void doAfterAllAnalysed(AnalysisContext context) {}
  61. }).sheet().doRead();
  62. //排序
  63. excelDateList.sort(Comparator.comparingDouble(PrintExcelDTO::getX));
  64. return excelDateList;
  65. } catch (BaseException e){
  66. throw new BaseException(e.getMessage());
  67. } catch (Exception e){
  68. log.error("上传断面excel错误", e);
  69. throw new BaseException("上传断面excel错误");
  70. }
  71. }
  72. @Override
  73. public boolean insertBerthing(BerthingPointDTO berthingPointDTO){
  74. SiteInfo siteInfo = siteInfoMapper.queryById(berthingPointDTO.getSiteId());
  75. if (siteInfo == null) {
  76. throw new BaseException("站点不存在");
  77. }
  78. try {
  79. ArrayList<PrintExcelDTO> positions = berthingPointDTO.getPositions();
  80. positions.sort(Comparator.comparingDouble(PrintExcelDTO::getX));
  81. //设置他断面状态为无效
  82. berthingPointMapper.setBerthingStatus(siteInfo.getSiteId());
  83. BerthingPoint berthingPoint = new BerthingPoint();
  84. berthingPoint.setBerthingName(berthingPointDTO.getBerthingName());
  85. berthingPoint.setPositions(JSONUtil.toJsonStr(positions));
  86. berthingPoint.setSiteId(siteInfo.getSiteId());
  87. berthingPoint.setBerthingId(0L);
  88. berthingPoint.setCreateTime(new Date());
  89. berthingPoint.setIsDel(0);
  90. berthingPoint.setId(siteInfo.getId());
  91. berthingPoint.setStatus(1);
  92. boolean res = berthingPointMapper.insert(berthingPoint) > 0;
  93. //TODO 配置下发
  94. String topic = "down/" + siteInfo.getDeviceId() + "/section";
  95. HashMap<String, Object> requestMap = new HashMap<>();
  96. requestMap.put("id",siteInfo.getId());
  97. requestMap.put("points",positions);
  98. String data = JSONUtil.toJsonStr(requestMap);
  99. log.info("mqtt发送数据:topic:{} data:{}",topic,data);
  100. mqttGateWayService.sendMessageToMqtt(data, topic);
  101. return res;
  102. } catch (Exception e){
  103. log.error("新增断面错误", e);
  104. throw new BaseException("新增断面错误");
  105. }
  106. }
  107. @Override
  108. public Page<BerthingPoint> queryBerthing(BerthingPointQuery berthingPointQuery) {
  109. try {
  110. if (berthingPointQuery.getPage() == null || berthingPointQuery.getSize() == null) {
  111. berthingPointQuery.setPage(1L);
  112. berthingPointQuery.setSize(10L);
  113. }
  114. Page<SiteInfo> page = new Page<>(berthingPointQuery.getPage(), berthingPointQuery.getSize());
  115. return berthingPointMapper.queryAllByLimit(berthingPointQuery, page);
  116. } catch (Exception e) {
  117. log.error("查询站点错误", e);
  118. throw new BaseException("查询站点错误");
  119. }
  120. }
  121. @Override
  122. public BerthingPoint queryBerthingById(Long id) {
  123. return berthingPointMapper.queryById(id);
  124. }
  125. @Override
  126. public BerthingPoint queryBerthingBySiteId(Long siteId) {
  127. return berthingPointMapper.queryBySiteId(siteId);
  128. }
  129. @Override
  130. public Boolean deleteBerthingById(Long id) {
  131. BerthingPoint berthingPoint = berthingPointMapper.queryById(id);
  132. if (berthingPoint == null) {
  133. throw new BaseException("断面不存在");
  134. }
  135. BerthingPoint siteBerthingPoint = berthingPointMapper.queryBySiteId(berthingPoint.getSiteId());
  136. if (Objects.equals(berthingPoint.getBerthingId(), siteBerthingPoint.getBerthingId())) {
  137. throw new BaseException("最新断面不能删除");
  138. }
  139. berthingPointMapper.deleteById(id);
  140. return true;
  141. }
  142. @Override
  143. public boolean downFileById(Long id, HttpServletResponse response) {
  144. if (id == null) {
  145. throw new BaseException("断面ID不能为空");
  146. }
  147. BerthingPoint berthingPoint = berthingPointMapper.queryById(id);
  148. if (berthingPoint == null) {
  149. throw new BaseException("断面不存在");
  150. }
  151. return downloadExcel(berthingPoint, response);
  152. }
  153. @Override
  154. public boolean downFile(Long siteId, HttpServletResponse response) {
  155. if (siteId == null) {
  156. throw new BaseException("站点ID不能为空");
  157. }
  158. BerthingPoint berthingPoint = berthingPointMapper.queryBySiteId(siteId);
  159. if (berthingPoint == null) {
  160. throw new BaseException("没有断面数据");
  161. }
  162. return downloadExcel(berthingPoint, response);
  163. }
  164. private boolean downloadExcel(BerthingPoint berthingPoint, HttpServletResponse response) {
  165. try {
  166. List<PrintExcelDTO> positions = JSONUtil.toList(berthingPoint.getPositions(), PrintExcelDTO.class);
  167. response.setContentType("application/octet-stream; charset=utf-8");
  168. String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
  169. String fileName = "downFile_".concat(today);
  170. // 设置导出头信息,文件名放在里面
  171. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  172. // 设置自适应列宽,注册一个handler: LongestMatchColumnWidthStyleStrategy
  173. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
  174. // 创建sheet并设置好标题
  175. WriteSheet detailSheet = EasyExcel.writerSheet(0, "断面数据").head(PrintExcelDTO.class).build();
  176. // 向sheet中写入数据
  177. excelWriter.write(positions, detailSheet);
  178. // 数据下载,完成后会自动关闭流
  179. excelWriter.finish();
  180. return true;
  181. } catch (Exception e){
  182. log.error("下载文件错误", e);
  183. throw new BaseException("下载文件错误");
  184. }
  185. }
  186. @Override
  187. public boolean addStopPrint(BerthingPointConfigDTO berthingPointConfigDTO) {
  188. SiteInfo siteInfo = siteInfoMapper.queryById(berthingPointConfigDTO.getSiteId());
  189. if (siteInfo == null) {
  190. throw new BaseException("站点不存在");
  191. }
  192. if (berthingPointConfigDTO.getStopId() == null) {
  193. berthingPointConfigDTO.setStopId(0L);
  194. }
  195. if(berthingPointConfigDTO.getPositions().size() != berthingPointConfigDTO.getFactors().size()){
  196. throw new BaseException("修正系数与停泊点位长度不匹配");
  197. }
  198. BerthingPoint berthingPoint = berthingPointMapper.queryBySiteId(berthingPointConfigDTO.getSiteId());
  199. if (berthingPoint == null) {
  200. throw new BaseException("有效的断面数据未找到");
  201. }
  202. boolean res = false;
  203. BerthingPointConfig berthingPointConfig = berthingPointConfigMapper.queryById(berthingPointConfigDTO.getStopId());
  204. try {
  205. ArrayList<PrintExcelDTO> positions = berthingPointConfigDTO.getPositions();
  206. if (berthingPointConfig == null) {
  207. //新增
  208. berthingPointConfig = new BerthingPointConfig();
  209. //先查询有无重复添加
  210. berthingPointConfig.setWlevel(berthingPointConfigDTO.getWlevel());
  211. berthingPointConfig.setSiteId(berthingPointConfigDTO.getSiteId());
  212. long count = berthingPointConfigMapper.count(berthingPointConfig);
  213. if (count > 0){
  214. throw new BaseException("起测水位已存在");
  215. }
  216. //有无重复策略号
  217. berthingPointConfig.setPlanid(berthingPointConfigDTO.getPlanid());
  218. berthingPointConfig.setWlevel(null);
  219. count = berthingPointConfigMapper.count(berthingPointConfig);
  220. if (count > 0){
  221. throw new BaseException("策略编号已存在");
  222. }
  223. berthingPointConfig.setStopId(0L);
  224. berthingPointConfig.setSiteId(siteInfo.getSiteId());
  225. berthingPointConfig.setBerthingId(berthingPoint.getBerthingId());
  226. berthingPointConfig.setPositions(JSONUtil.toJsonStr(positions));
  227. berthingPointConfig.setPlanid(berthingPointConfigDTO.getPlanid());
  228. berthingPointConfig.setWlevel(berthingPointConfigDTO.getWlevel());
  229. berthingPointConfig.setFactors(JSONUtil.toJsonStr(berthingPointConfigDTO.getFactors()));
  230. berthingPointConfig.setCreateTime(new Date());
  231. berthingPointConfig.setIsDel(0);
  232. berthingPointConfig.setStatus(1);
  233. berthingPointConfig.setId(siteInfo.getId());
  234. res = berthingPointConfigMapper.insert(berthingPointConfig) > 0;
  235. } else {
  236. //先置旧停泊点策略无效 再新插入一条
  237. berthingPointConfig.setIsDel(1);
  238. berthingPointConfigMapper.update(berthingPointConfig);
  239. berthingPointConfig.setStopId(0L);
  240. berthingPointConfig.setPositions(JSONUtil.toJsonStr(positions));
  241. berthingPointConfig.setPlanid(berthingPointConfigDTO.getPlanid());
  242. berthingPointConfig.setWlevel(berthingPointConfigDTO.getWlevel());
  243. berthingPointConfig.setFactors(JSONUtil.toJsonStr(berthingPointConfigDTO.getFactors()));
  244. res = berthingPointConfigMapper.insert(berthingPointConfig) > 0;
  245. }
  246. //TODO 下发配置
  247. String topic = "down/" + siteInfo.getDeviceId() + "/stopPosition";
  248. HashMap<String, Object> requestMap = new HashMap<>();
  249. requestMap.put("id",siteInfo.getId());
  250. requestMap.put("wlevel",berthingPointConfig.getWlevel());
  251. requestMap.put("planid",berthingPointConfig.getPlanid());
  252. requestMap.put("positions",positions);
  253. requestMap.put("factors",berthingPointConfigDTO.getFactors());
  254. String data = JSONUtil.toJsonStr(requestMap);
  255. log.info("mqtt发送数据:topic:{} data:{}",topic,data);
  256. mqttGateWayService.sendMessageToMqtt(data, topic);
  257. } catch (BaseException e){
  258. throw new BaseException(e.getMessage());
  259. } catch (Exception e){
  260. log.error("新增或修改停泊点错误", e);
  261. throw new BaseException("新增或修改停泊点错误");
  262. }
  263. return res;
  264. }
  265. @Override
  266. public Page<BerthingPointConfig> queryStopPrint(StopPointQuery stopPointQuery) {
  267. try {
  268. if (stopPointQuery.getPage() == null || stopPointQuery.getSize() == null) {
  269. stopPointQuery.setPage(1L);
  270. stopPointQuery.setSize(10L);
  271. }
  272. return berthingPointConfigMapper.queryAllByLimit(stopPointQuery, new Page<>(stopPointQuery.getPage(), stopPointQuery.getSize()));
  273. } catch (Exception e) {
  274. log.error("查询停泊点错误", e);
  275. throw new BaseException("查询停泊点错误");
  276. }
  277. }
  278. @Override
  279. public BerthingPointConfig queryStopPointsById(Long id) {
  280. return berthingPointConfigMapper.queryById(id);
  281. }
  282. @Override
  283. public Boolean deleteStopPointsById(Long id) {
  284. berthingPointConfigMapper.deleteById(id);
  285. return true;
  286. }
  287. @Override
  288. public boolean addTimePrint(BerthingTimeDTO berthingTimeDTO) {
  289. SiteInfo siteInfo = siteInfoMapper.queryById(berthingTimeDTO.getSiteId());
  290. if (siteInfo == null) {
  291. throw new BaseException("站点不存在");
  292. }
  293. if (berthingTimeDTO.getTimeId() == null) {
  294. berthingTimeDTO.setTimeId(0L);
  295. }
  296. boolean res = false;
  297. BerthingTimeConfig berthingTime = berthingTimeConfigMapper.queryById(berthingTimeDTO.getTimeId());
  298. try {
  299. ArrayList<String> times = berthingTimeDTO.getTimes();
  300. if (berthingTime == null) {
  301. berthingTime = new BerthingTimeConfig();
  302. //先查询有无重复添加
  303. berthingTime.setWlevel(berthingTimeDTO.getWlevel());
  304. berthingTime.setSiteId(siteInfo.getSiteId());
  305. long count = berthingTimeConfigMapper.count(berthingTime);
  306. if (count > 0){
  307. throw new BaseException("起测水位已存在");
  308. }
  309. //有无重复策略号
  310. berthingTime.setPlanid(berthingTimeDTO.getPlanid());
  311. berthingTime.setWlevel(null);
  312. count = berthingTimeConfigMapper.count(berthingTime);
  313. if (count > 0){
  314. throw new BaseException("策略编号已存在");
  315. }
  316. //新增
  317. berthingTime.setTimeId(0L);
  318. berthingTime.setWlevel(berthingTimeDTO.getWlevel());
  319. berthingTime.setPlanid(berthingTimeDTO.getPlanid());
  320. berthingTime.setTimes(JSONUtil.toJsonStr(times));
  321. berthingTime.setCreateTime(new Date());
  322. berthingTime.setIsDel(0);
  323. berthingTime.setStatus(1);
  324. berthingTime.setId(siteInfo.getId());
  325. berthingTime.setType(berthingTimeDTO.getType());
  326. res = berthingTimeConfigMapper.insert(berthingTime) > 0;
  327. } else {
  328. //更新
  329. berthingTime.setPlanid(berthingTimeDTO.getPlanid());
  330. berthingTime.setWlevel(berthingTimeDTO.getWlevel());
  331. berthingTime.setTimes(JSONUtil.toJsonStr(times));
  332. berthingTime.setType(berthingTimeDTO.getType());
  333. res = berthingTimeConfigMapper.update(berthingTime) > 0;
  334. }
  335. //TODO 下发配置
  336. String topic = "down/" + siteInfo.getDeviceId() + "/measureMode";
  337. HashMap<String, Object> requestMap = new HashMap<>();
  338. requestMap.put("id",siteInfo.getId());
  339. requestMap.put("type",berthingTime.getType());
  340. requestMap.put("planid",berthingTime.getPlanid());
  341. requestMap.put("wlevel",berthingTime.getWlevel());
  342. requestMap.put("times",times);
  343. String data = JSONUtil.toJsonStr(requestMap);
  344. log.info("mqtt发送数据:topic:{} data:{}",topic,data);
  345. mqttGateWayService.sendMessageToMqtt(data, topic);
  346. } catch (BaseException e){
  347. throw new BaseException(e.getMessage());
  348. } catch (Exception e){
  349. log.error("新增或修改时间策略错误", e);
  350. throw new BaseException("新增或修改时间策略错误");
  351. }
  352. return res;
  353. }
  354. @Override
  355. public Page<BerthingTimeConfig> queryTimePrint(TimePointQuery timePointQuery) {
  356. try {
  357. if (timePointQuery.getPage() == null || timePointQuery.getSize() == null) {
  358. timePointQuery.setPage(1L);
  359. timePointQuery.setSize(10L);
  360. }
  361. return berthingTimeConfigMapper.queryAllByLimit(timePointQuery, new Page<>(timePointQuery.getPage(), timePointQuery.getSize()));
  362. } catch (Exception e) {
  363. log.error("查询停泊点错误", e);
  364. throw new BaseException("查询停泊点错误");
  365. }
  366. }
  367. @Override
  368. public BerthingTimeConfig queryStopTimeById(Long id) {
  369. return berthingTimeConfigMapper.queryById(id);
  370. }
  371. @Override
  372. public boolean deleteStopTimeById(Long id) {
  373. berthingTimeConfigMapper.deleteById(id);
  374. return true;
  375. }
  376. @Override
  377. public boolean addFloatPrint(BerthingFloatDTO berthingFloatDTO) {
  378. SiteInfo siteInfo = siteInfoMapper.queryById(berthingFloatDTO.getSiteId());
  379. if (siteInfo == null) {
  380. throw new BaseException("站点不存在");
  381. }
  382. if (berthingFloatDTO.getFloatId() == null) {
  383. berthingFloatDTO.setFloatId(0L);
  384. }
  385. boolean res;
  386. BerthingFloatConfig berthingFloat = berthingFloatConfigMapper.queryById(berthingFloatDTO.getFloatId());
  387. try {
  388. if (berthingFloat == null) {
  389. //新增
  390. berthingFloat = new BerthingFloatConfig();
  391. berthingFloat.setWlevel(berthingFloatDTO.getWlevel());
  392. berthingFloat.setSiteId(siteInfo.getSiteId());
  393. long count = berthingFloatConfigMapper.count(berthingFloat);
  394. if (count > 0) {
  395. throw new BaseException("起浮水位已存在");
  396. }
  397. //有无重复策略号
  398. berthingFloat.setPlanid(berthingFloatDTO.getPlanid());
  399. berthingFloat.setWlevel(null);
  400. count = berthingFloatConfigMapper.count(berthingFloat);
  401. if (count > 0){
  402. throw new BaseException("策略编号已存在");
  403. }
  404. berthingFloat.setFloatId(0L);
  405. berthingFloat.setPlanid(berthingFloatDTO.getPlanid());
  406. berthingFloat.setWlevel(berthingFloatDTO.getWlevel());
  407. berthingFloat.setCreateTime(new Date());
  408. berthingFloat.setIsDel(0);
  409. berthingFloat.setStatus(1);
  410. berthingFloat.setId(siteInfo.getId());
  411. berthingFloat.setTimespan(berthingFloatDTO.getTimespan());
  412. berthingFloat.setWlevelchange(berthingFloatDTO.getWlevelchange());
  413. berthingFloat.setType(berthingFloatDTO.getType());
  414. res = berthingFloatConfigMapper.insert(berthingFloat) > 0;
  415. } else {
  416. //更新
  417. berthingFloat.setType(berthingFloatDTO.getType());
  418. berthingFloat.setPlanid(berthingFloatDTO.getPlanid());
  419. berthingFloat.setWlevel(berthingFloatDTO.getWlevel());
  420. berthingFloat.setTimespan(berthingFloatDTO.getTimespan());
  421. berthingFloat.setWlevelchange(berthingFloatDTO.getWlevelchange());
  422. res = berthingFloatConfigMapper.update(berthingFloat) > 0;
  423. }
  424. //TODO 下发配置
  425. String topic = "down/" + siteInfo.getDeviceId() + "/addMode";
  426. HashMap<String, Object> requestMap = new HashMap<>();
  427. requestMap.put("id", siteInfo.getId());
  428. requestMap.put("type", berthingFloat.getType());
  429. requestMap.put("planid", berthingFloat.getPlanid());
  430. requestMap.put("wlevel", berthingFloat.getWlevel());
  431. requestMap.put("timespan", berthingFloat.getTimespan());
  432. requestMap.put("wlevelchange", berthingFloat.getWlevelchange());
  433. String data = JSONUtil.toJsonStr(requestMap);
  434. log.info("mqtt发送数据:topic:{} data:{}", topic, data);
  435. mqttGateWayService.sendMessageToMqtt(data, topic);
  436. }catch (BaseException e){
  437. throw new BaseException(e.getMessage());
  438. } catch (Exception e){
  439. log.error("新增或修改水位变幅策略错误", e);
  440. throw new BaseException("新增或修改水位变幅策略错误");
  441. }
  442. return res;
  443. }
  444. @Override
  445. public Page<BerthingFloatConfig> queryFloatPrint(FloatPointQuery floatPointQuery) {
  446. try {
  447. if (floatPointQuery.getPage() == null || floatPointQuery.getSize() == null) {
  448. floatPointQuery.setPage(1L);
  449. floatPointQuery.setSize(10L);
  450. }
  451. return berthingFloatConfigMapper.queryAllByLimit(floatPointQuery, new Page<>(floatPointQuery.getPage(), floatPointQuery.getSize()));
  452. } catch (Exception e) {
  453. log.error("查询水位变幅策略错误", e);
  454. throw new BaseException("查询水位变幅策略错误");
  455. }
  456. }
  457. @Override
  458. public BerthingFloatConfig queryStopWaterById(Long id) {
  459. return berthingFloatConfigMapper.queryById(id);
  460. }
  461. @Override
  462. public boolean deleteStopWaterById(Long id) {
  463. berthingFloatConfigMapper.deleteById(id);
  464. return true;
  465. }
  466. }