ppt.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/ppt_english"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services/alarm_msg"
  9. "eta/eta_api/services/ppt2img"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "sort"
  13. "time"
  14. )
  15. const (
  16. ElementsTypeText = "text"
  17. ElementsTypeImage = "image"
  18. ElementsTypeChart = "chart"
  19. ElementsTypeSheet = "sheet"
  20. )
  21. type PPTContent struct {
  22. //Id int `json:"id" description:"此处因目录改版类型有int也有string且并没有使用到该字段所以注释掉"`
  23. Key int `json:"key"`
  24. ModelId int `json:"modelId"`
  25. Title string `json:"title"`
  26. Elements []PPTContentElements `json:"elements"`
  27. }
  28. type PPTContentElements struct {
  29. Type string `json:"type"`
  30. Position int `json:"position"`
  31. Content string `json:"content"`
  32. RichContent string `json:"richContent"`
  33. ChartId string `json:"chartId"`
  34. SheetId string `json:"sheetId"`
  35. SheetHeight string `json:"sheetHeight"`
  36. Src string `json:"src"`
  37. }
  38. // SavePPTReport 保存PPT报告
  39. func SavePPTReport(pptId, classifyIdSecond int, title string, adminInfo *system.Admin) (reportId int, reportCode, errMsg string, err error) {
  40. defer func() {
  41. if err != nil {
  42. utils.FileLog.Info("%s", err.Error())
  43. go alarm_msg.SendAlarmMsg("PPT转报告失败, SavePPTReport Msg: "+errMsg+", Err: "+err.Error(), 3)
  44. }
  45. }()
  46. if pptId == 0 {
  47. errMsg = "参数有误"
  48. err = errors.New("参数有误")
  49. return
  50. }
  51. item, e := models.GetPptV2ById(pptId)
  52. if e != nil {
  53. errMsg = "获取PPT失败"
  54. err = errors.New("获取PPT失败, Err: " + e.Error())
  55. return
  56. }
  57. // PPT内容转HTML
  58. htm, e := pptContent2Html(item.Content, false)
  59. if e != nil {
  60. errMsg = "转换失败"
  61. err = e
  62. return
  63. }
  64. // 2023-02-21 PPT可多次转为报告, 不做关联
  65. if classifyIdSecond == 0 {
  66. errMsg = "请选择报告类型"
  67. err = errors.New("请选择报告类型")
  68. return
  69. }
  70. if title == "" {
  71. errMsg = "标题不能为空"
  72. err = errors.New("标题不能为空")
  73. return
  74. }
  75. // 获取分类及父级分类
  76. classifyList, e := models.GetAllClassify()
  77. if e != nil {
  78. errMsg = "转换失败"
  79. err = errors.New("获取分类列表失败, Err: " + e.Error())
  80. return
  81. }
  82. classifyMap := make(map[int]*models.Classify, 0)
  83. for _, v := range classifyList {
  84. classifyMap[v.Id] = v
  85. }
  86. classifyIdFirst := 0
  87. classifyNameFirst := ""
  88. classifyNameSecond := ""
  89. secondClassify := classifyMap[classifyIdSecond]
  90. if secondClassify != nil {
  91. classifyNameSecond = secondClassify.ClassifyName
  92. firstClassify := classifyMap[secondClassify.ParentId]
  93. if firstClassify != nil {
  94. classifyIdFirst = firstClassify.Id
  95. classifyNameFirst = firstClassify.ClassifyName
  96. }
  97. }
  98. // 新增报告
  99. nowTime := time.Now().Local()
  100. reportReq := &models.AddReq{
  101. AddType: 1,
  102. ClassifyIdFirst: classifyIdFirst,
  103. ClassifyNameFirst: classifyNameFirst,
  104. ClassifyIdSecond: classifyIdSecond,
  105. ClassifyNameSecond: classifyNameSecond,
  106. Title: title,
  107. Abstract: "",
  108. Author: "FICC团队",
  109. Frequency: utils.ReportFrequencyDefault,
  110. State: 1,
  111. Content: htm,
  112. CreateTime: nowTime.Format(utils.FormatDateTime),
  113. ReportVersion: 2,
  114. }
  115. newReportId, newCode, _, e := CreateNewReport(*reportReq, adminInfo)
  116. if e != nil {
  117. errMsg = "转换失败"
  118. err = errors.New("新增报告失败, Err: " + e.Error())
  119. return
  120. }
  121. reportId = int(newReportId)
  122. reportCode = newCode
  123. // 更新报告中的ppt图片
  124. go saveReportPptImg(pptId, reportId, item.PptxUrl)
  125. return
  126. }
  127. // PPT2内容转HTML
  128. func pptContent2Html(content string, isEnglish bool) (htm string, err error) {
  129. contents := make([]PPTContent, 0)
  130. if e := json.Unmarshal([]byte(content), &contents); e != nil {
  131. err = errors.New("PPT内容转换失败")
  132. return
  133. }
  134. pageLen := len(contents)
  135. htmlContent := ``
  136. // iframe图表/表格域名
  137. // 获取基础配置, 若未配置则直接返回
  138. // 获取配置好的短信模版
  139. smsCond := ` AND conf_key = ? `
  140. smsPars := make([]interface{}, 0)
  141. smsPars = append(smsPars, "ChartViewUrl")
  142. conf := new(models.BusinessConf)
  143. conf, e := conf.GetItemByCondition(smsCond, smsPars)
  144. if e != nil {
  145. if e.Error() == utils.ErrNoRow() {
  146. err = fmt.Errorf("请先配置公共图库的地址")
  147. return
  148. }
  149. err = fmt.Errorf("获取聚合短信配置信息失败, Err: %s", e.Error())
  150. return
  151. }
  152. if conf.ConfVal == "" {
  153. err = fmt.Errorf("请先配置公共图库的地址")
  154. return
  155. }
  156. chartRoot := conf.ConfVal
  157. if pageLen > 0 {
  158. htmlPrefix := `<p style="text-align: left; margin-top: 10px; font-size: 16px;">`
  159. htmlSuffix := `</p>`
  160. htmlBr := `<br>`
  161. for i := 0; i < pageLen; i++ {
  162. // 每页标题加粗居中
  163. title := contents[i].Title
  164. if title != "" {
  165. htmlContent += `<p style="font-size: 16px; text-align: left;"><strong>`
  166. htmlContent += title
  167. htmlContent += `</strong></p>`
  168. }
  169. ele := contents[i].Elements
  170. // 每页元素按照Position升序排序
  171. sort.Slice(ele, func(k, j int) bool {
  172. return ele[k].Position < ele[j].Position
  173. })
  174. for _, v := range ele {
  175. // 根据不同的Type拼接不同的内容
  176. htmlContent += htmlPrefix
  177. switch v.Type {
  178. case ElementsTypeText:
  179. htmlContent += v.RichContent
  180. case ElementsTypeImage:
  181. htmlContent += fmt.Sprint(`<img src="`, v.Src, `" class="fr-fic fr-dib fr-draggable">`)
  182. case ElementsTypeChart:
  183. if isEnglish {
  184. // 英文研报图表src多加一个fromPage=en, 表格暂时没有区分
  185. 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)
  186. break
  187. }
  188. htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId)
  189. case ElementsTypeSheet:
  190. 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)
  191. }
  192. htmlContent += htmlSuffix
  193. }
  194. // 每页中间插入一个换行符, 最后一页不插入
  195. currentPage := i + 1
  196. if currentPage != pageLen {
  197. htmlContent += htmlPrefix + htmlBr + htmlSuffix
  198. }
  199. }
  200. }
  201. htm = htmlContent
  202. return
  203. }
  204. // ResetPPTReport 重置PPT关联的报告(如删除关联的报告后)
  205. func ResetPPTReport(reportId int, isEnglish bool) (err error) {
  206. defer func() {
  207. if err != nil {
  208. utils.FileLog.Info("%s", err.Error())
  209. go alarm_msg.SendAlarmMsg("重置PPT关联报告失败, ResetPPTReport Err: "+err.Error(), 3)
  210. }
  211. }()
  212. // 英文报告
  213. if isEnglish {
  214. en, e := ppt_english.GetPptEnglishByReportId(reportId)
  215. if e != nil && e.Error() != utils.ErrNoRow() {
  216. err = errors.New("获取英文PPT失败, Err: " + e.Error())
  217. return
  218. }
  219. if en != nil {
  220. updateCols := []string{"ReportId", "ReportCode"}
  221. en.ReportId = 0
  222. en.ReportCode = ""
  223. if e = en.Update(updateCols); e != nil {
  224. err = errors.New("更新英文PPT关联报告失败, Err: " + e.Error())
  225. return
  226. }
  227. }
  228. return
  229. }
  230. // 中文报告
  231. item, e := models.GetPptV2ByReportId(reportId)
  232. if e != nil && e.Error() != utils.ErrNoRow() {
  233. err = errors.New("获取PPT失败, Err: " + e.Error())
  234. return
  235. }
  236. if item != nil {
  237. updateCols := []string{"ReportId", "ReportCode"}
  238. item.ReportId = 0
  239. item.ReportCode = ""
  240. if e = item.Update(updateCols); e != nil {
  241. err = errors.New("更新PPT关联报告失败, Err: " + e.Error())
  242. return
  243. }
  244. }
  245. return
  246. }
  247. // saveReportPptImg ppt转报告后,需要再次将ppt转图片
  248. func saveReportPptImg(pptId, reportId int, pptUrl string) {
  249. var err error
  250. defer func() {
  251. if err != nil {
  252. utils.FileLog.Info(fmt.Sprintf("将ppt转图片失败, saveReportPptImg Err:%s", err.Error()))
  253. go alarm_msg.SendAlarmMsg("将ppt转图片失败, saveReportPptImg Err: "+err.Error(), 3)
  254. }
  255. }()
  256. // 更新报告内容
  257. report, e := models.GetReportByReportId(reportId)
  258. if e != nil && e.Error() != utils.ErrNoRow() {
  259. err = errors.New("获取报告失败, Err: " + e.Error())
  260. return
  261. }
  262. if report == nil {
  263. return
  264. }
  265. // 获取ppt转图片的结果
  266. list, err := ppt2img.Ppt2Img(pptUrl)
  267. if err != nil {
  268. return
  269. }
  270. reportPptImgList := make([]*models.ReportPptImg, 0)
  271. for _, v := range list {
  272. reportPptImg := &models.ReportPptImg{
  273. //ReportPptImgId: 0,
  274. PptId: pptId,
  275. ReportId: reportId,
  276. ReportChapterId: 0,
  277. ImgUrl: v,
  278. CreateTime: time.Now(),
  279. }
  280. reportPptImgList = append(reportPptImgList, reportPptImg)
  281. }
  282. //批量添加Ppt转报告的图片记录
  283. err = models.AddAndEditMultiReportPptImg(pptId, reportPptImgList)
  284. return
  285. }
  286. // SaveEnglishPPTReport 保存英文PPT报告
  287. func SaveEnglishPPTReport(pptId, classifyIdFirst, classifyIdSecond int, title, abstract string, adminInfo *system.Admin) (reportId int, reportCode, errMsg string, err error) {
  288. defer func() {
  289. if err != nil {
  290. utils.FileLog.Info("%s", err.Error())
  291. go alarm_msg.SendAlarmMsg("PPT转报告失败, SavePPTReport Msg: "+errMsg+", Err: "+err.Error(), 3)
  292. }
  293. }()
  294. if pptId == 0 {
  295. errMsg = "参数有误"
  296. err = errors.New("参数有误")
  297. return
  298. }
  299. item, e := ppt_english.GetPptEnglishById(pptId)
  300. if e != nil {
  301. errMsg = "获取PPT失败"
  302. err = errors.New("获取PPT失败, Err: " + e.Error())
  303. return
  304. }
  305. // PPT内容转HTML
  306. htm, e := pptContent2Html(item.Content, true)
  307. if e != nil {
  308. errMsg = "转换失败"
  309. err = e
  310. return
  311. }
  312. // 2023-02-21 PPT可多次转为报告, 不做关联
  313. if title == "" {
  314. errMsg = "标题不能为空"
  315. err = errors.New("标题不能为空")
  316. return
  317. }
  318. if classifyIdFirst <= 0 {
  319. errMsg = "请选择报告分类"
  320. err = errors.New("报告分类不能为空")
  321. return
  322. }
  323. // 分类
  324. classifyList, e := models.GetAllEnglishClassify()
  325. if e != nil {
  326. errMsg = "转换失败"
  327. err = errors.New("获取分类列表失败, Err: " + e.Error())
  328. return
  329. }
  330. classifyMap := make(map[int]string, 0)
  331. for _, v := range classifyList {
  332. classifyMap[v.Id] = v.ClassifyName
  333. }
  334. classifyNameFirst := classifyMap[classifyIdFirst]
  335. classifyNameSecond := classifyMap[classifyIdSecond]
  336. // 新增报告
  337. nowTime := time.Now().Local()
  338. reportReq := &models.AddEnglishReportReq{
  339. AddType: 1,
  340. ClassifyIdFirst: classifyIdFirst,
  341. ClassifyNameFirst: classifyNameFirst,
  342. ClassifyIdSecond: classifyIdSecond,
  343. ClassifyNameSecond: classifyNameSecond,
  344. Title: title,
  345. Abstract: abstract,
  346. Author: "Horizon Insights FICC Team",
  347. Frequency: utils.ReportFrequencyDefault,
  348. State: 1,
  349. Content: htm,
  350. CreateTime: nowTime.Format(utils.FormatDateTime),
  351. }
  352. newReportId, newCode, e := CreateNewEnglishReport(*reportReq, adminInfo)
  353. if e != nil {
  354. errMsg = "转换失败"
  355. err = errors.New("新增报告失败, Err: " + e.Error())
  356. return
  357. }
  358. reportId = int(newReportId)
  359. reportCode = newCode
  360. return
  361. }
  362. // UpdatePptEditing 更新PPT编辑状态
  363. func UpdatePptEditing(pptId, status, userId int, userName string, isEn bool) (ret ppt_english.PPTEditingCache, err error) {
  364. if pptId <= 0 {
  365. return
  366. }
  367. cacheKey := ""
  368. if isEn {
  369. cacheKey = fmt.Sprint(utils.CACHE_EN_PPT_EDITING, pptId)
  370. } else {
  371. cacheKey = fmt.Sprint(utils.CACHE_PPT_EDITING, pptId)
  372. }
  373. // 完成编辑
  374. if status == 2 {
  375. _ = utils.Rc.Delete(cacheKey)
  376. return
  377. }
  378. // 读取缓存中的结果
  379. var editor ppt_english.PPTEditingCache
  380. strCache, _ := utils.Rc.RedisString(cacheKey)
  381. fmt.Println(strCache)
  382. if strCache != "" {
  383. e := json.Unmarshal([]byte(strCache), &editor)
  384. if e != nil {
  385. err = fmt.Errorf("解析缓存内容失败: %s", e.Error())
  386. return
  387. }
  388. }
  389. // 标记编辑中
  390. if status == 1 {
  391. // 无人编辑, 写入缓存
  392. if !editor.IsEditing {
  393. ret.IsEditing = true
  394. ret.AdminId = userId
  395. ret.Editor = userName
  396. ret.Tips = fmt.Sprintf("当前%s正在编辑PPT", userName)
  397. b, _ := json.Marshal(ret)
  398. utils.Rc.SetNX(cacheKey, string(b), 3*time.Minute)
  399. return
  400. }
  401. // 有人编辑
  402. if editor.IsEditing {
  403. // 编辑用户与当前用户不一致, 返回编辑用户, 一致则更新缓存
  404. if userId == editor.AdminId {
  405. b, _ := json.Marshal(editor)
  406. utils.Rc.Do("SETEX", cacheKey, int64(180), string(b))
  407. }
  408. ret = editor
  409. return
  410. }
  411. } else {
  412. // 默认查询
  413. ret = editor
  414. }
  415. return
  416. }