TaskLogs.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package Task
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. _ "github.com/astaxie/beego/cache/redis"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. _ "github.com/go-sql-driver/mysql"
  9. "time"
  10. )
  11. type TaskLogs struct {
  12. Id int `orm:"column(ID);size(11);auto;pk"`
  13. T_uuid string `orm:"size(256);null"` // 操作用户uuid
  14. T_task_id string `orm:"size(256);null"` // 任务ID
  15. Logs_class string `orm:"size(256);null"` //
  16. Logs_Title string `orm:"size(256);null"` // 标题
  17. Logs_Txt string `orm:"type(text);null"` // 详情
  18. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  19. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  20. }
  21. func (t *TaskLogs) TableName() string {
  22. return "task_logs" // 数据库名称 // ************** 替换 FormulaList **************
  23. }
  24. func init() {
  25. //注册模型
  26. orm.RegisterModel(new(TaskLogs))
  27. }
  28. type TaskLogs_R struct {
  29. T_uuid string // 操作用户uuid
  30. T_name string // 操作用户name
  31. T_task_id string // 任务ID
  32. Logs_class string // 分类
  33. Logs_Title string // 标题
  34. Logs_Txt string // 详情
  35. CreateTime string
  36. }
  37. func TaskLogsToTaskLogs_R(T TaskLogs, adminMap map[string]string) (T_r TaskLogs_R) {
  38. T_r.T_uuid = T.T_uuid
  39. T_r.T_name = adminMap[T.T_uuid]
  40. T_r.T_task_id = T.T_task_id
  41. T_r.Logs_class = T.Logs_class
  42. T_r.Logs_Title = T.Logs_Title
  43. T_r.Logs_Txt = T.Logs_Txt
  44. T_r.CreateTime = T.CreateTime.Format("2006-01-02 15:04:05")
  45. return T_r
  46. }
  47. func Add_TaskLogs_T(T_uuid string, T_task_id string, Logs_class string, Logs_Title string, Logs_Txt_T interface{}) {
  48. o := orm.NewOrm()
  49. jsonStu, err := json.Marshal(Logs_Txt_T)
  50. if err != nil {
  51. fmt.Println("Add_TaskLogs_T JSON ,err=", err)
  52. }
  53. m := TaskLogs{T_uuid: T_uuid, T_task_id: T_task_id, Logs_class: Logs_class, Logs_Title: Logs_Title, Logs_Txt: string(jsonStu)}
  54. o.Insert(&m)
  55. }
  56. // 获取列表
  57. func Read_TaskLogs_List(T_task_id string, adminMap map[string]string, page int, page_z int) ([]TaskLogs_R, int) {
  58. o := orm.NewOrm()
  59. var offset int
  60. if page <= 1 {
  61. offset = 0
  62. } else {
  63. offset = (page - 1) * page_z
  64. }
  65. qs := o.QueryTable(new(TaskLogs))
  66. var r []TaskLogs
  67. cond := orm.NewCondition()
  68. cond1 := cond.And("T_task_id", T_task_id)
  69. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-CreateTime").All(&r)
  70. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  71. // 转换
  72. var TaskLogsList []TaskLogs_R
  73. for _, v := range r {
  74. TaskLogsList = append(TaskLogsList, TaskLogsToTaskLogs_R(v, adminMap))
  75. }
  76. return TaskLogsList, int(cnt)
  77. }