123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package Account
- import (
- "Cold_DeductionNotice/conf"
- "Cold_DeductionNotice/logs"
- "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"
- "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(256);null"` // 绑定公司管理 Pid| 如:P1|P2|
- T_power int `orm:"size(2);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(200);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 第一次保存时才设置时间
- }
- 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_Cold_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)
- }
- }
- // 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
- }
- // 获取 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.Println("Read_Admin_Byid", err)
- }
- return r, err
- }
- // 获取列表
- func Read_Admin_Tpids_List(CompanyID int) (maps []Admin) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_pids__icontains", CompanyID)
- qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- return maps
- }
- // 获取列表
- func Read_Admin_Tpid_List(CompanyID int) (maps []Admin) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Admin))
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_pid", CompanyID)
- qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
- return maps
- }
- // 获取 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 {
- fmt.Println(err)
- }
- return r, err
- }
|