|
@@ -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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|