package eta import ( "errors" "eta/eta_mini_ht_api/common/component/cache" logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/common/utils/redis" "eta/eta_mini_ht_api/models" "gorm.io/gorm" "strings" "time" ) const ( colunms = "id,author,abstract,title,publish_time,state,modify_time," detailColumn = "id,author,abstract,title,publish_time,content,content_sub,collaborate_type,report_layout,video_url,video_name,video_play_seconds,head_resource_id,end_resource_id,has_chapter,need_splice,state,modify_time" Published = 2 Passed = 6 limit = 500 TlbChartPermissionSearchKeyWordMapping = "chart_permission_search_key_word_mapping" ) var ( classifyIds = []string{"classify_id_third", "classify_id_second", "classify_id_first"} redisCache *cache.RedisCache ) func rd() *cache.RedisCache { if redisCache == nil { redisCache = cache.GetInstance() } return redisCache } func (ETAReport) TableName() string { return "report" } type ETAReport struct { ID int `gorm:"primary_key;auto_increment"` ClassifyID int `gorm:"_"` ClassifyIDFirst int `gorm:"column:classify_id_first;default:0"` ClassifyIDSecond int `gorm:"column:classify_id_second;default:0"` ClassifyIDThird int `gorm:"column:classify_id_third;default:0"` Title string `gorm:"column:title;size:125;"` Abstract string `gorm:"column:abstract;size:255;"` Author string `gorm:"column:author;size:50;"` PublishTime time.Time `gorm:"column:publish_time"` Content string `gorm:"column:content"` ContentSub string `gorm:"column:content_sub"` CollaborateType int `gorm:"column:collaborate_type"` ReportLayout int `gorm:"column:report_layout"` VideoUrl string `gorm:"column:video_url"` VideoName string `gorm:"column:video_name"` VideoPlaySeconds string `gorm:"column:video_play_seconds"` HeadResourceId int `gorm:"column:head_resource_id"` EndResourceId int `gorm:"column:end_resource_id"` HasChapter bool `gorm:"column:has_chapter"` NeedSplice bool `gorm:"column:need_splice"` ModifyTime time.Time `gorm:"column:modify_time"` State int `gorm:"column:state"` } type ReportPermission struct { ChartPermissionID int `gorm:"primaryKey;autoIncrement;column:chart_permission_id;comment:主键"` ParentID int `gorm:"size:11;default:0;column:parent_id;comment:父级权限id"` } func GetETAReports(id int) (reports []ETAReport, err error) { err = models.ETA().Table("report").Select(colunms+strings.Join(classifyIds, ",")).Where("state =? or state=?", Published, Passed).Where("id > ?", id).Order("id asc").Limit(limit).Find(&reports).Error if reports != nil { for i := 0; i < len(reports); i++ { setClassifyIdValue(&reports[i]) } } return } func GetUpdateETAReports() (filterList []ETAReport, err error) { duration := time.Now().Add(-30 * time.Second) modifyTime := duration.Format(time.DateTime) var reports []ETAReport err = models.ETA().Table("report").Select(colunms+strings.Join(classifyIds, ",")).Where("modify_time >=?", modifyTime).Order("id asc").Find(&reports).Error if err != nil { logger.Error("同步eta研报数据失败:%v", err) } if reports != nil { for i := 0; i < len(reports); i++ { setClassifyIdValue(&reports[i]) key := redis.GenerateReportRefreshKey("ETA", reports[i].ID, reports[i].ModifyTime.UnixMilli()) if rd().SetIfNotExist(key, "processed", 3600) { logger.Info("同步eta报告 ID :%d,修改时间:%v", reports[i].ID, reports[i].ModifyTime) filterList = append(filterList, reports[i]) } else { logger.Info("过滤ETA重复的消息 ID :%d,修改时间:%v", reports[i].ID, reports[i].ModifyTime) } } } return } func GetETAReportById(id int) (report ETAReport, err error) { //err = models.ETA().Table("report").Select(detailColumn).Where("id = ?", id).Where("state =? or state=?", Published, Passed).First(&report).Error // err = models.ETA().Table("report").Select(detailColumn).Where("id = ?", id).First(&report).Error if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { logger.Error("查询eta报告失败,报告已删除,报告ID:%d", id) } else { logger.Error("查询eta报告失败:%v", err) } return } if report.State != Published && report.State != Passed { logger.Error("eta报告未发布,ID:%d", id) err = errors.New("eta报告未发布") return } return } func GetReportClassifyById(id int) (classifyId int, err error) { db := models.ETA() err = db.Table("report"). Select("COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) classify_id"). Where("id =?", id).Scan(&classifyId).Error return } func setClassifyIdValue(report *ETAReport) { if report.ClassifyIDThird > 0 { report.ClassifyID = report.ClassifyIDThird return } if report.ClassifyIDSecond > 0 { report.ClassifyID = report.ClassifyIDSecond return } report.ClassifyID = report.ClassifyIDFirst }