BerthingServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 lombok.extern.slf4j.Slf4j;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import javax.annotation.Resource;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.text.SimpleDateFormat;
  25. import java.util.ArrayList;
  26. import java.util.Comparator;
  27. import java.util.Date;
  28. import java.util.List;
  29. /**
  30. * classname: 断面信息
  31. * description 服务实现类
  32. */
  33. @Service
  34. @Slf4j
  35. public class BerthingServiceImpl implements BerthingService {
  36. @Resource
  37. private SiteInfoMapper siteInfoMapper;
  38. @Resource
  39. private BerthingPointMapper berthingPointMapper;
  40. @Resource
  41. private BerthingPointConfigMapper berthingPointConfigMapper;
  42. @Resource
  43. private BerthingTimeConfigMapper berthingTimeConfigMapper;
  44. @Resource
  45. private BerthingFloatConfigMapper berthingFloatConfigMapper;
  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. return berthingPointMapper.insert(berthingPoint) > 0;
  93. //TODO 配置下发
  94. } catch (Exception e){
  95. log.error("新增断面错误", e);
  96. throw new BaseException("新增断面错误");
  97. }
  98. }
  99. @Override
  100. public Page<BerthingPoint> queryBerthing(BerthingPointQuery berthingPointQuery) {
  101. try {
  102. if (berthingPointQuery.getPage() == null || berthingPointQuery.getSize() == null) {
  103. berthingPointQuery.setPage(1L);
  104. berthingPointQuery.setSize(10L);
  105. }
  106. Page<SiteInfo> page = new Page<>(berthingPointQuery.getPage(), berthingPointQuery.getSize());
  107. return berthingPointMapper.queryAllByLimit(berthingPointQuery, page);
  108. } catch (Exception e) {
  109. log.error("查询站点错误", e);
  110. throw new BaseException("查询站点错误");
  111. }
  112. }
  113. @Override
  114. public boolean downFile(Long siteId, HttpServletResponse response) {
  115. if (siteId == null) {
  116. throw new BaseException("站点ID不能为空");
  117. }
  118. BerthingPoint berthingPoint = berthingPointMapper.queryBySiteId(siteId);
  119. if (berthingPoint == null) {
  120. throw new BaseException("没有断面数据");
  121. }
  122. try {
  123. List<PrintExcelDTO> positions = JSONUtil.toList(berthingPoint.getPositions(), PrintExcelDTO.class);
  124. response.setContentType("application/octet-stream; charset=utf-8");
  125. String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
  126. String fileName = "downFile_".concat(today);
  127. // 设置导出头信息,文件名放在里面
  128. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  129. // 设置自适应列宽,注册一个handler: LongestMatchColumnWidthStyleStrategy
  130. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
  131. // 创建sheet并设置好标题
  132. WriteSheet detailSheet = EasyExcel.writerSheet(0, "断面数据").head(PrintExcelDTO.class).build();
  133. // 向sheet中写入数据
  134. excelWriter.write(positions, detailSheet);
  135. // 数据下载,完成后会自动关闭流
  136. excelWriter.finish();
  137. return true;
  138. } catch (Exception e){
  139. log.error("下载文件错误", e);
  140. throw new BaseException("下载文件错误");
  141. }
  142. }
  143. @Override
  144. public boolean addStopPrint(BerthingPointConfigDTO berthingPointConfigDTO) {
  145. SiteInfo siteInfo = siteInfoMapper.queryById(berthingPointConfigDTO.getSiteId());
  146. if (siteInfo == null) {
  147. throw new BaseException("站点不存在");
  148. }
  149. if (berthingPointConfigDTO.getStopId() == null) {
  150. berthingPointConfigDTO.setStopId(0L);
  151. }
  152. if(berthingPointConfigDTO.getPositions().size() != berthingPointConfigDTO.getFactors().size()){
  153. throw new BaseException("修正系数与停泊点位长度不匹配");
  154. }
  155. boolean res = false;
  156. BerthingPointConfig berthingPointConfig = berthingPointConfigMapper.queryById(berthingPointConfigDTO.getStopId());
  157. try {
  158. ArrayList<PrintExcelDTO> positions = berthingPointConfigDTO.getPositions();
  159. if (berthingPointConfig == null) {
  160. //新增
  161. berthingPointConfig = new BerthingPointConfig();
  162. berthingPointConfig.setStopId(0L);
  163. berthingPointConfig.setSiteId(siteInfo.getSiteId());
  164. berthingPointConfig.setBerthingId(0L);
  165. berthingPointConfig.setPositions(JSONUtil.toJsonStr(positions));
  166. berthingPointConfig.setPlanid(berthingPointConfigDTO.getPlanid());
  167. berthingPointConfig.setWlevel(berthingPointConfigDTO.getWlevel());
  168. berthingPointConfig.setFactors(JSONUtil.toJsonStr(berthingPointConfigDTO.getFactors()));
  169. berthingPointConfig.setCreateTime(new Date());
  170. berthingPointConfig.setIsDel(0);
  171. berthingPointConfig.setStatus(1);
  172. berthingPointConfig.setId(siteInfo.getId());
  173. res = berthingPointConfigMapper.insert(berthingPointConfig) > 0;
  174. } else {
  175. //更新
  176. berthingPointConfig.setPositions(JSONUtil.toJsonStr(positions));
  177. berthingPointConfig.setPlanid(berthingPointConfigDTO.getPlanid());
  178. berthingPointConfig.setWlevel(berthingPointConfigDTO.getWlevel());
  179. berthingPointConfig.setFactors(JSONUtil.toJsonStr(berthingPointConfigDTO.getFactors()));
  180. res = berthingPointConfigMapper.update(berthingPointConfig) > 0;
  181. }
  182. } catch (Exception e){
  183. log.error("新增或修改停泊点错误", e);
  184. throw new BaseException("新增或修改停泊点错误");
  185. }
  186. //TODO 下发配置
  187. return res;
  188. }
  189. @Override
  190. public Page<BerthingPointConfig> queryStopPrint(StopPointQuery stopPointQuery) {
  191. try {
  192. if (stopPointQuery.getPage() == null || stopPointQuery.getSize() == null) {
  193. stopPointQuery.setPage(1L);
  194. stopPointQuery.setSize(10L);
  195. }
  196. return berthingPointConfigMapper.queryAllByLimit(stopPointQuery, new Page<>(stopPointQuery.getPage(), stopPointQuery.getSize()));
  197. } catch (Exception e) {
  198. log.error("查询停泊点错误", e);
  199. throw new BaseException("查询停泊点错误");
  200. }
  201. }
  202. @Override
  203. public boolean addTimePrint(BerthingTimeDTO berthingTimeDTO) {
  204. SiteInfo siteInfo = siteInfoMapper.queryById(berthingTimeDTO.getSiteId());
  205. if (siteInfo == null) {
  206. throw new BaseException("站点不存在");
  207. }
  208. if (berthingTimeDTO.getTimeId() == null) {
  209. berthingTimeDTO.setTimeId(0L);
  210. }
  211. boolean res = false;
  212. BerthingTimeConfig berthingTime = berthingTimeConfigMapper.queryById(berthingTimeDTO.getTimeId());
  213. try {
  214. ArrayList<String> times = berthingTimeDTO.getTimes();
  215. if (berthingTime == null) {
  216. //新增
  217. berthingTime = new BerthingTimeConfig();
  218. berthingTime.setTimeId(0L);
  219. berthingTime.setSiteId(siteInfo.getSiteId());
  220. berthingTime.setPlanid(berthingTimeDTO.getPlanid());
  221. berthingTime.setTimes(JSONUtil.toJsonStr(times));
  222. berthingTime.setWlevel(berthingTimeDTO.getWlevel());
  223. berthingTime.setCreateTime(new Date());
  224. berthingTime.setIsDel(0);
  225. berthingTime.setStatus(1);
  226. berthingTime.setId(siteInfo.getId());
  227. berthingTime.setType(berthingTimeDTO.getType());
  228. res = berthingTimeConfigMapper.insert(berthingTime) > 0;
  229. } else {
  230. //更新
  231. berthingTime.setPlanid(berthingTimeDTO.getPlanid());
  232. berthingTime.setWlevel(berthingTimeDTO.getWlevel());
  233. berthingTime.setTimes(JSONUtil.toJsonStr(times));
  234. berthingTime.setType(berthingTimeDTO.getType());
  235. res = berthingTimeConfigMapper.update(berthingTime) > 0;
  236. }
  237. } catch (Exception e){
  238. log.error("新增或修改时间策略错误", e);
  239. throw new BaseException("新增或修改时间策略错误");
  240. }
  241. //TODO 下发配置
  242. return res;
  243. }
  244. @Override
  245. public Page<BerthingTimeConfig> queryTimePrint(TimePointQuery timePointQuery) {
  246. try {
  247. if (timePointQuery.getPage() == null || timePointQuery.getSize() == null) {
  248. timePointQuery.setPage(1L);
  249. timePointQuery.setSize(10L);
  250. }
  251. return berthingTimeConfigMapper.queryAllByLimit(timePointQuery, new Page<>(timePointQuery.getPage(), timePointQuery.getSize()));
  252. } catch (Exception e) {
  253. log.error("查询停泊点错误", e);
  254. throw new BaseException("查询停泊点错误");
  255. }
  256. }
  257. @Override
  258. public boolean addFloatPrint(BerthingFloatDTO berthingFloatDTO) {
  259. SiteInfo siteInfo = siteInfoMapper.queryById(berthingFloatDTO.getSiteId());
  260. if (siteInfo == null) {
  261. throw new BaseException("站点不存在");
  262. }
  263. if (berthingFloatDTO.getFloatId() == null) {
  264. berthingFloatDTO.setFloatId(0L);
  265. }
  266. boolean res = false;
  267. BerthingFloatConfig berthingFloat = berthingFloatConfigMapper.queryById(berthingFloatDTO.getFloatId());
  268. try {
  269. if (berthingFloat == null) {
  270. //新增
  271. berthingFloat = new BerthingFloatConfig();
  272. berthingFloat.setFloatId(0L);
  273. berthingFloat.setSiteId(siteInfo.getSiteId());
  274. berthingFloat.setPlanid(berthingFloatDTO.getPlanid());
  275. berthingFloat.setWlevel(berthingFloatDTO.getWlevel());
  276. berthingFloat.setCreateTime(new Date());
  277. berthingFloat.setIsDel(0);
  278. berthingFloat.setStatus(1);
  279. berthingFloat.setId(siteInfo.getId());
  280. berthingFloat.setTimespan(berthingFloatDTO.getTimespan());
  281. berthingFloat.setWlevelchange(berthingFloatDTO.getWlevelchange());
  282. res = berthingFloatConfigMapper.insert(berthingFloat) > 0;
  283. } else {
  284. //更新
  285. berthingFloat.setPlanid(berthingFloatDTO.getPlanid());
  286. berthingFloat.setWlevel(berthingFloatDTO.getWlevel());
  287. berthingFloat.setTimespan(berthingFloatDTO.getTimespan());
  288. berthingFloat.setWlevelchange(berthingFloatDTO.getWlevelchange());
  289. res = berthingFloatConfigMapper.update(berthingFloat) > 0;
  290. }
  291. } catch (Exception e){
  292. log.error("新增或修改水位变幅策略错误", e);
  293. throw new BaseException("新增或修改水位变幅策略错误");
  294. }
  295. //TODO 下发配置
  296. return res;
  297. }
  298. @Override
  299. public Page<BerthingFloatConfig> queryFloatPrint(FloatPointQuery floatPointQuery) {
  300. try {
  301. if (floatPointQuery.getPage() == null || floatPointQuery.getSize() == null) {
  302. floatPointQuery.setPage(1L);
  303. floatPointQuery.setSize(10L);
  304. }
  305. return berthingFloatConfigMapper.queryAllByLimit(floatPointQuery, new Page<>(floatPointQuery.getPage(), floatPointQuery.getSize()));
  306. } catch (Exception e) {
  307. log.error("查询水位变幅策略错误", e);
  308. throw new BaseException("查询水位变幅策略错误");
  309. }
  310. }
  311. }