123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package report
- import (
- logger "eta_mini_ht_api/common/component/log"
- "eta_mini_ht_api/models"
- "fmt"
- "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
- CommonColumns = "id,org_id,author,abstract,title,published_time"
- )
- 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"`
- PublishedTime string `gorm:"column:published_time;comment:'发布时间'" json:"published_time"`
- 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
- }
- func GetListByCondition(column string, limit int, order models.Order) (reports []Report, err error) {
- db := models.Main()
- err = db.Select(CommonColumns).Order(fmt.Sprintf("%s %s", column, order)).Limit(limit).Find(&reports).Error
- if err != nil {
- logger.Error("查询报告列表失败:%v", err)
- }
- if reports == nil {
- return []Report{}, nil
- }
- return
- }
|