UserLogs.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package System
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. "time"
  8. )
  9. type UserLogs struct {
  10. Id int `orm:"column(ID);size(11);auto;pk"`
  11. Logs_uuid string `orm:"size(256);null"` //
  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 *UserLogs) TableName() string {
  19. return "user_logs" // 数据库名称 // ************** 替换 FormulaList **************
  20. }
  21. func init() {
  22. //注册模型
  23. orm.RegisterModel(new(UserLogs))
  24. }
  25. // 添加 System.Add_UserLogs("MqttServer","参数请求 [Rt_Parameter]","base")
  26. func Add_UserLogs(Logs_uuid string, Logs_class string, Logs_Title string, Logs_Txt string) {
  27. o := orm.NewOrm()
  28. m := UserLogs{Logs_uuid: Logs_uuid, Logs_class: Logs_class, Logs_Title: Logs_Title, Logs_Txt: Logs_Txt}
  29. o.Insert(&m)
  30. }
  31. func Add_UserLogs_T(Logs_uuid string, Logs_class string, Logs_Title string, Logs_Txt_T interface{}) {
  32. o := orm.NewOrm()
  33. jsonStu, err := json.Marshal(Logs_Txt_T)
  34. if err != nil {
  35. fmt.Println("Add_UserLogs_T JSON ,err=", err)
  36. }
  37. m := UserLogs{Logs_uuid: Logs_uuid, Logs_class: Logs_class, Logs_Title: Logs_Title, Logs_Txt: string(jsonStu)}
  38. o.Insert(&m)
  39. }
  40. // 获取列表
  41. func Read_UserLogs_ALL(Logs_uuid string, Logs_class string, page int, page_z int) (r []UserLogs, cnt int64) {
  42. o := orm.NewOrm()
  43. // 也可以直接使用 Model 结构体作为表名
  44. qs := o.QueryTable(new(UserLogs))
  45. var offset int64
  46. if page <= 1 {
  47. offset = 0
  48. } else {
  49. offset = int64((page - 1) * page_z)
  50. }
  51. cond := orm.NewCondition()
  52. cond1 := cond.And("Logs_uuid", Logs_uuid)
  53. if len(Logs_class) > 0 {
  54. cond1 = cond1.And("Logs_class", Logs_class)
  55. }
  56. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  57. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  58. return r, cnt
  59. }
  60. // 获取列表
  61. func Read_UserLogs_Class() (lists orm2.ParamsList) {
  62. o := orm.NewOrm()
  63. var pl_lists orm2.ParamsList
  64. num, err := o.Raw("SELECT DISTINCT logs_class FROM UserLogs LIMIT 0,1000").ValuesFlat(&pl_lists)
  65. if err == nil {
  66. fmt.Println("user nums: ", num)
  67. }
  68. fmt.Println(len(pl_lists))
  69. return pl_lists
  70. }