ppt.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models"
  6. "eta/eta_mobile/models/ppt_english"
  7. "eta/eta_mobile/services/alarm_msg"
  8. "eta/eta_mobile/utils"
  9. "fmt"
  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 := utils.PublicChartHost
  46. if pageLen > 0 {
  47. htmlPrefix := `<p style="text-align: left; margin-top: 10px; font-size: 16px;">`
  48. htmlSuffix := `</p>`
  49. htmlBr := `<br>`
  50. for i := 0; i < pageLen; i++ {
  51. // 每页标题加粗居中
  52. title := contents[i].Title
  53. if title != "" {
  54. htmlContent += `<p style="font-size: 16px; text-align: left;"><strong>`
  55. htmlContent += title
  56. htmlContent += `</strong></p>`
  57. }
  58. ele := contents[i].Elements
  59. // 每页元素按照Position升序排序
  60. sort.Slice(ele, func(k, j int) bool {
  61. return ele[k].Position < ele[j].Position
  62. })
  63. for _, v := range ele {
  64. // 根据不同的Type拼接不同的内容
  65. htmlContent += htmlPrefix
  66. switch v.Type {
  67. case ElementsTypeText:
  68. htmlContent += v.RichContent
  69. case ElementsTypeImage:
  70. htmlContent += fmt.Sprint(`<img src="`, v.Src, `" class="fr-fic fr-dib fr-draggable">`)
  71. case ElementsTypeChart:
  72. if isEnglish {
  73. // 英文研报图表src多加一个fromPage=en, 表格暂时没有区分
  74. 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)
  75. break
  76. }
  77. htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId)
  78. case ElementsTypeSheet:
  79. 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)
  80. }
  81. htmlContent += htmlSuffix
  82. }
  83. // 每页中间插入一个换行符, 最后一页不插入
  84. currentPage := i + 1
  85. if currentPage != pageLen {
  86. htmlContent += htmlPrefix + htmlBr + htmlSuffix
  87. }
  88. }
  89. }
  90. htm = htmlContent
  91. return
  92. }
  93. // ResetPPTReport 重置PPT关联的报告(如删除关联的报告后)
  94. func ResetPPTReport(reportId int, isEnglish bool) (err error) {
  95. defer func() {
  96. if err != nil {
  97. utils.FileLog.Info("%s", err.Error())
  98. go alarm_msg.SendAlarmMsg("重置PPT关联报告失败, ResetPPTReport Err: "+err.Error(), 3)
  99. }
  100. }()
  101. // 英文报告
  102. if isEnglish {
  103. en, e := ppt_english.GetPptEnglishByReportId(reportId)
  104. if e != nil && e.Error() != utils.ErrNoRow() {
  105. err = errors.New("获取英文PPT失败, Err: " + e.Error())
  106. return
  107. }
  108. if en != nil {
  109. updateCols := []string{"ReportId", "ReportCode"}
  110. en.ReportId = 0
  111. en.ReportCode = ""
  112. if e = en.Update(updateCols); e != nil {
  113. err = errors.New("更新英文PPT关联报告失败, Err: " + e.Error())
  114. return
  115. }
  116. }
  117. return
  118. }
  119. // 中文报告
  120. item, e := models.GetPptV2ByReportId(reportId)
  121. if e != nil && e.Error() != utils.ErrNoRow() {
  122. err = errors.New("获取PPT失败, Err: " + e.Error())
  123. return
  124. }
  125. if item != nil {
  126. updateCols := []string{"ReportId", "ReportCode"}
  127. item.ReportId = 0
  128. item.ReportCode = ""
  129. if e = item.Update(updateCols); e != nil {
  130. err = errors.New("更新PPT关联报告失败, Err: " + e.Error())
  131. return
  132. }
  133. }
  134. return
  135. }