user.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/system/dao"
  5. "iot_manager_service/app/system/model"
  6. "iot_manager_service/app/system/service"
  7. "iot_manager_service/util"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. )
  12. var User = new(user)
  13. type user struct{}
  14. func (c *user) GetDetail(ctx *gin.Context) {
  15. id := ctx.Query("id")
  16. iId, err := strconv.ParseInt(id, 10, 64)
  17. if err != nil {
  18. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  19. return
  20. }
  21. detail, err := service.UserService.Get(iId)
  22. if err != nil {
  23. ctx.JSON(http.StatusOK, err)
  24. return
  25. }
  26. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, detail))
  27. }
  28. func (c *user) List(ctx *gin.Context) {
  29. current, _ := strconv.Atoi(ctx.Query("current"))
  30. size, _ := strconv.Atoi(ctx.Query("size"))
  31. account := ctx.Query("account")
  32. realName := ctx.Query("realName")
  33. if current == 0 {
  34. current = 1
  35. }
  36. if size <= 0 || size > 100 {
  37. size = 10
  38. }
  39. users, err := service.UserService.List(account, realName, current, size)
  40. if err != nil {
  41. ctx.JSON(http.StatusOK, err)
  42. return
  43. }
  44. pages := math.Ceil(float64(len(users)) / float64(size))
  45. rsp := model.RsqUserList{
  46. Current: current,
  47. Size: size,
  48. Total: len(users),
  49. Pages: int(pages),
  50. Records: users,
  51. }
  52. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
  53. }
  54. func (c *user) Submit(ctx *gin.Context) {
  55. var req dao.User
  56. if err := ctx.ShouldBindJSON(&req); err != nil {
  57. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  58. return
  59. }
  60. err := service.UserService.Submit(req)
  61. ctx.JSON(http.StatusOK, err)
  62. }
  63. func (c *user) Update(ctx *gin.Context) {
  64. var req dao.User
  65. if err := ctx.ShouldBindJSON(&req); err != nil {
  66. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  67. return
  68. }
  69. err := service.UserService.Update(req)
  70. ctx.JSON(http.StatusOK, err)
  71. }
  72. func (c *user) Remove(ctx *gin.Context) {
  73. id, _ := strconv.ParseInt(ctx.Query("id"), 10, 64)
  74. err := service.UserService.Remove(id)
  75. ctx.JSON(http.StatusOK, err)
  76. }