report.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package report
  2. import (
  3. logger "eta_mini_ht_api/common/component/log"
  4. "eta_mini_ht_api/models"
  5. "fmt"
  6. "gorm.io/gorm"
  7. "time"
  8. )
  9. type ReportStatus string
  10. type ReportSource string
  11. const (
  12. SourceETA ReportSource = "ETA"
  13. SourceHT ReportSource = "HT"
  14. StatusInit ReportStatus = "INIT"
  15. StatusPending ReportStatus = "PENDING"
  16. StatusDone ReportStatus = "DONE"
  17. MaxBatchNum = 1000
  18. CommonColumns = "id,org_id,author,abstract,title,published_time"
  19. )
  20. type Report struct {
  21. ID int `gorm:"column:id;primary_key;comment:'id'" json:"id"`
  22. OrgID int `gorm:"column:org_id;comment:'原始id'" json:"org_id"`
  23. Source ReportSource `gorm:"column:source;comment:'研报来源1:eta 2:海通'" json:"source"`
  24. Title string `gorm:"column:title;comment:'标题'" json:"title"`
  25. Abstract string `gorm:"column:abstract;comment:'摘要'" json:"abstract"`
  26. Author string `gorm:"column:author;comment:'作者'" json:"author"`
  27. Status ReportStatus `gorm:"column:status;comment:'报告状态 init:初始化 pending:同步中 done:完成同步'" json:"status"`
  28. PublishedTime string `gorm:"column:published_time;comment:'发布时间'" json:"published_time"`
  29. CreatedTime time.Time `gorm:"column:created_time;comment:'创建时间'" json:"created_time"`
  30. UpdatedTime time.Time `gorm:"column:updated_time;comment:'修改时间'" json:"updated_time"`
  31. }
  32. func BatchInsertReport(list *[]Report) error {
  33. db := models.Main()
  34. //手动事务
  35. tx := db.Begin()
  36. err := db.CreateInBatches(list, MaxBatchNum).Error
  37. if err != nil {
  38. logger.Error("批量插入研报失败:%v", err)
  39. }
  40. tx.Commit()
  41. return nil
  42. }
  43. func (t *Report) BeforeCreate(_ *gorm.DB) (err error) {
  44. t.CreatedTime = time.Now()
  45. return
  46. }
  47. func GetETALatestReportId() (id int, err error) {
  48. sql := "select IFNULL(max(org_id),0) from reports where source = ?"
  49. err = DoSql(sql, &id, SourceETA)
  50. return
  51. }
  52. func DoSql(sql string, result interface{}, values ...interface{}) (err error) {
  53. db := models.Main()
  54. err = db.Raw(sql, values...).Scan(&result).Error
  55. if err != nil {
  56. logger.Error("执行sql[%v]失败:%v", sql, err)
  57. }
  58. return
  59. }
  60. func GetListByCondition(column string, limit int, order models.Order) (reports []Report, err error) {
  61. db := models.Main()
  62. err = db.Select(CommonColumns).Order(fmt.Sprintf("%s %s", column, order)).Limit(limit).Find(&reports).Error
  63. if err != nil {
  64. logger.Error("查询报告列表失败:%v", err)
  65. }
  66. if reports == nil {
  67. return []Report{}, nil
  68. }
  69. return
  70. }