smart_report.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package services
  2. import (
  3. "context"
  4. "eta/eta_task/models"
  5. "eta/eta_task/services/alarm_msg"
  6. "eta/eta_task/utils"
  7. "fmt"
  8. "html"
  9. "strconv"
  10. "time"
  11. )
  12. // PublishSmartReport 定时发布智能研报
  13. func PublishSmartReport(cont context.Context) (err error) {
  14. defer func() {
  15. if err != nil {
  16. go alarm_msg.SendAlarmMsg("PublishSmartReport-定时发布智能研报失败, ErrMsg:\n"+err.Error(), 3)
  17. }
  18. }()
  19. now := time.Now().Format(utils.FormatDateTimeMinute)
  20. startTime := now + ":00"
  21. endTime := now + ":59"
  22. list, e := models.GetPrePublishSmartReports(startTime, endTime)
  23. if e != nil {
  24. err = fmt.Errorf("GetPrePublishSmartReports err: %s", e.Error())
  25. return
  26. }
  27. listLen := len(list)
  28. if listLen == 0 {
  29. return
  30. }
  31. // 比对时间(分钟), 时间相等则发布并推送
  32. for i := 0; i < listLen; i++ {
  33. item := list[i]
  34. var publishTime time.Time
  35. // 如果报告曾经发布过,并且已经发送过模版消息,则发布时间为原发布时间
  36. if item.MsgIsSend == 1 && !item.PublishTime.IsZero() {
  37. publishTime = item.PublishTime
  38. } else {
  39. publishTime = time.Now()
  40. }
  41. // 发布报告, 同时更新消息推送状态, 消息推送目前仅为预留功能, 后面小程序需要售卖的时候再细化消息推送
  42. if err = models.PublishSmartReportById(item.SmartReportId, publishTime); err != nil {
  43. return
  44. }
  45. // 写入队列
  46. var queue models.Report2ImgQueueReq
  47. queue.ReportType = 2
  48. queue.ReportCode = item.ReportCode
  49. _ = utils.Rc.LPush(utils.CACHE_CREATE_REPORT_IMGPDF_QUEUE, queue)
  50. // 生成音频, 更新ES
  51. go func() {
  52. if item.VideoUrl == "" {
  53. SmartReportBuildVideoAndUpdate(item)
  54. }
  55. _ = SmartReportElasticUpsert(item.SmartReportId, 2)
  56. }()
  57. recordItem := &models.ReportStateRecord{
  58. ReportId: item.SmartReportId,
  59. ReportType: 2,
  60. State: 2,
  61. AdminId: item.AdminId,
  62. AdminName: item.AdminRealName,
  63. CreateTime: time.Now(),
  64. }
  65. go func() {
  66. _, _ = models.AddReportStateRecord(recordItem)
  67. }()
  68. }
  69. return
  70. }
  71. // SmartReportBuildVideoAndUpdate 生成音频
  72. func SmartReportBuildVideoAndUpdate(item *models.SmartReport) {
  73. if item == nil {
  74. return
  75. }
  76. var err error
  77. defer func() {
  78. if err != nil {
  79. tips := fmt.Sprintf("智能研报-音频生成, errMsg: %s", err.Error())
  80. go alarm_msg.SendAlarmMsg(tips, 2)
  81. }
  82. }()
  83. videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(item.Title, item.Content, time.Now().Local().Format(utils.FormatDateTime))
  84. if e != nil {
  85. err = fmt.Errorf("create audio err: %s", e.Error())
  86. return
  87. }
  88. item.VideoUrl = videoUrl
  89. item.VideoName = videoName
  90. item.VideoSize = videoSize
  91. item.VideoPlaySeconds = fmt.Sprintf("%.2f", videoPlaySeconds)
  92. item.ModifyTime = time.Now().Local()
  93. cols := []string{"VideoUrl", "VideoName", "VideoSize", "VideoPlaySeconds", "ModifyTime"}
  94. if e = item.Update(cols); e != nil {
  95. err = fmt.Errorf("smart report update err: %s", e.Error())
  96. return
  97. }
  98. }
  99. // SmartReportElasticUpsert 新增/编辑报告es
  100. func SmartReportElasticUpsert(smartReportId int, state int) (err error) {
  101. if smartReportId <= 0 {
  102. return
  103. }
  104. reportOB := new(models.SmartReport)
  105. item, e := reportOB.GetItemById(smartReportId)
  106. if e != nil {
  107. if e.Error() == utils.ErrNoRow() {
  108. // 可能被删了就直接忽略掉
  109. return
  110. }
  111. err = fmt.Errorf("获取报告失败, Err: %s", e.Error())
  112. return
  113. }
  114. esReport := new(models.ElasticSmartReport)
  115. esReport.SmartReportId = item.SmartReportId
  116. esReport.Title = item.Title
  117. esReport.Abstract = item.Abstract
  118. esReport.BodyContent = utils.TrimHtml(html.UnescapeString(item.Content))
  119. esReport.PublishTime = item.PublishTime.Format(utils.FormatDateTime)
  120. esReport.PublishState = state
  121. esReport.Author = item.Author
  122. esReport.ClassifyIdFirst = item.ClassifyIdFirst
  123. esReport.ClassifyNameFirst = item.ClassifyNameFirst
  124. esReport.ClassifyIdSecond = item.ClassifyIdSecond
  125. esReport.ClassifyNameSecond = item.ClassifyNameSecond
  126. esReport.StageStr = strconv.Itoa(item.Stage)
  127. esReport.Frequency = item.Frequency
  128. if err = EsAddOrEditSmartReport(utils.SmartReportIndexName, strconv.Itoa(item.SmartReportId), esReport); err != nil {
  129. return
  130. }
  131. return
  132. }