ppt.go 14 KB

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