123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- package Account
- import (
- "ColdVerify_server/lib"
- "ColdVerify_server/logs"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- uuid "github.com/satori/go.uuid"
- "log"
- "time"
- )
- type User struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_uuid string `orm:"size(256);null"` //
- //T_power int `orm:"size(2);default(0)"` // 权限
- T_name string `orm:"size(256);null"` // 某某公司名称
- T_pass string `orm:"size(256);null"` // 密码 MD5
- T_passstr string `orm:"size(256);null"` // 密码明文
- T_Show int `orm:"size(200);default(1)"` // 0隐藏 1公开
- T_State int `orm:"size(200);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 User_R struct {
- T_uuid string //
- //T_power int // 权限
- T_name string // 某某公司名称
- T_passstr string // 密码明文
- T_Show int // 0 1
- T_State int // 0 1
- }
- func (t *User) TableName() string {
- return "user" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(User))
- }
- // -------------------------------------------------------------
- func UserToUser_R(T User) (T_r User_R) {
- T_r.T_uuid = T.T_uuid
- //T_r.T_power = T.T_power
- T_r.T_name = T.T_name
- T_r.T_passstr = T.T_passstr
- T_r.T_Show = T.T_Show
- T_r.T_State = T.T_State
- //......
- return T_r
- }
- // 获取 ById
- func Read_User_ByT_uuid(T_uuid string) (e error, r User) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(User))
- e = qs.Filter("T_uuid", T_uuid).Filter("T_State", 1).One(&r)
- return e, r
- }
- func Read_User_verification(T_user string, T_pass string) (error, User) {
- o := orm.NewOrm()
- r := User{T_name: T_user, T_pass: T_pass, T_State: 1}
- err := o.Read(&r, "T_name", "T_pass", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return err, r
- }
- // ----------------
- // 获取 ById
- func Read_User_ById(id int) (r User, is bool) {
- o := orm.NewOrm()
- r = User{Id: id, T_State: 1}
- err := o.Read(&r, "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, false
- }
- return r, true
- }
- // 获取 By
- func Read_User(T_uuid string) (r User, is bool) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(User))
- err := qs.Filter("T_uuid", T_uuid).Filter("T_State", 1).One(&r)
- if err != nil {
- return r, false
- }
- return r, true
- }
- // 获取 By T_name
- func Read_UserByT_name(T_name string) (r User, is bool) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(User))
- err := qs.Filter("T_name", T_name).Filter("T_State", 1).One(&r)
- if err != nil {
- return r, false
- }
- return r, true
- }
- // 添加
- func Add_User(r User) (id int64, is bool) {
- o := orm.NewOrm()
- // 生成编号
- rand_x := 0
- for true {
- r.T_uuid = uuid.NewV4().String()
- err := o.Read(&r, "T_uuid") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- break
- }
- rand_x += 1
- }
- id, err := o.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return 0, false
- }
- return id, true
- }
- // 删除
- func Delete_User(v User) bool {
- o := orm.NewOrm()
- if num, err := o.Delete(&v); err == nil {
- logs.Println("Number of records deleted in database:", num)
- } else {
- return false
- }
- return true
- }
- // 删除
- func Delete_User_(v User) bool {
- o := orm.NewOrm()
- v.T_State = 0
- if num, err := o.Update(&v, "T_State"); err == nil {
- log.Println("Number of records updated in database:", num)
- } else {
- return false
- }
- return true
- }
- // 修改
- func Update_User(m User, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- log.Println("Number of records updated in database:", num)
- return true
- }
- return false
- }
- // 获取列表
- func Read_User_List(T_name string, page int, page_z int) ([]User_R, int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- var r []User
- qs := o.QueryTable(new(User))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).AndCond(cond.Or("T_name__icontains", T_name))
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
- cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
- // 转换
- var User_r []User_R
- for _, v := range r {
- User_r = append(User_r, UserToUser_R(v))
- }
- return User_r, cnt
- }
- // 获取全部列表
- func Read_User_List_ALL() (maps []User) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(Admin))
- qs.Filter("T_State", 1).OrderBy("Id").All(&maps)
- return maps
- }
- // 获取全部列表-包括软删除数据
- func Read_User_List_ALL_1() (maps []User) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(User))
- qs.OrderBy("Id").All(&maps)
- return maps
- }
- // {"uuid":"name"}
- func UserListToMap(T []User) map[string]string {
- maps := make(map[string]string, len(T))
- for _, v := range T {
- maps[v.T_uuid] = v.T_name
- }
- return maps
- }
|