sys_dict_data_controller.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/gin-gonic/gin/binding"
  5. "gogs.baozhida.cn/zoie/OAuth-core/api"
  6. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  7. "Medical_OAuth/app/admin/model"
  8. "Medical_OAuth/app/admin/service"
  9. "Medical_OAuth/app/admin/service/dto"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  11. )
  12. type SysDictDataController struct {
  13. api.Api
  14. }
  15. // GetPage 获取字典数据列表
  16. // @Summary 获取字典数据列表
  17. // @Description 获取字典数据列表
  18. // @Tags 字典数据
  19. // @Param status query string false "status"
  20. // @Param dictCode query string false "dictCode"
  21. // @Param dictType query string false "dictType"
  22. // @Param pageSize query int false "页条数"
  23. // @Param pageIndex query int false "页码"
  24. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysDictData}} "{"code": 200, "data": [...]}"
  25. // @Router /api/dict/data [get]
  26. // @Security Bearer
  27. func (e SysDictDataController) GetPage(c *gin.Context) {
  28. s := service.SysDictDataService{}
  29. req := dto.SysDictDataGetPageReq{}
  30. err := e.MakeContext(c).
  31. MakeOrm().
  32. Bind(&req, binding.Form).
  33. MakeService(&s.Service).
  34. Errors
  35. if err != nil {
  36. e.Logger.Error(err)
  37. e.Error(500, err, err.Error())
  38. return
  39. }
  40. list := make([]model.SysDictData, 0)
  41. var count int64
  42. err = s.GetPage(&req, &list, &count)
  43. if err != nil {
  44. e.Error(500, err, err.Error())
  45. return
  46. }
  47. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  48. }
  49. // Get 通过id获取字典数据
  50. // @Summary 通过id获取字典数据
  51. // @Description 通过id获取字典数据
  52. // @Tags 字典数据
  53. // @Param dictCode path int true "字典编码"
  54. // @Success 200 {object} response.Response{data=model.SysDictData} "{"code": 200, "data": [...]}"
  55. // @Router /api/dict/data/{dictCode} [get]
  56. // @Security Bearer
  57. func (e SysDictDataController) Get(c *gin.Context) {
  58. s := service.SysDictDataService{}
  59. req := dto.SysDictDataGetReq{}
  60. err := e.MakeContext(c).
  61. MakeOrm().
  62. Bind(&req, nil).
  63. MakeService(&s.Service).
  64. Errors
  65. if err != nil {
  66. e.Logger.Error(err)
  67. e.Error(500, err, err.Error())
  68. return
  69. }
  70. var object model.SysDictData
  71. err = s.Get(&req, &object)
  72. if err != nil {
  73. e.Logger.Warnf("Get error: %s", err.Error())
  74. e.Error(500, err, err.Error())
  75. return
  76. }
  77. e.OK(object, "查询成功")
  78. }
  79. // Insert 添加字典数据
  80. // @Summary 添加字典数据
  81. // @Description 添加字典数据
  82. // @Tags 字典数据
  83. // @Accept application/json
  84. // @Product application/json
  85. // @Param data body dto.SysDictDataInsertReq true "data"
  86. // @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}"
  87. // @Router /api/dict/data [post]
  88. // @Security Bearer
  89. func (e SysDictDataController) Insert(c *gin.Context) {
  90. s := service.SysDictDataService{}
  91. req := dto.SysDictDataInsertReq{}
  92. err := e.MakeContext(c).
  93. MakeOrm().
  94. Bind(&req, binding.JSON).
  95. MakeService(&s.Service).
  96. Errors
  97. if err != nil {
  98. e.Logger.Error(err)
  99. e.Error(500, err, err.Error())
  100. return
  101. }
  102. req.SetCreateBy(user.GetUserId(c))
  103. err = s.Insert(&req)
  104. if err != nil {
  105. e.Error(500, err, err.Error())
  106. return
  107. }
  108. e.OK(req.GetId(), "创建成功")
  109. }
  110. // Update 修改字典数据
  111. // @Summary 修改字典数据
  112. // @Description 修改字典数据
  113. // @Tags 字典数据
  114. // @Accept application/json
  115. // @Product application/json
  116. // @Param dictCode path int true "字典编码"
  117. // @Param data body dto.SysDictDataUpdateReq true "body"
  118. // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
  119. // @Router /api/dict/data/{dictCode} [put]
  120. // @Security Bearer
  121. func (e SysDictDataController) Update(c *gin.Context) {
  122. s := service.SysDictDataService{}
  123. req := dto.SysDictDataUpdateReq{}
  124. err := e.MakeContext(c).
  125. MakeOrm().
  126. Bind(&req, binding.JSON, nil).
  127. MakeService(&s.Service).
  128. Errors
  129. if err != nil {
  130. e.Logger.Error(err)
  131. e.Error(500, err, err.Error())
  132. return
  133. }
  134. req.SetUpdateBy(user.GetUserId(c))
  135. err = s.Update(&req)
  136. if err != nil {
  137. e.Error(500, err, err.Error())
  138. return
  139. }
  140. e.OK(req.GetId(), "更新成功")
  141. }
  142. // Delete 删除字典数据
  143. // @Summary 删除字典数据
  144. // @Description 删除字典数据
  145. // @Tags 字典数据
  146. // @Param dictCode body dto.SysDictDataDeleteReq true "body"
  147. // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
  148. // @Router /api/dict/data [delete]
  149. // @Security Bearer
  150. func (e SysDictDataController) Delete(c *gin.Context) {
  151. s := service.SysDictDataService{}
  152. req := dto.SysDictDataDeleteReq{}
  153. err := e.MakeContext(c).
  154. MakeOrm().
  155. Bind(&req, binding.JSON, nil).
  156. MakeService(&s.Service).
  157. Errors
  158. if err != nil {
  159. e.Logger.Error(err)
  160. e.Error(500, err, err.Error())
  161. return
  162. }
  163. req.SetUpdateBy(user.GetUserId(c))
  164. err = s.Remove(&req)
  165. if err != nil {
  166. e.Error(500, err, err.Error())
  167. return
  168. }
  169. e.OK(req.GetId(), "删除成功")
  170. }
  171. // GetAll 通过key获取数据字典【业务页面使用】
  172. // @Summary 通过key获取数据字典【业务页面使用】
  173. // @Description 通过key获取数据字典【业务页面使用】
  174. // @Tags 字典数据
  175. // @Param dictType query string true "dictType"
  176. // @Success 200 {object} response.Response{data=[]dto.SysDictDataGetAllResp} "{"code": 200, "data": [...]}"
  177. // @Router /api/dict-data/option-select [get]
  178. // @Security Bearer
  179. func (e SysDictDataController) GetAll(c *gin.Context) {
  180. s := service.SysDictDataService{}
  181. req := dto.SysDictDataGetPageReq{}
  182. err := e.MakeContext(c).
  183. MakeOrm().
  184. Bind(&req, binding.Query).
  185. MakeService(&s.Service).
  186. Errors
  187. if err != nil {
  188. e.Logger.Error(err)
  189. e.Error(500, err, err.Error())
  190. return
  191. }
  192. list := make([]model.SysDictData, 0)
  193. err = s.GetAll(&req, &list)
  194. if err != nil {
  195. e.Error(500, err, err.Error())
  196. return
  197. }
  198. l := make([]dto.SysDictDataGetAllResp, 0)
  199. for _, i := range list {
  200. d := dto.SysDictDataGetAllResp{}
  201. e.Translate(i, &d)
  202. l = append(l, d)
  203. }
  204. e.OK(l, "查询成功")
  205. }