DictionaryDataController.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package jnpf.base.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import io.swagger.v3.oas.annotations.Parameter;
  4. import io.swagger.v3.oas.annotations.Parameters;
  5. import io.swagger.v3.oas.annotations.tags.Tag;
  6. import io.swagger.v3.oas.annotations.Operation;
  7. import jnpf.base.ActionResult;
  8. import jnpf.base.model.dictionarytype.DictionaryExportModel;
  9. import jnpf.base.service.DictionaryDataService;
  10. import jnpf.base.service.DictionaryTypeService;
  11. import jnpf.base.vo.DownloadVO;
  12. import jnpf.base.vo.ListVO;
  13. import jnpf.base.entity.DictionaryDataEntity;
  14. import jnpf.base.entity.DictionaryTypeEntity;
  15. import jnpf.config.ConfigValueUtil;
  16. import jnpf.constant.MsgCode;
  17. import jnpf.exception.DataException;
  18. import jnpf.base.model.dictionarydata.*;
  19. import jnpf.base.model.dictionarytype.DictionaryTypeSelectModel;
  20. import jnpf.base.model.dictionarytype.DictionaryTypeSelectVO;
  21. import jnpf.util.FileUtil;
  22. import jnpf.util.JsonUtil;
  23. import jnpf.util.JsonUtilEx;
  24. import jnpf.util.StringUtil;
  25. import jnpf.emnus.ModuleTypeEnum;
  26. import jnpf.util.treeutil.ListToTreeUtil;
  27. import jnpf.util.treeutil.SumTree;
  28. import jnpf.util.treeutil.newtreeutil.TreeDotUtils;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.http.MediaType;
  31. import org.springframework.web.bind.annotation.*;
  32. import org.springframework.web.multipart.MultipartFile;
  33. import jakarta.validation.Valid;
  34. import java.util.*;
  35. import java.util.stream.Collectors;
  36. /**
  37. * 字典数据
  38. *
  39. * @author JNPF开发平台组
  40. * @version V3.1.0
  41. * @copyright 引迈信息技术有限公司
  42. * @date 2019年9月27日 上午9:18
  43. */
  44. @Tag(name = "数据字典", description = "DictionaryData")
  45. @RestController
  46. @RequestMapping("/api/system/DictionaryData")
  47. public class DictionaryDataController extends SuperController<DictionaryDataService, DictionaryDataEntity> {
  48. @Autowired
  49. private DictionaryDataService dictionaryDataService;
  50. @Autowired
  51. private DictionaryTypeService dictionaryTypeService;
  52. @Autowired
  53. private ConfigValueUtil configValueUtil;
  54. /**
  55. * 获取数据字典列表
  56. *
  57. * @param dictionaryTypeId 数据字典id
  58. * @param pageDictionaryData 分页参数
  59. * @return ignore
  60. */
  61. @Operation(summary = "获取数据字典列表")
  62. @Parameters({
  63. @Parameter(name = "dictionaryTypeId", description = "数据分类id", required = true)
  64. })
  65. @GetMapping("/{dictionaryTypeId}")
  66. public ActionResult bindDictionary(@PathVariable("dictionaryTypeId") String dictionaryTypeId, PageDictionaryData pageDictionaryData) {
  67. List<DictionaryDataEntity> data = dictionaryDataService.getList(dictionaryTypeId);
  68. List<DictionaryDataEntity> dataAll = data;
  69. if (StringUtil.isNotEmpty(pageDictionaryData.getKeyword())) {
  70. data = data.stream().filter(t -> t.getFullName().contains(pageDictionaryData.getKeyword()) || t.getEnCode().contains(pageDictionaryData.getKeyword())).collect(Collectors.toList());
  71. }
  72. if (pageDictionaryData.getIsTree() != null && "1".equals(pageDictionaryData.getIsTree())) {
  73. List<DictionaryDataEntity> treeData = JsonUtil.getJsonToList(ListToTreeUtil.treeWhere(data, dataAll), DictionaryDataEntity.class);
  74. List<DictionaryDataModel> voListVO = JsonUtil.getJsonToList(treeData, DictionaryDataModel.class);
  75. List<SumTree<DictionaryDataModel>> sumTrees = TreeDotUtils.convertListToTreeDot(voListVO);
  76. List<DictionaryDataListVO> list = JsonUtil.getJsonToList(sumTrees, DictionaryDataListVO.class);
  77. ListVO<DictionaryDataListVO> treeVo = new ListVO<>();
  78. treeVo.setList(list);
  79. return ActionResult.success(treeVo);
  80. }
  81. List<DictionaryDataModel> voListVO = JsonUtil.getJsonToList(data, DictionaryDataModel.class);
  82. ListVO<DictionaryDataModel> treeVo = new ListVO<>();
  83. treeVo.setList(voListVO);
  84. return ActionResult.success(treeVo);
  85. }
  86. /**
  87. * 获取数据字典列表
  88. *
  89. * @return ignore
  90. */
  91. @Operation(summary = "获取数据字典列表(分类+内容)")
  92. @GetMapping("/All")
  93. public ActionResult<ListVO<Map<String, Object>>> allBindDictionary() {
  94. List<DictionaryTypeEntity> dictionaryTypeList = dictionaryTypeService.getList();
  95. List<Map<String, Object>> list = new ArrayList<>();
  96. for (DictionaryTypeEntity dictionaryTypeEntity : dictionaryTypeList) {
  97. List<DictionaryDataEntity> childNodeList = dictionaryDataService.getList(dictionaryTypeEntity.getId(), true);
  98. if (dictionaryTypeEntity.getIsTree().compareTo(1) == 0) {
  99. List<Map<String, Object>> selectList = new ArrayList<>();
  100. for (DictionaryDataEntity item : childNodeList) {
  101. Map<String, Object> ht = new HashMap<>(16);
  102. ht.put("fullName", item.getFullName());
  103. ht.put("enCode", item.getEnCode());
  104. ht.put("id", item.getId());
  105. ht.put("parentId", item.getParentId());
  106. selectList.add(ht);
  107. }
  108. List<DictionaryDataAllModel> jsonToList = JsonUtil.getJsonToList(selectList, DictionaryDataAllModel.class);
  109. //==============转换树
  110. List<SumTree<DictionaryDataAllModel>> list1 = TreeDotUtils.convertListToTreeDot(jsonToList);
  111. List<DictionaryDataAllVO> list2 = JsonUtil.getJsonToList(list1, DictionaryDataAllVO.class);
  112. //==============
  113. Map<String, Object> htItem = new HashMap<>(16);
  114. htItem.put("id", dictionaryTypeEntity.getId());
  115. htItem.put("enCode", dictionaryTypeEntity.getEnCode());
  116. htItem.put("dictionaryList", list2);
  117. htItem.put("isTree", 1);
  118. list.add(htItem);
  119. } else {
  120. List<Map<String, Object>> selectList = new ArrayList<>();
  121. for (DictionaryDataEntity item : childNodeList) {
  122. Map<String, Object> ht = new HashMap<>(16);
  123. ht.put("enCode", item.getEnCode());
  124. ht.put("id", item.getId());
  125. ht.put("fullName", item.getFullName());
  126. selectList.add(ht);
  127. }
  128. Map<String, Object> htItem = new HashMap<>(16);
  129. htItem.put("id", dictionaryTypeEntity.getId());
  130. htItem.put("enCode", dictionaryTypeEntity.getEnCode());
  131. htItem.put("dictionaryList", selectList);
  132. htItem.put("isTree", 0);
  133. list.add(htItem);
  134. }
  135. }
  136. ListVO<Map<String, Object>> vo = new ListVO<>();
  137. vo.setList(list);
  138. return ActionResult.success(vo);
  139. }
  140. /**
  141. * 获取数据字典下拉框数据
  142. *
  143. * @param dictionaryTypeId 类别主键
  144. * @param isTree 是否为树
  145. * @param id 主键
  146. * @return ignore
  147. */
  148. @Operation(summary = "获取数据字典分类下拉框数据")
  149. @Parameters({
  150. @Parameter(name = "dictionaryTypeId", description = "数据分类id", required = true),
  151. @Parameter(name = "isTree", description = "是否树形"),
  152. @Parameter(name = "id", description = "主键", required = true)
  153. })
  154. @GetMapping("{dictionaryTypeId}/Selector/{id}")
  155. public ActionResult<ListVO<DictionaryDataSelectVO>> treeView(@PathVariable("dictionaryTypeId") String dictionaryTypeId, String isTree, @PathVariable("id") String id) {
  156. DictionaryTypeEntity typeEntity = dictionaryTypeService.getInfo(dictionaryTypeId);
  157. List<DictionaryDataModel> treeList = new ArrayList<>();
  158. DictionaryDataModel treeViewModel = new DictionaryDataModel();
  159. treeViewModel.setId("0");
  160. treeViewModel.setFullName(typeEntity.getFullName());
  161. treeViewModel.setParentId("-1");
  162. treeViewModel.setIcon("fa fa-tags");
  163. treeList.add(treeViewModel);
  164. if ("1".equals(isTree)) {
  165. List<DictionaryDataEntity> data = dictionaryDataService.getList(dictionaryTypeId).stream().filter(t -> "1".equals(String.valueOf(t.getEnabledMark()))).collect(Collectors.toList());
  166. //过滤子集
  167. if (!"0".equals(id)) {
  168. data.remove(dictionaryDataService.getInfo(id));
  169. }
  170. for (DictionaryDataEntity entity : data) {
  171. DictionaryDataModel treeModel = new DictionaryDataModel();
  172. treeModel.setId(entity.getId());
  173. treeModel.setFullName(entity.getFullName());
  174. treeModel.setParentId("-1".equals(entity.getParentId()) ? entity.getDictionaryTypeId() : entity.getParentId());
  175. treeList.add(treeModel);
  176. }
  177. }
  178. List<SumTree<DictionaryDataModel>> sumTrees = TreeDotUtils.convertListToTreeDotFilter(treeList);
  179. List<DictionaryDataSelectVO> list = JsonUtil.getJsonToList(sumTrees, DictionaryDataSelectVO.class);
  180. ListVO<DictionaryDataSelectVO> treeVo = new ListVO<>();
  181. treeVo.setList(list);
  182. return ActionResult.success(treeVo);
  183. }
  184. /**
  185. * 获取字典分类
  186. *
  187. * @param dictionaryTypeId 分类id、分类编码
  188. * @return ignore
  189. */
  190. @Operation(summary = "获取某个字典数据下拉框列表")
  191. @Parameters({
  192. @Parameter(name = "dictionaryTypeId", description = "数据分类id", required = true)
  193. })
  194. @GetMapping("/{dictionaryTypeId}/Data/Selector")
  195. public ActionResult<ListVO<DictionaryTypeSelectVO>> selectorOneTreeView(@PathVariable("dictionaryTypeId") String dictionaryTypeId) {
  196. List<DictionaryDataEntity> data = dictionaryDataService.getList(dictionaryTypeId, true);
  197. if(data.isEmpty()){
  198. DictionaryTypeEntity typeEntity = dictionaryTypeService.getInfoByEnCode(dictionaryTypeId);
  199. if(typeEntity != null){
  200. data = dictionaryDataService.getList(typeEntity.getId(), true);
  201. }
  202. }
  203. List<DictionaryTypeSelectModel> voListVO = JsonUtil.getJsonToList(data, DictionaryTypeSelectModel.class);
  204. List<SumTree<DictionaryTypeSelectModel>> sumTrees = TreeDotUtils.convertListToTreeDot(voListVO);
  205. List<DictionaryTypeSelectVO> list = JsonUtil.getJsonToList(sumTrees, DictionaryTypeSelectVO.class);
  206. ListVO<DictionaryTypeSelectVO> vo = new ListVO<>();
  207. vo.setList(list);
  208. return ActionResult.success(vo);
  209. }
  210. /**
  211. * 获取数据字典信息
  212. *
  213. * @param id 主键
  214. * @return ignore
  215. * @throws DataException ignore
  216. */
  217. @Operation(summary = "获取数据字典信息")
  218. @Parameters({
  219. @Parameter(name = "id", description = "主键值", required = true)
  220. })
  221. @GetMapping("/{id}/Info")
  222. public ActionResult<DictionaryDataInfoVO> info(@PathVariable("id") String id) throws DataException {
  223. DictionaryDataEntity entity = dictionaryDataService.getInfo(id);
  224. DictionaryDataInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, DictionaryDataInfoVO.class);
  225. return ActionResult.success(vo);
  226. }
  227. /**
  228. * 重复验证(名称)
  229. *
  230. * @param dictionaryTypeId 类别主键
  231. * @param fullName 名称
  232. * @param id 主键值
  233. * @return ignore
  234. */
  235. @Operation(summary = "(待定)重复验证(名称)")
  236. @GetMapping("/IsExistByFullName")
  237. public ActionResult isExistByFullName(String dictionaryTypeId, String fullName, String id) {
  238. boolean data = dictionaryDataService.isExistByFullName(dictionaryTypeId, fullName, id);
  239. return ActionResult.success(data);
  240. }
  241. /**
  242. * 重复验证(编码)
  243. *
  244. * @param dictionaryTypeId 类别主键
  245. * @param enCode 编码
  246. * @param id 主键值
  247. * @return ignore
  248. */
  249. @Operation(summary = "(待定)重复验证(编码)")
  250. @GetMapping("/IsExistByEnCode")
  251. public ActionResult isExistByEnCode(String dictionaryTypeId, String enCode, String id) {
  252. boolean data = dictionaryDataService.isExistByEnCode(dictionaryTypeId, enCode, id);
  253. return ActionResult.success(data);
  254. }
  255. /**
  256. * 添加数据字典
  257. *
  258. * @param dictionaryDataCrForm 实体对象
  259. * @return ignore
  260. */
  261. @Operation(summary = "添加数据字典")
  262. @Parameters({
  263. @Parameter(name = "dictionaryDataCrForm", description = "实体对象", required = true)
  264. })
  265. @SaCheckPermission("sysData.dictionary")
  266. @PostMapping
  267. public ActionResult create(@RequestBody @Valid DictionaryDataCrForm dictionaryDataCrForm) {
  268. DictionaryDataEntity entity = JsonUtil.getJsonToBean(dictionaryDataCrForm, DictionaryDataEntity.class);
  269. if (dictionaryDataService.isExistByFullName(entity.getDictionaryTypeId(), entity.getFullName(), entity.getId())) {
  270. return ActionResult.fail(MsgCode.EXIST001.get());
  271. }
  272. if (dictionaryDataService.isExistByEnCode(entity.getDictionaryTypeId(), entity.getEnCode(), entity.getId())) {
  273. return ActionResult.fail(MsgCode.EXIST002.get());
  274. }
  275. dictionaryDataService.create(entity);
  276. return ActionResult.success(MsgCode.SU001.get());
  277. }
  278. /**
  279. * 修改数据字典
  280. *
  281. * @param id 主键值
  282. * @param dictionaryDataUpForm 实体对象
  283. * @return ignore
  284. */
  285. @Operation(summary = "修改数据字典")
  286. @Parameters({
  287. @Parameter(name = "id", description = "主键值", required = true),
  288. @Parameter(name = "dictionaryDataUpForm", description = "实体对象", required = true)
  289. })
  290. @SaCheckPermission("sysData.dictionary")
  291. @PutMapping("/{id}")
  292. public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid DictionaryDataUpForm dictionaryDataUpForm) {
  293. DictionaryDataEntity entity = JsonUtil.getJsonToBean(dictionaryDataUpForm, DictionaryDataEntity.class);
  294. if (dictionaryDataService.isExistByFullName(entity.getDictionaryTypeId(), entity.getFullName(), id)) {
  295. return ActionResult.fail(MsgCode.EXIST001.get());
  296. }
  297. if (dictionaryDataService.isExistByEnCode(entity.getDictionaryTypeId(), entity.getEnCode(), id)) {
  298. return ActionResult.fail(MsgCode.EXIST002.get());
  299. }
  300. boolean flag = dictionaryDataService.update(id, entity);
  301. if (!flag) {
  302. return ActionResult.fail(MsgCode.FA002.get());
  303. }
  304. return ActionResult.success(MsgCode.SU004.get());
  305. }
  306. /**
  307. * 删除数据字典
  308. *
  309. * @param id 主键值
  310. * @return ignore
  311. */
  312. @Operation(summary = "删除数据字典")
  313. @Parameters({
  314. @Parameter(name = "id", description = "主键值", required = true)
  315. })
  316. @SaCheckPermission("sysData.dictionary")
  317. @DeleteMapping("/{id}")
  318. public ActionResult delete(@PathVariable("id") String id) {
  319. DictionaryDataEntity entity = dictionaryDataService.getInfo(id);
  320. if (entity != null) {
  321. if (dictionaryDataService.isExistSubset(entity.getId())) {
  322. return ActionResult.fail(MsgCode.SYS014.get());
  323. }
  324. dictionaryDataService.delete(entity);
  325. return ActionResult.success(MsgCode.SU003.get());
  326. }
  327. return ActionResult.fail(MsgCode.FA003.get());
  328. }
  329. /**
  330. * 更新字典状态
  331. *
  332. * @param id 主键值
  333. * @return ignore
  334. */
  335. @Operation(summary = "更新字典状态")
  336. @Parameters({
  337. @Parameter(name = "id", description = "主键值", required = true)
  338. })
  339. @SaCheckPermission("sysData.dictionary")
  340. @PutMapping("/{id}/Actions/State")
  341. public ActionResult update(@PathVariable("id") String id) {
  342. DictionaryDataEntity entity = dictionaryDataService.getInfo(id);
  343. if (entity != null) {
  344. if ("1".equals(String.valueOf(entity.getEnabledMark()))) {
  345. entity.setEnabledMark(0);
  346. } else {
  347. entity.setEnabledMark(1);
  348. }
  349. boolean flag = dictionaryDataService.update(entity.getId(), entity);
  350. if (!flag) {
  351. return ActionResult.success(MsgCode.FA002.get());
  352. }
  353. }
  354. return ActionResult.success(MsgCode.SU004.get());
  355. }
  356. /**
  357. * 数据字典导出功能
  358. *
  359. * @param id 接口id
  360. * @return ignore
  361. */
  362. @Operation(summary = "导出数据字典数据")
  363. @Parameters({
  364. @Parameter(name = "id", description = "主键值", required = true)
  365. })
  366. @SaCheckPermission("sysData.dictionary")
  367. @GetMapping("/{id}/Actions/Export")
  368. public ActionResult exportFile(@PathVariable("id") String id) {
  369. DownloadVO downloadVO = dictionaryDataService.exportData(id);
  370. return ActionResult.success(downloadVO);
  371. }
  372. /**
  373. * 数据字典导入功能
  374. *
  375. * @param multipartFile 文件
  376. * @return ignore
  377. * @throws DataException ignore
  378. */
  379. @Operation(summary = "数据字典导入功能")
  380. @SaCheckPermission("sysData.dictionary")
  381. @PostMapping(value = "/Actions/Import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  382. public ActionResult importFile(@RequestPart("file") MultipartFile multipartFile,
  383. @RequestParam("type") Integer type) throws DataException {
  384. //判断是否为.json结尾
  385. if (FileUtil.existsSuffix(multipartFile, ModuleTypeEnum.SYSTEM_DICTIONARYDATA.getTableName())) {
  386. return ActionResult.fail(MsgCode.IMP002.get());
  387. }
  388. try {
  389. //获取文件内容
  390. String fileContent = FileUtil.getFileContent(multipartFile);
  391. DictionaryExportModel exportModel = JsonUtil.getJsonToBean(fileContent, DictionaryExportModel.class);
  392. List<DictionaryTypeEntity> list = exportModel.getList();
  393. //父级分类id不存在的话,直接抛出异常
  394. //如果分类只有一个
  395. if (list.size() == 1 && !"-1".equals(list.get(0).getParentId()) && dictionaryTypeService.getInfo(list.get(0).getParentId()) == null) {
  396. return ActionResult.fail(MsgCode.IMP010.get());
  397. }
  398. //如果有多个需要验证分类是否存在
  399. if (list.stream().filter(t -> "-1".equals(t.getParentId())).count() < 1) {
  400. boolean exist = false;
  401. for (DictionaryTypeEntity dictionaryTypeEntity : list) {
  402. //判断父级是否存在
  403. if (dictionaryTypeService.getInfo(dictionaryTypeEntity.getParentId()) != null) {
  404. exist = true;
  405. }
  406. }
  407. if (!exist) {
  408. return ActionResult.fail(MsgCode.IMP010.get());
  409. }
  410. }
  411. //判断数据是否存在
  412. return dictionaryDataService.importData(exportModel, type);
  413. } catch (Exception e) {
  414. throw new DataException(MsgCode.IMP004.get());
  415. }
  416. }
  417. }