sys_dict_data.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package apis
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/gin-gonic/gin/binding"
  5. "github.com/go-admin-team/go-admin-core/sdk/api"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  7. _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  8. "go-admin/app/admin/models"
  9. "go-admin/app/admin/service"
  10. "go-admin/app/admin/service/dto"
  11. )
  12. type SysDictData struct {
  13. api.Api
  14. }
  15. // GetPage
  16. // @Summary 字典数据列表
  17. // @Description 获取JSON
  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 "{"code": 200, "data": [...]}"
  25. // @Router /api/v1/dict/data [get]
  26. // @Security Bearer
  27. func (e SysDictData) GetPage(c *gin.Context) {
  28. s := service.SysDictData{}
  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([]models.SysDictData, 0)
  41. var count int64
  42. err = s.GetPage(&req, &list, &count)
  43. if err != nil {
  44. e.Error(500, err, "查询失败")
  45. return
  46. }
  47. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  48. }
  49. // Get
  50. // @Summary 通过编码获取字典数据
  51. // @Description 获取JSON
  52. // @Tags 字典数据
  53. // @Param dictCode path int true "字典编码"
  54. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  55. // @Router /api/v1/dict/data/{dictCode} [get]
  56. // @Security Bearer
  57. func (e SysDictData) Get(c *gin.Context) {
  58. s := service.SysDictData{}
  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 models.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, "查询失败")
  75. return
  76. }
  77. e.OK(object, "查询成功")
  78. }
  79. // Insert
  80. // @Summary 添加字典数据
  81. // @Description 获取JSON
  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/v1/dict/data [post]
  88. // @Security Bearer
  89. func (e SysDictData) Insert(c *gin.Context) {
  90. s := service.SysDictData{}
  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, "创建失败")
  106. return
  107. }
  108. e.OK(req.GetId(), "创建成功")
  109. }
  110. // Update
  111. // @Summary 修改字典数据
  112. // @Description 获取JSON
  113. // @Tags 字典数据
  114. // @Accept application/json
  115. // @Product application/json
  116. // @Param data body dto.SysDictDataUpdateReq true "body"
  117. // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
  118. // @Router /api/v1/dict/data/{dictCode} [put]
  119. // @Security Bearer
  120. func (e SysDictData) Update(c *gin.Context) {
  121. s := service.SysDictData{}
  122. req := dto.SysDictDataUpdateReq{}
  123. err := e.MakeContext(c).
  124. MakeOrm().
  125. Bind(&req, binding.JSON, nil).
  126. MakeService(&s.Service).
  127. Errors
  128. if err != nil {
  129. e.Logger.Error(err)
  130. e.Error(500, err, err.Error())
  131. return
  132. }
  133. req.SetUpdateBy(user.GetUserId(c))
  134. err = s.Update(&req)
  135. if err != nil {
  136. e.Error(500, err, "更新失败")
  137. return
  138. }
  139. e.OK(req.GetId(), "更新成功")
  140. }
  141. // Delete
  142. // @Summary 删除字典数据
  143. // @Description 删除数据
  144. // @Tags 字典数据
  145. // @Param dictCode body dto.SysDictDataDeleteReq true "body"
  146. // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
  147. // @Router /api/v1/dict/data [delete]
  148. // @Security Bearer
  149. func (e SysDictData) Delete(c *gin.Context) {
  150. s := service.SysDictData{}
  151. req := dto.SysDictDataDeleteReq{}
  152. err := e.MakeContext(c).
  153. MakeOrm().
  154. Bind(&req, binding.JSON, nil).
  155. MakeService(&s.Service).
  156. Errors
  157. if err != nil {
  158. e.Logger.Error(err)
  159. e.Error(500, err, err.Error())
  160. return
  161. }
  162. req.SetUpdateBy(user.GetUserId(c))
  163. err = s.Remove(&req)
  164. if err != nil {
  165. e.Error(500, err, "删除失败")
  166. return
  167. }
  168. e.OK(req.GetId(), "删除成功")
  169. }
  170. // GetAll 数据字典根据key获取 业务页面使用
  171. // @Summary 数据字典根据key获取
  172. // @Description 数据字典根据key获取
  173. // @Tags 字典数据
  174. // @Param dictType query int true "dictType"
  175. // @Success 200 {object} response.Response{data=[]dto.SysDictDataGetAllResp} "{"code": 200, "data": [...]}"
  176. // @Router /api/v1/dict-data/option-select [get]
  177. // @Security Bearer
  178. func (e SysDictData) GetAll(c *gin.Context) {
  179. s := service.SysDictData{}
  180. req := dto.SysDictDataGetPageReq{}
  181. err := e.MakeContext(c).
  182. MakeOrm().
  183. Bind(&req).
  184. MakeService(&s.Service).
  185. Errors
  186. if err != nil {
  187. e.Logger.Error(err)
  188. e.Error(500, err, err.Error())
  189. return
  190. }
  191. list := make([]models.SysDictData, 0)
  192. err = s.GetAll(&req, &list)
  193. if err != nil {
  194. e.Error(500, err, "查询失败")
  195. return
  196. }
  197. l := make([]dto.SysDictDataGetAllResp, 0)
  198. for _, i := range list {
  199. d := dto.SysDictDataGetAllResp{}
  200. e.Translate(i, &d)
  201. l = append(l, d)
  202. }
  203. e.OK(l,"查询成功")
  204. }