TlInspectionLocationLogServiceImpl.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.ruoyi.qdtl.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.convert.Convert;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.NumberUtil;
  6. import cn.hutool.core.util.ObjectUtil;
  7. import cn.hutool.json.JSONObject;
  8. import com.ruoyi.common.utils.DateUtils;
  9. import com.ruoyi.qdtl.domain.TlInspectionLocationLog;
  10. import com.ruoyi.qdtl.domain.UserCheckPoint;
  11. import com.ruoyi.qdtl.mapper.TlInspectionLocationLogMapper;
  12. import com.ruoyi.qdtl.service.ITlInspectionLocationLogService;
  13. import org.gavaghan.geodesy.Ellipsoid;
  14. import org.gavaghan.geodesy.GeodeticCalculator;
  15. import org.gavaghan.geodesy.GlobalCoordinates;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.stream.Collectors;
  23. /**
  24. * 巡检记录管理Service业务层处理
  25. *
  26. * @author ruoyi
  27. * @date 2022-03-10
  28. */
  29. @Service
  30. public class TlInspectionLocationLogServiceImpl implements ITlInspectionLocationLogService {
  31. @Autowired
  32. private TlInspectionLocationLogMapper tlInspectionLocationLogMapper;
  33. /**
  34. * 查询巡检记录管理
  35. *
  36. * @param id 巡检记录管理主键
  37. * @return 巡检记录管理
  38. */
  39. @Override
  40. public TlInspectionLocationLog selectTlInspectionLocationLogById(Long id) {
  41. return tlInspectionLocationLogMapper.selectTlInspectionLocationLogById(id);
  42. }
  43. /**
  44. * 查询巡检记录管理列表
  45. *
  46. * @param tlInspectionLocationLog 巡检记录管理
  47. * @return 巡检记录管理
  48. */
  49. @Override
  50. public List<TlInspectionLocationLog> selectTlInspectionLocationLogList(TlInspectionLocationLog tlInspectionLocationLog) {
  51. return tlInspectionLocationLogMapper.selectTlInspectionLocationLogList(tlInspectionLocationLog);
  52. }
  53. @Override
  54. public List<TlInspectionLocationLog> selectTlInspectionLocationLogTrailList(TlInspectionLocationLog tlInspectionLocationLog) {
  55. return tlInspectionLocationLogMapper.selectTlInspectionLocationLogTrailList(tlInspectionLocationLog);
  56. }
  57. /**
  58. * 新增巡检记录管理
  59. *
  60. * @param tlInspectionLocationLog 巡检记录管理
  61. * @return 结果
  62. */
  63. @Override
  64. public int insertTlInspectionLocationLog(TlInspectionLocationLog tlInspectionLocationLog) {
  65. if (ObjectUtil.isNull(tlInspectionLocationLog.getCreateTime())) {
  66. tlInspectionLocationLog.setCreateTime(DateUtils.getNowDate());
  67. }
  68. return tlInspectionLocationLogMapper.insertTlInspectionLocationLog(tlInspectionLocationLog);
  69. }
  70. /**
  71. * 修改巡检记录管理
  72. *
  73. * @param tlInspectionLocationLog 巡检记录管理
  74. * @return 结果
  75. */
  76. @Override
  77. public int updateTlInspectionLocationLog(TlInspectionLocationLog tlInspectionLocationLog) {
  78. return tlInspectionLocationLogMapper.updateTlInspectionLocationLog(tlInspectionLocationLog);
  79. }
  80. /**
  81. * 批量删除巡检记录管理
  82. *
  83. * @param ids 需要删除的巡检记录管理主键
  84. * @return 结果
  85. */
  86. @Override
  87. public int deleteTlInspectionLocationLogByIds(Long[] ids) {
  88. return tlInspectionLocationLogMapper.deleteTlInspectionLocationLogByIds(ids);
  89. }
  90. /**
  91. * 删除巡检记录管理信息
  92. *
  93. * @param id 巡检记录管理主键
  94. * @return 结果
  95. */
  96. @Override
  97. public int deleteTlInspectionLocationLogById(Long id) {
  98. return tlInspectionLocationLogMapper.deleteTlInspectionLocationLogById(id);
  99. }
  100. @Override
  101. public Date queryMaxDate() {
  102. return tlInspectionLocationLogMapper.queryMaxDate();
  103. }
  104. @Override
  105. public List<TlInspectionLocationLog> queryByCondition(String date, String checkpointCard, String deviceId) {
  106. return tlInspectionLocationLogMapper.queryByCondition(date, checkpointCard, deviceId);
  107. }
  108. /**
  109. * 根据人员统计巡检次数
  110. *
  111. * @return
  112. */
  113. @Override
  114. public List<UserCheckPoint> queryUserGroup() {
  115. return tlInspectionLocationLogMapper.queryUserGroup();
  116. }
  117. /**
  118. * 查询经纬度非空的轨迹list数据
  119. *
  120. * @param date
  121. * @return
  122. */
  123. @Override
  124. public List<TlInspectionLocationLog> selectListByDate(String date) {
  125. return tlInspectionLocationLogMapper.selectListByDate(date);
  126. }
  127. @Override
  128. public double queryDistance(String date, String deviceId) {
  129. List<TlInspectionLocationLog> deviceTrails = this.selectListByDate(date);
  130. // 对轨迹按照设备编码分组
  131. Map<String, List<TlInspectionLocationLog>> deviceTrail = deviceTrails.stream().collect(Collectors.groupingBy(TlInspectionLocationLog::getDeviceCode));
  132. // 对每一个设备循环累计里程
  133. double distance = 0;
  134. for (Map.Entry<String, List<TlInspectionLocationLog>> stringListEntry : deviceTrail.entrySet()) {
  135. List<TlInspectionLocationLog> origin = stringListEntry.getValue();
  136. if (origin.size() < 2) {
  137. continue;
  138. }
  139. // 对原始list分成2组list,一个去掉头节点,一个去掉尾节点
  140. List<TlInspectionLocationLog> subPre = CollUtil.sub(origin, 0, origin.size() - 1);
  141. List<TlInspectionLocationLog> subAfter = CollUtil.sub(origin, 1, origin.size());
  142. for (int i = 0; i < subPre.size(); i++) {
  143. String fencePre = subPre.get(i).getFence();
  144. String fenceAfter = subAfter.get(i).getFence();
  145. // 计算2点之间距离
  146. GlobalCoordinates source = new GlobalCoordinates(Convert.toDouble(fencePre.split(",")[1]), Convert.toDouble(fencePre.split(",")[0]));
  147. GlobalCoordinates target = new GlobalCoordinates(Convert.toDouble(fenceAfter.split(",")[1]), Convert.toDouble(fenceAfter.split(",")[0]));
  148. // 创建GeodeticCalculator,调用计算方法
  149. double ellipsoidalDistance = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, source, target).getEllipsoidalDistance();
  150. distance = NumberUtil.add(distance, ellipsoidalDistance);
  151. }
  152. }
  153. return NumberUtil.div(distance, 1000, 3);
  154. }
  155. @Override
  156. public List<JSONObject> queryDistanceByDateRange(String startDate, String endDate) {
  157. List<TlInspectionLocationLog> locationLogList = tlInspectionLocationLogMapper.queryDistanceByDateRange(startDate, endDate);
  158. // 解析根据日期分组
  159. Map<String, List<TlInspectionLocationLog>> collect = locationLogList.stream().collect(Collectors.groupingBy(a -> DateUtil.formatDate(a.getCreateTime())));
  160. List<JSONObject> result = new ArrayList<>();
  161. for (Map.Entry<String, List<TlInspectionLocationLog>> stringListEntry : collect.entrySet()) {
  162. double distance = 0;
  163. List<TlInspectionLocationLog> origin = stringListEntry.getValue();
  164. if (origin.size() < 2) {
  165. continue;
  166. }
  167. // 对原始list分成2组list,一个去掉头节点,一个去掉尾节点
  168. List<TlInspectionLocationLog> subPre = CollUtil.sub(origin, 0, origin.size() - 1);
  169. List<TlInspectionLocationLog> subAfter = CollUtil.sub(origin, 1, origin.size());
  170. for (int i = 0; i < subPre.size(); i++) {
  171. String fencePre = subPre.get(i).getFence();
  172. String fenceAfter = subAfter.get(i).getFence();
  173. // 计算2点之间距离
  174. GlobalCoordinates source = new GlobalCoordinates(Convert.toDouble(fencePre.split(",")[1]), Convert.toDouble(fencePre.split(",")[0]));
  175. GlobalCoordinates target = new GlobalCoordinates(Convert.toDouble(fenceAfter.split(",")[1]), Convert.toDouble(fenceAfter.split(",")[0]));
  176. // 创建GeodeticCalculator,调用计算方法
  177. double ellipsoidalDistance = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, source, target).getEllipsoidalDistance();
  178. distance = NumberUtil.add(distance, ellipsoidalDistance);
  179. }
  180. JSONObject jb = new JSONObject();
  181. jb.set("date", stringListEntry.getKey());
  182. jb.set("week", DateUtil.dayOfWeekEnum(DateUtil.parseDate(stringListEntry.getKey())).toChinese("周"));
  183. jb.set("distance", NumberUtil.div(distance, 1000, 3));
  184. result.add(jb);
  185. }
  186. return CollUtil.sortByProperty(result, "date");
  187. }
  188. }