cooler_box.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package controller
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/app/admin/service"
  5. "cold-delivery/app/admin/service/dto"
  6. "cold-delivery/common/actions"
  7. "github.com/gin-gonic/gin"
  8. "github.com/gin-gonic/gin/binding"
  9. "gogs.baozhida.cn/zoie/OAuth-core/api"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  11. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  12. )
  13. type CoolerBoxController struct {
  14. api.Api
  15. }
  16. // GetPage 获取保温箱列表
  17. // @Summary 获取保温箱列表
  18. // @Description 获取保温箱列表
  19. // @Tags 保温箱
  20. // @Param name query string false "保温箱名称"
  21. // @Param sn query string false "保温箱关联sn"
  22. // @Param status query string false "状态:1-停用 2-启用"
  23. // @Param showTemp query bool false "是否展示最新温度"
  24. // @Param pageSize query int false "页条数"
  25. // @Param page query int false "页码"
  26. // @Success 200 {object} response.Response{data=response.Page{list=[]model.CoolerBox}} "{"code": 200, "data": [...]}"
  27. // @Router /api/cooler-box [get]
  28. // @Security Bearer
  29. func (e CoolerBoxController) GetPage(c *gin.Context) {
  30. s := service.CoolerBox{}
  31. req := dto.CoolerBoxGetPageReq{}
  32. err := e.MakeContext(c).
  33. MakeOrm().
  34. Bind(&req, binding.Query).
  35. MakeService(&s.Service).
  36. Errors
  37. if err != nil {
  38. e.Logger.Error(err)
  39. e.Error(500, err, err.Error())
  40. return
  41. }
  42. //数据权限检查
  43. p := actions.GetPermissionFromContext(c)
  44. list := make([]model.CoolerBox, 0)
  45. var count int64
  46. err = s.GetPage(&req, &list, &count, p)
  47. if err != nil {
  48. e.Error(500, err, err.Error())
  49. return
  50. }
  51. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  52. }
  53. // Get 通过id获取保温箱
  54. // @Summary 通过id获取保温箱
  55. // @Description 通过id获取保温箱
  56. // @Tags 保温箱
  57. // @Param id path string true "保温箱id"
  58. // @Success 200 {object} response.Response{data=model.CoolerBox} "{"code": 200, "data": [...]}"
  59. // @Router /api/cooler-box/{id} [get]
  60. // @Security Bearer
  61. func (e CoolerBoxController) Get(c *gin.Context) {
  62. s := service.CoolerBox{}
  63. req := dto.CoolerBoxGetReq{}
  64. err := e.MakeContext(c).
  65. MakeOrm().
  66. Bind(&req, nil).
  67. MakeService(&s.Service).
  68. Errors
  69. if err != nil {
  70. e.Logger.Error(err)
  71. e.Error(500, err, err.Error())
  72. return
  73. }
  74. var object model.CoolerBox
  75. p := actions.GetPermissionFromContext(c)
  76. //数据权限检查
  77. err = s.Get(&req, &object, p)
  78. if err != nil {
  79. e.Error(500, err, err.Error())
  80. return
  81. }
  82. e.OK(object, "查询成功")
  83. }
  84. // Insert 添加保温箱
  85. // @Summary 添加保温箱
  86. // @Description 添加保温箱
  87. // @Tags 保温箱
  88. // @Accept application/json
  89. // @Product application/json
  90. // @Param data body dto.CoolerBoxInsertReq true "data"
  91. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  92. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  93. // @Router /api/cooler-box [post]
  94. // @Security Bearer
  95. func (e CoolerBoxController) Insert(c *gin.Context) {
  96. s := service.CoolerBox{}
  97. req := dto.CoolerBoxInsertReq{}
  98. err := e.MakeContext(c).
  99. MakeOrm().
  100. Bind(&req, binding.JSON).
  101. MakeService(&s.Service).
  102. Errors
  103. if err != nil {
  104. e.Logger.Error(err)
  105. e.Error(500, err, err.Error())
  106. return
  107. }
  108. p := actions.GetPermissionFromContext(c)
  109. // 设置创建人
  110. req.SetCreateBy(user.GetUserId(c))
  111. req.SetDeptId(p.DeptId)
  112. err = s.Insert(&req, p)
  113. if err != nil {
  114. e.Error(500, err, err.Error())
  115. return
  116. }
  117. e.OK(req.GetId(), "添加成功")
  118. }
  119. // Update 修改保温箱
  120. // @Summary 修改保温箱
  121. // @Description 修改保温箱
  122. // @Tags 保温箱
  123. // @Accept application/json
  124. // @Product application/json
  125. // @Param data body dto.CoolerBoxUpdateReq true "body"
  126. // @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
  127. // @Success 200 {string} string "{"code": -1, "message": "修改失败"}"
  128. // @Router /api/cooler-box [put]
  129. // @Security Bearer
  130. func (e CoolerBoxController) Update(c *gin.Context) {
  131. s := service.CoolerBox{}
  132. req := dto.CoolerBoxUpdateReq{}
  133. err := e.MakeContext(c).
  134. MakeOrm().
  135. Bind(&req).
  136. MakeService(&s.Service).
  137. Errors
  138. if err != nil {
  139. e.Logger.Error(err)
  140. e.Error(500, err, err.Error())
  141. return
  142. }
  143. p := actions.GetPermissionFromContext(c)
  144. req.SetUpdateBy(user.GetUserId(c))
  145. err = s.Update(&req, p)
  146. if err != nil {
  147. e.Error(500, err, err.Error())
  148. return
  149. }
  150. e.OK(req.GetId(), "修改成功")
  151. }
  152. // Delete 删除保温箱
  153. // @Summary 删除保温箱
  154. // @Description 删除保温箱
  155. // @Tags 保温箱
  156. // @Accept application/json
  157. // @Product application/json
  158. // @Param data body dto.CoolerBoxDeleteReq true "body"
  159. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  160. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  161. // @Router /api/cooler-box [delete]
  162. // @Security Bearer
  163. func (e CoolerBoxController) Delete(c *gin.Context) {
  164. s := service.CoolerBox{}
  165. req := dto.CoolerBoxDeleteReq{}
  166. err := e.MakeContext(c).
  167. MakeOrm().
  168. Bind(&req, binding.JSON, nil).
  169. MakeService(&s.Service).
  170. Errors
  171. if err != nil {
  172. e.Logger.Error(err)
  173. e.Error(500, err, err.Error())
  174. return
  175. }
  176. //数据权限检查
  177. p := actions.GetPermissionFromContext(c)
  178. err = s.Remove(&req, p)
  179. if err != nil {
  180. e.Error(500, err, err.Error())
  181. return
  182. }
  183. e.OK(req.GetId(), "删除成功")
  184. }
  185. // BatchInsert 批量添加保温箱
  186. // @Summary 批量添加保温箱
  187. // @Description 批量添加保温箱
  188. // @Tags 保温箱
  189. // @Accept application/json
  190. // @Product application/json
  191. // @Param data body dto.CoolerBoxBatchInsertReq true "data"
  192. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  193. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  194. // @Router /api/cooler-box/import [post]
  195. // @Security Bearer
  196. func (e CoolerBoxController) BatchInsert(c *gin.Context) {
  197. s := service.CoolerBox{}
  198. req := dto.CoolerBoxBatchInsertReq{}
  199. err := e.MakeContext(c).
  200. MakeOrm().
  201. Bind(&req, binding.JSON).
  202. MakeService(&s.Service).
  203. Errors
  204. if err != nil {
  205. e.Logger.Error(err)
  206. e.Error(500, err, err.Error())
  207. return
  208. }
  209. p := actions.GetPermissionFromContext(c)
  210. // 设置创建人
  211. req.SetCreateBy(user.GetUserId(c))
  212. req.SetDeptId(p.DeptId)
  213. err = s.BatchInsert(&req)
  214. if err != nil {
  215. e.Error(500, err, err.Error())
  216. return
  217. }
  218. e.OK(nil, "添加成功")
  219. }