package report import ( logger "eta_mini_ht_api/common/component/log" "eta_mini_ht_api/models" "gorm.io/gorm" "time" ) type ReportStatus string type ReportSource string const ( SourceETA ReportSource = "ETA" SourceHT ReportSource = "HT" StatusInit ReportStatus = "INIT" StatusPending ReportStatus = "PENDING" StatusDone ReportStatus = "DONE" MaxBatchNum = 1000 ) type Report struct { ID int `gorm:"column:id;primary_key;comment:'id'" json:"id"` OrgID int `gorm:"column:org_id;comment:'原始id'" json:"org_id"` Source ReportSource `gorm:"column:source;comment:'研报来源1:eta 2:海通'" json:"source"` Title string `gorm:"column:title;comment:'标题'" json:"title"` Abstract string `gorm:"column:abstract;comment:'摘要'" json:"abstract"` Author string `gorm:"column:author;comment:'作者'" json:"author"` Status ReportStatus `gorm:"column:status;comment:'报告状态 init:初始化 pending:同步中 done:完成同步'" json:"status"` CreatedTime time.Time `gorm:"column:created_time;comment:'创建时间'" json:"created_time"` UpdatedTime time.Time `gorm:"column:updated_time;comment:'修改时间'" json:"updated_time"` } func BatchInsertReport(list *[]Report) error { db := models.Main() //手动事务 tx := db.Begin() err := db.CreateInBatches(list, MaxBatchNum).Error if err != nil { logger.Error("批量插入研报失败:%v", err) } tx.Commit() return nil } func (t *Report) BeforeCreate(_ *gorm.DB) (err error) { t.CreatedTime = time.Now() return } func GetETALatestReportId() (id int, err error) { sql := "select IFNULL(max(org_id),0) from reports where source = ?" err = DoSql(sql, &id, SourceETA) return } func DoSql(sql string, result interface{}, values ...interface{}) (err error) { db := models.Main() err = db.Raw(sql, values...).Scan(&result).Error if err != nil { logger.Error("执行sql[%v]失败:%v", sql, err) } return }