UserLogs.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package System
  2. import (
  3. "Cold_Api/conf"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  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 "UserLogs" // 数据库名称 // ************** 替换 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. o.Insert(&m)
  31. }
  32. func Add_UserLogs_T(Logs_uuid string, Logs_class string, Logs_Title string, Logs_Txt_T interface{}) {
  33. o := orm.NewOrm()
  34. jsonStu, err := json.Marshal(Logs_Txt_T)
  35. if err != nil {
  36. fmt.Println("Add_UserLogs_T JSON ,err=", err)
  37. }
  38. m := UserLogs{Logs_uuid: Logs_uuid, Logs_class: Logs_class, Logs_Title: Logs_Title, Logs_Txt: string(jsonStu)}
  39. o.Insert(&m)
  40. }
  41. // 获取列表
  42. func Read_UserLogs_ALL(page int, Logs_class string) (r []UserLogs, cnt int64) {
  43. o := orm.NewOrm()
  44. // 也可以直接使用 Model 结构体作为表名
  45. qs := o.QueryTable(new(UserLogs))
  46. var offset int64
  47. if page <= 1 {
  48. offset = 0
  49. } else {
  50. offset = int64((page - 1) * conf.Page_size)
  51. }
  52. qs.Limit(conf.Page_size, offset).Filter("Logs_class__icontains", Logs_class).OrderBy("-Id").All(&r)
  53. cnt, _ = qs.Filter("Logs_class__icontains", Logs_class).Count()
  54. return r, cnt
  55. }
  56. // 获取列表
  57. func Read_V2_UserLogs_ALL(Logs_uuid string, Logs_class string, page int, page_z int) (r []UserLogs, cnt int64) {
  58. o := orm.NewOrm()
  59. // 也可以直接使用 Model 结构体作为表名
  60. qs := o.QueryTable(new(UserLogs))
  61. var offset int64
  62. if page <= 1 {
  63. offset = 0
  64. } else {
  65. offset = int64((page - 1) * page_z)
  66. }
  67. cond := orm.NewCondition()
  68. cond1 := cond.And("Logs_uuid", Logs_uuid)
  69. if len(Logs_class) > 0 {
  70. cond1 = cond1.And("Logs_class", Logs_class)
  71. }
  72. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  73. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  74. return r, cnt
  75. }
  76. // 获取列表
  77. func Read_UserLogs_Class() (lists orm2.ParamsList) {
  78. o := orm.NewOrm()
  79. var pl_lists orm2.ParamsList
  80. num, err := o.Raw("SELECT DISTINCT logs_class FROM UserLogs LIMIT 0,1000").ValuesFlat(&pl_lists)
  81. if err == nil {
  82. fmt.Println("user nums: ", num)
  83. }
  84. fmt.Println(len(pl_lists))
  85. return pl_lists
  86. }