smart_report.go 3.8 KB

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