123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- package Account
- import (
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- "github.com/beego/beego/v2/core/logs"
- _ "github.com/go-sql-driver/mysql"
- "strconv"
- "strings"
- "sync"
- "time"
- )
- type Admin struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_uuid string `orm:"size(256);null"` // 用户编号
- T_pid int `orm:"size(200);null"` // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
- T_pids string `orm:"type(text);null"` // 绑定公司管理 Pid| 如 P1|P2
- T_power int `orm:"size(20);default(0)"` // 权限 (关联权限表)
- T_user string `orm:"size(256);null"` // 用户名 (唯一)
- T_pass string `orm:"size(256);null"` // MD5
- T_name string `orm:"size(256);null"` // 姓名
- T_phone string `orm:"size(256);null"` // 电话
- T_mail string `orm:"size(200);null"` // 邮箱
- T_wx string `orm:"type(text);null"` // 微信
- T_State int `orm:"size(2);default(1)"` // 0删除 1 正常
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
- }
- type Admin_R struct {
- T_uuid string // 用户编号
- T_pid int // 绑定公司 ( 只有创建公司用户时添加,内部人员 为0)
- T_pids string // 绑定公司管理 Pid| 如 P1|P2
- T_power int // 权限 (关联权限表)
- T_power_name string // 权限名称
- T_user string // 用户名 (唯一)
- T_pass string // MD5,密码
- T_name string // 姓名
- T_phone string // 电话
- T_mail string // 邮箱
- T_wx string // 微信
- }
- type Admin_ struct {
- T_name string // 姓名
- T_phone string // 电话
- }
- func AdminToAdmin_R(r Admin) (v Admin_R) {
- v.T_uuid = r.T_uuid
- v.T_pid = r.T_pid
- v.T_pids = r.T_pids
- v.T_power = r.T_power
- v.T_power_name = Read_Power_Get(r.T_power)
- v.T_user = r.T_user
- v.T_pass = ""
- v.T_name = r.T_name
- v.T_phone = r.T_phone
- v.T_mail = r.T_mail
- v.T_wx = r.T_wx
- return v
- }
- func Admin_RToAdmin_(r Admin_R) (v Admin_) {
- v.T_name = r.T_name
- v.T_phone = r.T_phone
- return v
- }
- func (t *Admin) TableName() string {
- return "admin" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redisCache_Admin cache.Cache
- var AdminMap *sync.Map // 泛型
- func init() {
- //注册模型
- orm.RegisterModel(new(Admin))
- AdminMap = new(sync.Map)
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_User_Admin", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_Admin, err = cache.NewCache("redis", config)
- if err != nil || redisCache_Admin == nil {
- errMsg := "failed to init redis"
- logs.Error(errMsg, err)
- panic(errMsg)
- }
- }
- // ---------------- Redis -------------------
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_Admin_Set(r Admin) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_Admin.Put(r.T_uuid, str, 24*time.Hour)
- if err != nil {
- logs.Error("Redis_Admin_Set", "set key:", r.T_uuid, ",value:", str, err)
- }
- return
- }
- // if r,is :=Redis_Get(T_sn);is{
- // return r,nil
- // }
- func Redis_Admin_Get(key string) (r Admin, is bool) {
- if redisCache_Admin.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_Admin.Get(key)
- err := json.Unmarshal(v.([]byte), &r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return Admin{}, false
- }
- return r, true
- }
- //println("没有 找到key:",key)
- return Admin{}, false
- }
- func Redis_Admin_DelK(key string) (err error) {
- err = redisCache_Admin.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // ---------------- 特殊方法 -------------------
- // 添加
- func Add_Admin(m Admin) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return id, err
- }
- m.Id = int(id)
- Redis_Admin_Set(m)
- return id, err
- }
- //// 修改
- //func Update_Delete_Admin_ByT_uuid(T_uuid string) (err error) {
- // o := orm.NewOrm()
- // v := Admin{T_uuid: T_uuid}
- // // ascertain id exists in the database
- // if err = o.Read(&v, "T_uuid"); err == nil {
- // var num int64
- // v.T_State = 0
- //
- // if num, err = o.Update(&v, "T_State"); err == nil {
- // logs.Info("Number of records updated in database:", num)
- // }
- //
- // Redis_Admin_Set(v)
- // }
- // return err
- //}
- func Read_Admin_T_user(T_user string) (Admin, error) {
- o := orm.NewOrm()
- r := Admin{T_user: T_user, T_State: 1}
- err := o.Read(&r, "T_user", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return r, err
- }
- func Read_AdminLogin_verification(T_user string, T_pass string) (error, Admin) {
- o := orm.NewOrm()
- r := Admin{T_user: T_user, T_pass: T_pass, T_State: 1}
- err := o.Read(&r, "T_user", "T_pass", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err, r
- }
- Redis_Admin_Set(r) // Redis 更新缓存
- return err, r
- }
- // 修改
- func Update_Admin(m Admin, cols ...string) bool {
- o := orm.NewOrm()
- _, err := o.Update(&m, cols...)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false
- }
- Redis_Admin_Set(m) // Redis 更新缓存
- return true
- }
- // 删除
- func Delete_Admin(v Admin) bool {
- o := orm.NewOrm()
- v.T_State = 0
- _, err := o.Update(&v, "T_State")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false
- }
- Redis_Admin_DelK(v.T_uuid) // Redis 更新缓存
- return true
- }
- // 获取 ById
- func Read_Admin_ById(Id int) (r Admin, err error) {
- o := orm.NewOrm()
- r = Admin{Id: Id, T_State: 1}
- err = o.Read(&r, "Id", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return r, err
- }
- // 获取 ByT_uuid
- func Read_Admin_ByUuid(T_uuid string) (r Admin, err error) {
- if r, is := Redis_Admin_Get(T_uuid); is {
- //println("Redis_Get OK")
- return r, nil
- }
- o := orm.NewOrm()
- r = Admin{T_uuid: T_uuid, T_State: 1}
- err = o.Read(&r, "T_uuid", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- Redis_Admin_Set(r)
- return r, err
- }
- // 获取列表
- func Read_Admin_List(T_pid int, T_name string, page int, page_z int) (AdminList []Admin_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- var maps []Admin
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_pid", T_pid)
- if len(T_name) > 0 {
- cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
- }
- var err error
- if page_z == 9999 {
- _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("Id").All(&maps)
- } else {
- _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").All(&maps)
- }
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- for _, v := range maps {
- AdminList = append(AdminList, AdminToAdmin_R(v))
- }
- return AdminList, cnt
- }
- // 获取所有管理员用户列表
- func Read_Admin_List_All() (AdminList []Admin_R) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- var maps []Admin
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_pid", 0).AndNot("T_pids", "")
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return AdminList
- }
- for _, v := range maps {
- AdminList = append(AdminList, AdminToAdmin_R(v))
- }
- return AdminList
- }
- func Read_Admin_List_T_pids(T_pid int) (AdminList []Admin_R) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- var maps []Admin
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_pid", 0).And("T_pids__icontains", "P"+strconv.Itoa(T_pid)+"|")
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return AdminList
- }
- for _, v := range maps {
- AdminList = append(AdminList, AdminToAdmin_R(v))
- }
- return AdminList
- }
- func Read_Admin_List_T_pid(T_pid int) (AdminList []Admin_R) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- var maps []Admin
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_pid", T_pid)
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return AdminList
- }
- for _, v := range maps {
- AdminList = append(AdminList, AdminToAdmin_R(v))
- }
- return AdminList
- }
- func Read_Parent_Admin_List(T_pid []int) (AdminList []Admin_R) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- var maps []Admin
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_pid__in", T_pid)
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return AdminList
- }
- for _, v := range maps {
- AdminList = append(AdminList, AdminToAdmin_R(v))
- }
- return AdminList
- }
- func Read_Admin_Num_ByT_power(T_power int) int {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- cnt, err := qs.Filter("T_State", 1).Filter("T_power", T_power).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return 0
- }
- return int(cnt)
- }
- // 查询 用户关联的公司总数
- func Read_Admin_Num_ByT_pid(T_pids []int) int {
- if len(T_pids) == 0 {
- return 0
- }
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- cnt, err := qs.Filter("T_State", 1).Filter("T_pid__in", T_pids).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return 0
- }
- return int(cnt)
- }
- // 获取所有用户列表
- func Read_User_List_All() (AdminList []Admin_R) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- var maps []Admin
- _, err := qs.All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return AdminList
- }
- for _, v := range maps {
- AdminList = append(AdminList, AdminToAdmin_R(v))
- }
- return AdminList
- }
- func Add_Company_bind(Admin_r Admin, T_pids []int) (err error) {
- o := orm.NewOrm()
- for _, T_pid := range T_pids {
- T_Pid_bind := "P" + strconv.Itoa(T_pid) + "|"
- Admin_r.T_pids = strings.Replace(Admin_r.T_pids, T_Pid_bind, "", -1)
- Admin_r.T_pids = Admin_r.T_pids + T_Pid_bind
- }
- _, err = o.Update(&Admin_r, "T_pids")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- Redis_Admin_Set(Admin_r) // Redis 更新缓存
- return nil
- }
- func Delete_Company_bind(Admin_r Admin, T_pids []int) (err error) {
- o := orm.NewOrm()
- for _, T_pid := range T_pids {
- T_Pid_bind := "P" + strconv.Itoa(T_pid) + "|"
- Admin_r.T_pids = strings.Replace(Admin_r.T_pids, T_Pid_bind, "", -1)
- }
- _, err = o.Update(&Admin_r, "T_pids")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- Redis_Admin_Set(Admin_r) // Redis 更新缓存
- return nil
- }
- // 删除公司后将管理员绑定的公司删除
- func Delete_Company_bind_T_pids(o orm.Ormer, T_pid int) (err error) {
- qs := o.QueryTable(new(Admin))
- var maps []Admin
- T_Pid_bind := "P" + strconv.Itoa(T_pid) + "|"
- cnt, err := qs.Filter("T_State", 1).Filter("T_pids__icontains", T_Pid_bind).All(&maps)
- if err != nil {
- return err
- }
- if cnt == 0 {
- return nil
- }
- for _, v := range maps {
- if err = o.Read(&v, "Id"); err == nil {
- v.T_pids = strings.Replace(v.T_pids, T_Pid_bind, "", -1)
- _, err = o.Update(&v, "T_pids")
- if err != nil {
- logs.Error("Delete_Company_bind_T_pids", v.T_uuid, T_pid, err)
- return err
- }
- Redis_Admin_DelK(v.T_uuid) // Redis 更新缓存
- }
- }
- return err
- }
- func Read_Admin_All_Map(A []Admin_R) {
- for _, v := range A {
- AdminMap.Store(v.T_uuid, v)
- }
- }
- func Read_Admin_T_name_Get(T_uuid string) string {
- // 有先加入 给全部人发消息
- v, ok := AdminMap.Load(T_uuid) /*如果确定是真实的,则存在,否则不存在 */
- if ok {
- return v.(Admin_R).T_name
- } else {
- return ""
- }
- }
|