report.go 2.0 KB

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