smart_report.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/smart_report"
  6. "eta/eta_api/services/alarm_msg"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "html"
  10. "strconv"
  11. "time"
  12. )
  13. // SmartReportBuildVideoAndUpdate 生成音频
  14. func SmartReportBuildVideoAndUpdate(item *smart_report.SmartReport) {
  15. if item == nil {
  16. return
  17. }
  18. var err error
  19. defer func() {
  20. if err != nil {
  21. tips := fmt.Sprintf("智能研报-音频生成, errMsg: %s", err.Error())
  22. go alarm_msg.SendAlarmMsg(tips, 2)
  23. }
  24. }()
  25. videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(item.Title, item.Content, time.Now().Local().Format(utils.FormatDateTime))
  26. if e != nil {
  27. err = fmt.Errorf("create audio err: %s", e.Error())
  28. return
  29. }
  30. item.VideoUrl = videoUrl
  31. item.VideoName = videoName
  32. item.VideoSize = videoSize
  33. item.VideoPlaySeconds = videoPlaySeconds
  34. item.ModifyTime = time.Now().Local()
  35. cols := []string{"VideoUrl", "VideoName", "VideoSize", "VideoPlaySeconds", "ModifyTime"}
  36. if e = item.Update(cols); e != nil {
  37. err = fmt.Errorf("smart report update err: %s", e.Error())
  38. return
  39. }
  40. }
  41. // UpdateSmartReportEditing 更新研报当前更新状态
  42. // status 枚举值 1:编辑中,0:完成编辑, 2:只做查询
  43. func UpdateSmartReportEditing(reportId, status, thisUserId int, thisUserName string, adminIdName map[int]string) (ret models.MarkReportResp, err error) {
  44. key := fmt.Sprint(utils.CACHE_SMART_REPORT_EDITING, reportId)
  45. ret.Status = 0
  46. ret.Msg = "无人编辑"
  47. opUserId, e := utils.Rc.RedisInt(key)
  48. var opUser models.MarkReportItem
  49. var classifyNameFirst string
  50. if e != nil {
  51. opUserInfoStr, tErr := utils.Rc.RedisString(key)
  52. if tErr == nil {
  53. tErr = json.Unmarshal([]byte(opUserInfoStr), &opUser)
  54. if tErr == nil {
  55. opUserId = opUser.AdminId
  56. }
  57. }
  58. }
  59. if opUserId > 0 && opUserId != thisUserId {
  60. editor := opUser.Editor
  61. if editor == "" {
  62. editor = adminIdName[opUserId]
  63. }
  64. ret.Status = 1
  65. ret.Msg = fmt.Sprintf("当前%s正在编辑报告", editor)
  66. ret.Editor = editor
  67. return
  68. }
  69. if status == 1 {
  70. nowUser := &models.MarkReportItem{AdminId: thisUserId, Editor: thisUserName, ReportClassifyNameFirst: classifyNameFirst}
  71. bt, e := json.Marshal(nowUser)
  72. if e != nil {
  73. err = fmt.Errorf("格式化编辑者信息失败")
  74. return
  75. }
  76. if opUserId > 0 {
  77. utils.Rc.Do("SETEX", key, int64(180), string(bt)) //3分钟缓存
  78. } else {
  79. utils.Rc.SetNX(key, string(bt), time.Second*60*3) //3分钟缓存
  80. }
  81. } else if status == 0 {
  82. //清除编辑缓存
  83. _ = utils.Rc.Delete(key)
  84. }
  85. return
  86. }
  87. // SmartReportElasticUpsert 新增/编辑报告es
  88. func SmartReportElasticUpsert(smartReportId int, state int) (err error) {
  89. if smartReportId <= 0 {
  90. return
  91. }
  92. reportOB := new(smart_report.SmartReport)
  93. item, e := reportOB.GetItemById(smartReportId)
  94. if e != nil {
  95. if e.Error() == utils.ErrNoRow() {
  96. // 可能被删了就直接忽略掉
  97. return
  98. }
  99. err = fmt.Errorf("获取报告失败, Err: %s", e.Error())
  100. return
  101. }
  102. esReport := new(smart_report.ElasticSmartReport)
  103. esReport.SmartReportId = item.SmartReportId
  104. esReport.Title = item.Title
  105. esReport.Abstract = item.Abstract
  106. esReport.BodyContent = utils.TrimHtml(html.UnescapeString(item.Content))
  107. esReport.PublishTime = item.PublishTime.Format(utils.FormatDateTime)
  108. esReport.PublishState = state
  109. esReport.Author = item.Author
  110. esReport.ClassifyIdFirst = item.ClassifyIdFirst
  111. esReport.ClassifyNameFirst = item.ClassifyNameFirst
  112. esReport.ClassifyIdSecond = item.ClassifyIdSecond
  113. esReport.ClassifyNameSecond = item.ClassifyNameSecond
  114. esReport.StageStr = strconv.Itoa(item.Stage)
  115. esReport.Frequency = item.Frequency
  116. if err = EsAddOrEditSmartReport(utils.SmartReportIndexName, strconv.Itoa(item.SmartReportId), esReport); err != nil {
  117. return
  118. }
  119. return
  120. }