tenant.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Tenant = new(tenant)
  13. type tenant struct{}
  14. func (c *tenant) GetInfo(ctx *gin.Context) {
  15. domain := ctx.Query("domain")
  16. tenant, _ := service.TenantService.GetByDomain(domain)
  17. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, tenant))
  18. }
  19. func (c *tenant) GetDetail(ctx *gin.Context) {
  20. id := ctx.Query("id")
  21. iId, err := strconv.ParseInt(id, 10, 64)
  22. if err != nil {
  23. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  24. return
  25. }
  26. tenant, err := service.TenantService.Get(iId)
  27. if err != nil {
  28. ctx.JSON(http.StatusOK, err)
  29. return
  30. }
  31. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, tenant))
  32. }
  33. func (c *tenant) List(ctx *gin.Context) {
  34. current, _ := strconv.Atoi(ctx.Query("current"))
  35. size, _ := strconv.Atoi(ctx.Query("size"))
  36. tenantId := ctx.Query("tenantId")
  37. tenantName := ctx.Query("tenantName")
  38. linkman := ctx.Query("linkman")
  39. if current == 0 {
  40. current = 1
  41. }
  42. if size <= 0 || size > 100 {
  43. size = 10
  44. }
  45. tId, err := strconv.Atoi(tenantId)
  46. if err != nil {
  47. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  48. return
  49. }
  50. tenants, err := service.TenantService.List(tId, tenantName, linkman, current, size)
  51. if err != nil {
  52. ctx.JSON(http.StatusOK, err)
  53. return
  54. }
  55. pages := math.Ceil(float64(len(tenants)) / float64(size))
  56. rsp := model.RsqTenantList{
  57. Current: current,
  58. Size: size,
  59. Total: len(tenants),
  60. Pages: int(pages),
  61. Records: tenants,
  62. }
  63. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
  64. }
  65. func (c *tenant) Submit(ctx *gin.Context) {
  66. var req dao.Tenant
  67. if err := ctx.ShouldBindJSON(&req); err != nil {
  68. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  69. return
  70. }
  71. err := service.TenantService.Submit(req)
  72. ctx.JSON(http.StatusOK, err)
  73. }
  74. func (c *tenant) Remove(ctx *gin.Context) {
  75. var req *model.ReqTenantRemove
  76. if err := ctx.ShouldBindJSON(&req); err != nil {
  77. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  78. return
  79. }
  80. err := service.TenantService.Remove(req.IDs)
  81. ctx.JSON(http.StatusOK, err)
  82. }
  83. func (c *tenant) GetList(ctx *gin.Context) {
  84. tenants, err := service.TenantService.GetList()
  85. if err != nil {
  86. ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
  87. }
  88. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, tenants))
  89. }