package report import ( "errors" logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/common/utils/date" silce_utils "eta/eta_mini_ht_api/common/utils/silce" "eta/eta_mini_ht_api/models" permissionDao "eta/eta_mini_ht_api/models/config" "fmt" "gorm.io/gorm" "gorm.io/gorm/clause" "time" ) type ReportStatus string type ReportSource string type SendStatus string const ( SEND SendStatus = "SEND" UNSEND SendStatus = "UNSEND" SourceETA ReportSource = "ETA" SourceHT ReportSource = "HT" StatusPublish = "PUBLISH" StatusUnPublish ReportStatus = "UNPUBLISH" StatusDeleted ReportStatus = "DELETED" MaxBatchNum = 1000 CommonColumns = "id,org_id,author,abstract,title,source,cover_src,published_time,status,plate_name,classify_id" taskColumns = "id,author,published_time,status,plate_name" ) 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"` ClassifyId int `gorm:"column:classify_id"` PlateName string `gorm:"column:plate_name;comment:'板块'" json:"plate_name"` Author string `gorm:"column:author;comment:'作者'" json:"author"` CoverSrc int `gorm:"column:cover_src;comment:'封面图片'" json:"cover_src"` Status ReportStatus `gorm:"column:status;comment:'报告状态 publish:发布 unpublish:未发布" json:"status"` SendStatus SendStatus `gorm:"column:send_status;comment:'发送状态'" json:"send_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 GetOrgIdsByPlateNames(plateName []string) (ids []int, err error) { db := models.Main() err = db.Model(&Report{}).Select("distinct org_id").Where(" source ='HT' and plate_name in ? ", plateName).Scan(&ids).Error return } func BatchInsertReport(list *[]Report) (err error) { db := models.Main() //手动事务 tx := db.Begin() err = tx.CreateInBatches(list, MaxBatchNum).Error if err != nil { logger.Error("批量插入研报失败:%v", err) tx.Rollback() return } tx.Commit() return nil } func InsertOrUpdateReport(list []Report, source string) (result []Report, err error) { var orgIds []int //现有的作者名字 for _, report := range list { orgIds = append(orgIds, report.OrgID) } orgIds = silce_utils.RemoveDuplicates(orgIds) db := models.Main() //数据库找到作者的名字 if err != nil { logger.Error("查询研报失败:%v", err) } for i := 0; i < len(list); i++ { var dbReport Report err = db.Model(&Report{}).Select(CommonColumns).Where("org_id = ? and source =? ", list[i].OrgID, source).First(&dbReport).Error if err != nil { logger.Error("查询数据库失败研报失败:%v,发布时间不更新", err) } if dbReport.SendStatus == SEND { list[i].PublishedTime = dbReport.PublishedTime } } //手动事务 tx := db.Begin() OnConflictFunc := clause.OnConflict{ Columns: []clause.Column{{Name: "org_id"}, {Name: "source"}}, DoUpdates: clause.AssignmentColumns([]string{"abstract", "title", "author", "published_time", "status", "classify_id"}), } // 执行批量插入或更新操作 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 // } // } //} if err != nil { logger.Error("批量插入或更新研报失败:%v", err) tx.Rollback() return } tx.Commit() err = db.Select(CommonColumns).Where("org_id in ? and source =? ", orgIds, source).Find(&result).Error if err != nil { logger.Error("查询更新的研报数据失败:%v", err) err = nil } return } func (t *Report) BeforeCreate(_ *gorm.DB) (err error) { t.CreatedTime = time.Now() return } func GetAuthorByOrgId(orgId int, source string) (names []string, err error) { db := models.Main() err = db.Model(&Report{}).Select("author").Where("org_id = ? and source =? ", orgId, source).Scan(&names).Error return } func GetReportByOrgId(orgId int, source string) (reports Report, err error) { db := models.Main() err = db.Select(CommonColumns).Where("org_id = ? and source =? ", orgId, source).Find(&reports).Error return } func GetReportById(reportId int) (report Report, err error) { db := models.Main() err = db.Select(CommonColumns).Where("id = ?", reportId).First(&report).Error if err != nil { logger.Error("查询报告失败:%v", err) } return } func GetLatestReportIdBySource(source ReportSource) (id int, err error) { sql := "select IFNULL(max(org_id),0) from reports where source = ?" err = DoSql(sql, &id, source) 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 GetListOrderByCondition(week bool, column string, limit int, order models.Order) (reports []Report, err error) { db := models.Main() if week { current := time.Now() begin := date.GetBeginOfTheWeek(current, time.Monday).Format(time.DateOnly) end := current.Format(time.DateOnly) err = db.Select(CommonColumns).Where("status = ?", StatusPublish).Where(" STR_TO_DATE(published_time,'%Y-%m-%d') BETWEEN ? AND ?", begin, end).Order(fmt.Sprintf("%s %s", column, order)).Limit(limit).Find(&reports).Error } else { err = db.Select(CommonColumns).Where("status = ?", StatusPublish).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 } func GetListByCondition[T any](column string, values []T) (reports []Report, err error) { if len(values) == 0 { logger.Error("查询条件的值不能为空") return []Report{}, nil } db := models.Main() err = db.Select(CommonColumns).Where(fmt.Sprintf("%s in ?", column), values).Find(&reports).Error if err != nil { logger.Error("查询报告列表失败:%v", err) } if reports == nil { return []Report{}, nil } return } func GetReportIdListByOrgIds(orgIds map[string][]int) (ids []int, err error) { db := models.Main() if len(orgIds["ETA"]) == 0 && len(orgIds["HT"]) == 0 { return } if len(orgIds["ETA"]) == 0 { err = db.Model(&Report{}).Select("id").Where("status = ?", StatusPublish).Where(" source='HT' and org_id in ?", orgIds["HT"]).Scan(&ids).Error if err != nil { logger.Error("获取报告ID列表失败:%v", err) return } return } if len(orgIds["HT"]) == 0 { err = db.Model(&Report{}).Select("id").Where("status = ?", StatusPublish).Where(" source='ETA' and org_id in ?", orgIds["ETA"]).Scan(&ids).Error if err != nil { logger.Error("获取报告ID列表失败:%v", err) return } return } err = db.Model(&Report{}).Select("id").Where("status = ?", StatusPublish).Where(" source='ETA' and org_id in ?", orgIds["ETA"]).Or("source='HT' and org_id in ?", orgIds["HT"]).Scan(&ids).Error if err != nil { logger.Error("获取报告ID列表失败:%v", err) return } return } func GetMaxIdByPermissionIds(orgIds map[string][]int, disCardIds []int) (total int64, maxId int64, err error) { db := models.Main() if len(orgIds["ETA"]) == 0 && len(orgIds["HT"]) == 0 { maxId = 0 total = 0 return } countQuery := db.Model(&Report{}).Select("count(*)").Where("status = ? ", StatusPublish) maxQuery := db.Model(&Report{}).Select("MAX(id) id").Where("status = ? ", StatusPublish) if len(disCardIds) > 0 { countQuery.Where("id not in ?", disCardIds) maxQuery.Where("id not in ?", disCardIds) } if len(orgIds["ETA"]) == 0 { err = countQuery.Where(" source='HT' and org_id in ?", orgIds["HT"]).Scan(&total).Error if err != nil { logger.Error("获取记录条数失败:%v", err) return } err = maxQuery.Where(" source='HT' and org_id in ? ", orgIds["HT"]).Scan(&maxId).Error if err != nil { logger.Error("获取报告最大ID失败:%v", err) return } return } if len(orgIds["HT"]) == 0 { err = countQuery.Where(" source='ETA' and org_id in ? ", orgIds["ETA"]).Scan(&total).Error if err != nil { logger.Error("获取报告最大ID失败:%v", err) return } err = maxQuery.Where(" source='ETA' and org_id in ? ", orgIds["ETA"]).Scan(&maxId).Error if err != nil { logger.Error("获取报告最大ID失败:%v", err) return } return } err = countQuery.Where(" source='ETA' and org_id in ? ", orgIds["ETA"]).Or("source='HT' and org_id in ?", orgIds["HT"]).Scan(&total).Error if err != nil { logger.Error("获取报告最大ID失败:%v", err) return } err = maxQuery.Where(" source='ETA' and org_id in ?", orgIds["ETA"]).Or("source='HT' and org_id in ?", orgIds["HT"]).Scan(&maxId).Error if err != nil { logger.Error("获取报告最大ID失败:%v", err) return } return } func GetTotalPageCount() (total int64, latestId int64, err error) { db := models.Main() err = db.Model(&Report{}).Select("MAX(id) id").Where("status = ?", StatusPublish).Scan(&latestId).Error if err != nil { logger.Error("获取最大id失败:%v", err) } err = db.Model(&Report{}).Where("status = ?", StatusPublish).Count(&total).Error if err != nil { logger.Error("统计报告数量失败:%v", err) } return } func GetReportsByAnalyst(analyst string) (ids []int, err error) { db := models.Main() err = db.Model(&Report{}).Select("id").Where("status = ?", StatusPublish).Where("author like ?", "%"+analyst+"%").Scan(&ids).Error if err != nil { return } return } func GetTotalPageCountByAnalyst(analyst string) (total int64, latestId int64) { db := models.Main() err := db.Model(&Report{}).Where("status = ?", StatusPublish).Where("author like ?", "%"+analyst+"%").Count(&total).Error if err != nil { return } err = db.Model(&Report{}).Select("Max(id)").Where("status = ?", StatusPublish).Where("author like ?", "%"+analyst+"%").Scan(&latestId).Error if err != nil { return } return } func GetReportPage(latestId int64, limit int, offset int) (list []Report, err error) { if latestId < 0 { err = errors.New("非法的id参数") logger.Error("非法的id参数:%d", latestId) return } if limit <= 0 { err = errors.New("非法的limit参数") logger.Error("非法的limit参数:%d", limit) } db := models.Main() err = db.Select(CommonColumns).Where("id<= ?", latestId).Where("status = ?", StatusPublish).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error return } func GetReportPageByOrgIds(latestId int64, limit int, offset int, orgIds map[string][]int, discardIds []int) (list []Report, err error) { if latestId < 0 { err = errors.New("非法的id参数") logger.Error("非法的id参数:%d", latestId) return } if limit <= 0 { err = errors.New("非法的limit参数") logger.Error("非法的limit参数:%d", limit) } db := models.Main() listQuery := db.Model(&Report{}).Select(CommonColumns).Where("status = ? ", StatusPublish).Where("id<= ?", latestId) if len(discardIds) > 0 { listQuery.Where("id not in ?", discardIds) } if len(orgIds["ETA"]) == 0 { err = listQuery.Where(" source='HT' and org_id in ?", orgIds["HT"]).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error return } if len(orgIds["HT"]) == 0 { err = listQuery.Where("source='ETA' and org_id in ?", orgIds["ETA"]).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error return } err = listQuery.Where("(source='ETA' and org_id in ? ) or (source='HT' and org_id in ?) ", orgIds["ETA"], orgIds["HT"]).Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error return } func GetNewReportByPublishTime(time time.Time) (list []Report) { db := models.Main() err := db.Select(taskColumns).Where("status = ?", StatusPublish).Where("published_time >= ?", time).Order("published_time desc").Find(&list).Error if err != nil { logger.Error("查询新发布的报告列表失败:%v", err) } return } func GetReportPageByAnalyst(latestId int64, limit int, offset int, analyst string, reportIds []int) (list []Report, err error) { if len(reportIds) == 0 { logger.Info("reportIds为空") return } if latestId < 0 { err = errors.New("非法的id参数") logger.Error("非法的id参数:%d", latestId) return } if limit <= 0 { err = errors.New("非法的limit参数") logger.Error("非法的limit参数:%d", limit) } db := models.Main() err = db.Select(CommonColumns).Where("status = ?", StatusPublish).Where("id<= ? and id in ? and author like ?", latestId, reportIds, "%"+analyst+"%").Order("published_time desc").Limit(limit).Offset(offset).Find(&list).Error return } func GetETAReportIdsByClassifyIds(classifyIds []int) (orgIds []int, err error) { db := models.Main() err = db.Model(&Report{}).Select("DISTINCT org_id").Where("source=? and classify_id in (?)", SourceETA, classifyIds).Scan(&orgIds).Error return } func GetETAReportByClassifyIds(classifyIds []int) (reportList []Report, err error) { db := models.Main() err = db.Model(&Report{}).Select(CommonColumns).Where("source=? and classify_id in (?)", SourceETA, classifyIds).Find(&reportList).Error return } func GetReportClassifyById(id int) (classifyId int, err error) { db := models.Main() err = db.Model(&Report{}). Select("classify_id"). Where("org_id=? and source ='ETA' ", id).Scan(&classifyId).Error return } func CountPermissionWeight(ids []int) (list []permissionDao.PermissionWeight, err error) { db := models.Main() sql := `select a.permission_id,count(*) from (select p.permission_id from reports r LEFT JOIN(SELECT *from permissions) p on p.name=r.plate_name where r.id in (?) and r.source='HT' UNION ALL select cpm.permission_id from reports r LEFT JOIN (SELECT classify_id,permission_id FROM permission_classify_mapping) cpm on cpm.classify_id=r.classify_id where r.id in (?) and r.source='ETA') a GROUP BY a.permission_id` err = db.Raw(sql, ids, ids).Find(&list).Error return } func GetHiddenReportIds(classifyIds []int, plateNames []string) (reportIds []int, err error) { db := models.Main() exc := db.Model(&Report{}).Select("id") if len(classifyIds) > 0 { exc.Where("(source='ETA' and classify_id in ?)", classifyIds) } if len(plateNames) > 0 { exc.Or("(source='HT' and plate_name in ?)", plateNames) } err = exc.Scan(&reportIds).Error return } func FilterReportIds(ids []int) (total int64, reportIds []int, err error) { db := models.Main() etaSubQuery := ` select a.classify_id from ( select classify_id, IFNULL( GROUP_CONCAT(permissions.risk_level SEPARATOR ','),'') as risks from permission_classify_mapping left join permissions on permissions.permission_id = permission_classify_mapping.permission_id group by classify_id ) a where a.risks = '' ` htSubQuery := ` select a.name from (SELECT name,IFNULL(risk_level,'') as risk_level FROM permissions) a WHERE a.risk_level ='' ` err = db.Model(&Report{}).Select("id"). Where("id in ?", ids). Where("(classify_id not in (?) and source=?) or (plate_name not in (?) and source=?)", gorm.Expr(etaSubQuery), SourceETA, gorm.Expr(htSubQuery), SourceHT). Where("Status = ?", StatusPublish). Scan(&reportIds).Error if err != nil { logger.Error("查询过滤后的报告失败: %v", err) } total = int64(len(reportIds)) return } func GetReportListById(ids []int) (reports []Report, err error) { db := models.Main() err = db.Select(CommonColumns). Where("id in ?", ids).Find(&reports).Error return } func DeleteReport(id int) error { db := models.Main() return db.Model(&Report{}).Where("id=?", id).Update("status", StatusDeleted).Error }