report_service.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. package report
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_ht_api/common/component/config"
  5. "eta/eta_mini_ht_api/common/component/es"
  6. logger "eta/eta_mini_ht_api/common/component/log"
  7. "eta/eta_mini_ht_api/common/contants"
  8. "eta/eta_mini_ht_api/common/utils/page"
  9. stringUtils "eta/eta_mini_ht_api/common/utils/string"
  10. analystService "eta/eta_mini_ht_api/domian/financial_analyst"
  11. userService "eta/eta_mini_ht_api/domian/user"
  12. "eta/eta_mini_ht_api/models"
  13. "eta/eta_mini_ht_api/models/eta"
  14. etaDao "eta/eta_mini_ht_api/models/eta"
  15. "eta/eta_mini_ht_api/models/ht"
  16. reportDao "eta/eta_mini_ht_api/models/report"
  17. "github.com/google/uuid"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. const (
  23. SourceETA = "ETA"
  24. SourceHT = "HT"
  25. DESC models.Order = "desc"
  26. ASC models.Order = "asc"
  27. ESColumn = "title"
  28. ESRangeColumn = "reportId"
  29. )
  30. var (
  31. sortField = []string{"_score:desc"}
  32. htConfig = config.GetConfig(contants.HT).(*config.HTBizConfig)
  33. )
  34. func elastic() *es.ESClient {
  35. return es.GetInstance()
  36. }
  37. // ESReport Report ES研报mapping
  38. type ESReport struct {
  39. ReportID int `json:"reportId"`
  40. OrgId int `json:"orgId"`
  41. Title string `json:"title"`
  42. Author string `json:"author"`
  43. Source reportDao.ReportSource `json:"source"`
  44. Abstract string `json:"abstract"`
  45. CoverSrc string `json:"coverSrc"`
  46. Status reportDao.ReportStatus `json:"status"`
  47. PublishedTime string `json:"publishedTime"`
  48. }
  49. type ReportDTO struct {
  50. ReportID int `json:"reportId"`
  51. OrgId int `json:"orgId"`
  52. Title string `json:"title"`
  53. Author string `json:"author"`
  54. Source string `json:"source"`
  55. Abstract string `json:"abstract"`
  56. PublishedTime string `json:"publishedTime"`
  57. SecondPermission map[int]string `json:"-"`
  58. Permissions map[int]string `json:"-"`
  59. PermissionNames interface{} `json:"permissionNames,omitempty"`
  60. Highlight []string `json:"highlight,omitempty"`
  61. Detail json.RawMessage `json:"detail,omitempty"`
  62. CoverSrc string `json:"coverSrc"`
  63. }
  64. type Detail struct {
  65. }
  66. type PermissionDTO struct {
  67. ID int
  68. Name string
  69. ParentID int
  70. }
  71. func GetGetReportById(reportId int) (ReportDTO ReportDTO, err error) {
  72. report, err := reportDao.GetReportById(reportId)
  73. if err != nil {
  74. return
  75. }
  76. orgId := report.OrgID
  77. names, _ := reportDao.GetAuthorByOrgId(orgId, string(report.Source))
  78. if names != nil && len(names) > 1 {
  79. names = stringUtils.UniqueItems(names)
  80. report.Author = strings.Join(names, ",")
  81. }
  82. report.PublishedTime = report.PublishedTime[:10]
  83. ReportDTO = convertReportDTO(report)
  84. return
  85. }
  86. func GetTotalPageCount() (total int64) {
  87. return reportDao.GetTotalPageCount()
  88. }
  89. func GetTotalPageCountByAnalyst(analyst string) (total int64, latestId int64) {
  90. return reportDao.GetTotalPageCountByAnalyst(analyst)
  91. }
  92. func SearchMaxReportId(key string) (total int64, reportId int64) {
  93. sort := []string{"reportId:desc"}
  94. request := matchAll(sort, key)
  95. //同步es
  96. re, err := elastic().Count(request)
  97. if err != nil {
  98. logger.Error("es搜索异常:%v", err)
  99. }
  100. count := re.Count
  101. total = int64(count)
  102. if total > 0 {
  103. request = match(key, 0, count, sort)
  104. re, err = elastic().Search(request)
  105. if err != nil {
  106. logger.Error("es搜索异常:%v", err)
  107. }
  108. hits := elastic().GetSource(re.Hits)
  109. data := hits[0].Source
  110. report := ReportDTO{}
  111. err = json.Unmarshal(data, &report)
  112. if err != nil {
  113. logger.Error("获取当前最大研报id失败:%v", err)
  114. return
  115. }
  116. reportId = int64(report.ReportID)
  117. }
  118. return
  119. }
  120. func SearchReportList(key string, from int, size int, max int64) (reports []ReportDTO, err error) {
  121. //同步es
  122. sorts := append(sortField, "publishedTime:desc")
  123. request := matchRange(key, from, size, max, sorts)
  124. re, err := elastic().Search(request)
  125. if err != nil {
  126. logger.Error("es搜索异常:%v", err)
  127. }
  128. hits := elastic().GetSource(re.Hits)
  129. if len(hits) == 0 {
  130. reports = []ReportDTO{}
  131. return
  132. }
  133. for _, hit := range hits {
  134. var content map[string][]string
  135. err = json.Unmarshal(hit.Highlight, &content)
  136. report := ReportDTO{}
  137. err = json.Unmarshal(hit.Source, &report)
  138. if err != nil {
  139. logger.Error("解析研报数据失败:%v", err)
  140. continue
  141. }
  142. report.Highlight = content[ESColumn]
  143. report.Title = report.Highlight[0]
  144. report.PublishedTime = report.PublishedTime[:10]
  145. reports = append(reports, report)
  146. }
  147. return
  148. }
  149. func GetReportPageByAnalyst(pageInfo page.PageInfo, analyst string) (list []ReportDTO, err error) {
  150. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  151. reports, err := reportDao.GetReportPageByAnalyst(pageInfo.LatestId, pageInfo.PageSize, offset, analyst)
  152. if err != nil {
  153. logger.Error("分页查询报告列表失败:%v", err)
  154. return
  155. }
  156. list = make([]ReportDTO, 0)
  157. if reports != nil {
  158. for _, report := range reports {
  159. dto := convertReportDTO(report)
  160. list = append(list, dto)
  161. }
  162. }
  163. return
  164. }
  165. func GetReportPageByOrgIds(pageInfo page.PageInfo, orgIds []int) (list []ReportDTO, err error) {
  166. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  167. reports, err := reportDao.GetReportPageByOrgIds(pageInfo.LatestId, pageInfo.PageSize, offset, orgIds)
  168. if err != nil {
  169. logger.Error("分页查询报告列表失败:%v", err)
  170. return
  171. }
  172. list = make([]ReportDTO, 0)
  173. if reports != nil {
  174. for _, report := range reports {
  175. dto := convertReportDTO(report)
  176. list = append(list, dto)
  177. }
  178. }
  179. return
  180. }
  181. func GetNewReportByPublishTime(time time.Time) (reports []ReportDTO) {
  182. list := reportDao.GetNewReportByPublishTime(time)
  183. if list != nil {
  184. for _, report := range list {
  185. dto := convertReportDTO(report)
  186. reports = append(reports, dto)
  187. }
  188. }
  189. return
  190. }
  191. func GetReportPage(pageInfo page.PageInfo) (list []ReportDTO, err error) {
  192. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  193. reports, err := reportDao.GetReportPage(pageInfo.LatestId, pageInfo.PageSize, offset)
  194. if err != nil {
  195. logger.Error("分页查询报告列表失败:%v", err)
  196. return
  197. }
  198. list = make([]ReportDTO, 0)
  199. if reports != nil {
  200. for _, report := range reports {
  201. dto := convertReportDTO(report)
  202. list = append(list, dto)
  203. }
  204. }
  205. return
  206. }
  207. func getETAReportFirstPermissions(id int) (permissionDTOs []PermissionDTO) {
  208. classifyId, err := etaDao.GetReportClassifyById(id)
  209. if err != nil || classifyId == 0 {
  210. logger.Error("获取研报分类信息失败:%v", err)
  211. return
  212. }
  213. permissions, err := etaDao.GetFirstPermissionsByClassifyID(classifyId)
  214. if err != nil {
  215. logger.Error("获取研报一级品种信息失败:%v", err)
  216. return
  217. }
  218. for _, permission := range permissions {
  219. permissionDTOs = append(permissionDTOs, convertPermissionDTO(permission))
  220. }
  221. return
  222. }
  223. func getETAReportSecondPermissions(id int) (permissionDTOs []PermissionDTO) {
  224. classifyId, err := etaDao.GetReportClassifyById(id)
  225. if err != nil || classifyId == 0 {
  226. logger.Error("获取研报分类信息失败:%v", err)
  227. return
  228. }
  229. permissions, err := etaDao.GetSecondPermissionsByClassifyID(classifyId)
  230. if err != nil {
  231. logger.Error("获取研报二级品种信息失败:%v", err)
  232. return
  233. }
  234. for _, permission := range permissions {
  235. permissionDTOs = append(permissionDTOs, convertPermissionDTO(permission))
  236. }
  237. return
  238. }
  239. func (es ESReport) GetId() string {
  240. return strconv.Itoa(es.ReportID)
  241. }
  242. func GetETALatestReportId() (id int, err error) {
  243. return reportDao.GetLatestReportIdBySource(reportDao.SourceETA)
  244. }
  245. func GetHTLatestReportId() (id int, err error) {
  246. return reportDao.GetLatestReportIdBySource(reportDao.SourceHT)
  247. }
  248. func SyncETAReportList(list []eta.ETAReport) (err error) {
  249. logger.Info("同步研报数量%d", len(list))
  250. var reports []reportDao.Report
  251. for _, etaRp := range list {
  252. authorNames := strings.Split(etaRp.Author, ",")
  253. authorNamesWithOutEmpty := stringUtils.RemoveEmptyStrings(authorNames)
  254. for _, authorName := range authorNamesWithOutEmpty {
  255. destRp := convertEtaReport(etaRp)
  256. destRp.Author = authorName
  257. reports = append(reports, destRp)
  258. }
  259. }
  260. err = reportDao.BatchInsertReport(&reports)
  261. if err != nil {
  262. logger.Error("同步ETA研报失败:%v", err)
  263. return
  264. }
  265. //for _, etaRp := range reports {
  266. // esRp := convertEsReport(etaRp)
  267. // esReports = append(esReports, esRp)
  268. //}
  269. return syncESAndSendMessage(reports)
  270. ////同步es
  271. //err = elastic().BulkInsert(htConfig.GetReportIndex(), esReports)
  272. //if err != nil {
  273. // logger.Error("同步ETA研报到es失败:%v", err)
  274. // return
  275. //}
  276. ////生产meta信息
  277. //logger.Info("生成推送META信息")
  278. //for _, report := range reports {
  279. // userIds := userService.GetPostUser(report.Author, report.PublishedTime)
  280. // var author analystService.FinancialAnalystDTO
  281. // author, err = analystService.GetAnalystByName(report.Author)
  282. // if err != nil {
  283. // logger.Error("获取研报作者失败:%v", err)
  284. // continue
  285. // }
  286. // if len(userIds) > 0 {
  287. // usersStr := stringUtils.IntToStringSlice(userIds)
  288. // Meta := userService.MetaData{
  289. // AuthorName: report.Author,
  290. // AuthorId: author.Id,
  291. // SourceId: report.ID,
  292. // PublishedTime: report.PublishedTime,
  293. // }
  294. // metaStr, _ := json.Marshal(Meta)
  295. // toStr := strings.Join(usersStr, ",")
  296. // UUID := uuid.New()
  297. // uuidStr := UUID.String()
  298. // metaContent := userService.MetaInfoDTO{
  299. // From: "ETA",
  300. // Uid: "report:" + uuidStr,
  301. // Meta: string(metaStr),
  302. // MetaType: "USER_NOTICE",
  303. // SourceType: "REPORT",
  304. // To: toStr,
  305. // }
  306. // err = userService.CreateMetaInfo(metaContent)
  307. // if err != nil {
  308. // logger.Error("创建Meta信息失败:%v", err)
  309. // return err
  310. // }
  311. // }
  312. //}
  313. }
  314. func syncESAndSendMessage(reports []reportDao.Report) (err error) {
  315. var esReports []es.ESBase
  316. for _, etaRp := range reports {
  317. esRp := convertEsReport(etaRp)
  318. esReports = append(esReports, esRp)
  319. }
  320. //同步es
  321. err = elastic().BulkInsert(htConfig.GetReportIndex(), esReports)
  322. if err != nil {
  323. logger.Error("同步ETA研报到es失败:%v", err)
  324. return
  325. }
  326. //生产meta信息
  327. logger.Info("生成推送META信息")
  328. for _, report := range reports {
  329. userIds := userService.GetPostUser(report.Author, report.PublishedTime)
  330. var author analystService.FinancialAnalystDTO
  331. author, err = analystService.GetAnalystByName(report.Author)
  332. if err != nil {
  333. logger.Error("获取研报作者失败:%v", err)
  334. continue
  335. }
  336. if len(userIds) > 0 {
  337. usersStr := stringUtils.IntToStringSlice(userIds)
  338. Meta := userService.MetaData{
  339. AuthorName: report.Author,
  340. AuthorId: author.Id,
  341. SourceId: report.ID,
  342. PublishedTime: report.PublishedTime,
  343. }
  344. metaStr, _ := json.Marshal(Meta)
  345. toStr := strings.Join(usersStr, ",")
  346. UUID := uuid.New()
  347. uuidStr := UUID.String()
  348. metaContent := userService.MetaInfoDTO{
  349. From: "HT",
  350. Uid: "report:" + uuidStr,
  351. Meta: string(metaStr),
  352. MetaType: "USER_NOTICE",
  353. SourceType: "REPORT",
  354. To: toStr,
  355. }
  356. err = userService.CreateMetaInfo(metaContent)
  357. if err != nil {
  358. logger.Error("创建Meta信息失败:%v", err)
  359. return err
  360. }
  361. }
  362. }
  363. return
  364. }
  365. func SyncHTReportList(list []ht.HTReport) (err error) {
  366. logger.Info("同步研报数量%d", len(list))
  367. var reports []reportDao.Report
  368. for _, htRp := range list {
  369. var authorStr string
  370. authorStr, err = reportDao.GetGLAuthorNames(htRp.Plate, htRp.Permission)
  371. if err != nil {
  372. logger.Error("获取钢联研报作者失败:%v", err)
  373. }
  374. if authorStr != "" {
  375. htRp.Author = authorStr
  376. }
  377. authorNames := strings.Split(htRp.Author, ",")
  378. authorNamesWithOutEmpty := stringUtils.RemoveEmptyStrings(authorNames)
  379. for _, authorName := range authorNamesWithOutEmpty {
  380. destRp := convertHTReport(htRp)
  381. destRp.Author = authorName
  382. reports = append(reports, destRp)
  383. }
  384. }
  385. err = reportDao.BatchInsertReport(&reports)
  386. if err != nil {
  387. logger.Error("同步HT研报失败:%v", err)
  388. return
  389. }
  390. return syncESAndSendMessage(reports)
  391. }
  392. func GetListOrderByConditionWeekly(week bool, column string, limit int, order models.Order) (dtoList []ReportDTO, err error) {
  393. reports, err := reportDao.GetListOrderByCondition(week, column, limit, order)
  394. if err != nil {
  395. logger.Error("获取研报失败:%v", err)
  396. return
  397. }
  398. for _, report := range reports {
  399. dto := convertReportDTO(report)
  400. dtoList = append(dtoList, dto)
  401. }
  402. return
  403. }
  404. func GetListByCondition[T any](column string, ids []T) (dtoList []ReportDTO, err error) {
  405. var values []interface{}
  406. for _, id := range ids {
  407. values = append(values, id)
  408. }
  409. reports, err := reportDao.GetListByCondition(column, ids)
  410. if err != nil {
  411. logger.Error("获取研报失败:%v", err)
  412. return
  413. }
  414. for _, report := range reports {
  415. dto := convertReportDTO(report)
  416. dtoList = append(dtoList, dto)
  417. }
  418. return
  419. }
  420. func GetTotalPageCountByPermissionIds(permissionIds []int) (total int64, latestId int64, ids []int) {
  421. //TODO 一期品种筛选reportIds
  422. htIds := []int{}
  423. etaIds, err := GetETAReportIdsByPermissionIds(permissionIds)
  424. if err != nil {
  425. logger.Error("品种筛选eta报告id失败:%v", err)
  426. etaIds = []int{}
  427. }
  428. total = int64(len(etaIds) + len(htIds))
  429. ids = append(etaIds, htIds...)
  430. latestId = reportDao.GetMaxIdByPermissionIds(ids)
  431. return
  432. }
  433. func convertEtaReport(etaRp eta.ETAReport) reportDao.Report {
  434. return reportDao.Report{
  435. OrgID: etaRp.ID,
  436. Title: etaRp.Title,
  437. Abstract: etaRp.Abstract,
  438. Author: etaRp.Author,
  439. PublishedTime: etaRp.PublishTime,
  440. Source: reportDao.SourceETA,
  441. SendStatus: reportDao.UNSEND,
  442. Status: reportDao.StatusInit,
  443. }
  444. }
  445. func convertHTReport(etaRp ht.HTReport) reportDao.Report {
  446. return reportDao.Report{
  447. OrgID: etaRp.ID,
  448. Title: etaRp.Title,
  449. Abstract: etaRp.Abstract,
  450. Author: etaRp.Author,
  451. PublishedTime: etaRp.PublishTime,
  452. Source: reportDao.SourceHT,
  453. SendStatus: reportDao.UNSEND,
  454. Status: reportDao.StatusInit,
  455. }
  456. }
  457. func convertEsReport(report reportDao.Report) ESReport {
  458. return ESReport{
  459. ReportID: report.ID,
  460. Title: report.Title,
  461. OrgId: report.OrgID,
  462. Author: report.Author,
  463. Source: report.Source,
  464. Abstract: report.Abstract,
  465. Status: report.Status,
  466. CoverSrc: report.CoverSrc,
  467. PublishedTime: report.PublishedTime,
  468. }
  469. }
  470. func convertReportDTO(report reportDao.Report) (reportDTO ReportDTO) {
  471. reportDTO = ReportDTO{
  472. ReportID: report.ID,
  473. Title: report.Title,
  474. OrgId: report.OrgID,
  475. Author: report.Author,
  476. Source: string(report.Source),
  477. Abstract: report.Abstract,
  478. PublishedTime: report.PublishedTime,
  479. }
  480. publishDate, err := time.Parse(time.DateTime, reportDTO.PublishedTime)
  481. if err == nil {
  482. reportDTO.PublishedTime = publishDate.Format(time.DateOnly)
  483. }
  484. return
  485. }
  486. func matchAll(sorts []string, key string) (request *es.ESQueryRequest) {
  487. req := new(es.ESQueryRequest)
  488. return req.CreateESQueryRequest(htConfig.GetReportIndex(), ESColumn, key, 0, 1, sorts, es.MatchAll)
  489. }
  490. func match(key string, from int, to int, sorts []string) (request *es.ESQueryRequest) {
  491. req := new(es.ESQueryRequest)
  492. return req.CreateESQueryRequest(htConfig.GetReportIndex(), ESColumn, key, from, to, sorts, es.Match)
  493. }
  494. func matchRange(key string, from int, to int, max int64, sorts []string) (request *es.ESQueryRequest) {
  495. req := new(es.ESQueryRequest)
  496. return req.CreateESQueryRequest(htConfig.GetReportIndex(), ESColumn, key, from, to, sorts, es.Range).Range(0, max, ESRangeColumn)
  497. }