RsSystemDictionaryEndPoint.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.yihu.ehr.resource.controller;
  2. import com.yihu.ehr.constants.ServiceApi;
  3. import com.yihu.ehr.constants.ApiVersion;
  4. import com.yihu.ehr.model.resource.MRsSystemDictionary;
  5. import com.yihu.ehr.resource.model.RsSystemDictionary;
  6. import com.yihu.ehr.resource.model.RsSystemDictionaryEntry;
  7. import com.yihu.ehr.resource.service.RsSystemDictionaryEntryService;
  8. import com.yihu.ehr.resource.service.RsSystemDictionaryService;
  9. import com.yihu.ehr.controller.EnvelopRestEndPoint;
  10. import com.yihu.ehr.util.id.BizObject;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import io.swagger.annotations.ApiParam;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.web.bind.annotation.*;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. /**
  22. * @author linaz
  23. * @created 2016.05.17 16:33
  24. */
  25. @RestController
  26. @RequestMapping(value = ApiVersion.Version1_0)
  27. @Api(value = "RsSystemDictionaryEndPoint", description = "资源系统字典", tags = {"资源服务-资源系统字典"})
  28. public class RsSystemDictionaryEndPoint extends EnvelopRestEndPoint {
  29. @Autowired
  30. private RsSystemDictionaryService rsSystemDictionaryService;
  31. @Autowired
  32. private RsSystemDictionaryEntryService rsSystemDictionaryEntryService;
  33. @RequestMapping(value = ServiceApi.Resources.SystemDictList, method = RequestMethod.GET)
  34. @ApiOperation(value = "根据查询条件获取系统字典列表", notes = "根据查询条件获取系统字典列表")
  35. public List<MRsSystemDictionary> searchRsSystemDictionaries(
  36. @ApiParam(name = "fields", value = "返回的字段,为空返回全部字段", defaultValue = "id,name,secret,url,createTime")
  37. @RequestParam(value = "fields", required = false) String fields,
  38. @ApiParam(name = "filters", value = "过滤器,为空检索所有条件", defaultValue = "")
  39. @RequestParam(value = "filters", required = false) String filters,
  40. @ApiParam(name = "sorts", value = "排序,规则参见说明文档", defaultValue = "+name,+createTime")
  41. @RequestParam(value = "sorts", required = false) String sorts,
  42. @ApiParam(name = "page", value = "页码", defaultValue = "1")
  43. @RequestParam(value = "page", required = false) int page,
  44. @ApiParam(name = "size", value = "分页大小", defaultValue = "15")
  45. @RequestParam(value = "size", required = false) int size,
  46. HttpServletRequest request,
  47. HttpServletResponse response) throws Exception {
  48. List<RsSystemDictionary> systemDictionaries = rsSystemDictionaryService.search(fields, filters, sorts, page, size);
  49. pagedResponse(request, response, rsSystemDictionaryService.getCount(filters), page, size);
  50. return (List<MRsSystemDictionary>) convertToModels(systemDictionaries, new ArrayList<MRsSystemDictionary>(systemDictionaries.size()), MRsSystemDictionary.class, fields);
  51. }
  52. @RequestMapping(value = ServiceApi.Resources.SystemDictList, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  53. @ApiOperation(value = "创建系统字典", notes = "创建系统字典")
  54. public MRsSystemDictionary createRsSystemDictionary(
  55. @ApiParam(name = "json_data", value = "", defaultValue = "")
  56. @RequestBody String jsonData) throws Exception {
  57. RsSystemDictionary systemDictionary = toEntity(jsonData, RsSystemDictionary.class);
  58. String code = systemDictionary.getCode();
  59. if(isExistence(code)){
  60. throw new Exception("字典代码不能重复");
  61. }
  62. systemDictionary.setId(getObjectId(BizObject.RsSystemDictionary));
  63. rsSystemDictionaryService.save(systemDictionary);
  64. return convertToModel(systemDictionary, MRsSystemDictionary.class, null);
  65. }
  66. @RequestMapping(value = ServiceApi.Resources.SystemDictList, method = RequestMethod.PUT)
  67. @ApiOperation(value = "修改系统字典", notes = "修改系统字典")
  68. public MRsSystemDictionary updateRsSystemDictionary(
  69. @ApiParam(name = "json_data", value = "")
  70. @RequestBody String jsonData) throws Exception {
  71. RsSystemDictionary rsSystemDictionary = toEntity(jsonData, RsSystemDictionary.class);
  72. String id = rsSystemDictionary.getId();
  73. RsSystemDictionary d = rsSystemDictionaryService.findById(id);
  74. String code = rsSystemDictionary.getCode();
  75. if(code!=d.getCode()){
  76. throw new Exception("字典代码不可修改");
  77. }
  78. rsSystemDictionaryService.save(rsSystemDictionary);
  79. return convertToModel(rsSystemDictionary, MRsSystemDictionary.class, null);
  80. }
  81. @RequestMapping(value = ServiceApi.Resources.SystemDict, method = RequestMethod.DELETE)
  82. @ApiOperation(value = "删除系统字典", notes = "删除系统字典")
  83. public boolean deleteRsSystemDictionary(
  84. @ApiParam(name = "id", value = "id", defaultValue = "")
  85. @PathVariable(value = "id") String id) throws Exception {
  86. RsSystemDictionary systemDictionary = rsSystemDictionaryService.findById(id);
  87. String code = systemDictionary.getCode();
  88. List<RsSystemDictionaryEntry> systemDictionaryEntries = rsSystemDictionaryEntryService.findByField("dictCode",code);
  89. if(systemDictionaryEntries!=null && systemDictionaryEntries.size()!=0){
  90. throw new Exception("该字典包含字典项,不可删除");
  91. }
  92. rsSystemDictionaryService.delete(id);
  93. return true;
  94. }
  95. public boolean isExistence(String code) {
  96. return rsSystemDictionaryService.findByField("code",code) != null;
  97. }
  98. }