User.go 5.4 KB

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