123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- package common
- import (
- "cp-organization-management/errors"
- "cp-organization-management/model"
- "encoding/json"
- "fmt"
- "github.com/jaryhe/gopkgs/cache"
- "github.com/jaryhe/gopkgs/database"
- "github.com/jinzhu/gorm"
- "go.uber.org/zap"
- "strconv"
- "github.com/jaryhe/gopkgs/logger"
- )
- const (
- USERBASEINFOPREFIX = "user_base_info"
- USERZONEPREFIX = "user_zone"
- SUPERGROUPPREFIX = "super_group_id"
- AllZones = "all_zones"
- AllZoneMap = "all_zone_map"
- AllZoneLevel = "all_zone_level"
- )
- func GetAllZone(dbname string) ([]model.Zone, error) {
- str, _ := cache.Redis().Get(AllZones)
- if str != "" {
- list := []model.Zone{}
- json.Unmarshal([]byte(str), &list)
- return list, nil
- }
- zone := model.NewZone(dbname)
- list := []model.Zone{}
- err := zone.QueryAll(database.DB(), nil, &list)
- if err != nil {
- return nil, errors.DataBaseError
- }
- bytes, _ := json.Marshal(list)
- cache.Redis().SetEx(AllZones, 3600, string(bytes))
- return list, nil
- }
- func GetAllZoneMap(dbname string) (map[string]string, error) {
- str, _ := cache.Redis().Get(AllZoneMap)
- ret := map[string]string{}
- if str != "" {
- json.Unmarshal([]byte(str), &ret)
- return ret, nil
- }
- list, err := GetAllZone(dbname)
- if err != nil {
- return nil, err
- }
- for _, v := range list {
- ret[v.ZoneCode] = v.ParentZoneCode
- }
- bytes, _ := json.Marshal(ret)
- cache.Redis().SetEx(AllZoneMap, 3600, string(bytes))
- return ret, nil
- }
- func GetAllZoneLevl(dbname string) (map[string]int, error) {
- str, _ := cache.Redis().Get(AllZoneLevel)
- ret := map[string]int{}
- if str != "" {
- json.Unmarshal([]byte(str), &ret)
- return ret, nil
- }
- list, err := GetAllZone(dbname)
- if err != nil {
- return nil, err
- }
- for _, v := range list {
- ret[v.ZoneCode] = v.Level
- }
- bytes, _ := json.Marshal(ret)
- cache.Redis().SetEx(AllZoneLevel, 3600, string(bytes))
- return ret, nil
- }
- func DelAllZone() error {
- _, err := cache.Redis().Del(AllZones)
- if err != nil {
- return errors.RedisError
- }
- _, err = cache.Redis().Del(AllZoneMap)
- if err != nil {
- return errors.RedisError
- }
- _, err = cache.Redis().Del(AllZoneLevel)
- if err != nil {
- return errors.RedisError
- }
- return nil
- }
- func GetAllZoneCodeName(dbname string) (map[string]string, error) {
- list, err := GetAllZone(dbname)
- if err != nil {
- return nil, err
- }
- ret := map[string]string{}
- for _, v := range list {
- ret[v.ZoneCode] = v.ZoneName
- }
- return ret, nil
- }
- func GetSuperGroup(dbname string) (int64, error) {
- str, _ := cache.Redis().Get(fmt.Sprintf("%s%s", dbname, SUPERGROUPPREFIX))
- ret,_ := strconv.ParseInt(str, 10, 64)
- if ret > 0 {
- return ret, nil
- }
- group := model.NewRbacGroup(dbname)
- where := map[string]interface{}{
- "is_super_group":true,
- }
- err := group.Find(database.DB(), where)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return 0, errors.UserGroupNotExist
- }
- return 0, errors.DataBaseError
- }
- PutSuperGroup(dbname, group.Id)
- return group.Id, nil
- }
- func PutSuperGroup(dbname string, id int64) error {
- _, err := cache.Redis().Set(fmt.Sprintf("%s%s", dbname, SUPERGROUPPREFIX), fmt.Sprintf("%d", id))
- if err != nil {
- logger.Error("Redis",
- zap.String("func", "PutSuperGroup"),
- zap.String("call", "Set"),
- zap.String("key", fmt.Sprintf("%s%s", dbname, SUPERGROUPPREFIX)),
- zap.String("error", err.Error()))
- return errors.RedisError
- }
- return nil
- }
- func GetUserBaseInfo(id int64, dbname string) (*model.RbacUser, error) {
- str, _ := cache.Redis().Get(fmt.Sprintf("%s%s%d", dbname, USERBASEINFOPREFIX, id))
- if str != "" {
- user := model.RbacUser{}
- json.Unmarshal([]byte(str), &user)
- return &user, nil
- }
- user := model.NewRbacUser(dbname)
- where := map[string]interface{}{
- "id":id,
- }
- err := user.Find(database.DB(), where)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return user, errors.UserNotExist
- }
- return user, errors.DataBaseError
- }
- PutUserBaseInfo(user, dbname)
- return user, nil
- }
- func PutUserBaseInfo(user *model.RbacUser, dbname string) error {
- bytes, _ := json.Marshal(user)
- _, err := cache.Redis().SetEx(fmt.Sprintf("%s%s%d", dbname, USERBASEINFOPREFIX, user.Id), 3600, string(bytes))
- if err != nil {
- logger.Error("Redis",
- zap.String("func", "PutUserBaseInfo"),
- zap.String("call", "Set"),
- zap.String("key", fmt.Sprintf("%s%s%d", dbname, USERBASEINFOPREFIX, user.Id)),
- zap.String("error", err.Error()))
- return errors.RedisError
- }
- return nil
- }
- func getUserZoneFromDb(uid int64, dbname string)(bool, map[string]string, error) {
- // is supper
- user := model.NewRbacUser(dbname)
- where := map[string]interface{}{
- "id":uid,
- }
- err := user.Find(database.DB(), where)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return false, nil, errors.ErrRecordNotFound
- }
- return false, nil, errors.DataBaseError
- }
- if user.IsSuperUser {
- PutUserZone(uid, true, nil, dbname)
- return true, nil, nil
- }
- userZone := model.NewUserZone(dbname)
- where = map[string]interface{}{
- "user_id":uid,
- }
- list := []model.UserZone{}
- err = userZone.QueryAll(database.DB(), where, &list)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return false, nil, nil
- }
- return false, nil, errors.DataBaseError
- }
- m := map[string]string{}
- for _, v := range list {
- m[v.ZoneCode] = ""
- }
- PutUserZone(uid, false, m, dbname)
- return false, m, nil
- }
- func GetUserZone(uid int64, dbname string)(bool, map[string]string, error) {
- ret, _ := cache.Redis().Get(fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, uid))
- if ret == "" {
- return getUserZoneFromDb(uid, dbname)
- }
- if ret == "super" {
- return true, nil, nil
- }
- m := map[string]string{}
- json.Unmarshal([]byte(ret), &m)
- return false, m, nil
- }
- func GetUserTopSubZone(uid int64, dbname string) (bool, int, map[string]string, map[string]string, error) {
- super, m, err := GetUserZone(uid, dbname)
- if err != nil {
- return super, 0, nil, nil, err
- }
- levelMap, err := GetAllZoneLevl(dbname)
- if err != nil {
- return super, 0, nil, nil, err
- }
- //top := map[string]string{}
- sub := map[string]string{}
- topLevel := 9999
- for k, v := range m {
- if _, ok := m[v]; !ok {
- //top[k] = v
- if topLevel > levelMap[k] {
- topLevel = levelMap[k]
- }
- continue
- }
- sub[k] = v
- }
- return super, topLevel, sub, m, nil
- }
- func PutUserZone(id int64, super bool, zones map[string]string, dbname string) error {
- if super {
- _, err := cache.Redis().SetEx(fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id), 3600, "super")
- if err != nil {
- logger.Error("Redis",
- zap.String("func", "PutUserZone"),
- zap.String("call", "Set"),
- zap.String("key", fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id)),
- zap.String("error", err.Error()))
- return errors.RedisError
- }
- return nil
- }
- allZoneM, err := GetAllZoneMap(dbname)
- if err != nil {
- return err
- }
- for k, _ := range zones {
- zones[k] = allZoneM[k]
- }
- bytes, _ := json.Marshal(zones)
- _, err = cache.Redis().SetEx(fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id), 3600, string(bytes))
- if err != nil {
- logger.Error("Redis",
- zap.String("func", "PutUserZone"),
- zap.String("call", "Set"),
- zap.String("key", fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id)),
- zap.String("error", err.Error()))
- return errors.RedisError
- }
- return nil
- }
- func DelUserZone(id int64, dbname string) error {
- _, err := cache.Redis().Del(fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id))
- if err != nil {
- logger.Error("Redis",
- zap.String("func", "DelUserZone"),
- zap.String("call", "Del"),
- zap.String("key", fmt.Sprintf("%s%s%d", dbname, USERZONEPREFIX, id)),
- zap.String("error", err.Error()))
- return errors.RedisError
- }
- return nil
- }
- func DelUserBaseInfo(id int64, dbname string) error {
- _, err := cache.Redis().Del(fmt.Sprintf("%s%s%d", dbname, USERBASEINFOPREFIX, id))
- if err != nil {
- logger.Error("Redis",
- zap.String("func", "DelUserBaseInfo"),
- zap.String("call", "Del"),
- zap.String("key", fmt.Sprintf("%s%s%d", dbname, USERBASEINFOPREFIX, id)),
- zap.String("error", err.Error()))
- return errors.RedisError
- }
- return nil
- }
|