SysLogs.go 3.2 KB

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