|
@@ -8,6 +8,7 @@ import (
|
|
|
"eta/eta_mini_ht_api/models"
|
|
|
"fmt"
|
|
|
"gorm.io/gorm"
|
|
|
+ "gorm.io/gorm/clause"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -66,32 +67,53 @@ func BatchInsertReport(list *[]Report) (err error) {
|
|
|
|
|
|
func InsertOrUpdateReport(list *[]Report, source string) (ids []int, err error) {
|
|
|
var orgIds []int
|
|
|
+ //现有的作者名字
|
|
|
+ var authorNames []string
|
|
|
for _, report := range *list {
|
|
|
orgIds = append(orgIds, report.OrgID)
|
|
|
+ authorNames = append(authorNames, report.Author)
|
|
|
}
|
|
|
orgIds = silce_utils.RemoveDuplicates(orgIds)
|
|
|
if orgIds == nil || len(orgIds) == 0 {
|
|
|
return
|
|
|
}
|
|
|
db := models.Main()
|
|
|
- //手动事务
|
|
|
- err = db.Model(&Report{}).Select("distinct id").Where("org_id in ? and source =? ", orgIds, source).Scan(&ids).Error
|
|
|
+ //数据库找到作者的名字
|
|
|
+ var dbAuthors []string
|
|
|
+ err = db.Model(&Report{}).Select("distinct author").Where("org_id in ? and source =? ", orgIds, source).Scan(&dbAuthors).Error
|
|
|
if err != nil {
|
|
|
logger.Error("查询研报失败:%v", err)
|
|
|
- return
|
|
|
}
|
|
|
+ var deleteAuthors []string
|
|
|
+ for _, dbAuthor := range dbAuthors {
|
|
|
+ found := false
|
|
|
+ for _, insertAuthor := range authorNames {
|
|
|
+ if dbAuthor == insertAuthor {
|
|
|
+ found = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !found {
|
|
|
+ deleteAuthors = append(deleteAuthors, dbAuthor)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //手动事务
|
|
|
tx := db.Begin()
|
|
|
- err = tx.Where("org_id in ? and source =? ", orgIds, source).Delete(&Report{}).Error
|
|
|
- if err != nil {
|
|
|
- logger.Error("批量删除研报失败:%v", err)
|
|
|
- tx.Rollback()
|
|
|
- return
|
|
|
+ OnConflictFunc := clause.OnConflict{
|
|
|
+ Columns: []clause.Column{{Name: "org_id"}, {Name: "author"}, {Name: "source"}},
|
|
|
+ DoUpdates: clause.AssignmentColumns([]string{"abstract", "title", "published_time"}),
|
|
|
}
|
|
|
- err = tx.CreateInBatches(list, MaxBatchNum).Error
|
|
|
- if err != nil {
|
|
|
- logger.Error("批量插入研报失败:%v", err)
|
|
|
- tx.Rollback()
|
|
|
- return
|
|
|
+ // 执行批量插入或更新操作
|
|
|
+ err = tx.Clauses(OnConflictFunc).Create(&list).Error
|
|
|
+ if deleteAuthors != nil {
|
|
|
+ for _, deleteAuthor := range deleteAuthors {
|
|
|
+ err = tx.Where("org_id in ? and source =? and author=?", orgIds, source, deleteAuthor).Delete(&Report{}).Error
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("批量删除研报失败:%v", err)
|
|
|
+ tx.Rollback()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
tx.Commit()
|
|
|
return
|