UserLogs.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package System
  2. import (
  3. "ERP_user/logs"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  9. "time"
  10. )
  11. type UserLogs struct {
  12. Id int `orm:"column(ID);size(11);auto;pk"`
  13. T_uuid string `orm:"size(32);null"` //
  14. T_class string `orm:"size(256);null"` //
  15. T_title string `orm:"size(256);null"` // 标题
  16. T_txt string `orm:"type(text);null"` // 详情
  17. CreateTime time.Time `orm:"auto_now_add;type(datetime)"` //auto_now 每次 model 保存时都会对时间自动更新
  18. }
  19. type UserLogs_R struct {
  20. T_class string `orm:"size(256);null"` //
  21. T_title string `orm:"size(256);null"` // 标题
  22. T_txt string `orm:"type(text);null"` // 详情
  23. CreateTime string
  24. }
  25. func (t *UserLogs) TableName() string {
  26. return "user_logs" // 数据库名称 // ************** 替换 FormulaList **************
  27. }
  28. func init() {
  29. //注册模型
  30. orm.RegisterModel(new(UserLogs))
  31. }
  32. func UserLogsToUserLogs_R(r UserLogs) (m UserLogs_R) {
  33. m.T_class = r.T_class
  34. m.T_title = r.T_title
  35. m.T_txt = r.T_txt
  36. m.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05")
  37. return
  38. }
  39. // 添加 System.Add_UserLogs("MqttServer","参数请求 [Rt_Parameter]","base")
  40. func Add_UserLogs(T_uuid string, UserLogs_class string, UserLogs_title string, UserLogs_txt string) {
  41. o := orm.NewOrm()
  42. m := UserLogs{T_uuid: T_uuid, T_class: UserLogs_class, T_title: UserLogs_title, T_txt: UserLogs_txt}
  43. _, err := o.Insert(&m)
  44. if err != nil {
  45. logs.Error(lib.FuncName(), err)
  46. }
  47. }
  48. func Add_UserLogs_T(Logs_uuid string, Logs_class string, Logs_Title string, Logs_Txt_T interface{}) {
  49. o := orm.NewOrm()
  50. jsonStu, err := json.Marshal(Logs_Txt_T)
  51. if err != nil {
  52. logs.Error(lib.FuncName(), err)
  53. }
  54. m := UserLogs{T_uuid: Logs_uuid, T_class: Logs_class, T_title: Logs_Title, T_txt: string(jsonStu)}
  55. _, err = o.Insert(&m)
  56. if err != nil {
  57. logs.Error(lib.FuncName(), err)
  58. }
  59. }
  60. // 获取列表
  61. func Read_UserLogs_List(T_uuid string, UserLogs_class string, UserLogs_title string, page int, page_z int) (r_ []UserLogs_R, cnt int64) {
  62. o := orm.NewOrm()
  63. // 也可以直接使用 Model 结构体作为表名
  64. qs := o.QueryTable(new(UserLogs))
  65. var offset int64
  66. if page <= 1 {
  67. offset = 0
  68. } else {
  69. offset = int64((page - 1) * page_z)
  70. }
  71. // 过滤
  72. cond := orm.NewCondition()
  73. cond1 := cond.And("T_uuid", T_uuid) //.And("T_State", 1) .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  74. if len(UserLogs_class) > 0 {
  75. //cond1.AndCond(cond.And("T_class", class))
  76. cond1 = cond1.And("T_class", UserLogs_class)
  77. }
  78. if len(UserLogs_title) > 0 {
  79. //cond1.AndCond(cond.And("T_class", class))
  80. cond1 = cond1.And("T_title", UserLogs_title)
  81. }
  82. // 查询
  83. var r []UserLogs
  84. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  85. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  86. for _, v := range r {
  87. r_ = append(r_, UserLogsToUserLogs_R(v))
  88. }
  89. return r_, cnt
  90. }
  91. type CLASS_lists struct {
  92. UserLogs_class string
  93. }
  94. // 获取列表
  95. func Read_UserLogs_Class() (lists orm2.ParamsList) {
  96. o := orm.NewOrm()
  97. var pl_lists orm2.ParamsList
  98. num, err := o.Raw("SELECT DISTINCT t_class FROM UserLogs LIMIT 0,1000").ValuesFlat(&pl_lists)
  99. if err == nil {
  100. fmt.Println("user nums: ", num)
  101. }
  102. fmt.Println(len(pl_lists))
  103. return pl_lists
  104. }