SystemDictEntryEndPoint.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.yihu.ehr.basic.dict.controller;
  2. import com.yihu.ehr.basic.dict.service.SystemDictEntryService;
  3. import com.yihu.ehr.basic.dict.service.SystemDictService;
  4. import com.yihu.ehr.constants.ApiVersion;
  5. import com.yihu.ehr.constants.ErrorCode;
  6. import com.yihu.ehr.constants.ServiceApi;
  7. import com.yihu.ehr.entity.dict.DictEntryKey;
  8. import com.yihu.ehr.entity.dict.SystemDict;
  9. import com.yihu.ehr.entity.dict.SystemDictEntry;
  10. import com.yihu.ehr.exception.ApiException;
  11. import com.yihu.ehr.model.common.ListResult;
  12. import com.yihu.ehr.model.dict.MDictionaryEntry;
  13. import com.yihu.ehr.controller.EnvelopRestEndPoint;
  14. import com.yihu.ehr.util.rest.Envelop;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiOperation;
  17. import io.swagger.annotations.ApiParam;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.data.domain.Page;
  20. import org.springframework.http.MediaType;
  21. import org.springframework.web.bind.annotation.*;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. /**
  30. * @author Sand
  31. * @version 1.0
  32. * @created 2016.02.15 18:25
  33. */
  34. @RestController
  35. @RequestMapping(ApiVersion.Version1_0)
  36. @Api(value = "DictionaryEntry", description = "系统全局字典项管理", tags = {"系统字典-系统全局字典项管理"})
  37. public class SystemDictEntryEndPoint extends EnvelopRestEndPoint {
  38. @Autowired
  39. private SystemDictService dictService;
  40. @Autowired
  41. private SystemDictEntryService systemDictEntryService;
  42. @ApiOperation(value = "获取字典项列表")
  43. @RequestMapping(value = "/dictionaries/entries", method = RequestMethod.GET)
  44. public List<MDictionaryEntry> getDictEntries(
  45. @ApiParam(name = "fields", value = "返回的字段,为空返回全部字段", defaultValue = "")
  46. @RequestParam(value = "fields", required = false) String fields,
  47. @ApiParam(name = "filters", value = "过滤器", defaultValue = "")
  48. @RequestParam(value = "filters", required = false) String filters,
  49. @ApiParam(name = "sorts", value = "排序", defaultValue = "")
  50. @RequestParam(value = "sorts", required = false) String sorts,
  51. @ApiParam(name = "size", value = "分页大小", defaultValue = "15")
  52. @RequestParam(value = "size", required = false) Integer size,
  53. @ApiParam(name = "page", value = "页码", defaultValue = "1")
  54. @RequestParam(value = "page", required = false) Integer page,
  55. HttpServletRequest request,
  56. HttpServletResponse response) throws Exception {
  57. List<SystemDictEntry> systemDictEntryList = systemDictEntryService.search(fields,filters,sorts,page,size);
  58. pagedResponse(request, response,systemDictEntryService.getCount(filters), page, size);
  59. return (List<MDictionaryEntry>)convertToModels(systemDictEntryList,new ArrayList<MDictionaryEntry>(systemDictEntryList.size()),MDictionaryEntry.class,null);
  60. }
  61. @ApiOperation(value = "创建字典项")
  62. @RequestMapping(value = "/dictionaries/entries", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  63. public MDictionaryEntry createDictEntry (
  64. @ApiParam(name = "entry", value = "字典JSON结构")
  65. @RequestBody String entryJson) throws IOException{
  66. SystemDictEntry entry = toEntity(entryJson, SystemDictEntry.class);
  67. SystemDict systemDict = dictService.retrieve(entry.getDictId());
  68. if (systemDict == null) {
  69. throw new ApiException(ErrorCode.NOT_FOUND, "所属字典不存在");
  70. }
  71. int nextSort = systemDictEntryService.getNextSN(entry.getDictId());
  72. entry.setSort(nextSort);
  73. systemDictEntryService.createDictEntry(entry);
  74. return convertToModel(entry, MDictionaryEntry.class, null);
  75. }
  76. @ApiOperation(value = "获取字典项")
  77. @RequestMapping(value = "/dictionaries/{dict_id}/entries/{code}", method = RequestMethod.GET)
  78. public MDictionaryEntry getDictEntry(
  79. @ApiParam(name = "dict_id", value = "字典ID", required = true)
  80. @PathVariable(value = "dict_id") long dictId,
  81. @ApiParam(name = "code", value = "字典项代码", required = true)
  82. @PathVariable(value = "code") String code) {
  83. SystemDictEntry systemDictEntry = systemDictEntryService.getDictEntry(dictId, code);
  84. return convertToModel(systemDictEntry, MDictionaryEntry.class);
  85. }
  86. @ApiOperation(value = "删除字典项")
  87. @RequestMapping(value = "/dictionaries/{dict_id}/entries/{code}", method = RequestMethod.DELETE)
  88. public Object deleteDictEntry(
  89. @ApiParam(name = "dict_id", value = "字典ID", defaultValue = "")
  90. @PathVariable(value = "dict_id") long dictId,
  91. @ApiParam(name = "code", value = "字典ID", defaultValue = "")
  92. @PathVariable(value = "code") String code) throws Exception{
  93. systemDictEntryService.deleteDictEntry(dictId, code);
  94. return true;
  95. }
  96. @ApiOperation(value = "修改字典项")
  97. @RequestMapping(value = "/dictionaries/entries", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  98. public MDictionaryEntry updateDictEntry(
  99. @ApiParam(name = "entry", value = "字典JSON结构")
  100. @RequestBody String entryJson) throws IOException {
  101. SystemDictEntry entry = toEntity(entryJson, SystemDictEntry.class);
  102. SystemDictEntry temp = systemDictEntryService.retrieve(new DictEntryKey(entry.getCode(), entry.getDictId()));
  103. if (null == temp) {
  104. throw new ApiException(ErrorCode.NOT_FOUND, "字典项不存在");
  105. }
  106. systemDictEntryService.saveDictEntry(entry);
  107. return convertToModel(entry, MDictionaryEntry.class, null);
  108. }
  109. @RequestMapping(value = "/dictionaries/existence/{dict_id}" , method = RequestMethod.GET)
  110. @ApiOperation(value = "根据dictId和code判断提交的字典项名称是否已经存在")
  111. public boolean isDictEntryCodeExists(
  112. @ApiParam(name = "dict_id", value = "dict_id", defaultValue = "")
  113. @PathVariable(value = "dict_id") long dictId,
  114. @ApiParam(name = "code", value = "code", defaultValue = "")
  115. @RequestParam(value = "code") String code){
  116. return systemDictEntryService.isDictContainEntry(dictId, code);
  117. }
  118. @RequestMapping(value ="/dictionaries/systemDictEntryList/{dict_id}",method = RequestMethod.GET)
  119. @ApiOperation(value = "根据dictId获取所有字典项")
  120. public ListResult GetSystemDictEntryListByDictId(
  121. @ApiParam(name = "dict_id", value = "dict_id", defaultValue = "")
  122. @PathVariable(value = "dict_id") long dictId) throws Exception{
  123. int page=0;
  124. int size=1000;
  125. ListResult re = new ListResult(page,size);
  126. Page<SystemDictEntry> cardList = systemDictEntryService.findByDictId(dictId, page,size);
  127. if(cardList!=null) {
  128. re.setDetailModelList(cardList.getContent());
  129. re.setTotalCount(cardList.getTotalPages());
  130. }
  131. return re;
  132. }
  133. @RequestMapping(value ="/dictionary/entryList/{dictId}", method = RequestMethod.GET)
  134. @ApiOperation(value = "根据dictId获取所有字典项")
  135. public Envelop listByDictId(
  136. @ApiParam(name = "dictId", value = "dictId", required = true)
  137. @PathVariable(value = "dictId") long dictId) {
  138. Envelop envelop = new Envelop();
  139. int page = 0;
  140. int size = 1000;
  141. //ListResult re = new ListResult(page, size);
  142. Page<SystemDictEntry> page1 = systemDictEntryService.findByDictId(dictId, page,size);
  143. if(page1 != null) {
  144. envelop.setDetailModelList(page1.getContent());
  145. envelop.setSuccessFlg(true);
  146. }
  147. return envelop;
  148. }
  149. @ApiOperation(value = "未登录获取字典项")
  150. @RequestMapping(value = ServiceApi.SystemDict.GetDictEntryByDictIdAndEntryCode, method = RequestMethod.GET)
  151. public Envelop getDictEntryByDictIdAndEntryCode(
  152. @ApiParam(name = "dictId", value = "字典ID", required = true)
  153. @RequestParam(value = "dictId") long dictId,
  154. @ApiParam(name = "code", value = "字典项代码", required = true)
  155. @RequestParam(value = "code") String code) {
  156. Envelop envelop =new Envelop();
  157. SystemDictEntry systemDictEntry = systemDictEntryService.getDictEntry(dictId, code);
  158. MDictionaryEntry mDictionaryEntry=convertToModel(systemDictEntry, MDictionaryEntry.class);
  159. envelop.setObj(mDictionaryEntry);
  160. envelop.setSuccessFlg(true);
  161. return envelop;
  162. }
  163. @RequestMapping(value = ServiceApi.SystemDict.getDictEntryCodeAndValueByDictId, method = RequestMethod.GET)
  164. @ApiOperation("根据字典id获取所有字典项的code和值")
  165. public Envelop getDictEntryCodeAndValueByDictId(
  166. @ApiParam(name = "dictId", value = "字典id")
  167. @RequestParam(value = "dictId", required = false) String dictId) throws Exception {
  168. Envelop envelop = new Envelop();
  169. Map<String, String> map = new HashMap<>();
  170. List list = systemDictEntryService.getDictEntryCodeAndValueByDictId(dictId);
  171. String code = "";
  172. String value = "";
  173. for (int i = 0; i < list.size(); i++) {
  174. Object[] obj = (Object[]) list.get(i);
  175. if (null != obj[0] && null != obj[1]) {
  176. code = obj[0].toString();
  177. value = obj[1].toString();
  178. map.put(code, value);
  179. }
  180. }
  181. envelop.setSuccessFlg(true);
  182. envelop.setObj(map);
  183. return envelop;
  184. }
  185. }