redis.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package util
  2. import (
  3. "fmt"
  4. "github.com/go-redis/redis"
  5. device "iot_manager_service/app/device/dao"
  6. system "iot_manager_service/app/system/dao"
  7. "iot_manager_service/config"
  8. "strconv"
  9. "time"
  10. )
  11. var Redis *redis.Client
  12. func InitRedis() error {
  13. cfg := config.Instance()
  14. addr := cfg.Redis.Host
  15. Redis = redis.NewClient(&redis.Options{
  16. Addr: addr,
  17. DialTimeout: 10 * time.Second,
  18. ReadTimeout: 30 * time.Second,
  19. WriteTimeout: 30 * time.Second,
  20. PoolSize: 10,
  21. PoolTimeout: 30 * time.Second,
  22. Password: cfg.Redis.Password,
  23. })
  24. _, err := Redis.Ping().Result()
  25. if err != nil {
  26. return err
  27. }
  28. return nil
  29. }
  30. const (
  31. UserKey = "%d:sys:user:%d"
  32. ModuleTypeKey = "%d:dict:module_type:%d"
  33. ControlTypeKey = "%d:dict:control_type:%d"
  34. TenantNameKey = "%d:sys:tenant_name:%d"
  35. RoleNameKey = "%d:sys:role_name:%d"
  36. SexNameKey = "%d:sys:sex_name:%d"
  37. DeviceStateKey = "dev_state:%s"
  38. ControlType = "control_type"
  39. ModuleType = "module_his_type"
  40. Sex = "sex"
  41. )
  42. func GetUserName(tenantId int, id int64) string {
  43. var name string
  44. key := fmt.Sprintf(UserKey, tenantId, id)
  45. name, err := Redis.Get(key).Result()
  46. if err != nil {
  47. user := &system.User{ID: id}
  48. if err := user.GetUser(); err != nil {
  49. fmt.Printf("GetUser err = %s \n", err)
  50. } else {
  51. name = user.Name
  52. Redis.Set(key, name, time.Second*10)
  53. }
  54. }
  55. return name
  56. }
  57. func GetModuleName(tenantId int, id int) string {
  58. var name string
  59. key := fmt.Sprintf(ModuleTypeKey, tenantId, id)
  60. name, err := Redis.Get(key).Result()
  61. if err != nil {
  62. dict := &device.Dict{Code: ModuleType, DictKey: strconv.Itoa(id)}
  63. if err := dict.GetByCodeAndKey(); err != nil {
  64. fmt.Printf("GetByCodeAndKey err = %d \n", err)
  65. } else {
  66. name = dict.DictValue
  67. Redis.Set(key, name, -1)
  68. }
  69. }
  70. return name
  71. }
  72. func GetControlType(tenantId int, id int) string {
  73. var name string
  74. key := fmt.Sprintf(ControlTypeKey, tenantId, id)
  75. name, err := Redis.Get(key).Result()
  76. if err != nil {
  77. dict := &device.Dict{Code: ControlType, DictKey: strconv.Itoa(id)}
  78. if err := dict.GetByCodeAndKey(); err != nil {
  79. fmt.Printf("GetByCodeAndKey err = %d \n", err)
  80. } else {
  81. name = dict.DictValue
  82. Redis.Set(key, name, -1)
  83. }
  84. }
  85. return name
  86. }
  87. func GetSexName(tenantId int, id int) string {
  88. var name string
  89. key := fmt.Sprintf(SexNameKey, tenantId, id)
  90. name, err := Redis.Get(key).Result()
  91. if err != nil {
  92. dict := &device.Dict{Code: Sex, DictKey: strconv.Itoa(id)}
  93. if err := dict.GetByCodeAndKey(); err != nil {
  94. fmt.Printf("GetByCodeAndKey err = %d \n", err)
  95. } else {
  96. name = dict.DictValue
  97. Redis.Set(key, name, -1)
  98. }
  99. }
  100. return name
  101. }
  102. func GetTenantName(tenantId int, id int) string {
  103. var name string
  104. key := fmt.Sprintf(TenantNameKey, tenantId, id)
  105. name, err := Redis.Get(key).Result()
  106. if err != nil {
  107. tenant := &system.Tenant{TenantId: id}
  108. if err := tenant.GetTenant(); err != nil {
  109. fmt.Printf("GetTenant err = %s \n", err)
  110. } else {
  111. name = tenant.TenantName
  112. Redis.Set(key, name, -1)
  113. }
  114. }
  115. return name
  116. }
  117. func GetRoleName(tenantId int, roleId int64) string {
  118. var name string
  119. key := fmt.Sprintf(RoleNameKey, tenantId, roleId)
  120. name, err := Redis.Get(key).Result()
  121. if err != nil {
  122. role := &system.Role{ID: roleId}
  123. if err := role.GetRole(); err != nil {
  124. fmt.Printf("GetRole err = %s \n", err)
  125. } else {
  126. name = role.RoleName
  127. Redis.Set(key, name, -1)
  128. }
  129. }
  130. return name
  131. }
  132. func GetDeviceState(sn string) int {
  133. key := fmt.Sprintf(DeviceStateKey, sn)
  134. value, err := Redis.Get(key).Int()
  135. state := 2
  136. if err != nil {
  137. fmt.Printf("GetDeviceState err = %s \n", err)
  138. return state
  139. }
  140. if value == 1 {
  141. state = 1
  142. }
  143. return state
  144. }