smart_report.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. "os/exec"
  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 := 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 = EsAddOrEditSmartReport(utils.SmartReportIndexName, strconv.Itoa(item.SmartReportId), esReport); err != nil {
  118. return
  119. }
  120. return
  121. }
  122. func ReportToPdf(reportUrl, filePath string) (err error) {
  123. pyCode := `
  124. import asyncio
  125. from pyppeteer import launch
  126. @asyncio.coroutine
  127. def main():
  128. # 异步代码
  129. browser = yield from launch({
  130. 'executablePath': '%s',
  131. 'headless': True,
  132. 'args': ['--disable-infobars', '--no-sandbox']
  133. })
  134. page = yield from browser.newPage()
  135. yield from page.setViewport({
  136. 'width': 1920,
  137. 'height': 1080,
  138. })
  139. yield from page.goto('%s', {
  140. 'waitUntil': 'networkidle0',
  141. 'timeout': 1000000 # 设置超时时间为 100 秒
  142. })
  143. yield from page.pdf({
  144. 'path': "%s",
  145. 'printBackground': True,
  146. 'format': "A2",
  147. 'margin': {
  148. 'top': '10mm',
  149. 'bottom': '10mm',
  150. 'left': '10mm',
  151. 'right': '10mm'
  152. }
  153. })
  154. yield from browser.close()
  155. # 创建事件循环
  156. loop = asyncio.get_event_loop()
  157. # 使用事件循环运行main函数
  158. try:
  159. loop.run_until_complete(main())
  160. finally:
  161. # 关闭事件循环
  162. loop.close()
  163. `
  164. pyCode = fmt.Sprintf(pyCode, utils.ChromeDrivePath, reportUrl, filePath)
  165. cmd := exec.Command("python3", "-c", pyCode)
  166. _, err = cmd.CombinedOutput()
  167. defer func() {
  168. cmd.Process.Kill()
  169. }()
  170. return
  171. }
  172. func ReportToJpeg(reportUrl, filePath string) (err error) {
  173. pyCode := `
  174. import asyncio
  175. from pyppeteer import launch
  176. @asyncio.coroutine
  177. def main():
  178. # 异步代码
  179. browser = yield from launch({
  180. 'executablePath': '%s',
  181. 'headless': True,
  182. 'args': ['--disable-infobars', '--no-sandbox']
  183. })
  184. page = yield from browser.newPage()
  185. yield from page.setViewport({
  186. 'width': 1920,
  187. 'height': 1080,
  188. })
  189. yield from page.goto('%s', {
  190. 'waitUntil': 'networkidle0',
  191. 'timeout': 1000000 # 设置超时时间为 100 秒
  192. })
  193. yield from page.screenshot({
  194. 'path': "%s",
  195. 'type': "jpeg",
  196. 'fullPage': True,
  197. })
  198. yield from browser.close()
  199. # 创建事件循环
  200. loop = asyncio.get_event_loop()
  201. # 使用事件循环运行main函数
  202. try:
  203. loop.run_until_complete(main())
  204. finally:
  205. # 关闭事件循环
  206. loop.close()
  207. `
  208. pyCode = fmt.Sprintf(pyCode, utils.ChromeDrivePath, reportUrl, filePath)
  209. cmd := exec.Command("python3", "-c", pyCode)
  210. _, err = cmd.CombinedOutput()
  211. defer func() {
  212. cmd.Process.Kill()
  213. }()
  214. return
  215. }