123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package Account
- import (
- "bzd_server/logs"
- "fmt"
- "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"
- "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);0"` // 权限
- T_name string `orm:"size(256);null"` // 某某公司名称
- T_user string `orm:"size(256);null"` // 登录用户名
- T_pass string `orm:"size(256);null"` // 密码 MD5
- T_passstr 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 第一次保存时才设置时间
- }
- type User_R struct {
- T_uuid string `orm:"size(256);null"` //
- T_power int `orm:"size(2);0"` // 权限
- T_name string `orm:"size(256);null"` // 某某公司名称
- T_user string `orm:"size(256);null"` // 登录用户名
- T_passstr string `orm:"size(256);null"` // 密码明文
- T_State int `orm:"size(200);1"` // 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_user = T.T_user
- T_r.T_passstr = T.T_passstr
- 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).One(&r)
- return e, r
- }
- func Read_User_verification(T_user string, T_pass string) (error, User) {
- o := orm.NewOrm()
- r := User{T_user: T_user, T_pass: T_pass}
- err := o.Read(&r, "T_user", "T_pass") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- fmt.Println(err)
- }
- return err, r
- }
- // ----------------
- // 获取 ById
- func Read_User_ById(id int) (r User, is bool) {
- o := orm.NewOrm()
- r = User{Id: id}
- err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Println(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).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
- }
- err := o.Read(&r, "T_user") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err == nil {
- return 0, false
- }
- id, err = o.Insert(&r)
- if err != nil {
- logs.Println(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 {
- fmt.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 {
- fmt.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.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- 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
- }
|