common.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package common
  2. import (
  3. "cp-organization-management/errors"
  4. "cp-organization-management/model"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/jaryhe/gopkgs/cache"
  8. "github.com/jaryhe/gopkgs/database"
  9. "github.com/jinzhu/gorm"
  10. "go.uber.org/zap"
  11. "strconv"
  12. "github.com/jaryhe/gopkgs/logger"
  13. )
  14. const (
  15. USERBASEINFOPREFIX = "user_base_info"
  16. USERZONEPREFIX = "user_zone"
  17. SUPERGROUPPREFIX = "super_group_id"
  18. AllZones = "all_zones"
  19. AllZoneMap = "all_zone_map"
  20. AllZoneLevel = "all_zone_level"
  21. )
  22. func GetAllZone(dbname string) ([]model.Zone, error) {
  23. str, _ := cache.Redis().Get(AllZones)
  24. if str != "" {
  25. list := []model.Zone{}
  26. json.Unmarshal([]byte(str), &list)
  27. return list, nil
  28. }
  29. zone := model.NewZone(dbname)
  30. list := []model.Zone{}
  31. err := zone.QueryAll(database.DB(), nil, &list)
  32. if err != nil {
  33. return nil, errors.DataBaseError
  34. }
  35. bytes, _ := json.Marshal(list)
  36. cache.Redis().SetEx(AllZones, 3600, string(bytes))
  37. return list, nil
  38. }
  39. func GetAllZoneMap(dbname string) (map[string]string, error) {
  40. str, _ := cache.Redis().Get(AllZoneMap)
  41. ret := map[string]string{}
  42. if str != "" {
  43. json.Unmarshal([]byte(str), &ret)
  44. return ret, nil
  45. }
  46. list, err := GetAllZone(dbname)
  47. if err != nil {
  48. return nil, err
  49. }
  50. for _, v := range list {
  51. ret[v.ZoneCode] = v.ParentZoneCode
  52. }
  53. bytes, _ := json.Marshal(ret)
  54. cache.Redis().SetEx(AllZoneMap, 3600, string(bytes))
  55. return ret, nil
  56. }
  57. func GetAllZoneLevl(dbname string) (map[string]int, error) {
  58. str, _ := cache.Redis().Get(AllZoneLevel)
  59. ret := map[string]int{}
  60. if str != "" {
  61. json.Unmarshal([]byte(str), &ret)
  62. return ret, nil
  63. }
  64. list, err := GetAllZone(dbname)
  65. if err != nil {
  66. return nil, err
  67. }
  68. for _, v := range list {
  69. ret[v.ZoneCode] = v.Level
  70. }
  71. bytes, _ := json.Marshal(ret)
  72. cache.Redis().SetEx(AllZoneLevel, 3600, string(bytes))
  73. return ret, nil
  74. }
  75. func DelAllZone() error {
  76. _, err := cache.Redis().Del(AllZones)
  77. if err != nil {
  78. return errors.RedisError
  79. }
  80. _, err = cache.Redis().Del(AllZoneMap)
  81. if err != nil {
  82. return errors.RedisError
  83. }
  84. _, err = cache.Redis().Del(AllZoneLevel)
  85. if err != nil {
  86. return errors.RedisError
  87. }
  88. return nil
  89. }
  90. func GetAllZoneCodeName(dbname string) (map[string]string, error) {
  91. list, err := GetAllZone(dbname)
  92. if err != nil {
  93. return nil, err
  94. }
  95. ret := map[string]string{}
  96. for _, v := range list {
  97. ret[v.ZoneCode] = v.ZoneName
  98. }
  99. return ret, nil
  100. }
  101. func GetSuperGroup(dbname string) (int64, error) {
  102. str, _ := cache.Redis().Get(fmt.Sprintf("%s%s", dbname, SUPERGROUPPREFIX))
  103. ret,_ := strconv.ParseInt(str, 10, 64)
  104. if ret > 0 {
  105. return ret, nil
  106. }
  107. group := model.NewRbacGroup(dbname)
  108. where := map[string]interface{}{
  109. "is_super_group":true,
  110. }
  111. err := group.Find(database.DB(), where)
  112. if err != nil {
  113. if err == gorm.ErrRecordNotFound {
  114. return 0, errors.UserGroupNotExist
  115. }
  116. return 0, errors.DataBaseError
  117. }
  118. PutSuperGroup(dbname, group.Id)
  119. return group.Id, nil
  120. }
  121. func PutSuperGroup(dbname string, id int64) error {
  122. _, err := cache.Redis().Set(fmt.Sprintf("%s%s", dbname, SUPERGROUPPREFIX), fmt.Sprintf("%d", id))
  123. if err != nil {
  124. logger.Error("Redis",
  125. zap.String("func", "PutSuperGroup"),
  126. zap.String("call", "Set"),
  127. zap.String("key", fmt.Sprintf("%s%s", dbname, SUPERGROUPPREFIX)),
  128. zap.String("error", err.Error()))
  129. return errors.RedisError
  130. }
  131. return nil
  132. }
  133. func GetUserBaseInfo(id int64, dbname string) (*model.RbacUser, error) {
  134. str, _ := cache.Redis().Get(fmt.Sprintf("%s%s%d", dbname, USERBASEINFOPREFIX, id))
  135. if str != "" {
  136. user := model.RbacUser{}
  137. json.Unmarshal([]byte(str), &user)
  138. return &user, nil
  139. }
  140. user := model.NewRbacUser(dbname)
  141. where := map[string]interface{}{
  142. "id":id,
  143. }
  144. err := user.Find(database.DB(), where)
  145. if err != nil {
  146. if err == gorm.ErrRecordNotFound {
  147. return user, errors.UserNotExist
  148. }
  149. return user, errors.DataBaseError
  150. }
  151. PutUserBaseInfo(user, dbname)
  152. return user, nil
  153. }
  154. func PutUserBaseInfo(user *model.RbacUser, dbname string) error {
  155. bytes, _ := json.Marshal(user)
  156. _, err := cache.Redis().SetEx(fmt.Sprintf("%s%s%d", dbname, USERBASEINFOPREFIX, user.Id), 3600, string(bytes))
  157. if err != nil {
  158. logger.Error("Redis",
  159. zap.String("func", "PutUserBaseInfo"),
  160. zap.String("call", "Set"),
  161. zap.String("key", fmt.Sprintf("%s%s%d", dbname, USERBASEINFOPREFIX, user.Id)),
  162. zap.String("error", err.Error()))
  163. return errors.RedisError
  164. }
  165. return nil
  166. }
  167. func getUserZoneFromDb(uid int64, dbname string)(bool, map[string]string, error) {
  168. // is supper
  169. user := model.NewRbacUser(dbname)
  170. where := map[string]interface{}{
  171. "id":uid,
  172. }
  173. err := user.Find(database.DB(), where)
  174. if err != nil {
  175. if err == gorm.ErrRecordNotFound {
  176. return false, nil, errors.ErrRecordNotFound
  177. }
  178. return false, nil, errors.DataBaseError
  179. }
  180. if user.IsSuperUser {
  181. PutUserZone(uid, true, nil, dbname)
  182. return true, nil, nil
  183. }
  184. userZone := model.NewUserZone(dbname)
  185. where = map[string]interface{}{
  186. "user_id":uid,
  187. }
  188. list := []model.UserZone{}
  189. err = userZone.QueryAll(database.DB(), where, &list)
  190. if err != nil {
  191. if err == gorm.ErrRecordNotFound {
  192. return false, nil, nil
  193. }
  194. return false, nil, errors.DataBaseError
  195. }
  196. m := map[string]string{}
  197. for _, v := range list {
  198. m[v.ZoneCode] = ""
  199. }
  200. PutUserZone(uid, false, m, dbname)
  201. return false, m, nil
  202. }
  203. func GetUserZone(uid int64, dbname string)(bool, map[string]string, error) {
  204. ret, _ := cache.Redis().Get(fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, uid))
  205. if ret == "" {
  206. return getUserZoneFromDb(uid, dbname)
  207. }
  208. if ret == "super" {
  209. return true, nil, nil
  210. }
  211. m := map[string]string{}
  212. json.Unmarshal([]byte(ret), &m)
  213. return false, m, nil
  214. }
  215. func GetUserTopSubZone(uid int64, dbname string) (bool, int, map[string]string, map[string]string, error) {
  216. super, m, err := GetUserZone(uid, dbname)
  217. if err != nil {
  218. return super, 0, nil, nil, err
  219. }
  220. levelMap, err := GetAllZoneLevl(dbname)
  221. if err != nil {
  222. return super, 0, nil, nil, err
  223. }
  224. //top := map[string]string{}
  225. sub := map[string]string{}
  226. topLevel := 9999
  227. for k, v := range m {
  228. if _, ok := m[v]; !ok {
  229. //top[k] = v
  230. if topLevel > levelMap[k] {
  231. topLevel = levelMap[k]
  232. }
  233. continue
  234. }
  235. sub[k] = v
  236. }
  237. return super, topLevel, sub, m, nil
  238. }
  239. func PutUserZone(id int64, super bool, zones map[string]string, dbname string) error {
  240. if super {
  241. _, err := cache.Redis().SetEx(fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id), 3600, "super")
  242. if err != nil {
  243. logger.Error("Redis",
  244. zap.String("func", "PutUserZone"),
  245. zap.String("call", "Set"),
  246. zap.String("key", fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id)),
  247. zap.String("error", err.Error()))
  248. return errors.RedisError
  249. }
  250. return nil
  251. }
  252. allZoneM, err := GetAllZoneMap(dbname)
  253. if err != nil {
  254. return err
  255. }
  256. for k, _ := range zones {
  257. zones[k] = allZoneM[k]
  258. }
  259. bytes, _ := json.Marshal(zones)
  260. _, err = cache.Redis().SetEx(fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id), 3600, string(bytes))
  261. if err != nil {
  262. logger.Error("Redis",
  263. zap.String("func", "PutUserZone"),
  264. zap.String("call", "Set"),
  265. zap.String("key", fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id)),
  266. zap.String("error", err.Error()))
  267. return errors.RedisError
  268. }
  269. return nil
  270. }
  271. func DelUserZone(id int64, dbname string) error {
  272. _, err := cache.Redis().Del(fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id))
  273. if err != nil {
  274. logger.Error("Redis",
  275. zap.String("func", "DelUserZone"),
  276. zap.String("call", "Del"),
  277. zap.String("key", fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id)),
  278. zap.String("error", err.Error()))
  279. return errors.RedisError
  280. }
  281. return nil
  282. }
  283. func DelUserBaseInfo(id int64, dbname string) error {
  284. _, err := cache.Redis().Del(fmt.Sprintf("%s%s%d", dbname, USERBASEINFOPREFIX, id))
  285. if err != nil {
  286. logger.Error("Redis",
  287. zap.String("func", "DelUserBaseInfo"),
  288. zap.String("call", "Del"),
  289. zap.String("key", fmt.Sprintf("%s%s%d", dbname, USERBASEINFOPREFIX, id)),
  290. zap.String("error", err.Error()))
  291. return errors.RedisError
  292. }
  293. return nil
  294. }