Logs.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package System
  2. import (
  3. "Cold_Api/controllers/lib"
  4. "encoding/json"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. "github.com/beego/beego/v2/core/logs"
  8. "time"
  9. )
  10. type Logs struct {
  11. Id int `orm:"column(ID);size(11);auto;pk"`
  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 *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. _, err := o.Insert(&m)
  30. if err != nil {
  31. logs.Error(lib.FuncName(), err)
  32. }
  33. }
  34. func Add_Logs_T(Logs_class string, Logs_Title string, Logs_Txt_T interface{}) {
  35. o := orm.NewOrm()
  36. jsonStu, err := json.Marshal(Logs_Txt_T)
  37. if err != nil {
  38. logs.Error(lib.FuncName(), err)
  39. }
  40. m := Logs{Logs_class: Logs_class, Logs_Title: Logs_Title, Logs_Txt: string(jsonStu)}
  41. _, err = o.Insert(&m)
  42. if err != nil {
  43. logs.Error(lib.FuncName(), err)
  44. }
  45. }
  46. // 获取列表
  47. func Read_Logs_ALL(Logs_class string, page int, page_z int) (r []Logs, cnt int64) {
  48. o := orm.NewOrm()
  49. // 也可以直接使用 Model 结构体作为表名
  50. qs := o.QueryTable(new(Logs))
  51. var offset int64
  52. if page <= 1 {
  53. offset = 0
  54. } else {
  55. offset = int64((page - 1) * page_z)
  56. }
  57. cond := orm.NewCondition()
  58. cond1 := cond
  59. if len(Logs_class) > 0 {
  60. cond1 = cond1.And("Logs_class", Logs_class)
  61. }
  62. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").All(&r)
  63. if err != nil {
  64. logs.Error(lib.FuncName(), err)
  65. return
  66. }
  67. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  68. if err != nil {
  69. logs.Error(lib.FuncName(), err)
  70. return
  71. }
  72. return r, cnt
  73. }
  74. type CLASS_lists struct {
  75. Logs_class string
  76. }
  77. // 获取列表
  78. func Read_Logs_Class() (lists orm2.ParamsList) {
  79. o := orm.NewOrm()
  80. var pl_lists orm2.ParamsList
  81. _, err := o.Raw("SELECT DISTINCT logs_class FROM Logs LIMIT 0,1000").ValuesFlat(&pl_lists)
  82. if err != nil {
  83. logs.Error(lib.FuncName(), err)
  84. }
  85. return pl_lists
  86. }