ppt.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_ETA_mobile_api/models"
  7. "hongze/hongze_ETA_mobile_api/models/ppt_english"
  8. "hongze/hongze_ETA_mobile_api/services/alarm_msg"
  9. "hongze/hongze_ETA_mobile_api/utils"
  10. "sort"
  11. )
  12. const (
  13. ElementsTypeText = "text"
  14. ElementsTypeImage = "image"
  15. ElementsTypeChart = "chart"
  16. ElementsTypeSheet = "sheet"
  17. )
  18. type PPTContent struct {
  19. //Id int `json:"id" description:"此处因目录改版类型有int也有string且并没有使用到该字段所以注释掉"`
  20. Key int `json:"key"`
  21. ModelId int `json:"modelId"`
  22. Title string `json:"title"`
  23. Elements []PPTContentElements `json:"elements"`
  24. }
  25. type PPTContentElements struct {
  26. Type string `json:"type"`
  27. Position int `json:"position"`
  28. Content string `json:"content"`
  29. RichContent string `json:"richContent"`
  30. ChartId string `json:"chartId"`
  31. SheetId string `json:"sheetId"`
  32. SheetHeight string `json:"sheetHeight"`
  33. Src string `json:"src"`
  34. }
  35. // PPT2内容转HTML
  36. func pptContent2Html(content string, isEnglish bool) (htm string, err error) {
  37. contents := make([]PPTContent, 0)
  38. if e := json.Unmarshal([]byte(content), &contents); e != nil {
  39. err = errors.New("PPT内容转换失败")
  40. return
  41. }
  42. pageLen := len(contents)
  43. htmlContent := ``
  44. // iframe图表/表格域名
  45. chartRoot := `https://chartlib.hzinsights.com`
  46. if utils.RunMode == "debug" {
  47. chartRoot = `https://charttest.hzinsights.com`
  48. }
  49. if pageLen > 0 {
  50. htmlPrefix := `<p style="text-align: left; margin-top: 10px; font-size: 16px;">`
  51. htmlSuffix := `</p>`
  52. htmlBr := `<br>`
  53. for i := 0; i < pageLen; i++ {
  54. // 每页标题加粗居中
  55. title := contents[i].Title
  56. if title != "" {
  57. htmlContent += `<p style="font-size: 16px; text-align: left;"><strong>`
  58. htmlContent += title
  59. htmlContent += `</strong></p>`
  60. }
  61. ele := contents[i].Elements
  62. // 每页元素按照Position升序排序
  63. sort.Slice(ele, func(k, j int) bool {
  64. return ele[k].Position < ele[j].Position
  65. })
  66. for _, v := range ele {
  67. // 根据不同的Type拼接不同的内容
  68. htmlContent += htmlPrefix
  69. switch v.Type {
  70. case ElementsTypeText:
  71. htmlContent += v.RichContent
  72. case ElementsTypeImage:
  73. htmlContent += fmt.Sprint(`<img src="`, v.Src, `" class="fr-fic fr-dib fr-draggable">`)
  74. case ElementsTypeChart:
  75. if isEnglish {
  76. // 英文研报图表src多加一个fromPage=en, 表格暂时没有区分
  77. htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s&fromPage=en" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId)
  78. break
  79. }
  80. htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId)
  81. case ElementsTypeSheet:
  82. htmlContent += fmt.Sprintf(`<iframe src="%s/sheetshow?code=%s" class="iframe%s" width="100%%" height="%s" style="border-width:0px;"></iframe>`, chartRoot, v.SheetId, v.SheetId, v.SheetHeight)
  83. }
  84. htmlContent += htmlSuffix
  85. }
  86. // 每页中间插入一个换行符, 最后一页不插入
  87. currentPage := i + 1
  88. if currentPage != pageLen {
  89. htmlContent += htmlPrefix + htmlBr + htmlSuffix
  90. }
  91. }
  92. }
  93. htm = htmlContent
  94. return
  95. }
  96. // ResetPPTReport 重置PPT关联的报告(如删除关联的报告后)
  97. func ResetPPTReport(reportId int, isEnglish bool) (err error) {
  98. defer func() {
  99. if err != nil {
  100. utils.FileLog.Info("%s", err.Error())
  101. go alarm_msg.SendAlarmMsg("重置PPT关联报告失败, ResetPPTReport Err: "+err.Error(), 3)
  102. }
  103. }()
  104. // 英文报告
  105. if isEnglish {
  106. en, e := ppt_english.GetPptEnglishByReportId(reportId)
  107. if e != nil && e.Error() != utils.ErrNoRow() {
  108. err = errors.New("获取英文PPT失败, Err: " + e.Error())
  109. return
  110. }
  111. if en != nil {
  112. updateCols := []string{"ReportId", "ReportCode"}
  113. en.ReportId = 0
  114. en.ReportCode = ""
  115. if e = en.Update(updateCols); e != nil {
  116. err = errors.New("更新英文PPT关联报告失败, Err: " + e.Error())
  117. return
  118. }
  119. }
  120. return
  121. }
  122. // 中文报告
  123. item, e := models.GetPptV2ByReportId(reportId)
  124. if e != nil && e.Error() != utils.ErrNoRow() {
  125. err = errors.New("获取PPT失败, Err: " + e.Error())
  126. return
  127. }
  128. if item != nil {
  129. updateCols := []string{"ReportId", "ReportCode"}
  130. item.ReportId = 0
  131. item.ReportCode = ""
  132. if e = item.Update(updateCols); e != nil {
  133. err = errors.New("更新PPT关联报告失败, Err: " + e.Error())
  134. return
  135. }
  136. }
  137. return
  138. }