UserLogs.go 2.3 KB

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