meta_info.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package user
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/models"
  5. "gorm.io/gorm"
  6. "time"
  7. )
  8. type StatusType string
  9. type SourceType string
  10. type MetaType string
  11. const (
  12. UserNoticeType MetaType = "USER_NOTICE"
  13. InitStatusType StatusType = "INIT"
  14. PendingStatusType StatusType = "PENDING"
  15. FinishStatusType StatusType = "FINISH"
  16. FailedStatusType StatusType = "FAILED"
  17. ReportSourceType SourceType = "REPORT"
  18. VideoSourceType SourceType = "VIDEO"
  19. AudioSourceType SourceType = "AUDIO"
  20. )
  21. // MetaInfo 表示 meta_infos 表的模型
  22. type MetaInfo struct {
  23. Id int `gorm:"primaryKey;autoIncrement;column:id"`
  24. Meta string `gorm:"column:meta"`
  25. From string `gorm:"column:from"`
  26. To string `gorm:"column:to"`
  27. SourceType SourceType `gorm:"column:source_type;type:enum('REPORT','VIDEO','AUDIO')"`
  28. MetaType MetaType `gorm:"column:meta_type;type:enum('USER_NOTICE')"`
  29. Status StatusType `gorm:"column:status;type:enum('INIT','PENDING','FINISH','FAILED')"`
  30. CreatedTime time.Time
  31. UpdatedTime time.Time
  32. }
  33. func (mt *MetaInfo) BeforeCreate(_ *gorm.DB) (err error) {
  34. mt.CreatedTime = time.Now()
  35. mt.Status = InitStatusType
  36. return nil
  37. }
  38. func CreateMetaInfo(metaInfo MetaInfo) (err error) {
  39. db := models.Main()
  40. return db.Create(&metaInfo).Error
  41. }
  42. func GetInitMetaInfos() (list []MetaInfo) {
  43. db := models.Main()
  44. err := db.Where("status = ?", InitStatusType).Order("id asc").Limit(100).Find(&list).Error
  45. if err != nil {
  46. logger.Error("获取meta数据失败:%v", err)
  47. return []MetaInfo{}
  48. }
  49. return list
  50. }
  51. func PendingMetaInfo(id int) bool {
  52. return updateStatus(id, PendingStatusType)
  53. }
  54. func FinishMetaInfo(id int) bool {
  55. return updateStatus(id, FinishStatusType)
  56. }
  57. func FailedMetaInfo(id int) bool {
  58. return updateStatus(id, FailedStatusType)
  59. }
  60. func updateStatus(id int, status StatusType) bool {
  61. db := models.Main()
  62. err := db.Model(&MetaInfo{}).Where("id = ?", id).Update("status", status).Error
  63. if err != nil {
  64. logger.Error("更新meta数据失败:%v", err)
  65. return false
  66. }
  67. return true
  68. }