index_task.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package data_manage
  2. import (
  3. "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // IndexTask index这边的任务表
  10. type IndexTask struct {
  11. IndexTaskID int `gorm:"primaryKey;column:index_task_id" description:"-"`
  12. TaskName string `gorm:"column:task_name" description:"任务名称"`
  13. TaskType string `gorm:"column:task_type" description:"任务类型"`
  14. Status string `gorm:"column:status" description:"任务状态,枚举值:待处理,处理成功,处理失败,暂停处理"`
  15. StartTime time.Time `gorm:"column:start_time" description:"开始时间"`
  16. EndTime time.Time `gorm:"column:end_time" description:"结束时间"`
  17. CreateTime time.Time `gorm:"column:create_time" description:"创建时间"`
  18. UpdateTime time.Time `gorm:"column:update_time" description:"更新时间"`
  19. Logs string `gorm:"column:logs" description:"日志"`
  20. Errormessage string `gorm:"column:ErrorMessage" description:"错误信息"`
  21. Priority int `gorm:"column:priority" description:"优先级"`
  22. RetryCount int `gorm:"column:retry_count" description:"重试次数"`
  23. Remark string `gorm:"column:remark" description:"备注"`
  24. SysUserID int `gorm:"column:sys_user_id" description:"任务创建人id"`
  25. SysUserRealName string `gorm:"column:sys_user_real_name" description:"任务创建人名称"`
  26. }
  27. // TableName get sql table name.获取数据库表名
  28. func (m *IndexTask) TableName() string {
  29. return "index_task"
  30. }
  31. // IndexTaskColumns get sql column name.获取数据库列名
  32. var IndexTaskColumns = struct {
  33. IndexTaskID string
  34. TaskName string
  35. TaskType string
  36. Status string
  37. StartTime string
  38. EndTime string
  39. CreateTime string
  40. UpdateTime string
  41. Logs string
  42. Errormessage string
  43. Priority string
  44. RetryCount string
  45. Remark string
  46. SysUserID string
  47. SysUserRealName string
  48. }{
  49. IndexTaskID: "index_task_id",
  50. TaskName: "task_name",
  51. TaskType: "task_type",
  52. Status: "status",
  53. StartTime: "start_time",
  54. EndTime: "end_time",
  55. CreateTime: "create_time",
  56. UpdateTime: "update_time",
  57. Logs: "logs",
  58. Errormessage: "ErrorMessage",
  59. Priority: "priority",
  60. RetryCount: "retry_count",
  61. Remark: "remark",
  62. SysUserID: "sys_user_id",
  63. SysUserRealName: "sys_user_real_name",
  64. }
  65. func (m *IndexTask) Create() (err error) {
  66. err = global.DbMap[utils.DbNameAI].Create(&m).Error
  67. return
  68. }
  69. func (m *IndexTask) Update(updateCols []string) (err error) {
  70. err = global.DbMap[utils.DbNameAI].Select(updateCols).Updates(&m).Error
  71. return
  72. }
  73. func (m *IndexTask) Del() (err error) {
  74. err = global.DbMap[utils.DbNameAI].Delete(&m).Error
  75. return
  76. }
  77. func (m *IndexTask) GetByID(id int) (item *IndexTask, err error) {
  78. err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ?", IndexTaskColumns.IndexTaskID), id).First(&item).Error
  79. return
  80. }
  81. func (m *IndexTask) GetByCondition(condition string, pars []interface{}) (item *IndexTask, err error) {
  82. sqlStr := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  83. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).First(&item).Error
  84. return
  85. }
  86. func (m *IndexTask) GetListByCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*IndexTask, err error) {
  87. if field == "" {
  88. field = "*"
  89. }
  90. sqlStr := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s order by index_task_id desc LIMIT ?,?`, field, m.TableName(), condition)
  91. pars = append(pars, startSize, pageSize)
  92. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Find(&items).Error
  93. return
  94. }
  95. func (m *IndexTask) GetCountByCondition(condition string, pars []interface{}) (total int, err error) {
  96. var intNull sql.NullInt64
  97. sqlStr := fmt.Sprintf(`SELECT COUNT(1) total FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  98. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Scan(&intNull).Error
  99. if err == nil && intNull.Valid {
  100. total = int(intNull.Int64)
  101. }
  102. return
  103. }
  104. // AddIndexTask
  105. // @Description: 添加Index模块的任务
  106. // @author: Roc
  107. // @datetime 2025-04-16 16:55:36
  108. // @param indexTask *IndexTask
  109. // @param indexRecordList []*IndexTaskRecord
  110. // @return err error
  111. func AddIndexTask(indexTask *IndexTask, indexRecordList []*IndexTaskRecord) (err error) {
  112. to := global.DbMap[utils.DbNameAI].Begin()
  113. defer func() {
  114. if err != nil {
  115. _ = to.Rollback()
  116. } else {
  117. _ = to.Commit()
  118. }
  119. }()
  120. err = to.Create(indexTask).Error
  121. if err != nil {
  122. return
  123. }
  124. for _, indexTaskRecord := range indexRecordList {
  125. indexTaskRecord.IndexTaskID = indexTask.IndexTaskID
  126. }
  127. err = to.CreateInBatches(indexRecordList, utils.MultiAddNum).Error
  128. if err != nil {
  129. return
  130. }
  131. return
  132. }