task.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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)
  32. if limit > 0 {
  33. result = result.Limit(limit).Find(&hgShFlNotasks)
  34. } else {
  35. result = result.Find(&hgShFlNotasks)
  36. }
  37. return hgShFlNotasks, result.Error
  38. }
  39. func SaveProcessedData(ctx context.Context, data *ProcessedData) error {
  40. return utils.Db.WithContext(ctx).Model(&HgFlightSearchTask{}).Create(data).Error
  41. }
  42. func UpdateTaskStatus(ctx context.Context, taskID uint, status string, attempts int) error {
  43. return utils.Db.WithContext(ctx).Model(&HgFlightSearchTask{}).
  44. Where("id = ?", taskID).
  45. Updates(map[string]interface{}{
  46. "status": status,
  47. "attempts": attempts,
  48. }).Error
  49. }