task.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package models
  2. import (
  3. "context"
  4. "go-policy-service/utils"
  5. "gorm.io/gorm"
  6. )
  7. type HgFlightSearchTask struct {
  8. ID uint `gorm:"primaryKey;column:id"`
  9. Dep string `gorm:"type:varchar(12);column:dep"`
  10. Arr string `gorm:"type:varchar(12);column:arr"`
  11. DateRange string `gorm:"type:varchar(12);column:date_range"`
  12. TripType string `gorm:"type:char(2);column:trip_type"`
  13. FlightNo string `gorm:"type:varchar(12);column:flight_no"`
  14. Flight string `gorm:"type:char(2);column:flight"`
  15. CreatedAt uint `gorm:"column:created_at"` // 如果你想用 time.Time,可以改成 time.Time 并加解析
  16. UpdatedAt uint `gorm:"column:updated_at"`
  17. MerchantID uint `gorm:"column:merchant_id;default:3"`
  18. ServiceTag string `gorm:"type:varchar(45);column:service_tag"`
  19. }
  20. // TableName 设置表名
  21. func (HgFlightSearchTask) TableName() string {
  22. return "hg_flight_search_task"
  23. }
  24. type ProcessedData struct {
  25. gorm.Model
  26. TaskID uint
  27. Data string `gorm:"type:text"`
  28. }
  29. func GetPendingTasks(ctx context.Context, limit int) ([]HgFlightSearchTask, error) {
  30. var hgShFlNotasks []HgFlightSearchTask
  31. result := utils.Db.WithContext(ctx).Model(&HgFlightSearchTask{}).Where("status = ?", 1).Limit(limit).Find(&hgShFlNotasks)
  32. return hgShFlNotasks, result.Error
  33. }
  34. func SaveProcessedData(ctx context.Context, data *ProcessedData) error {
  35. return utils.Db.WithContext(ctx).Model(&HgFlightSearchTask{}).Create(data).Error
  36. }
  37. func UpdateTaskStatus(ctx context.Context, taskID uint, status string, attempts int) error {
  38. return utils.Db.WithContext(ctx).Model(&HgFlightSearchTask{}).
  39. Where("id = ?", taskID).
  40. Updates(map[string]interface{}{
  41. "status": status,
  42. "attempts": attempts,
  43. }).Error
  44. }