report_service.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package report
  2. import (
  3. "eta_mini_ht_api/common/component/es"
  4. logger "eta_mini_ht_api/common/component/log"
  5. "eta_mini_ht_api/models"
  6. "eta_mini_ht_api/models/eta"
  7. etaDao "eta_mini_ht_api/models/eta"
  8. reportDao "eta_mini_ht_api/models/report"
  9. "strconv"
  10. )
  11. const (
  12. indexName = "report_index"
  13. )
  14. func elastic() *es.ESClient {
  15. return es.GetInstance()
  16. }
  17. // ESReport Report ES研报mapping
  18. type ESReport struct {
  19. ReportID int `json:"report_id"`
  20. OrgId int `json:"org_id"`
  21. Author string `json:"author"`
  22. Source reportDao.ReportSource `json:"source"`
  23. Abstract string `json:"abstract"`
  24. Status reportDao.ReportStatus `json:"status"`
  25. }
  26. type ReportDTO struct {
  27. ReportID int `json:"report_id"`
  28. OrgId int `json:"org_id"`
  29. Title string `json:"title"`
  30. Author string `json:"author"`
  31. Source reportDao.ReportSource `json:"source"`
  32. Abstract string `json:"abstract"`
  33. PublishedTime string `json:"published_time"`
  34. }
  35. type PermissionDTO struct {
  36. ID int
  37. Name string
  38. ParentID int
  39. }
  40. func GetPermissionList() (dtoList []PermissionDTO, err error) {
  41. list, err := etaDao.GetChartPermissionList()
  42. if err != nil {
  43. logger.Error("获取研报列表失败")
  44. return
  45. }
  46. dtoList = make([]PermissionDTO, 0)
  47. for _, node := range list {
  48. dto := convertPermissionDTO(node)
  49. dtoList = append(dtoList, dto)
  50. }
  51. return
  52. }
  53. func GetReportLabelsById(id int, source reportDao.ReportSource) {
  54. switch source {
  55. case reportDao.SourceETA:
  56. //eta.GetReportLabelById()
  57. return
  58. case reportDao.SourceHT:
  59. return
  60. }
  61. }
  62. func (es ESReport) GetId() string {
  63. return strconv.Itoa(es.ReportID)
  64. }
  65. func GetETALatestReportId() (id int, err error) {
  66. return reportDao.GetETALatestReportId()
  67. }
  68. func SyncETAReportList(list []eta.ETAReport) (err error) {
  69. logger.Info("同步研报数量%d", len(list))
  70. var reports []reportDao.Report
  71. var esReports []es.ESBase
  72. for _, etaRp := range list {
  73. destRp := convertEtaReport(etaRp)
  74. reports = append(reports, destRp)
  75. }
  76. err = reportDao.BatchInsertReport(&reports)
  77. if err != nil {
  78. logger.Error("同步ETA研报失败:%v", err)
  79. return
  80. }
  81. for _, etaRp := range reports {
  82. esRp := convertEsReport(etaRp)
  83. esReports = append(esReports, esRp)
  84. }
  85. //同步es
  86. err = elastic().BulkInsert(indexName, esReports)
  87. if err != nil {
  88. logger.Error("同步ETA研报到es失败:%v", err)
  89. return
  90. }
  91. return
  92. }
  93. func GetListByConditionDesc(column string, limit int) (dtoList []ReportDTO, err error) {
  94. reports, err := reportDao.GetListByCondition(column, limit, models.Desc)
  95. if err != nil {
  96. logger.Error("获取研报失败:%v", err)
  97. return
  98. }
  99. dtoList = []ReportDTO{}
  100. for _, report := range reports {
  101. dtoList = append(dtoList, convertReportDTO(report))
  102. }
  103. return
  104. }
  105. func GetListByConditionAsc(column string, limit int) (dtoList []ReportDTO, err error) {
  106. reports, err := reportDao.GetListByCondition(column, limit, models.Asc)
  107. if err != nil {
  108. logger.Error("获取研报失败:%v", err)
  109. return
  110. }
  111. for _, report := range reports {
  112. dtoList = append(dtoList, convertReportDTO(report))
  113. }
  114. return
  115. }
  116. func convertEtaReport(etaRp eta.ETAReport) reportDao.Report {
  117. return reportDao.Report{
  118. OrgID: etaRp.ID,
  119. Title: etaRp.Title,
  120. Abstract: etaRp.Abstract,
  121. Author: etaRp.Author,
  122. PublishedTime: etaRp.PublishTime,
  123. Source: reportDao.SourceETA,
  124. Status: reportDao.StatusInit,
  125. }
  126. }
  127. func convertEsReport(report reportDao.Report) ESReport {
  128. return ESReport{
  129. ReportID: report.ID,
  130. OrgId: report.OrgID,
  131. Author: report.Author,
  132. Source: report.Source,
  133. Abstract: report.Abstract,
  134. Status: report.Status,
  135. }
  136. }
  137. func convertReportDTO(report reportDao.Report) ReportDTO {
  138. return ReportDTO{
  139. ReportID: report.ID,
  140. Title: report.Title,
  141. OrgId: report.OrgID,
  142. Author: report.Author,
  143. Source: report.Source,
  144. Abstract: report.Abstract,
  145. PublishedTime: report.PublishedTime,
  146. }
  147. }
  148. func convertPermissionDTO(node etaDao.ChartPermission) PermissionDTO {
  149. return PermissionDTO{
  150. ID: node.ChartPermissionID,
  151. Name: node.PermissionName,
  152. ParentID: node.ParentID,
  153. }
  154. }