UserLogs.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package System
  2. import (
  3. "ColdVerify_server/logs"
  4. "encoding/json"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. "time"
  8. )
  9. type UserLogs struct {
  10. Id int `orm:"column(ID);size(11);auto;pk"`
  11. Logs_uuid string `orm:"size(256);null"` //
  12. Logs_class string `orm:"size(256);null"` //
  13. Logs_Title string `orm:"size(256);null"` // 标题
  14. Logs_Txt string `orm:"type(text);null"` // 详情
  15. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  16. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  17. }
  18. func (t *UserLogs) TableName() string {
  19. return "user_logs" // 数据库名称 // ************** 替换 FormulaList **************
  20. }
  21. func init() {
  22. //注册模型
  23. orm.RegisterModel(new(UserLogs))
  24. }
  25. // 添加 System.Add_UserLogs(user_r.T_user,"登陆", "管理员登陆", "")
  26. func Add_UserLogs(Logs_uuid string, Logs_class string, Logs_Title string, Logs_Txt string) {
  27. o := orm.NewOrm()
  28. m := UserLogs{Logs_uuid: Logs_uuid, Logs_class: Logs_class, Logs_Title: Logs_Title, Logs_Txt: Logs_Txt}
  29. o.Insert(&m)
  30. }
  31. func Add_UserLogs_T(Logs_uuid string, Logs_class string, Logs_Title string, Logs_Txt_T interface{}) {
  32. o := orm.NewOrm()
  33. jsonStu, err := json.Marshal(Logs_Txt_T)
  34. if err != nil {
  35. logs.Println("Add_UserLogs_T JSON ,err=", err)
  36. }
  37. m := UserLogs{Logs_uuid: Logs_uuid, Logs_class: Logs_class, Logs_Title: Logs_Title, Logs_Txt: string(jsonStu)}
  38. o.Insert(&m)
  39. }
  40. // 获取列表
  41. func Read_UserLogs_ALL(page, page_z int, Logs_class string) (r []UserLogs, cnt int64) {
  42. o := orm.NewOrm()
  43. // 也可以直接使用 Model 结构体作为表名
  44. qs := o.QueryTable(new(UserLogs))
  45. var offset int64
  46. if page <= 1 {
  47. offset = 0
  48. } else {
  49. offset = int64((page - 1) * page_z)
  50. }
  51. qs.Limit(page_z, offset).Filter("Logs_class__icontains", Logs_class).OrderBy("-Id").All(&r)
  52. cnt, _ = qs.Filter("Logs_class__icontains", Logs_class).Count()
  53. return r, cnt
  54. }
  55. // 获取列表
  56. func Read_UserLogs_Class() (lists orm2.ParamsList) {
  57. o := orm.NewOrm()
  58. var pl_lists orm2.ParamsList
  59. num, err := o.Raw("SELECT DISTINCT logs_class FROM UserLogs LIMIT 0,1000").ValuesFlat(&pl_lists)
  60. if err == nil {
  61. logs.Println("user nums: ", num)
  62. }
  63. return pl_lists
  64. }