tenant.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/middleware"
  5. "iot_manager_service/app/system/dao"
  6. "iot_manager_service/app/system/model"
  7. "iot_manager_service/app/system/service"
  8. "iot_manager_service/util/common"
  9. "math"
  10. "net/http"
  11. "strconv"
  12. )
  13. var Tenant = new(tenant)
  14. type tenant struct{}
  15. func (c *tenant) GetInfo(ctx *gin.Context) {
  16. domain := ctx.Query("domain")
  17. tenant, _ := service.TenantService.GetByDomain(domain)
  18. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, tenant))
  19. }
  20. func (c *tenant) GetDetail(ctx *gin.Context) {
  21. id := ctx.Query("id")
  22. iId, err := strconv.Atoi(id)
  23. if err != nil {
  24. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  25. return
  26. }
  27. tenant, err := service.TenantService.Get(iId)
  28. if err != nil {
  29. ctx.JSON(http.StatusOK, err)
  30. return
  31. }
  32. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, tenant))
  33. }
  34. func (c *tenant) List(ctx *gin.Context) {
  35. current, _ := strconv.Atoi(ctx.Query("current"))
  36. size, _ := strconv.Atoi(ctx.Query("size"))
  37. tId := ctx.Query("tenantId")
  38. tenantName := ctx.Query("tenantName")
  39. linkman := ctx.Query("linkman")
  40. if current == 0 {
  41. current = 1
  42. }
  43. if size <= 0 || size > 100 {
  44. size = 10
  45. }
  46. tenantId := ""
  47. if tId != "" {
  48. tenantId = (tId)
  49. }
  50. tenants, counts, err := service.TenantService.List(tenantId, tenantName, linkman, current, size)
  51. if err != nil {
  52. ctx.JSON(http.StatusOK, err)
  53. return
  54. }
  55. pages := math.Ceil(float64(counts) / float64(size))
  56. rsp := model.RsqTenantList{
  57. Current: current,
  58. Size: size,
  59. Total: counts,
  60. Pages: int(pages),
  61. Records: tenants,
  62. }
  63. ctx.JSON(http.StatusOK, common.SuccessResponse(common.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, common.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. value, _ := ctx.Get(middleware.Authorization)
  76. claims := value.(*middleware.Claims)
  77. var req *model.ReqTenantRemove
  78. if err := ctx.ShouldBindJSON(&req); err != nil {
  79. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  80. return
  81. }
  82. err := service.TenantService.Remove(claims.UserId, req.IDs)
  83. ctx.JSON(http.StatusOK, err)
  84. }
  85. func (c *tenant) GetList(ctx *gin.Context) {
  86. tenants, err := service.TenantService.GetList()
  87. if err != nil {
  88. ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
  89. }
  90. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, tenants))
  91. }