dictdata.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package dict
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/gin-gonic/gin/binding"
  6. "device-manage/app/admin/models"
  7. "device-manage/tools"
  8. "device-manage/tools/app"
  9. )
  10. // @Summary 字典数据列表
  11. // @Description 获取JSON
  12. // @Tags 字典数据
  13. // @Param status query string false "status"
  14. // @Param dictCode query string false "dictCode"
  15. // @Param dictType query string false "dictType"
  16. // @Param pageSize query int false "页条数"
  17. // @Param pageIndex query int false "页码"
  18. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  19. // @Router /api/v1/dict/data/list [get]
  20. // @Security Bearer
  21. func GetDictDataList(c *gin.Context) {
  22. var data models.DictData
  23. var err error
  24. var pageSize = 10
  25. var pageIndex = 1
  26. if size := c.Request.FormValue("pageSize"); size != "" {
  27. pageSize, err = tools.StringToInt(size)
  28. }
  29. if index := c.Request.FormValue("pageIndex"); index != "" {
  30. pageIndex, err = tools.StringToInt(index)
  31. }
  32. data.DictLabel = c.Request.FormValue("dictLabel")
  33. data.Status = c.Request.FormValue("status")
  34. data.DictType = c.Request.FormValue("dictType")
  35. id := c.Request.FormValue("dictCode")
  36. data.DictCode, _ = tools.StringToInt(id)
  37. data.DataScope = tools.GetUserIdStr(c)
  38. result, count, err := data.GetPage(pageSize, pageIndex)
  39. tools.HasError(err, "", -1)
  40. var mp = make(map[string]interface{}, 3)
  41. mp["list"] = result
  42. mp["count"] = count
  43. mp["pageIndex"] = pageIndex
  44. mp["pageSize"] = pageSize
  45. var res app.Response
  46. res.Data = mp
  47. c.JSON(http.StatusOK, res.ReturnOK())
  48. }
  49. // @Summary 通过编码获取字典数据
  50. // @Description 获取JSON
  51. // @Tags 字典数据
  52. // @Param dictCode path int true "字典编码"
  53. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  54. // @Router /api/v1/dict/data/{dictCode} [get]
  55. // @Security Bearer
  56. func GetDictData(c *gin.Context) {
  57. var DictData models.DictData
  58. DictData.DictLabel = c.Request.FormValue("dictLabel")
  59. DictData.DictCode, _ = tools.StringToInt(c.Param("dictCode"))
  60. result, err := DictData.GetByCode()
  61. tools.HasError(err, "抱歉未找到相关信息", -1)
  62. var res app.Response
  63. res.Data = result
  64. c.JSON(http.StatusOK, res.ReturnOK())
  65. }
  66. // @Summary 通过字典类型获取字典数据
  67. // @Description 获取JSON
  68. // @Tags 字典数据
  69. // @Param dictType path int true "dictType"
  70. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  71. // @Router /api/v1/dict/databyType/{dictType} [get]
  72. // @Security Bearer
  73. func GetDictDataByDictType(c *gin.Context) {
  74. var DictData models.DictData
  75. DictData.DictType = c.Param("dictType")
  76. result, err := DictData.Get()
  77. tools.HasError(err, "抱歉未找到相关信息", -1)
  78. var res app.Response
  79. res.Data = result
  80. c.JSON(http.StatusOK, res.ReturnOK())
  81. }
  82. // @Summary 添加字典数据
  83. // @Description 获取JSON
  84. // @Tags 字典数据
  85. // @Accept application/json
  86. // @Product application/json
  87. // @Param data body models.DictType true "data"
  88. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  89. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  90. // @Router /api/v1/dict/data [post]
  91. // @Security Bearer
  92. func InsertDictData(c *gin.Context) {
  93. var data models.DictData
  94. err := c.ShouldBindJSON(&data)
  95. data.CreateBy = tools.GetUserIdStr(c)
  96. tools.HasError(err, "", 500)
  97. result, err := data.Create()
  98. tools.HasError(err, "", -1)
  99. var res app.Response
  100. res.Data = result
  101. c.JSON(http.StatusOK, res.ReturnOK())
  102. }
  103. // @Summary 修改字典数据
  104. // @Description 获取JSON
  105. // @Tags 字典数据
  106. // @Accept application/json
  107. // @Product application/json
  108. // @Param data body models.DictType true "body"
  109. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  110. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  111. // @Router /api/v1/dict/data [put]
  112. // @Security Bearer
  113. func UpdateDictData(c *gin.Context) {
  114. var data models.DictData
  115. err := c.BindWith(&data, binding.JSON)
  116. data.UpdateBy = tools.GetUserIdStr(c)
  117. tools.HasError(err, "", -1)
  118. result, err := data.Update(data.DictCode)
  119. tools.HasError(err, "", -1)
  120. var res app.Response
  121. res.Data = result
  122. c.JSON(http.StatusOK, res.ReturnOK())
  123. }
  124. // @Summary 删除字典数据
  125. // @Description 删除数据
  126. // @Tags 字典数据
  127. // @Param dictCode path int true "dictCode"
  128. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  129. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  130. // @Router /api/v1/dict/data/{dictCode} [delete]
  131. func DeleteDictData(c *gin.Context) {
  132. var data models.DictData
  133. data.UpdateBy = tools.GetUserIdStr(c)
  134. IDS := tools.IdsStrToIdsIntGroup("dictCode", c)
  135. result, err := data.BatchDelete(IDS)
  136. tools.HasError(err, "修改失败", 500)
  137. app.OK(c, result, "删除成功")
  138. }