SysAreaController.java
2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.bs.web.controller.system;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.domain.vo.SysAreaVo;
import com.bs.common.core.redis.RedisService;
import com.bs.common.exception.wecom.WeComException;
import com.bs.web.domain.SysArea;
import com.bs.web.service.ISysAreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;
/**
* 行政区划信息
*
* @author danmo
*/
@RestController
@RequestMapping("/system/area")
@Tag(name = "行政区划")
public class SysAreaController extends BaseController {
@Autowired
private ISysAreaService sysAreaService;
@Autowired
private RedisService redisService;
@GetMapping("/list")
@Operation(summary = "列表")
public AjaxResult list(SysArea sysArea) {
List<Tree<Integer>> areaList = redisService.getCacheList("area");
if(CollectionUtil.isNotEmpty(areaList)){
return AjaxResult.success(areaList);
}
List<SysArea> list = sysAreaService.getList(sysArea);
if (CollectionUtil.isEmpty(list)) {
throw new WeComException("暂无数据");
}
List<TreeNode<Integer>> treeNodes = list.stream().map(area -> {
return new TreeNode<>(area.getId(), area.getParentId(), area.getExtName(), area.getExtId());
}).collect(Collectors.toList());
List<Tree<Integer>> treeList = TreeUtil.build(treeNodes, 0);
if(CollectionUtil.isNotEmpty(treeList)){
redisService.setCacheList("area",treeList);
}
return AjaxResult.success(treeList);
}
@GetMapping("/getChildListById")
@Operation(summary = "根据ID获取子列表")
public AjaxResult<SysAreaVo> getChildListById(@NotNull(message = "ID不允许为空") Integer id) {
List<SysAreaVo> list = sysAreaService.getChildListById(id);
return AjaxResult.success(list);
}
/**
* 根据Id获取数据
*
* @param id
* @return {@link AjaxResult< SysAreaVo>}
* @author WangYX
* @date 2022/10/17 14:40
*/
@GetMapping("/getAreaById/{id}")
@Operation(summary = "根据ID获取数据")
public AjaxResult<SysAreaVo> getAreaById(@PathVariable("id") Integer id) {
SysArea sysArea = sysAreaService.getById(id);
return AjaxResult.success(sysArea);
}
}