Logs.go 1.9 KB

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