SystemDictEndPoint.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.yihu.ehr.basic.dict.controller;
  2. import com.yihu.ehr.basic.dict.service.SystemDictService;
  3. import com.yihu.ehr.constants.ApiVersion;
  4. import com.yihu.ehr.constants.ErrorCode;
  5. import com.yihu.ehr.constants.ServiceApi;
  6. import com.yihu.ehr.entity.dict.SystemDict;
  7. import com.yihu.ehr.exception.ApiException;
  8. import com.yihu.ehr.model.dict.MSystemDict;
  9. import com.yihu.ehr.controller.EnvelopRestEndPoint;
  10. import com.yihu.ehr.util.phonics.PinyinUtil;
  11. import com.yihu.ehr.util.rest.Envelop;
  12. import io.swagger.annotations.Api;
  13. import io.swagger.annotations.ApiOperation;
  14. import io.swagger.annotations.ApiParam;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.data.domain.Page;
  18. import org.springframework.http.MediaType;
  19. import org.springframework.web.bind.annotation.*;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.List;
  26. /**
  27. * Created by linaz on 2015/8/12.
  28. */
  29. @RestController
  30. @RequestMapping(ApiVersion.Version1_0)
  31. @Api(value = "Dictionary", description = "系统全局字典管理", tags = {"系统字典-系统全局字典管理"})
  32. public class SystemDictEndPoint extends EnvelopRestEndPoint {
  33. @Autowired
  34. private SystemDictService dictService;
  35. @ApiOperation(value = "获取字典列表", response = MSystemDict.class, responseContainer = "List")
  36. @RequestMapping(value = ServiceApi.SystemDict.Crud, method = RequestMethod.GET)
  37. public Collection<MSystemDict> getDictionaries(
  38. @ApiParam(name = "fields", value = "返回的字段,为空返回全部字段", defaultValue = "")
  39. @RequestParam(value = "fields", required = false) String fields,
  40. @ApiParam(name = "filters", value = "过滤器", defaultValue = "")
  41. @RequestParam(value = "filters", required = false) String filters,
  42. @ApiParam(name = "sorts", value = "排序", defaultValue = "")
  43. @RequestParam(value = "sorts", required = false) String sorts,
  44. @ApiParam(name = "size", value = "分页大小", defaultValue = "15")
  45. @RequestParam(value = "size", required = false) Integer size,
  46. @ApiParam(name = "page", value = "页码", defaultValue = "1")
  47. @RequestParam(value = "page", required = false) Integer page,
  48. HttpServletRequest request,
  49. HttpServletResponse response) throws Exception {
  50. page = reducePage(page);
  51. if (StringUtils.isEmpty(filters)) {
  52. Page<SystemDict> systemDictPage = dictService.getDictList(sorts, page, size);
  53. pagedResponse(request, response, systemDictPage.getTotalElements(), page, size);
  54. return convertToModels(systemDictPage.getContent(), new ArrayList<>(systemDictPage.getNumber()), MSystemDict.class, fields);
  55. } else {
  56. List<SystemDict> systemDictList = dictService.search(fields, filters, sorts, page, size);
  57. pagedResponse(request, response, dictService.getCount(filters), page, size);
  58. return convertToModels(systemDictList, new ArrayList<>(systemDictList.size()), MSystemDict.class, fields);
  59. }
  60. }
  61. @ApiOperation(value = "创建字典", response = SystemDict.class)
  62. @RequestMapping(value = ServiceApi.SystemDict.Crud, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  63. public Envelop createDictionary(
  64. @ApiParam(name = "dictionary", value = "字典JSON结构")
  65. @RequestBody SystemDict dict) throws IOException {
  66. if (StringUtils.isEmpty(dict.getName())) {
  67. return failed("字典名称不能为空");
  68. }
  69. if (StringUtils.isEmpty(dict.getPhoneticCode())) {
  70. dict.setPhoneticCode(PinyinUtil.getPinYinHeadChar(dict.getName(), true));
  71. }
  72. if (StringUtils.isEmpty(dict.getCode())) {
  73. dict.setCode(dict.getPhoneticCode());
  74. }
  75. SystemDict systemDict = dictService.createDict(dict);
  76. return success(systemDict);
  77. }
  78. @ApiOperation(value = "获取字典", response = MSystemDict.class)
  79. @RequestMapping(value = ServiceApi.SystemDict.FindById, method = RequestMethod.GET)
  80. public SystemDict getDictionary(
  81. @ApiParam(name = "id", value = "字典ID", defaultValue = "")
  82. @PathVariable(value = "id") long id) {
  83. SystemDict dict = dictService.retrieve(id);
  84. if (dict == null) {
  85. throw new ApiException(ErrorCode.NOT_FOUND, "字典不存在");
  86. }
  87. return dict;
  88. }
  89. @ApiOperation(value = "获取字典", response = SystemDict.class)
  90. @RequestMapping(value = ServiceApi.SystemDict.FindByPhoneticCode, method = RequestMethod.GET)
  91. public SystemDict getDictionaryByPhoneticCode(
  92. @ApiParam(name = "phoneticCode", value = "拼音编码", required = true)
  93. @PathVariable(value = "phoneticCode") String phoneticCode) {
  94. SystemDict dict = dictService.findByPhoneticCode(phoneticCode);
  95. if (dict == null) {
  96. throw new ApiException(ErrorCode.NOT_FOUND, "字典不存在");
  97. }
  98. return dict;
  99. }
  100. @ApiOperation(value = "更新字典")
  101. @RequestMapping(value = ServiceApi.SystemDict.Crud, method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  102. public Envelop updateDictionary(
  103. @ApiParam(name = "dictionary", value = "字典JSON结构")
  104. @RequestBody SystemDict dict) throws Exception {
  105. SystemDict oldDict = dictService.findOne(dict.getId());
  106. if (null == oldDict) {
  107. return failed("字典不存在", ErrorCode.OBJECT_NOT_FOUND.value());
  108. }
  109. if (StringUtils.isEmpty(dict.getName())) {
  110. return failed("字典名称不能为空");
  111. }
  112. if (!oldDict.getName().equals(dict.getName()) && dictService.findByField("name", dict.getName()).size() > 0) {
  113. return failed("字典名称在系统中已存在");
  114. }
  115. if (StringUtils.isEmpty(dict.getPhoneticCode())) {
  116. dict.setPhoneticCode(PinyinUtil.getPinYinHeadChar(dict.getName(), true));
  117. }
  118. if (StringUtils.isEmpty(dict.getCode())) {
  119. dict.setCode(dict.getPhoneticCode());
  120. }
  121. oldDict.setName(dict.getName());
  122. oldDict.setPhoneticCode(dict.getPhoneticCode());
  123. oldDict.setCode(dict.getCode());
  124. dictService.updateDict(oldDict);
  125. return success(oldDict);
  126. }
  127. @ApiOperation(value = "删除字典")
  128. @RequestMapping(value = ServiceApi.SystemDict.DeleteById, method = RequestMethod.DELETE)
  129. public Boolean deleteDictionary(
  130. @ApiParam(name = "id", value = "字典ID")
  131. @PathVariable(value = "id") long id) throws Exception{
  132. dictService.deleteDict(id);
  133. return true;
  134. }
  135. @RequestMapping(value = ServiceApi.SystemDict.CheckName, method = RequestMethod.GET)
  136. @ApiOperation(value = "判断提交的字典名称是否已经存在")
  137. public boolean isDictNameExists(
  138. @ApiParam(name = "dict_name", value = "字典名称")
  139. @RequestParam(value = "dict_name") String dictName){
  140. return dictService.isDictNameExists(dictName);
  141. }
  142. @RequestMapping(value = ServiceApi.SystemDict.CheckCode, method = RequestMethod.GET)
  143. @ApiOperation(value = "判断提交的字典编码是否已经存在")
  144. public boolean checkCode(
  145. @ApiParam(name = "code", value = "编码")
  146. @RequestParam(value = "code") String code) {
  147. if (dictService.findByField("code", code).size() > 0) {
  148. return true;
  149. }
  150. return false;
  151. }
  152. }