123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- package Account
- import (
- "ColdP_server/conf"
- "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/go-sql-driver/mysql"
- "strconv"
- "strings"
- "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:"size(200);null"` // 绑定公司管理 Pid| 如 P1|P2
- T_power int `orm:"size(2);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:"size(256);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_user string // 用户名 (唯一)
- 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_user = r.T_user
- 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 AdminToAdmin_(r Admin) (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
- func init() {
- //注册模型
- orm.RegisterModel(new(Admin))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_ColdP_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"
- fmt.Println(errMsg, err)
- }
- }
- // ---------------- 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 {
- fmt.Print(err)
- return
- }
- err = redisCache_Admin.Put(r.T_uuid, str, 24*time.Hour)
- if err != nil {
- fmt.Println("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)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- //println("没有 找到key:",key)
- return Admin{}, false
- }
- func Redis_Admin_DelK(key string) (err error) {
- err = redisCache_Admin.Delete(key)
- return
- }
- // ---------------- 特殊方法 -------------------
- // 添加
- func Add_Admin(m Admin) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- fmt.Println(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 {
- // fmt.Println("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 {
- fmt.Println(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 {
- fmt.Println(err)
- }
- Redis_Admin_Set(r) // Redis 更新缓存
- return err, r
- }
- // 修改
- func Update_Admin(m Admin, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- fmt.Println("Number of records updated in database:", num)
- Redis_Admin_Set(m) // Redis 更新缓存
- return true
- }
- return false
- }
- // 删除
- func Delete_Admin(v Admin) bool {
- o := orm.NewOrm()
- v.T_State = 0
- if num, err := o.Update(&v, "T_State"); err == nil {
- fmt.Println("Number of records updated in database:", num)
- } else {
- 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 {
- fmt.Println(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()
- //redis 保存方式 uuid|公司pid
- arr := strings.Split(T_uuid, "|")
- r = Admin{T_uuid: arr[0], T_State: 1}
- err = o.Read(&r, "T_uuid", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- fmt.Println(err)
- }
- //设置为redis中存储的pid
- pid, _ := strconv.ParseInt(arr[1], 10, 64)
- r.T_pid = int(pid)
- 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_z == 0 {
- page_z = conf.Page_size
- }
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1)
- if T_pid > 0 {
- cond1 = cond1.AndCond(cond.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))
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- for _, v := range maps {
- AdminList = append(AdminList, AdminToAdmin_R(v))
- }
- return AdminList, cnt
- }
- func Read_Admin_List_T_pids(T_pid int) (AdminList []Admin_) {
- 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)+"|")
- qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- for _, v := range maps {
- AdminList = append(AdminList, AdminToAdmin_(v))
- }
- return AdminList
- }
- // 获取列表
- func Read_Admin_List_All() (AdminList []Admin_R) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- var maps []Admin
- qs.All(&maps)
- for _, v := range maps {
- AdminList = append(AdminList, AdminToAdmin_R(v))
- }
- return AdminList
- }
|