Browse Source

开发地理围栏相关的接口

jinshihui 4 days ago
parent
commit
0b8e14e7a1

+ 43 - 5
nightFragrance-admin/src/main/java/com/ylx/web/controller/massage/TFareSettingController.java

@@ -2,6 +2,7 @@ package com.ylx.web.controller.massage;
 
 
 import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.ylx.common.core.controller.BaseController;
@@ -54,7 +55,16 @@ public class TFareSettingController extends BaseController {
     @GetMapping("selectAll")
     @ApiOperation("分页查询数据")
     public R selectAll(Page<TFareSetting> page, TFareSetting tFareSetting) {
-        return R.ok(this.tFareSettingService.page(page, new QueryWrapper<>(tFareSetting)));
+        try {
+            LambdaQueryWrapper<TFareSetting> queryWrapper = new LambdaQueryWrapper<>();
+            if(StringUtils.isNotBlank(tFareSetting.getCityName())){
+                queryWrapper.like(TFareSetting::getCityName, tFareSetting.getCityName());
+            }
+            return R.ok(this.tFareSettingService.page(page, queryWrapper));
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
     }
 
     /**
@@ -98,20 +108,48 @@ public class TFareSettingController extends BaseController {
      * @return R 修改结果
      */
     @PostMapping("update")
-    @ApiOperation("修改数据")
+    @ApiOperation("修改车费设置数据")
     public R update(@RequestBody TFareSetting tFareSetting) {
-        return R.ok(this.tFareSettingService.updateFareSetting(tFareSetting));
+        try {
+            return R.ok(this.tFareSettingService.updateFareSetting(tFareSetting));
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
     }
 
     /**
-     * 删除数据
+     * 根据主键ID删除车费设置数据
+     *
+     * @param id 主键
+     * @return R 删除结果
+     */
+    @DeleteMapping("delete/{id}")
+    @ApiOperation("根据主键ID删除车费设置数据")
+    public R delete(@PathVariable String id) {
+        try {
+            return R.ok(this.tFareSettingService.removeById(id));
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 批量删除车费设置数据
      *
      * @param idList 主键结合
      * @return R 删除结果
      */
     @PostMapping("delete")
+    @ApiOperation("批量删除车费设置数据")
     public R delete(@RequestBody List<String> idList) {
-        return R.ok(this.tFareSettingService.removeByIds(idList));
+        try {
+            return R.ok(this.tFareSettingService.removeByIds(idList));
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
     }
 
     /**

+ 137 - 0
nightFragrance-admin/src/main/java/com/ylx/web/controller/massage/TGeoFenceController.java

@@ -0,0 +1,137 @@
+package com.ylx.web.controller.massage;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ylx.common.annotation.Log;
+import com.ylx.common.core.domain.R;
+import com.ylx.common.enums.BusinessType;
+import com.ylx.common.exception.ServiceException;
+import com.ylx.massage.domain.TGeoFence;
+import com.ylx.massage.service.TGeoFenceService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * 地理围栏控制器
+ */
+@RestController
+@RequestMapping("/api/geoFence/v1")
+@Slf4j
+@Api(tags = {"地理围栏管理"})
+public class TGeoFenceController {
+
+    @Resource
+    private TGeoFenceService geoFenceService;
+
+    /**
+     * 分页查询地理围栏。
+     *
+     * @param page 分页参数
+     * @param geoFence 查询参数
+     * @return R<Page<TGeoFence>>
+     */
+    @GetMapping("/select")
+    @ApiOperation("分页查询地理围栏")
+    public R<Page<TGeoFence>> select(Page<TGeoFence> page, TGeoFence geoFence) {
+        try {
+            return R.ok(geoFenceService.pageGeoFence(page, geoFence));
+        } catch (ServiceException s) {
+            log.error("分页查询地理围栏失败:{}", s.getMessage());
+            return R.fail(s.getMessage());
+        } catch (Exception e) {
+            log.error("分页查询地理围栏系统异常", e);
+            return R.fail("系统异常");
+        }
+    }
+
+    /**
+     * 查询地图展示用地理围栏。
+     *
+     * @param geoFence 查询参数
+     * @return R<List<TGeoFence>>
+     */
+    @GetMapping("/map")
+    @ApiOperation("查看地图地理围栏")
+    public R<List<TGeoFence>> map(TGeoFence geoFence) {
+        try {
+            return R.ok(geoFenceService.listMapGeoFence(geoFence));
+        } catch (ServiceException s) {
+            log.error("查询地图地理围栏失败:{}", s.getMessage());
+            return R.fail(s.getMessage());
+        } catch (Exception e) {
+            log.error("查询地图地理围栏系统异常", e);
+            return R.fail("系统异常");
+        }
+    }
+
+    /**
+     * 新增地理围栏。
+     *
+     * @param geoFence 地理围栏
+     * @return R
+     */
+    @Log(title = "地理围栏管理", businessType = BusinessType.INSERT)
+    @PostMapping("/add")
+    @ApiOperation("新增地理围栏")
+    public R add(@RequestBody TGeoFence geoFence) {
+        try {
+            return R.ok(geoFenceService.addGeoFence(geoFence));
+        } catch (ServiceException s) {
+            log.error("新增地理围栏失败:{}", s.getMessage());
+            return R.fail(s.getMessage());
+        } catch (Exception e) {
+            log.error("新增地理围栏系统异常", e);
+            return R.fail("系统异常");
+        }
+    }
+
+    /**
+     * 编辑地理围栏。
+     *
+     * @param geoFence 地理围栏
+     * @return R
+     */
+    @Log(title = "地理围栏管理", businessType = BusinessType.UPDATE)
+    @PostMapping("/update")
+    @ApiOperation("编辑地理围栏")
+    public R update(@RequestBody TGeoFence geoFence) {
+        try {
+            return R.ok(geoFenceService.updateGeoFence(geoFence));
+        } catch (ServiceException s) {
+            log.error("编辑地理围栏失败:{}", s.getMessage());
+            return R.fail(s.getMessage());
+        } catch (Exception e) {
+            log.error("编辑地理围栏系统异常", e);
+            return R.fail("系统异常");
+        }
+    }
+
+    /**
+     * 删除地理围栏。
+     *
+     * @param geoFence 地理围栏
+     * @return R
+     */
+    @Log(title = "地理围栏管理", businessType = BusinessType.DELETE)
+    @PostMapping("/del")
+    @ApiOperation("删除地理围栏")
+    public R del(@RequestBody TGeoFence geoFence) {
+        try {
+            return R.ok(geoFenceService.deleteGeoFence(geoFence));
+        } catch (ServiceException s) {
+            log.error("删除地理围栏失败:{}", s.getMessage());
+            return R.fail(s.getMessage());
+        } catch (Exception e) {
+            log.error("删除地理围栏系统异常", e);
+            return R.fail("系统异常");
+        }
+    }
+}

+ 124 - 0
nightFragrance-massage/src/main/java/com/ylx/massage/domain/TGeoFence.java

@@ -0,0 +1,124 @@
+package com.ylx.massage.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+
+/**
+ * 地理围栏
+ */
+@Getter
+@Setter
+@Accessors(chain = true)
+@TableName("t_geo_fence")
+@ApiModel(value = "TGeoFence", description = "地理围栏")
+public class TGeoFence implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 围栏ID。
+     */
+    @TableId(value = "id", type = IdType.AUTO)
+    @ApiModelProperty("围栏ID")
+    private Long id;
+
+    /**
+     * 城市编码。
+     */
+    @TableField("city_code")
+    @ApiModelProperty("城市编码")
+    private String cityCode;
+
+    /**
+     * 城市名称。
+     */
+    @TableField("city_name")
+    @ApiModelProperty("城市名称")
+    private String cityName;
+
+    /**
+     * 围栏名称。
+     */
+    @TableField("fence_name")
+    @ApiModelProperty("围栏名称")
+    private String fenceName;
+
+    /**
+     * 围栏介绍。
+     */
+    @TableField("fence_intro")
+    @ApiModelProperty("围栏介绍")
+    private String fenceIntro;
+
+    /**
+     * 地图搜索或定位地址。
+     */
+    @TableField("address")
+    @ApiModelProperty("地图搜索或定位地址")
+    private String address;
+
+    /**
+     * 围栏中心点经度。
+     */
+    @TableField("longitude")
+    @ApiModelProperty("围栏中心点经度")
+    private BigDecimal longitude;
+
+    /**
+     * 围栏中心点纬度。
+     */
+    @TableField("latitude")
+    @ApiModelProperty("围栏中心点纬度")
+    private BigDecimal latitude;
+
+    /**
+     * 围栏半径,单位:KM。
+     */
+    @TableField("radius_km")
+    @ApiModelProperty("围栏半径,单位:KM")
+    private BigDecimal radiusKm;
+
+    /**
+     * 风险等级:1-低风险,2-中风险,3-高风险。
+     */
+    @TableField("risk_level")
+    @ApiModelProperty("风险等级:1-低风险,2-中风险,3-高风险")
+    private Integer riskLevel;
+
+    /**
+     * 创建时间。
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @TableField("create_time")
+    @ApiModelProperty("创建时间")
+    private Date createTime;
+
+    /**
+     * 更新时间。
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @TableField("update_time")
+    @ApiModelProperty("更新时间")
+    private Date updateTime;
+
+    /**
+     * 是否删除:0-否,1-是。
+     */
+    @TableLogic
+    @TableField("is_delete")
+    @ApiModelProperty("是否删除:0-否,1-是")
+    private Integer isDelete;
+}

+ 10 - 0
nightFragrance-massage/src/main/java/com/ylx/massage/mapper/TGeoFenceMapper.java

@@ -0,0 +1,10 @@
+package com.ylx.massage.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.ylx.massage.domain.TGeoFence;
+
+/**
+ * 地理围栏 Mapper 接口
+ */
+public interface TGeoFenceMapper extends BaseMapper<TGeoFence> {
+}

+ 8 - 1
nightFragrance-massage/src/main/java/com/ylx/massage/service/TFareSettingService.java

@@ -14,12 +14,19 @@ public interface TFareSettingService extends IService<TFareSetting> {
 
     /**
      * 新增车费设置数据
+     *
      * @param tFareSetting
      * @return boolean
      */
     boolean add(TFareSetting tFareSetting);
 
-    TFareSetting updateFareSetting(TFareSetting tFareSetting);
+    /**
+     * 更新车费设置数据
+     *
+     * @param tFareSetting
+     * @return boolean
+     */
+    boolean updateFareSetting(TFareSetting tFareSetting);
 
     /**
      * 根据城市编码获取车费设置 C端

+ 54 - 0
nightFragrance-massage/src/main/java/com/ylx/massage/service/TGeoFenceService.java

@@ -0,0 +1,54 @@
+package com.ylx.massage.service;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.ylx.massage.domain.TGeoFence;
+
+import java.util.List;
+
+/**
+ * 地理围栏服务接口
+ */
+public interface TGeoFenceService extends IService<TGeoFence> {
+
+    /**
+     * 分页查询地理围栏。
+     *
+     * @param page 分页参数
+     * @param geoFence 查询参数
+     * @return 分页结果
+     */
+    Page<TGeoFence> pageGeoFence(Page<TGeoFence> page, TGeoFence geoFence);
+
+    /**
+     * 查询地图展示用地理围栏。
+     *
+     * @param geoFence 查询参数
+     * @return 地理围栏列表
+     */
+    List<TGeoFence> listMapGeoFence(TGeoFence geoFence);
+
+    /**
+     * 新增地理围栏。
+     *
+     * @param geoFence 地理围栏
+     * @return 是否新增成功
+     */
+    Boolean addGeoFence(TGeoFence geoFence);
+
+    /**
+     * 编辑地理围栏。
+     *
+     * @param geoFence 地理围栏
+     * @return 是否编辑成功
+     */
+    Boolean updateGeoFence(TGeoFence geoFence);
+
+    /**
+     * 删除地理围栏。
+     *
+     * @param geoFence 地理围栏
+     * @return 是否删除成功
+     */
+    Boolean deleteGeoFence(TGeoFence geoFence);
+}

+ 8 - 6
nightFragrance-massage/src/main/java/com/ylx/massage/service/impl/TFareSettingServiceImpl.java

@@ -59,21 +59,23 @@ public class TFareSettingServiceImpl extends ServiceImpl<TFareSettingMapper, TFa
     }
 
     @Override
-    public TFareSetting updateFareSetting(TFareSetting tFareSetting) {
+    public boolean updateFareSetting(TFareSetting tFareSetting) {
         if (tFareSetting.getEnable().equals(MassageConstants.INTEGER_ONE)) {
             LambdaQueryWrapper<TFareSetting> tFareSettingLambdaQueryWrapper = new LambdaQueryWrapper<>();
-            tFareSettingLambdaQueryWrapper.eq(TFareSetting::getCityCode, tFareSetting.getCityCode())
-                    .eq(TFareSetting::getEnable, MassageConstants.INTEGER_ONE);
+            tFareSettingLambdaQueryWrapper.eq(TFareSetting::getCityCode, tFareSetting.getCityCode()).eq(TFareSetting::getEnable, MassageConstants.INTEGER_ONE);
             //该部门已启用的
             List<TFareSetting> fareSettings = this.list(tFareSettingLambdaQueryWrapper);
             if (!Collections.isEmpty(fareSettings)) {
                 if(!fareSettings.get(MassageConstants.INTEGER_ZERO).getId().equals(tFareSetting.getId())){
-                    throw new ServiceException("该部门已有启用的设置");
+                    throw new ServiceException("该城市已有启用的设置");
                 }
             }
         }
-        this.updateById(tFareSetting);
-        return tFareSetting;
+        boolean b = this.updateById(tFareSetting);
+        if(!b){
+            throw new ServiceException("更新失败");
+        }
+        return b;
     }
 
     /**

+ 190 - 0
nightFragrance-massage/src/main/java/com/ylx/massage/service/impl/TGeoFenceServiceImpl.java

@@ -0,0 +1,190 @@
+package com.ylx.massage.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ylx.common.exception.ServiceException;
+import com.ylx.common.utils.DateUtils;
+import com.ylx.massage.domain.TGeoFence;
+import com.ylx.massage.mapper.TGeoFenceMapper;
+import com.ylx.massage.service.TGeoFenceService;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+/**
+ * 地理围栏服务实现类
+ */
+@Service
+public class TGeoFenceServiceImpl extends ServiceImpl<TGeoFenceMapper, TGeoFence> implements TGeoFenceService {
+
+    private static final BigDecimal MIN_LONGITUDE = new BigDecimal("-180");
+    private static final BigDecimal MAX_LONGITUDE = new BigDecimal("180");
+    private static final BigDecimal MIN_LATITUDE = new BigDecimal("-90");
+    private static final BigDecimal MAX_LATITUDE = new BigDecimal("90");
+    private static final int RISK_LOW = 1;
+    private static final int RISK_MEDIUM = 2;
+    private static final int RISK_HIGH = 3;
+    private static final int NOT_DELETE = 0;
+
+    @Override
+    public Page<TGeoFence> pageGeoFence(Page<TGeoFence> page, TGeoFence geoFence) {
+        LambdaQueryWrapper<TGeoFence> queryWrapper = buildQueryWrapper(geoFence);
+        queryWrapper.orderByAsc(TGeoFence::getId);
+        return this.page(page, queryWrapper);
+    }
+
+    @Override
+    public List<TGeoFence> listMapGeoFence(TGeoFence geoFence) {
+        LambdaQueryWrapper<TGeoFence> queryWrapper = buildQueryWrapper(geoFence);
+        queryWrapper.select(TGeoFence::getId, TGeoFence::getCityCode, TGeoFence::getCityName,
+                TGeoFence::getFenceName, TGeoFence::getAddress, TGeoFence::getLongitude,
+                TGeoFence::getLatitude, TGeoFence::getRadiusKm, TGeoFence::getRiskLevel);
+        queryWrapper.isNotNull(TGeoFence::getLongitude)
+                .isNotNull(TGeoFence::getLatitude)
+                .orderByAsc(TGeoFence::getId);
+        return this.list(queryWrapper);
+    }
+
+    @Override
+    public Boolean addGeoFence(TGeoFence geoFence) {
+        validateGeoFenceParam(geoFence);
+        geoFence.setId(null);
+        geoFence.setIsDelete(NOT_DELETE);
+        geoFence.setCreateTime(DateUtils.getNowDate());
+        geoFence.setUpdateTime(DateUtils.getNowDate());
+
+        boolean saved = this.save(geoFence);
+        if (!saved) {
+            throw new ServiceException("新增围栏失败");
+        }
+        return true;
+    }
+
+    @Override
+    public Boolean updateGeoFence(TGeoFence geoFence) {
+        validateUpdateParam(geoFence);
+
+        TGeoFence exists = this.getById(geoFence.getId());
+        if (exists == null) {
+            throw new ServiceException("围栏不存在");
+        }
+
+        TGeoFence update = new TGeoFence();
+        update.setId(geoFence.getId());
+        update.setCityCode(geoFence.getCityCode());
+        update.setCityName(geoFence.getCityName());
+        update.setFenceName(geoFence.getFenceName());
+        update.setFenceIntro(geoFence.getFenceIntro());
+        update.setAddress(geoFence.getAddress());
+        update.setLongitude(geoFence.getLongitude());
+        update.setLatitude(geoFence.getLatitude());
+        update.setRadiusKm(geoFence.getRadiusKm());
+        update.setRiskLevel(geoFence.getRiskLevel());
+        update.setUpdateTime(DateUtils.getNowDate());
+
+        boolean updated = this.updateById(update);
+        if (!updated) {
+            throw new ServiceException("编辑围栏失败");
+        }
+        return true;
+    }
+
+    @Override
+    public Boolean deleteGeoFence(TGeoFence geoFence) {
+        Long id = getValidId(geoFence);
+        TGeoFence exists = this.getById(id);
+        if (exists == null) {
+            throw new ServiceException("围栏不存在");
+        }
+
+        boolean removed = this.removeById(id);
+        if (!removed) {
+            throw new ServiceException("删除围栏失败");
+        }
+        return true;
+    }
+
+    private void validateUpdateParam(TGeoFence geoFence) {
+        if (geoFence == null) {
+            throw new ServiceException("参数不能为空");
+        }
+        if (geoFence.getId() == null || geoFence.getId() <= 0) {
+            throw new ServiceException("围栏ID不能为空");
+        }
+        validateGeoFenceParam(geoFence);
+    }
+
+    private Long getValidId(TGeoFence geoFence) {
+        if (geoFence == null) {
+            throw new ServiceException("参数不能为空");
+        }
+        if (geoFence.getId() == null || geoFence.getId() <= 0) {
+            throw new ServiceException("围栏ID不能为空");
+        }
+        return geoFence.getId();
+    }
+
+    private void validateGeoFenceParam(TGeoFence geoFence) {
+        if (geoFence == null) {
+            throw new ServiceException("参数不能为空");
+        }
+        if (StringUtils.isBlank(geoFence.getCityCode())) {
+            throw new ServiceException("城市编码不能为空");
+        }
+        if (StringUtils.isBlank(geoFence.getCityName())) {
+            throw new ServiceException("城市名称不能为空");
+        }
+        if (StringUtils.isBlank(geoFence.getFenceName())) {
+            throw new ServiceException("围栏名称不能为空");
+        }
+        if (StringUtils.isBlank(geoFence.getFenceIntro())) {
+            throw new ServiceException("围栏介绍不能为空");
+        }
+        if (geoFence.getRadiusKm() == null || geoFence.getRadiusKm().compareTo(BigDecimal.ZERO) <= 0) {
+            throw new ServiceException("围栏半径必须大于0");
+        }
+        if (!isValidRiskLevel(geoFence.getRiskLevel())) {
+            throw new ServiceException("风险等级值不正确");
+        }
+        validateCoordinate(geoFence.getLongitude(), geoFence.getLatitude());
+    }
+
+    private boolean isValidRiskLevel(Integer riskLevel) {
+        return riskLevel != null && (riskLevel == RISK_LOW || riskLevel == RISK_MEDIUM || riskLevel == RISK_HIGH);
+    }
+
+    private LambdaQueryWrapper<TGeoFence> buildQueryWrapper(TGeoFence geoFence) {
+        LambdaQueryWrapper<TGeoFence> queryWrapper = new LambdaQueryWrapper<>();
+        if (geoFence != null) {
+            queryWrapper.eq(StringUtils.isNotBlank(geoFence.getCityCode()), TGeoFence::getCityCode, geoFence.getCityCode())
+                    .eq(StringUtils.isNotBlank(geoFence.getCityName()), TGeoFence::getCityName, geoFence.getCityName())
+                    .like(StringUtils.isNotBlank(geoFence.getFenceName()), TGeoFence::getFenceName, geoFence.getFenceName())
+                    .like(StringUtils.isNotBlank(geoFence.getAddress()), TGeoFence::getAddress, geoFence.getAddress());
+        }
+        return queryWrapper;
+    }
+
+    /**
+     * 验证地理坐标栏中心点坐标是否有效。
+     *
+     * @param longitude 经度
+     * @param latitude 纬度
+     */
+    private void validateCoordinate(BigDecimal longitude, BigDecimal latitude) {
+        if (longitude == null) {
+            throw new ServiceException("围栏中心点经度不能为空");
+        }
+        if (latitude == null) {
+            throw new ServiceException("围栏中心点纬度不能为空");
+        }
+        if (longitude.compareTo(MIN_LONGITUDE) < 0 || longitude.compareTo(MAX_LONGITUDE) > 0) {
+            throw new ServiceException("经度必须在-180到180之间");
+        }
+        if (latitude.compareTo(MIN_LATITUDE) < 0 || latitude.compareTo(MAX_LATITUDE) > 0) {
+            throw new ServiceException("纬度必须在-90到90之间");
+        }
+    }
+}