Logs.go 1.9 KB

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