shiwn преди 3 месеца
родител
ревизия
8b46c562f3

+ 75 - 0
src/main/java/com/sw/patroleditor/controller/AreaController.java

@@ -0,0 +1,75 @@
+package com.sw.patroleditor.controller;
+
+import com.sw.patroleditor.common.ResultData;
+import com.sw.patroleditor.domain.vo.AreaVO;
+import com.sw.patroleditor.exception.ErrorCode;
+import com.sw.patroleditor.service.AreaService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+
+/**
+ * Created by shiwn on 2024/8/8 16:09
+ */
+@Api(tags = {"区域接口"})
+@RestController
+@RequestMapping("area")
+public class AreaController {
+    @Resource
+    private AreaService areaService;
+
+    @ApiOperation(value = "保存区域")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "vo", value = "数据", dataType = "AreaVO", required = true)
+    })
+    @PostMapping("/addArea")
+    public ResultData addArea(@RequestBody AreaVO vo) {
+        int flag = areaService.addArea(vo);
+        if (flag > 0) {
+            return ResultData.success();
+        } else {
+            return ResultData.fail(ErrorCode.FAIL);
+        }
+    }
+
+    @ApiOperation(value = "删除区域")
+    @PostMapping("/deleteArea")
+    public ResultData deleteArea(@RequestBody Integer id) {
+        int flag = areaService.deleteArea(id);
+        if (flag > 0) {
+            return ResultData.success();
+        } else {
+            return ResultData.fail(ErrorCode.FAIL);
+        }
+    }
+
+    @ApiOperation(value = "修改区域")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "vo", value = "数据", dataType = "AreaVO", required = true)
+    })
+    @PostMapping("/updateArea")
+    public ResultData updateArea(@RequestBody AreaVO vo) {
+        int flag = areaService.updateArea(vo);
+        if (flag > 0) {
+            return ResultData.success();
+        } else {
+            return ResultData.fail(ErrorCode.FAIL);
+        }
+    }
+
+    @ApiOperation(value = "查询所有区域")
+    @GetMapping("/getAllAreas")
+    public ResultData getAllAreas() {
+        return ResultData.success(areaService.getAllAreas());
+    }
+
+    @ApiOperation(value = "根据级别查询区域")
+    @GetMapping("/getAreasByType")
+    public ResultData getAreasByType(Integer type) {
+        return ResultData.success(areaService.getAreasByType(type));
+    }
+}

+ 0 - 1
src/main/java/com/sw/patroleditor/controller/LocationController.java

@@ -15,7 +15,6 @@ import javax.annotation.Resource;
  */
 @Api(tags = {"标点接口"})
 @RestController
-
 @RequestMapping("location")
 public class LocationController {
     @Resource

+ 19 - 4
src/main/java/com/sw/patroleditor/domain/dto/AreaDTO.java

@@ -1,20 +1,35 @@
 package com.sw.patroleditor.domain.dto;
 
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
+import org.springframework.data.annotation.Id;
 
 @Data
 @TableName("area")
 public class AreaDTO {
-
+    /**
+     * 主键
+     */
+    @TableId(type = IdType.AUTO)
     private Integer id;
 
+    /**
+     * 区域名称
+     */
     private String areaName;
-
+    /**
+     * 区域级别
+     */
     private Integer type;
-
+    /**
+     * 区域级别名称
+     */
     private String typeName;
-
+    /**
+     * 父级主键id
+     */
     private Integer preLevel;
 
 }

+ 26 - 0
src/main/java/com/sw/patroleditor/domain/vo/AreaVO.java

@@ -0,0 +1,26 @@
+package com.sw.patroleditor.domain.vo;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * 区域
+ * Created by shiwn on 2024/8/8 16:00
+ */
+@Data
+public class AreaVO {
+    @ApiModelProperty(value = "主键")
+    private Integer id;
+
+    @ApiModelProperty(value = "区域名称")
+    private String areaName;
+
+    @ApiModelProperty(value = "区域级别")
+    private Integer type;
+
+    @ApiModelProperty(value = "区域级别名称")
+    private String typeName;
+
+    @ApiModelProperty(value = "父级主键id")
+    private Integer preLevel;
+}

+ 23 - 0
src/main/java/com/sw/patroleditor/mapper/AreaMapper.java

@@ -2,9 +2,32 @@ package com.sw.patroleditor.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.sw.patroleditor.domain.dto.AreaDTO;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
 import org.springframework.stereotype.Component;
 
+import java.util.List;
+
 @Component
 public interface AreaMapper extends BaseMapper<AreaDTO> {
 
+    @Insert("INSERT INTO area (area_name, type, type_name, pre_level) VALUES (#{areaName}, #{type}, #{typeName}, #{preLevel})")
+    int insertArea(AreaDTO areaDTO);
+
+    @Delete("DELETE FROM area WHERE id = #{id}")
+    int deleteArea(Integer id);
+
+    @Update("UPDATE area SET area_name = #{areaName}, type = #{type}, type_name = #{typeName}, pre_level = #{preLevel} WHERE id = #{id}")
+    int updateArea(AreaDTO areaDTO);
+
+    @Select("SELECT * FROM area")
+    List<AreaDTO> getAllAreas();
+
+    @Select("SELECT * FROM area WHERE type = #{type}")
+    List<AreaDTO> getAreasByType(Integer type);
+
+    @Select("SELECT * FROM area WHERE pre_level = #{preLevel}")
+    List<AreaDTO> getChildAreasByPreLevel(Integer preLevel);
 }

+ 20 - 0
src/main/java/com/sw/patroleditor/service/AreaService.java

@@ -0,0 +1,20 @@
+package com.sw.patroleditor.service;
+
+import com.sw.patroleditor.domain.vo.AreaVO;
+
+import java.util.List;
+
+/**
+ * Created by shiwn on 2024/8/8 15:47
+ */
+public interface AreaService {
+    int addArea(AreaVO vo);
+
+    int deleteArea(Integer id);
+
+    int updateArea(AreaVO vo);
+
+    List<AreaVO> getAllAreas();
+
+    List<AreaVO> getAreasByType(Integer type);
+}

+ 92 - 0
src/main/java/com/sw/patroleditor/service/impl/AreaServiceImpl.java

@@ -0,0 +1,92 @@
+package com.sw.patroleditor.service.impl;
+
+import com.sw.patroleditor.domain.dto.AreaDTO;
+import com.sw.patroleditor.domain.vo.AreaVO;
+import com.sw.patroleditor.mapper.AreaMapper;
+import com.sw.patroleditor.service.AreaService;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 区域服务
+ * Created by shiwn on 2024/8/8 15:47
+ */
+@Service
+public class AreaServiceImpl implements AreaService {
+    @Resource
+    private AreaMapper areaMapper;
+
+    // 增加区域
+    @Override
+    public int addArea(AreaVO vo) {
+        AreaDTO dto = new AreaDTO();
+        BeanUtils.copyProperties(vo, dto);
+        return areaMapper.insertArea(dto);
+    }
+
+    // 删除区域
+    @Override
+    public int deleteArea(Integer id) {
+        // 使用队列来存储待删除的区域 ID
+        List<Integer> areaIdsToDelete = new ArrayList<>();
+        areaIdsToDelete.add(id);
+
+        // 迭代删除
+        while (!areaIdsToDelete.isEmpty()) {
+            Integer currentId = areaIdsToDelete.remove(0);
+            // 获取当前区域的所有直接子集
+            List<AreaDTO> childAreas = areaMapper.getChildAreasByPreLevel(currentId);
+            for (AreaDTO childArea : childAreas) {
+                areaIdsToDelete.add(childArea.getId());
+            }
+            // 删除当前区域
+            areaMapper.deleteArea(currentId);
+        }
+        return 1;
+    }
+
+    // 修改区域
+    @Override
+    public int updateArea(AreaVO vo) {
+        AreaDTO dto = new AreaDTO();
+        BeanUtils.copyProperties(vo, dto);
+        return areaMapper.updateArea(dto);
+    }
+
+    // 查询所有区域
+    @Override
+    public List<AreaVO> getAllAreas() {
+        List<AreaDTO> listDto = areaMapper.getAllAreas();
+        return convertToVOList(listDto);
+    }
+
+    // 根据级别查询区域
+    @Override
+    public List<AreaVO> getAreasByType(Integer type) {
+        List<AreaDTO> listDto = areaMapper.getAreasByType(type);
+        return convertToVOList(listDto);
+    }
+
+    // 根据父级主键查询子区域
+    private List<AreaVO> getChildAreasByPreLevel(Integer preLevel) {
+        List<AreaDTO> listDto = areaMapper.getChildAreasByPreLevel(preLevel);
+        return convertToVOList(listDto);
+    }
+
+    //  ListDto转ListVo
+    private List<AreaVO> convertToVOList(List<AreaDTO> areaDTOList) {
+        List<AreaVO> areaVOList = new ArrayList<>();
+        for (AreaDTO areaDTO : areaDTOList) {
+            AreaVO areaVO = new AreaVO();
+            BeanUtils.copyProperties(areaDTO, areaVO);
+            areaVOList.add(areaVO);
+        }
+        return areaVOList;
+    }
+
+}