12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package task
- import (
- logger "eta/eta_mini_ht_api/common/component/log"
- "eta/eta_mini_ht_api/models"
- "gorm.io/gorm"
- "time"
- )
- // VerificationStatus is an enumeration for the status field.
- type TaskStatus string
- const (
- StatusPending TaskStatus = "PENDING"
- StatusProcess TaskStatus = "PROGRESS"
- StatusSuccess TaskStatus = "SUCCESS"
- StatusFailed TaskStatus = "FAILED"
- )
- type Task struct {
- ID int `gorm:"primary_key;auto_increment;comment:'主键'"`
- TaskType string `gorm:"type:enum('');default:null;comment:'任务类型'"`
- Details string `gorm:"type:json;default:null;comment:'任务详情'"`
- Status TaskStatus `gorm:"type:enum('PENDING','PROGRESS','SUCCESS','FAILED');not null;comment:'任务状态'"`
- CreatedTime time.Time `gorm:"default:null;comment:'创建时间'"`
- UpdatedTime time.Time `gorm:"default:null;comment:'更新时间'"`
- }
- // 在使用前,需要注册 Enum 类型
- func (v *Task) BeforeCreate(_ *gorm.DB) (err error) {
- v.CreatedTime = time.Now()
- if v.Status == "" {
- v.Status = StatusPending
- }
- return
- }
- func InitTask(task Task) (taskId int, err error) {
- err = models.Main().Create(&task).Error
- if err != nil {
- logger.Error("创建任务记录失败:%v", err)
- }
- taskId = task.ID
- return
- }
- func UpdateTaskStatusById(taskId int, status TaskStatus) (err error) {
- err = updateColumnById(taskId, "status", status)
- if err != nil {
- logger.Error("修改任务状态失败,id:[%d],状态:%v:%v", taskId, status, err)
- }
- return
- }
- func UpdateDetailsAndStatusById(taskId int, status TaskStatus, details string) {
- columns := map[string]interface{}{
- "details": details,
- "status": status,
- }
- err := updateColumnsById(taskId, columns)
- if err != nil {
- logger.Error("修改任务失败,id:[%d]:%v", taskId, err)
- }
- return
- }
- func updateColumnsById(id int, value interface{}) (err error) {
- err = models.Main().Model(&Task{}).Where("id =?", id).UpdateColumns(value).Error
- return
- }
- func updateColumnById(id int, column string, value interface{}) (err error) {
- err = models.Main().Model(&Task{}).Where("id =?", id).UpdateColumn(column, value).Error
- return
- }
|