TGeoFenceServiceImpl.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package com.ylx.massage.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.ylx.common.exception.ServiceException;
  6. import com.ylx.common.utils.DateUtils;
  7. import com.ylx.massage.domain.TGeoFence;
  8. import com.ylx.massage.mapper.TGeoFenceMapper;
  9. import com.ylx.massage.service.TGeoFenceService;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.springframework.stereotype.Service;
  12. import java.math.BigDecimal;
  13. import java.util.List;
  14. /**
  15. * 地理围栏服务实现类
  16. */
  17. @Service
  18. public class TGeoFenceServiceImpl extends ServiceImpl<TGeoFenceMapper, TGeoFence> implements TGeoFenceService {
  19. private static final BigDecimal MIN_LONGITUDE = new BigDecimal("-180");
  20. private static final BigDecimal MAX_LONGITUDE = new BigDecimal("180");
  21. private static final BigDecimal MIN_LATITUDE = new BigDecimal("-90");
  22. private static final BigDecimal MAX_LATITUDE = new BigDecimal("90");
  23. private static final int RISK_LOW = 1;
  24. private static final int RISK_MEDIUM = 2;
  25. private static final int RISK_HIGH = 3;
  26. private static final int NOT_DELETE = 0;
  27. @Override
  28. public Page<TGeoFence> pageGeoFence(Page<TGeoFence> page, TGeoFence geoFence) {
  29. LambdaQueryWrapper<TGeoFence> queryWrapper = buildQueryWrapper(geoFence);
  30. queryWrapper.orderByAsc(TGeoFence::getId);
  31. return this.page(page, queryWrapper);
  32. }
  33. @Override
  34. public List<TGeoFence> listMapGeoFence(TGeoFence geoFence) {
  35. LambdaQueryWrapper<TGeoFence> queryWrapper = buildQueryWrapper(geoFence);
  36. queryWrapper.select(TGeoFence::getId, TGeoFence::getCityCode, TGeoFence::getCityName,
  37. TGeoFence::getFenceName, TGeoFence::getAddress, TGeoFence::getLongitude,
  38. TGeoFence::getLatitude, TGeoFence::getRadiusKm, TGeoFence::getRiskLevel);
  39. queryWrapper.isNotNull(TGeoFence::getLongitude)
  40. .isNotNull(TGeoFence::getLatitude)
  41. .orderByAsc(TGeoFence::getId);
  42. return this.list(queryWrapper);
  43. }
  44. @Override
  45. public Boolean addGeoFence(TGeoFence geoFence) {
  46. validateGeoFenceParam(geoFence);
  47. geoFence.setId(null);
  48. geoFence.setIsDelete(NOT_DELETE);
  49. geoFence.setCreateTime(DateUtils.getNowDate());
  50. geoFence.setUpdateTime(DateUtils.getNowDate());
  51. boolean saved = this.save(geoFence);
  52. if (!saved) {
  53. throw new ServiceException("新增围栏失败");
  54. }
  55. return true;
  56. }
  57. @Override
  58. public Boolean updateGeoFence(TGeoFence geoFence) {
  59. validateUpdateParam(geoFence);
  60. TGeoFence exists = this.getById(geoFence.getId());
  61. if (exists == null) {
  62. throw new ServiceException("围栏不存在");
  63. }
  64. TGeoFence update = new TGeoFence();
  65. update.setId(geoFence.getId());
  66. update.setCityCode(geoFence.getCityCode());
  67. update.setCityName(geoFence.getCityName());
  68. update.setFenceName(geoFence.getFenceName());
  69. update.setFenceIntro(geoFence.getFenceIntro());
  70. update.setAddress(geoFence.getAddress());
  71. update.setLongitude(geoFence.getLongitude());
  72. update.setLatitude(geoFence.getLatitude());
  73. update.setRadiusKm(geoFence.getRadiusKm());
  74. update.setRiskLevel(geoFence.getRiskLevel());
  75. update.setUpdateTime(DateUtils.getNowDate());
  76. boolean updated = this.updateById(update);
  77. if (!updated) {
  78. throw new ServiceException("编辑围栏失败");
  79. }
  80. return true;
  81. }
  82. @Override
  83. public Boolean deleteGeoFence(TGeoFence geoFence) {
  84. Long id = getValidId(geoFence);
  85. TGeoFence exists = this.getById(id);
  86. if (exists == null) {
  87. throw new ServiceException("围栏不存在");
  88. }
  89. boolean removed = this.removeById(id);
  90. if (!removed) {
  91. throw new ServiceException("删除围栏失败");
  92. }
  93. return true;
  94. }
  95. private void validateUpdateParam(TGeoFence geoFence) {
  96. if (geoFence == null) {
  97. throw new ServiceException("参数不能为空");
  98. }
  99. if (geoFence.getId() == null || geoFence.getId() <= 0) {
  100. throw new ServiceException("围栏ID不能为空");
  101. }
  102. validateGeoFenceParam(geoFence);
  103. }
  104. private Long getValidId(TGeoFence geoFence) {
  105. if (geoFence == null) {
  106. throw new ServiceException("参数不能为空");
  107. }
  108. if (geoFence.getId() == null || geoFence.getId() <= 0) {
  109. throw new ServiceException("围栏ID不能为空");
  110. }
  111. return geoFence.getId();
  112. }
  113. private void validateGeoFenceParam(TGeoFence geoFence) {
  114. if (geoFence == null) {
  115. throw new ServiceException("参数不能为空");
  116. }
  117. if (StringUtils.isBlank(geoFence.getCityCode())) {
  118. throw new ServiceException("城市编码不能为空");
  119. }
  120. if (StringUtils.isBlank(geoFence.getCityName())) {
  121. throw new ServiceException("城市名称不能为空");
  122. }
  123. if (StringUtils.isBlank(geoFence.getFenceName())) {
  124. throw new ServiceException("围栏名称不能为空");
  125. }
  126. if (StringUtils.isBlank(geoFence.getFenceIntro())) {
  127. throw new ServiceException("围栏介绍不能为空");
  128. }
  129. if (geoFence.getRadiusKm() == null || geoFence.getRadiusKm().compareTo(BigDecimal.ZERO) <= 0) {
  130. throw new ServiceException("围栏半径必须大于0");
  131. }
  132. if (!isValidRiskLevel(geoFence.getRiskLevel())) {
  133. throw new ServiceException("风险等级值不正确");
  134. }
  135. validateCoordinate(geoFence.getLongitude(), geoFence.getLatitude());
  136. }
  137. private boolean isValidRiskLevel(Integer riskLevel) {
  138. return riskLevel != null && (riskLevel == RISK_LOW || riskLevel == RISK_MEDIUM || riskLevel == RISK_HIGH);
  139. }
  140. private LambdaQueryWrapper<TGeoFence> buildQueryWrapper(TGeoFence geoFence) {
  141. LambdaQueryWrapper<TGeoFence> queryWrapper = new LambdaQueryWrapper<>();
  142. if (geoFence != null) {
  143. queryWrapper.eq(StringUtils.isNotBlank(geoFence.getCityCode()), TGeoFence::getCityCode, geoFence.getCityCode())
  144. .eq(StringUtils.isNotBlank(geoFence.getCityName()), TGeoFence::getCityName, geoFence.getCityName())
  145. .like(StringUtils.isNotBlank(geoFence.getFenceName()), TGeoFence::getFenceName, geoFence.getFenceName())
  146. .like(StringUtils.isNotBlank(geoFence.getAddress()), TGeoFence::getAddress, geoFence.getAddress());
  147. }
  148. return queryWrapper;
  149. }
  150. /**
  151. * 验证地理坐标栏中心点坐标是否有效。
  152. *
  153. * @param longitude 经度
  154. * @param latitude 纬度
  155. */
  156. private void validateCoordinate(BigDecimal longitude, BigDecimal latitude) {
  157. if (longitude == null) {
  158. throw new ServiceException("围栏中心点经度不能为空");
  159. }
  160. if (latitude == null) {
  161. throw new ServiceException("围栏中心点纬度不能为空");
  162. }
  163. if (longitude.compareTo(MIN_LONGITUDE) < 0 || longitude.compareTo(MAX_LONGITUDE) > 0) {
  164. throw new ServiceException("经度必须在-180到180之间");
  165. }
  166. if (latitude.compareTo(MIN_LATITUDE) < 0 || latitude.compareTo(MAX_LATITUDE) > 0) {
  167. throw new ServiceException("纬度必须在-90到90之间");
  168. }
  169. }
  170. }