DataDictionaryController.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package com.jpsoft.smart.modules.sys.controller;
  2. import com.github.pagehelper.Page;
  3. import com.jpsoft.smart.modules.common.dto.MessageResult;
  4. import com.jpsoft.smart.modules.common.dto.Sort;
  5. import com.jpsoft.smart.modules.common.utils.PojoUtils;
  6. import com.jpsoft.smart.modules.sys.entity.DataDictionary;
  7. import com.jpsoft.smart.modules.sys.service.DataDictionaryService;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.web.bind.annotation.*;
  15. import java.util.*;
  16. @RestController
  17. @RequestMapping("/sys/dataDictionary")
  18. @Api(description = "数据字典")
  19. public class DataDictionaryController {
  20. private Logger logger = LoggerFactory.getLogger(getClass());
  21. @Autowired
  22. private DataDictionaryService dataDictionaryService;
  23. @ApiOperation(value="创建空记录")
  24. @GetMapping("create")
  25. public MessageResult<DataDictionary> create(){
  26. MessageResult<DataDictionary> msgResult = new MessageResult<>();
  27. DataDictionary dataDictionary = new DataDictionary();
  28. msgResult.setData(dataDictionary);
  29. msgResult.setResult(true);
  30. return msgResult;
  31. }
  32. @ApiOperation(value="添加信息")
  33. @PostMapping("add")
  34. public MessageResult<DataDictionary> add(@RequestBody DataDictionary dataDictionary,@RequestAttribute String subject){
  35. MessageResult<DataDictionary> msgResult = new MessageResult<>();
  36. try {
  37. dataDictionary.setId(UUID.randomUUID().toString());
  38. dataDictionary.setDelFlag(false);
  39. dataDictionary.setCreateBy(subject);
  40. dataDictionary.setCreateDate(new Date());
  41. int affectCount = dataDictionaryService.insert(dataDictionary);
  42. if (affectCount > 0) {
  43. msgResult.setResult(true);
  44. msgResult.setData(dataDictionary);
  45. } else {
  46. msgResult.setResult(false);
  47. msgResult.setMessage("添加失败");
  48. }
  49. }
  50. catch(Exception ex){
  51. logger.error(ex.getMessage(),ex);
  52. msgResult.setResult(false);
  53. msgResult.setMessage("添加失败");
  54. }
  55. return msgResult;
  56. }
  57. @ApiOperation(value="获取信息")
  58. @GetMapping("edit/{id}")
  59. public MessageResult<DataDictionary> edit(@PathVariable("id") String id){
  60. MessageResult<DataDictionary> msgResult = new MessageResult<>();
  61. try {
  62. DataDictionary dataDictionary = dataDictionaryService.get(id);
  63. if (dataDictionary != null) {
  64. msgResult.setResult(true);
  65. msgResult.setData(dataDictionary);
  66. } else {
  67. msgResult.setResult(false);
  68. msgResult.setMessage("不存在该记录!");
  69. }
  70. }
  71. catch(Exception ex){
  72. logger.error(ex.getMessage(),ex);
  73. msgResult.setResult(false);
  74. msgResult.setMessage(ex.getMessage());
  75. }
  76. return msgResult;
  77. }
  78. @ApiOperation(value="更新用户")
  79. @PostMapping("update")
  80. public MessageResult<DataDictionary> update(@RequestBody DataDictionary dataDictionary,@RequestAttribute String subject){
  81. MessageResult<DataDictionary> msgResult = new MessageResult<>();
  82. try {
  83. dataDictionary.setUpdateBy(subject);
  84. dataDictionary.setUpdateDate(new Date());
  85. int affectCount = dataDictionaryService.update(dataDictionary);
  86. if (affectCount > 0) {
  87. msgResult.setResult(true);
  88. msgResult.setData(dataDictionary);
  89. } else {
  90. msgResult.setResult(false);
  91. msgResult.setMessage("更新失败");
  92. }
  93. }
  94. catch(Exception ex){
  95. logger.error(ex.getMessage(),ex);
  96. msgResult.setResult(false);
  97. msgResult.setMessage("更新失败");
  98. }
  99. return msgResult;
  100. }
  101. @ApiOperation(value="删除")
  102. @PostMapping("delete/{id}")
  103. public MessageResult<Integer> delete(@PathVariable("id") String id,@RequestAttribute String subject){
  104. MessageResult<Integer> msgResult = new MessageResult<>();
  105. try {
  106. DataDictionary dataDictionary = dataDictionaryService.get(id);
  107. dataDictionary.setDelFlag(true);
  108. dataDictionary.setUpdateBy(subject);
  109. dataDictionary.setUpdateDate(new Date());
  110. int affectCount = dataDictionaryService.update(dataDictionary);
  111. if (affectCount > 0) {
  112. msgResult.setResult(true);
  113. msgResult.setData(affectCount);
  114. } else {
  115. msgResult.setResult(false);
  116. msgResult.setMessage("删除失败");
  117. }
  118. }
  119. catch(Exception ex){
  120. logger.error(ex.getMessage(),ex);
  121. msgResult.setResult(false);
  122. msgResult.setMessage(ex.getMessage());
  123. }
  124. return msgResult;
  125. }
  126. @ApiOperation(value="批量删除")
  127. @PostMapping("batchDelete")
  128. public MessageResult<Integer> batchDelete(@RequestBody List<String> idList,@RequestAttribute String subject){
  129. MessageResult<Integer> msgResult = new MessageResult<>();
  130. try {
  131. int affectCount = 0;
  132. for (String id : idList) {
  133. DataDictionary dataDictionary = dataDictionaryService.get(id);
  134. dataDictionary.setDelFlag(true);
  135. dataDictionary.setUpdateBy(subject);
  136. dataDictionary.setUpdateDate(new Date());
  137. affectCount += dataDictionaryService.update(dataDictionary);
  138. }
  139. if (affectCount > 0) {
  140. msgResult.setResult(true);
  141. msgResult.setData(affectCount);
  142. } else {
  143. msgResult.setResult(false);
  144. msgResult.setMessage("删除失败");
  145. }
  146. }
  147. catch(Exception ex){
  148. logger.error(ex.getMessage(),ex);
  149. msgResult.setResult(false);
  150. msgResult.setMessage(ex.getMessage());
  151. }
  152. return msgResult;
  153. }
  154. @ApiOperation(value="列表")
  155. @RequestMapping(value = "pageList",method = RequestMethod.POST)
  156. public MessageResult<Map> pageList(
  157. String id,
  158. String name,
  159. String parentId,
  160. @RequestParam(name="pageIndex",defaultValue = "1") int pageIndex,
  161. @RequestParam(name="pageSize",defaultValue = "10") int pageSize,
  162. @RequestAttribute String subject){
  163. //当前用户ID
  164. System.out.println(subject);
  165. MessageResult<Map> msgResult = new MessageResult<>();
  166. Map<String,Object> searchParams = new HashMap<>();
  167. List<Sort> sortList = new ArrayList<>();
  168. sortList.add(new Sort("sort_no","asc"));
  169. if (StringUtils.isNotEmpty(parentId)) {
  170. searchParams.put("parentId",parentId);
  171. }
  172. if (StringUtils.isNotEmpty(id)) {
  173. searchParams.put("id",id);
  174. }
  175. if (StringUtils.isNotEmpty(name)) {
  176. searchParams.put("name","%" + name + "%");
  177. }
  178. Page<DataDictionary> page = dataDictionaryService.pageSearch(searchParams,pageIndex, pageSize,sortList);
  179. msgResult.setResult(true);
  180. msgResult.setData(PojoUtils.pageWrapper(page));
  181. return msgResult;
  182. }
  183. @ApiOperation(value = "查询")
  184. @RequestMapping(value = "query", method = RequestMethod.POST)
  185. public MessageResult<List> query(
  186. String keywords,
  187. String excludeId,
  188. @RequestParam(value = "dataType", defaultValue = "null") String dataType,
  189. @RequestParam(value = "length", defaultValue = "20") int limit,
  190. @RequestAttribute String subject) {
  191. MessageResult<List> msgResult = new MessageResult<>();
  192. Map<String, Object> searchParams = new HashMap<>();
  193. List<Sort> sortList = new ArrayList<>();
  194. sortList.add(new Sort("a.sort_no", "asc"));
  195. if (StringUtils.isNotEmpty(keywords)) {
  196. searchParams.put("name", "%" + keywords + "%");
  197. }
  198. if (StringUtils.isNotEmpty(excludeId)) {
  199. searchParams.put("excludeId", excludeId);
  200. }
  201. if (StringUtils.isNotEmpty(dataType)) {
  202. //1查目录-2查值
  203. searchParams.put("dataType", dataType);
  204. }
  205. Page<DataDictionary> page = dataDictionaryService.pageSearch(searchParams,0, limit,sortList);
  206. List<DataDictionary> dataDictionaryList = page.getResult();
  207. msgResult.setResult(true);
  208. msgResult.setData(dataDictionaryList);
  209. return msgResult;
  210. }
  211. @ApiOperation(value = "查询字典")
  212. @RequestMapping(value = "queryChildren", method = RequestMethod.POST)
  213. public MessageResult<List> queryChildren(
  214. @RequestParam(value = "parentId", defaultValue = "") String parentId,
  215. @RequestAttribute String subject) {
  216. MessageResult<List> msgResult = new MessageResult<>();
  217. List<Map<String, Object>> dataDictionaryList = dataDictionaryService.queryChildren(parentId);
  218. msgResult.setResult(true);
  219. msgResult.setData(dataDictionaryList);
  220. return msgResult;
  221. }
  222. }