User.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package Account
  2. import (
  3. "bzd_server/logs"
  4. "fmt"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. _ "github.com/go-sql-driver/mysql"
  8. uuid "github.com/satori/go.uuid"
  9. "time"
  10. )
  11. type User struct {
  12. Id int `orm:"column(ID);size(11);auto;pk"`
  13. T_uuid string `orm:"size(256);null"` //
  14. T_power int `orm:"size(2);0"` // 权限
  15. T_name string `orm:"size(256);null"` // 某某公司名称
  16. T_user string `orm:"size(256);null"` // 登录用户名
  17. T_pass string `orm:"size(256);null"` // 密码 MD5
  18. T_passstr string `orm:"size(256);null"` // 密码明文
  19. T_State int `orm:"size(200);1"` // 0删除 1 正常
  20. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  21. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  22. }
  23. type User_R struct {
  24. T_uuid string `orm:"size(256);null"` //
  25. T_power int `orm:"size(2);0"` // 权限
  26. T_name string `orm:"size(256);null"` // 某某公司名称
  27. T_user string `orm:"size(256);null"` // 登录用户名
  28. T_passstr string `orm:"size(256);null"` // 密码明文
  29. T_State int `orm:"size(200);1"` // 0 1
  30. }
  31. func (t *User) TableName() string {
  32. return "User" // 数据库名称 // ************** 替换 FormulaList **************
  33. }
  34. func init() {
  35. //注册模型
  36. orm.RegisterModel(new(User))
  37. }
  38. // -------------------------------------------------------------
  39. func UserToUser_R(T User) (T_r User_R) {
  40. T_r.T_uuid = T.T_uuid
  41. T_r.T_power = T.T_power
  42. T_r.T_name = T.T_name
  43. T_r.T_user = T.T_user
  44. T_r.T_passstr = T.T_passstr
  45. T_r.T_State = T.T_State
  46. //......
  47. return T_r
  48. }
  49. // 获取 ById
  50. func Read_User_ByT_uuid(T_uuid string) (e error, r User) {
  51. o := orm.NewOrm()
  52. qs := o.QueryTable(new(User))
  53. e = qs.Filter("T_uuid", T_uuid).One(&r)
  54. return e, r
  55. }
  56. func Read_User_verification(T_user string, T_pass string) (error, User) {
  57. o := orm.NewOrm()
  58. r := User{T_user: T_user, T_pass: T_pass}
  59. err := o.Read(&r, "T_user", "T_pass") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  60. if err != nil {
  61. fmt.Println(err)
  62. }
  63. return err, r
  64. }
  65. // ----------------
  66. // 获取 ById
  67. func Read_User_ById(id int) (r User, is bool) {
  68. o := orm.NewOrm()
  69. r = User{Id: id}
  70. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  71. if err != nil {
  72. logs.Println(err)
  73. return r, false
  74. }
  75. return r, true
  76. }
  77. // 获取 By
  78. func Read_User(T_uuid string) (r User, is bool) {
  79. o := orm.NewOrm()
  80. qs := o.QueryTable(new(User))
  81. err := qs.Filter("T_uuid", T_uuid).One(&r)
  82. if err != nil {
  83. return r, false
  84. }
  85. return r, true
  86. }
  87. // 添加
  88. func Add_User(r User) (id int64, is bool) {
  89. o := orm.NewOrm()
  90. // 生成编号
  91. rand_x := 0
  92. for true {
  93. r.T_uuid = uuid.NewV4().String()
  94. err := o.Read(&r, "T_uuid") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  95. if err != nil {
  96. break
  97. }
  98. rand_x += 1
  99. }
  100. err := o.Read(&r, "T_user") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  101. if err == nil {
  102. return 0, false
  103. }
  104. id, err = o.Insert(&r)
  105. if err != nil {
  106. logs.Println(err)
  107. return 0, false
  108. }
  109. return id, true
  110. }
  111. // 删除
  112. func Delete_User(v User) bool {
  113. o := orm.NewOrm()
  114. if num, err := o.Delete(&v); err == nil {
  115. logs.Println("Number of records deleted in database:", num)
  116. } else {
  117. return false
  118. }
  119. return true
  120. }
  121. // 删除
  122. func Delete_User_(v User) bool {
  123. o := orm.NewOrm()
  124. v.T_State = 0
  125. if num, err := o.Update(&v, "T_State"); err == nil {
  126. fmt.Println("Number of records updated in database:", num)
  127. } else {
  128. return false
  129. }
  130. return true
  131. }
  132. // 修改
  133. func Update_User(m User,cols ...string) bool {
  134. o := orm.NewOrm()
  135. if num, err := o.Update(&m, cols...); err == nil {
  136. fmt.Println("Number of records updated in database:", num)
  137. return true
  138. }
  139. return false
  140. }
  141. // 获取列表
  142. func Read_User_List(T_name string, page int, page_z int) ( []User_R, int64) {
  143. o := orm.NewOrm()
  144. // 也可以直接使用 Model 结构体作为表名
  145. var r []User
  146. qs := o.QueryTable(new(User))
  147. var offset int64
  148. if page <= 1 {
  149. offset = 0
  150. } else {
  151. offset = int64((page - 1) * page_z)
  152. }
  153. cond := orm.NewCondition()
  154. cond1 := cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  155. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  156. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  157. // 转换
  158. var User_r []User_R
  159. for _,v := range r{
  160. User_r = append(User_r, UserToUser_R(v))
  161. }
  162. return User_r, cnt
  163. }