Browse Source

一期研报

kobe6258 7 months ago
parent
commit
23a8075355

+ 5 - 0
domian/report/permission_service.go

@@ -36,6 +36,11 @@ func GetFirstPermissionsByIds(ids []int) (permissionDTOS []PermissionDTO, err er
 	return
 }
 
+// 获取二级品种id
+func GetPermissionIdByName(name string) (permissionId int, err error) {
+	return etaDao.GetPermissionIdByName(name)
+}
+
 // 获取一级品种(海通的板块)
 func GetReportPermissionsById(id int, source string) (permissionIds []PermissionDTO) {
 	switch source {

+ 70 - 128
domian/report/report_service.go

@@ -326,54 +326,7 @@ func SyncETAReportList(list []eta.ETAReport) (err error) {
 		logger.Error("同步ETA研报失败:%v", err)
 		return
 	}
-	//for _, etaRp := range reports {
-	//	esRp := convertEsReport(etaRp)
-	//	esReports = append(esReports, esRp)
-	//}
 	return syncESAndSendMessage(reports)
-	////同步es
-	//err = elastic().BulkInsert(htConfig.GetReportIndex(), esReports)
-	//if err != nil {
-	//	logger.Error("同步ETA研报到es失败:%v", err)
-	//	return
-	//}
-	////生产meta信息
-	//logger.Info("生成推送META信息")
-	//for _, report := range reports {
-	//	userIds := userService.GetPostUser(report.Author, report.PublishedTime)
-	//	var author analystService.FinancialAnalystDTO
-	//	author, err = analystService.GetAnalystByName(report.Author)
-	//	if err != nil {
-	//		logger.Error("获取研报作者失败:%v", err)
-	//		continue
-	//	}
-	//	if len(userIds) > 0 {
-	//		usersStr := stringUtils.IntToStringSlice(userIds)
-	//		Meta := userService.MetaData{
-	//			AuthorName:    report.Author,
-	//			AuthorId:      author.Id,
-	//			SourceId:      report.ID,
-	//			PublishedTime: report.PublishedTime,
-	//		}
-	//		metaStr, _ := json.Marshal(Meta)
-	//		toStr := strings.Join(usersStr, ",")
-	//		UUID := uuid.New()
-	//		uuidStr := UUID.String()
-	//		metaContent := userService.MetaInfoDTO{
-	//			From:       "ETA",
-	//			Uid:        "report:" + uuidStr,
-	//			Meta:       string(metaStr),
-	//			MetaType:   "USER_NOTICE",
-	//			SourceType: "REPORT",
-	//			To:         toStr,
-	//		}
-	//		err = userService.CreateMetaInfo(metaContent)
-	//		if err != nil {
-	//			logger.Error("创建Meta信息失败:%v", err)
-	//			return err
-	//		}
-	//	}
-	//}
 }
 func syncESAndSendMessage(reports []reportDao.Report) (err error) {
 	var esReports []es.ESBase
@@ -439,90 +392,52 @@ func syncES(reports []reportDao.Report) (err error) {
 		logger.Error("同步ETA研报到es失败:%v", err)
 		return
 	}
-	////生产meta信息
-	//logger.Info("生成推送META信息")
-	//for _, report := range reports {
-	//	userIds := userService.GetPostUser(report.Author, report.PublishedTime)
-	//	var author analystService.FinancialAnalystDTO
-	//	author, err = analystService.GetAnalystByName(report.Author)
-	//	if err != nil {
-	//		logger.Error("获取研报作者失败:%v", err)
-	//		continue
-	//	}
-	//	if len(userIds) > 0 {
-	//		usersStr := stringUtils.IntToStringSlice(userIds)
-	//		Meta := userService.MetaData{
-	//			AuthorName:    report.Author,
-	//			AuthorId:      author.Id,
-	//			SourceId:      report.ID,
-	//			PublishedTime: report.PublishedTime,
-	//		}
-	//		metaStr, _ := json.Marshal(Meta)
-	//		toStr := strings.Join(usersStr, ",")
-	//		UUID := uuid.New()
-	//		uuidStr := UUID.String()
-	//		metaContent := userService.MetaInfoDTO{
-	//			From:       "HT",
-	//			Uid:        "report:" + uuidStr,
-	//			Meta:       string(metaStr),
-	//			MetaType:   "USER_NOTICE",
-	//			SourceType: "REPORT",
-	//			To:         toStr,
-	//		}
-	//		err = userService.CreateMetaInfo(metaContent)
-	//		if err != nil {
-	//			logger.Error("创建Meta信息失败:%v", err)
-	//			return err
-	//		}
-	//	}
-	//}
 	return
 }
 func InitHTReportList(list []ht.HTReport) (err error) {
 	logger.Info("同步研报数量%d", len(list))
 	var reports []reportDao.Report
-	for _, htRp := range list {
-		var authorStr string
-		authorStr, err = reportDao.GetGLAuthorNames(htRp.Plate, htRp.Permission)
-		if err != nil {
-			logger.Error("获取钢联研报作者失败:%v", err)
-		}
-		if authorStr != "" {
-			htRp.Author = authorStr
-		}
-		authorNames := strings.Split(htRp.Author, ",")
-		authorNamesWithOutEmpty := stringUtils.RemoveEmptyStrings(authorNames)
-		for _, authorName := range authorNamesWithOutEmpty {
-			destRp := convertHTReport(htRp)
-			destRp.Author = authorName
-			reports = append(reports, destRp)
-		}
-	}
-	err = reportDao.BatchInsertReport(&reports)
+	//获取系统中ht品种名
+	permissions, err := reportDao.GetGLAuthorNames()
 	if err != nil {
-		logger.Error("同步HT研报失败:%v", err)
+		logger.Error("获取钢联研报作者失败:%v", err)
 		return
 	}
-	return syncES(reports)
-}
-func SyncHTReportList(list []ht.HTReport) (err error) {
-	logger.Info("同步研报数量%d", len(list))
-	var reports []reportDao.Report
 	for _, htRp := range list {
-		var authorStr string
-		authorStr, err = reportDao.GetGLAuthorNames(htRp.Plate, htRp.Permission)
-		if err != nil {
-			logger.Error("获取钢联研报作者失败:%v", err)
-		}
-		if authorStr != "" {
-			htRp.Author = authorStr
-		}
-		authorNames := strings.Split(htRp.Author, ",")
-		authorNamesWithOutEmpty := stringUtils.RemoveEmptyStrings(authorNames)
-		for _, authorName := range authorNamesWithOutEmpty {
-			destRp := convertHTReport(htRp)
-			destRp.Author = authorName
-			reports = append(reports, destRp)
+		for _, permission := range permissions {
+			if htRp.PermissionName == permission.Permission {
+				if permission.AuthorNames != "" {
+					htRp.PublishUserName = permission.AuthorNames
+				}
+				authorNames := strings.Split(htRp.PublishUserName, ",")
+				authorNamesWithOutEmpty := stringUtils.RemoveEmptyStrings(authorNames)
+				for _, authorName := range authorNamesWithOutEmpty {
+					destRp := convertHTReport(htRp)
+					destRp.Author = authorName
+					var coverSrc int
+					permissionId, err := etaDao.GetPermissionIdByName(htRp.PermissionName)
+					if err != nil {
+						logger.Error("HT获取eta品种id失败:%v", err)
+						coverSrc = 0
+					}
+					ids, err := mediaDao.GetIdsByPermissionId(permissionId)
+					if err != nil {
+						logger.Error("获取图片资源失败:%v", err)
+						coverSrc = 0
+					}
+					if ids == nil || len(ids) == 0 {
+						coverSrc = 0
+					} else {
+						src := rand.NewSource(time.Now().UnixNano())
+						r := rand.New(src)
+						// 从切片中随机选择一个元素
+						randomIndex := r.Intn(len(ids))
+						coverSrc = ids[randomIndex]
+					}
+					destRp.CoverSrc = coverSrc
+					reports = append(reports, destRp)
+				}
+			}
 		}
 	}
 	err = reportDao.BatchInsertReport(&reports)
@@ -530,7 +445,35 @@ func SyncHTReportList(list []ht.HTReport) (err error) {
 		logger.Error("同步HT研报失败:%v", err)
 		return
 	}
-	return syncESAndSendMessage(reports)
+	return syncES(reports)
+}
+func SyncHTReportList(list []ht.HTReport) (err error) {
+	//logger.Info("同步研报数量%d", len(list))
+	//var reports []reportDao.Report
+	//for _, htRp := range list {
+	//	var authorStr string
+	//	authorStr, err = reportDao.GetGLAuthorNames(htRp.Plate, htRp.Permission)
+	//	if err != nil {
+	//		logger.Error("获取钢联研报作者失败:%v", err)
+	//	}
+	//	if authorStr != "" {
+	//		htRp.Author = authorStr
+	//	}
+	//	authorNames := strings.Split(htRp.Author, ",")
+	//	authorNamesWithOutEmpty := stringUtils.RemoveEmptyStrings(authorNames)
+	//	for _, authorName := range authorNamesWithOutEmpty {
+	//		destRp := convertHTReport(htRp)
+	//		destRp.Author = authorName
+	//		reports = append(reports, destRp)
+	//	}
+	//}
+	//err = reportDao.BatchInsertReport(&reports)
+	//if err != nil {
+	//	logger.Error("同步HT研报失败:%v", err)
+	//	return
+	//}
+	//return syncESAndSendMessage(reports)
+	return
 }
 func GetListOrderByConditionWeekly(week bool, column string, limit int, order models.Order) (dtoList []ReportDTO, err error) {
 	reports, err := reportDao.GetListOrderByCondition(week, column, limit, order)
@@ -595,11 +538,10 @@ func convertEtaReport(etaRp eta.ETAReport) reportDao.Report {
 
 func convertHTReport(etaRp ht.HTReport) reportDao.Report {
 	return reportDao.Report{
-		OrgID:         etaRp.ID,
-		Title:         etaRp.Title,
-		Abstract:      etaRp.Abstract,
-		Author:        etaRp.Author,
-		PublishedTime: etaRp.PublishTime,
+		OrgID:         etaRp.Id,
+		Title:         etaRp.ReportName,
+		Author:        etaRp.PublishUserName,
+		PublishedTime: etaRp.PublishedTime,
 		CoverSrc:      0,
 		Source:        reportDao.SourceHT,
 		SendStatus:    reportDao.UNSEND,

+ 53 - 17
main.go

@@ -7,10 +7,12 @@ import (
 	"eta/eta_mini_ht_api/common/exception"
 	"eta/eta_mini_ht_api/domian/report"
 	"eta/eta_mini_ht_api/models/eta"
+	"eta/eta_mini_ht_api/models/ht"
 	_ "eta/eta_mini_ht_api/routers"
 	_ "eta/eta_mini_ht_api/task"
 	"github.com/beego/beego/v2/server/web"
 	"sync"
+	"time"
 )
 
 func main() {
@@ -33,7 +35,7 @@ func main() {
 
 func initReport() {
 	var wg sync.WaitGroup
-	wg.Add(1)
+	wg.Add(2)
 	logger.Info("开始初始化研报库")
 	go func() {
 		defer wg.Done()
@@ -55,22 +57,56 @@ func initReport() {
 			}
 		}
 	}()
-	//go func() {
-	//	defer wg.Done()
-	//	id, err := report.GetHTLatestReportId()
-	//	var htReportList []ht.HTReport
-	//	htReportList, err = ht.GetHTReports(id)
-	//	if err != nil {
-	//		logger.Error("获取ETA研报列表失败:%v", err)
-	//	}
-	//	if len(htReportList) > 0 {
-	//		err = report.InitHTReportList(htReportList)
-	//		if err != nil {
-	//			logger.Error("同步ETA研报列表失败:%v", err)
-	//		}
-	//	}
-	//	logger.Info(contants.TaskFormat, "同步ETA研报库结束")
-	//}()
+	go func() {
+		defer wg.Done()
+		id, err := report.GetHTLatestReportId()
+		var htReportList []ht.HTReport
+		htReportList, err = ht.GetHTReports(id)
+		if err != nil {
+			logger.Error("获取ETA研报列表失败:%v", err)
+		}
+		for i := 0; i < len(htReportList); i++ {
+			timestamp := int64(htReportList[i].PublishTime)
+			t := time.UnixMilli(timestamp)
+			htReportList[i].PublishedTime = t.Format(time.DateTime)
+			plateId := htReportList[i].PlateId
+			plate, err := ht.GetPermissionNameById(plateId)
+			if err != nil || plate.ParentId == 0 {
+				htReportList[i].PermissionName = htReportList[i].PlateName
+			} else {
+				PermissionName, err := getPermissionNameById(plate.ParentId)
+				if err != nil {
+					logger.Error("获取ETA研报列表失败:%v", err)
+					htReportList[i].PermissionName = ""
+				} else {
+					htReportList[i].PermissionName = PermissionName
+				}
+			}
+		}
+		x := len(htReportList)
+		if x > 0 {
+			logger.Info(contants.TaskFormat, "同步ETA研报库结束")
+		}
+		if len(htReportList) > 0 {
+			err = report.InitHTReportList(htReportList)
+			if err != nil {
+				logger.Error("同步ETA研报列表失败:%v", err)
+			}
+		}
+		logger.Info(contants.TaskFormat, "同步ETA研报库结束")
+	}()
 	wg.Wait()
 	logger.Info("初始化研报库完成")
 }
+
+func getPermissionNameById(id int) (name string, err error) {
+	plate, err := ht.GetPermissionNameById(id)
+	if err != nil {
+		return
+	}
+	if plate.ParentId != 0 {
+		return getPermissionNameById(plate.ParentId)
+	} else {
+		return plate.PlateName, nil
+	}
+}

+ 6 - 0
models/eta/eta_permission.go

@@ -56,6 +56,12 @@ func GetFirstPermissionsByIds(ids []int) (chartPermissionList []ChartPermission,
 	err = doSql(sql, &chartPermissionList, ids)
 	return
 }
+
+func GetPermissionIdByName(name string) (id int, err error) {
+	sql := "select chart_permission_id from chart_permission where permission_name =? "
+	err = doSql(sql, &id, name)
+	return
+}
 func doSql(sql string, result interface{}, values ...interface{}) (err error) {
 	db := models.ETA()
 	err = db.Raw(sql, values...).Find(result).Error

+ 18 - 0
models/ht/ht_plate.go

@@ -0,0 +1,18 @@
+package ht
+
+import (
+	"eta/eta_mini_ht_api/models"
+)
+
+type HTPlate struct {
+	Id        int
+	ParentId  int
+	PlateName string
+}
+
+func GetPermissionNameById(id int) (htPlate HTPlate, err error) {
+	db := models.HT()
+	sql := "select id ,plate_name, parent_id from t_iirp_plate where id=?"
+	err = db.Model(&HTReport{}).Raw(sql, id).Scan(&htPlate).Error
+	return
+}

+ 9 - 8
models/ht/ht_report.go

@@ -3,18 +3,19 @@ package ht
 import "eta/eta_mini_ht_api/models"
 
 type HTReport struct {
-	ID          int    `gorm:"primary_key;auto_increment"`
-	Plate       string `gorm:"column:plate"`
-	Author      string `gorm:"column:author"`
-	Permission  string `gorm:"column:permission"`
-	Title       string `gorm:"column:title;size:125;"`
-	Abstract    string `gorm:"column:abstract;size:255;"`
-	PublishTime string `gorm:"column:publish_time"`
+	Id              int    `gorm:"primary_key;auto_increment"`
+	PlateId         int    `grom:"plate_id"`
+	PlateName       string `gorm:"column:plate_name"`
+	PermissionName  string `gorm:"column:permission_name"`
+	PublishUserName string `gorm:"column:publish_user_name"`
+	ReportName      string `gorm:"column:report_name"`
+	PublishTime     int    `gorm:"column:publish_time"`
+	PublishedTime   string `gorm:"column:published_time"`
 }
 
 func GetHTReports(id int) (reports []HTReport, err error) {
 	db := models.HT()
-	sql := "select * from report where   id >? order by id asc"
+	sql := "select tirtp.plate_id as plate_id, tip.plate_name as plate_name, t.id as id ,t.report_name as report_name,t.publish_user_name as publish_user_name,t.publish_time as publish_time from t_iirp_report_to_plate tirtp left join t_iirp_report_info t on t.id=tirtp.report_id left JOIN t_iirp_plate tip on tip.id=tirtp.plate_id  where t.is_delete =0 and t.`status` =3 and t.report_file_path<>'' and t.id > ? order by t.id asc"
 	err = db.Model(&HTReport{}).Raw(sql, id).Scan(&reports).Error
 	return
 }

+ 4 - 5
models/report/gl_report_author_mappings.go

@@ -2,23 +2,22 @@ package report
 
 import "eta/eta_mini_ht_api/models"
 
-type GLReportAuthorMappings struct {
+type GLReportAuthorMapping struct {
 	ID          int    `gorm:"column:id;primary_key;comment:'id'" json:"id"`
-	Plate       int    `gorm:"column:plate;comment:'板块'" json:"plate"`
 	Permission  string `gorm:" column:permission;comment:'权限'" json:"permission"`
 	AuthorNames string `gorm:"column:author_names;comment:'作者'" json:"author_names"f`
 }
 
-func GetGLAuthorNames(plate, permission string) (names string, err error) {
+func GetGLAuthorNames() (mappingList []GLReportAuthorMapping, err error) {
 	db := models.Main()
-	err = db.Model(&GLReportAuthorMappings{}).Select("author_names").Where("plate = ? and permission = ?", plate, permission).Scan(&names).Error
+	err = db.Model(&GLReportAuthorMapping{}).Select("author_names").Find(&mappingList).Error
 	return
 }
 
 func GetPlateByPermissionName(permissionName string) (plate string, err error) {
 	var plates []string
 	db := models.Main()
-	err = db.Model(&GLReportAuthorMappings{}).Select("distinct plate").Where("permission = ?", permissionName).Scan(&plates).Error
+	err = db.Model(&GLReportAuthorMapping{}).Select("distinct plate").Where("permission = ?", permissionName).Scan(&plates).Error
 	if err != nil {
 		plate = ""
 		return