AreaController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package jnpf.base.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import io.swagger.v3.oas.annotations.tags.Tag;
  4. import io.swagger.v3.oas.annotations.Parameter;
  5. import io.swagger.v3.oas.annotations.Parameters;
  6. import io.swagger.v3.oas.annotations.Operation;
  7. import jnpf.base.ActionResult;
  8. import jnpf.base.Page;
  9. import jnpf.base.service.ProvinceService;
  10. import jnpf.base.vo.ListVO;
  11. import jnpf.base.entity.ProvinceEntity;
  12. import jnpf.constant.MsgCode;
  13. import jnpf.exception.DataException;
  14. import jnpf.base.model.province.*;
  15. import jnpf.util.JsonUtil;
  16. import jnpf.util.JsonUtilEx;
  17. import jnpf.util.StringUtil;
  18. import jnpf.util.treeutil.ListToTreeUtil;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.web.bind.annotation.*;
  21. import jakarta.validation.Valid;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.LinkedList;
  25. import java.util.List;
  26. import java.util.stream.Collectors;
  27. /**
  28. * 行政区划
  29. *
  30. * @author JNPF开发平台组
  31. * @version V3.1.0
  32. * @copyright 引迈信息技术有限公司
  33. * @date 2019年9月27日 上午9:18
  34. */
  35. @Tag(name = "行政区划", description = "Area")
  36. @RestController
  37. @RequestMapping("/api/system/Area")
  38. public class AreaController extends SuperController<ProvinceService, ProvinceEntity> {
  39. @Autowired
  40. private ProvinceService provinceService;
  41. /**
  42. * 列表(异步加载)
  43. *
  44. * @param nodeId 节点主键
  45. * @param page 关键字
  46. * @return
  47. */
  48. @Operation(summary = "列表(异步加载)")
  49. @Parameters({
  50. @Parameter(name = "nodeId", description = "节点主键", required = true)
  51. })
  52. @SaCheckPermission("sysData.area")
  53. @GetMapping("/{nodeId}")
  54. public ActionResult<ListVO<ProvinceListVO>> list(@PathVariable("nodeId") String nodeId, PaginationProvince page) {
  55. List<ProvinceEntity> data = provinceService.getList(nodeId, page);
  56. List<ProvinceEntity> dataAll = data;
  57. List<ProvinceEntity> result = JsonUtil.getJsonToList(ListToTreeUtil.treeWhere(data, dataAll), ProvinceEntity.class);
  58. List<ProvinceListVO> treeList = JsonUtil.getJsonToList(result, ProvinceListVO.class);
  59. int i = 0;
  60. for (ProvinceListVO entity : treeList) {
  61. boolean childNode = provinceService.getList(entity.getId()).size() <= 0;
  62. ProvinceListVO provinceListVO = JsonUtil.getJsonToBean(entity, ProvinceListVO.class);
  63. provinceListVO.setIsLeaf(childNode);
  64. provinceListVO.setHasChildren(!childNode);
  65. treeList.set(i, provinceListVO);
  66. i++;
  67. }
  68. ListVO<ProvinceListVO> vo = new ListVO<>();
  69. vo.setList(treeList);
  70. return ActionResult.success(vo);
  71. }
  72. /**
  73. * 获取行政区划下拉框数据
  74. *
  75. * @param id 主键
  76. * @param ids 主键集合
  77. * @return
  78. */
  79. @Operation(summary = "获取行政区划下拉框数据")
  80. @Parameters({
  81. @Parameter(name = "id", description = "主键", required = true),
  82. @Parameter(name = "ids", description = "主键集合", required = true)
  83. })
  84. @GetMapping("/{id}/Selector/{ids}")
  85. public ActionResult<ListVO<ProvinceSelectListVO>> selectList(@PathVariable("id") String id, @PathVariable("ids") String ids) {
  86. List<ProvinceEntity> data = provinceService.getList(id);
  87. data = data.stream().filter(t -> t.getEnabledMark() == 1).collect(Collectors.toList());
  88. if (!"0".equals(ids)) {
  89. //排除子集
  90. filterData(data, new ArrayList<>(Arrays.asList(new String[]{ids})));
  91. }
  92. List<ProvinceSelectListVO> treeList = JsonUtil.getJsonToList(data, ProvinceSelectListVO.class);
  93. int i = 0;
  94. for (ProvinceSelectListVO entity : treeList) {
  95. // boolean childNode = provinceService.getList(entity.getId()).size() <= 0;
  96. ProvinceSelectListVO provinceListVO = JsonUtil.getJsonToBean(entity, ProvinceSelectListVO.class);
  97. provinceListVO.setIsLeaf(false);
  98. treeList.set(i, provinceListVO);
  99. i++;
  100. }
  101. ListVO<ProvinceSelectListVO> vo = new ListVO<>();
  102. vo.setList(treeList);
  103. return ActionResult.success(vo);
  104. }
  105. /**
  106. * 递归排除子集
  107. *
  108. * @param data 普通列表
  109. * @param id ignore
  110. */
  111. private void filterData(List<ProvinceEntity> data, List<String> id) {
  112. List<ProvinceEntity> collect = null;
  113. //获取子集信息
  114. for (String ids : id) {
  115. collect = data.stream().filter(t -> ids.equals(t.getParentId())).collect(Collectors.toList());
  116. data.removeAll(collect);
  117. }
  118. //递归移除子集的子集
  119. if(collect != null){
  120. if (collect.size() > 0) {
  121. filterData(data, collect.stream().map(t -> t.getId()).collect(Collectors.toList()));
  122. }
  123. }
  124. }
  125. /**
  126. * 信息
  127. *
  128. * @param id 主键值
  129. * @return ignore
  130. */
  131. @Operation(summary = "获取行政区划信息")
  132. @Parameters({
  133. @Parameter(name = "id", description = "主键", required = true)
  134. })
  135. @SaCheckPermission("sysData.area")
  136. @GetMapping("/{id}/Info")
  137. public ActionResult<ProvinceInfoVO> info(@PathVariable("id") String id) throws DataException {
  138. ProvinceEntity entity = provinceService.getInfo(id);
  139. ProvinceInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, ProvinceInfoVO.class);
  140. if (!"-1".equals(entity.getParentId())) {
  141. ProvinceEntity parent = provinceService.getInfo(entity.getParentId());
  142. vo.setParentName(parent.getFullName());
  143. }
  144. return ActionResult.success(vo);
  145. }
  146. /**
  147. * 新建
  148. *
  149. * @param provinceCrForm 实体对象
  150. * @return ignore
  151. */
  152. @Operation(summary = "添加行政区划")
  153. @Parameters({
  154. @Parameter(name = "provinceCrForm", description = "实体对象", required = true)
  155. })
  156. @SaCheckPermission("sysData.area")
  157. @PostMapping
  158. public ActionResult create(@RequestBody @Valid ProvinceCrForm provinceCrForm) {
  159. ProvinceEntity entity = JsonUtil.getJsonToBean(provinceCrForm, ProvinceEntity.class);
  160. if (provinceService.isExistByEnCode(provinceCrForm.getEnCode(), entity.getId())) {
  161. return ActionResult.fail(MsgCode.SYS001.get());
  162. }
  163. if (StringUtil.isEmpty(provinceCrForm.getParentId())) {
  164. entity.setParentId("-1");
  165. }
  166. if (entity.getParentId().equals("-1")) {
  167. entity.setType("1");
  168. } else {
  169. ProvinceEntity info = provinceService.getInfo(entity.getParentId());
  170. int type = info!=null? Integer.valueOf(info.getType()) + 1 : 1;
  171. entity.setType(String.valueOf(type));
  172. }
  173. provinceService.create(entity);
  174. return ActionResult.success(MsgCode.SU001.get());
  175. }
  176. /**
  177. * 更新
  178. *
  179. * @param id 主键值
  180. * @param provinceUpForm ignore
  181. * @return ignore
  182. */
  183. @Operation(summary = "修改行政区划")
  184. @Parameters({
  185. @Parameter(name = "id", description = "主键值", required = true),
  186. @Parameter(name = "provinceUpForm", description = "实体对象", required = true)
  187. })
  188. @SaCheckPermission("sysData.area")
  189. @PutMapping("/{id}")
  190. public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ProvinceUpForm provinceUpForm) {
  191. ProvinceEntity entity = JsonUtil.getJsonToBean(provinceUpForm, ProvinceEntity.class);
  192. if (provinceService.isExistByEnCode(provinceUpForm.getEnCode(), id)) {
  193. return ActionResult.fail(MsgCode.SYS001.get());
  194. }
  195. boolean flag = provinceService.update(id, entity);
  196. if (!flag) {
  197. return ActionResult.fail(MsgCode.FA002.get());
  198. }
  199. return ActionResult.success(MsgCode.SU004.get());
  200. }
  201. /**
  202. * 删除
  203. *
  204. * @param id 主键值
  205. * @return ignore
  206. */
  207. @Operation(summary = "删除")
  208. @Parameters({
  209. @Parameter(name = "id", description = "主键值", required = true)
  210. })
  211. @SaCheckPermission("sysData.area")
  212. @DeleteMapping("/{id}")
  213. public ActionResult delete(@PathVariable("id") String id) {
  214. if (provinceService.getList(id).size() == 0) {
  215. ProvinceEntity entity = provinceService.getInfo(id);
  216. if (entity != null) {
  217. provinceService.delete(entity);
  218. return ActionResult.success(MsgCode.SU003.get());
  219. }
  220. return ActionResult.fail(MsgCode.FA003.get());
  221. } else {
  222. return ActionResult.fail(MsgCode.SYS002.get());
  223. }
  224. }
  225. /**
  226. * 更新行政区划状态
  227. *
  228. * @param id 主键值
  229. * @return ignore
  230. */
  231. @Operation(summary = "更新行政区划状态")
  232. @Parameters({
  233. @Parameter(name = "id", description = "主键值", required = true)
  234. })
  235. @SaCheckPermission("sysData.area")
  236. @PutMapping("/{id}/Actions/State")
  237. public ActionResult upState(@PathVariable("id") String id) {
  238. ProvinceEntity entity = provinceService.getInfo(id);
  239. if (entity.getEnabledMark() == null || "1".equals(String.valueOf(entity.getEnabledMark()))) {
  240. entity.setEnabledMark(0);
  241. } else {
  242. entity.setEnabledMark(1);
  243. }
  244. boolean flag = provinceService.update(id, entity);
  245. if (!flag) {
  246. return ActionResult.fail(MsgCode.FA002.get());
  247. }
  248. return ActionResult.success(MsgCode.SU004.get());
  249. }
  250. /**
  251. * 行政区划id转名称
  252. *
  253. * @param model 二维数组
  254. * @return ignore
  255. */
  256. @Operation(summary = "行政区划id转名称")
  257. @Parameters({
  258. @Parameter(name = "model", description = "二维数组", required = true)
  259. })
  260. @PostMapping("/GetAreaByIds")
  261. public ActionResult getAreaByIds(@RequestBody AreaModel model) {
  262. // 返回给前端的list
  263. List<List<String>> list = new LinkedList<>();
  264. for (List<String> idList : model.getIdsList()) {
  265. List<ProvinceEntity> proList = provinceService.getProList(idList);
  266. List<String> collect = proList.stream().map(ProvinceEntity::getFullName).collect(Collectors.toList());
  267. list.add(collect);
  268. }
  269. return ActionResult.success(list);
  270. }
  271. }