tenantService.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/system/dao"
  5. "iot_manager_service/util/cache"
  6. "iot_manager_service/util/common"
  7. "time"
  8. )
  9. // 租户管理服务
  10. var TenantService = new(tenantService)
  11. type tenantService struct{}
  12. func (s *tenantService) GetTenantByPasskey(passKey string) (*dao.Tenant, error) {
  13. tenant := &dao.Tenant{
  14. PassKey: passKey,
  15. }
  16. err := tenant.GetTenantByPasskey()
  17. if err != nil {
  18. return nil, err
  19. }
  20. return tenant, nil
  21. }
  22. func (s *tenantService) GetOne(tenantId int) (*dao.Tenant, error) {
  23. tenant := &dao.Tenant{
  24. TenantId: tenantId,
  25. }
  26. err := tenant.GetTenant()
  27. if err != nil {
  28. return nil, err
  29. }
  30. return tenant, nil
  31. }
  32. func (s *tenantService) Get(id int64) (*dao.Tenant, error) {
  33. tenant := &dao.Tenant{
  34. ID: id,
  35. }
  36. err := tenant.Get()
  37. if err != nil {
  38. return nil, err
  39. }
  40. return tenant, nil
  41. }
  42. func (s *tenantService) GetByDomain(domain string) (*dao.Tenant, error) {
  43. tenant := &dao.Tenant{
  44. Domain: domain,
  45. }
  46. err := tenant.GetByDomain()
  47. if err != nil {
  48. return nil, err
  49. }
  50. return tenant, nil
  51. }
  52. func (s *tenantService) List(tenantId int, tenantName, linkman string, current, size int) ([]dao.Tenant, error) {
  53. tenant := &dao.Tenant{}
  54. offset := (current - 1) * size
  55. limit := size
  56. if tenantId > 0 {
  57. tenant.TenantId = tenantId
  58. }
  59. if tenantName != "" {
  60. tenant.TenantName = tenantName
  61. }
  62. if linkman != "" {
  63. tenant.Linkman = linkman
  64. }
  65. tenants, err := tenant.GetTenants(offset, limit)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return tenants, nil
  70. }
  71. func (s *tenantService) Submit(req dao.Tenant) *common.Errors {
  72. tenant := &req
  73. if tenant.ID == 0 {
  74. tenant.PassKey = common.RandomString(8)
  75. if tenant.IsExist() {
  76. tenant.PassKey = common.RandomString(8)
  77. }
  78. err := tenant.Save()
  79. if err != nil {
  80. return common.FailResponse(err.Error(), nil)
  81. }
  82. return common.SuccessResponse(common.Succeeded, nil)
  83. }
  84. err := tenant.Update()
  85. if err != nil {
  86. return common.FailResponse(err.Error(), nil)
  87. }
  88. return common.SuccessResponse(common.Succeeded, nil)
  89. }
  90. func (s *tenantService) GetList() ([]dao.Tenant, error) {
  91. tenant := &dao.Tenant{}
  92. tenants, err := tenant.GetAll()
  93. if err != nil {
  94. return nil, err
  95. }
  96. return tenants, nil
  97. }
  98. func (s *tenantService) Remove(userId, id int64) *common.Errors {
  99. tenant := &dao.Tenant{
  100. ID: id,
  101. IsDeleted: 1,
  102. UpdateUser: userId,
  103. UpdateTime: time.Now(),
  104. }
  105. err := tenant.Remove()
  106. if err != nil {
  107. return common.FailResponse(err.Error(), nil)
  108. }
  109. return nil
  110. }
  111. const TenantNameKey = "%d:sys:tenant_name:%d"
  112. func (s *tenantService) GetTenantName(tenantId int, id int) string {
  113. var name string
  114. key := fmt.Sprintf(TenantNameKey, tenantId, id)
  115. name, err := cache.Redis.Get(key).Result()
  116. if err != nil {
  117. tenant := &dao.Tenant{TenantId: id}
  118. if err := tenant.GetTenant(); err != nil {
  119. fmt.Printf("GetTenant err = %s \n", err)
  120. } else {
  121. name = tenant.TenantName
  122. cache.Redis.Set(key, name, -1)
  123. }
  124. }
  125. return name
  126. }