UserLogs.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 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 "user_logs" // 数据库名称 // ************** 替换 FormulaList **************
  21. }
  22. func init() {
  23. //注册模型
  24. orm.RegisterModel(new(UserLogs))
  25. }
  26. // 添加 System.Add_UserLogs("MqttServer","参数请求 [Rt_Parameter]","base")
  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. _, err := o.Insert(&m)
  31. if err != nil {
  32. logs.Error(lib.FuncName(), err)
  33. }
  34. }
  35. func Add_UserLogs_T(Logs_uuid string, Logs_class string, Logs_Title string, Logs_Txt_T interface{}) {
  36. o := orm.NewOrm()
  37. jsonStu, err := json.Marshal(Logs_Txt_T)
  38. if err != nil {
  39. logs.Error("Add_UserLogs_T JSON ,err=", err)
  40. }
  41. m := UserLogs{Logs_uuid: Logs_uuid, Logs_class: Logs_class, Logs_Title: Logs_Title, Logs_Txt: string(jsonStu)}
  42. _, err = o.Insert(&m)
  43. if err != nil {
  44. logs.Error(lib.FuncName(), err)
  45. }
  46. }
  47. // 获取列表
  48. func Read_UserLogs_ALL(Logs_uuid string, Logs_class string, page int, page_z int) (r []UserLogs, cnt int64) {
  49. o := orm.NewOrm()
  50. // 也可以直接使用 Model 结构体作为表名
  51. qs := o.QueryTable(new(UserLogs))
  52. var offset int64
  53. if page <= 1 {
  54. offset = 0
  55. } else {
  56. offset = int64((page - 1) * page_z)
  57. }
  58. cond := orm.NewCondition()
  59. cond1 := cond.And("Logs_uuid", Logs_uuid)
  60. if len(Logs_class) > 0 {
  61. cond1 = cond1.And("Logs_class", Logs_class)
  62. }
  63. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  64. if err != nil {
  65. logs.Error(lib.FuncName(), err)
  66. }
  67. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  68. if err != nil {
  69. logs.Error(lib.FuncName(), err)
  70. }
  71. return r, cnt
  72. }
  73. // 获取列表
  74. func Read_UserLogs_Class() (lists orm2.ParamsList) {
  75. o := orm.NewOrm()
  76. var pl_lists orm2.ParamsList
  77. _, err := o.Raw("SELECT DISTINCT logs_class FROM user_logs LIMIT 0,1000").ValuesFlat(&pl_lists)
  78. if err != nil {
  79. logs.Error(lib.FuncName(), err)
  80. return
  81. }
  82. return pl_lists
  83. }