tenantService.go 3.0 KB

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