123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- package services
- import (
- "encoding/json"
- "errors"
- "eta/eta_mobile/models"
- "eta/eta_mobile/models/ppt_english"
- "eta/eta_mobile/services/alarm_msg"
- "eta/eta_mobile/utils"
- "fmt"
- "sort"
- "time"
- )
- const (
- ElementsTypeText = "text"
- ElementsTypeImage = "image"
- ElementsTypeChart = "chart"
- ElementsTypeSheet = "sheet"
- )
- type PPTContent struct {
- //Id int `json:"id" description:"此处因目录改版类型有int也有string且并没有使用到该字段所以注释掉"`
- Key int `json:"key"`
- ModelId int `json:"modelId"`
- Title string `json:"title"`
- Elements []PPTContentElements `json:"elements"`
- }
- type PPTContentElements struct {
- Type string `json:"type"`
- Position int `json:"position"`
- Content string `json:"content"`
- RichContent string `json:"richContent"`
- ChartId string `json:"chartId"`
- SheetId string `json:"sheetId"`
- SheetHeight string `json:"sheetHeight"`
- Src string `json:"src"`
- }
- // PPT2内容转HTML
- func pptContent2Html(content string, isEnglish bool) (htm string, err error) {
- contents := make([]PPTContent, 0)
- if e := json.Unmarshal([]byte(content), &contents); e != nil {
- err = errors.New("PPT内容转换失败")
- return
- }
- pageLen := len(contents)
- htmlContent := ``
- // iframe图表/表格域名
- chartRoot := utils.PublicChartHost
- if pageLen > 0 {
- htmlPrefix := `<p style="text-align: left; margin-top: 10px; font-size: 16px;">`
- htmlSuffix := `</p>`
- htmlBr := `<br>`
- for i := 0; i < pageLen; i++ {
- // 每页标题加粗居中
- title := contents[i].Title
- if title != "" {
- htmlContent += `<p style="font-size: 16px; text-align: left;"><strong>`
- htmlContent += title
- htmlContent += `</strong></p>`
- }
- ele := contents[i].Elements
- // 每页元素按照Position升序排序
- sort.Slice(ele, func(k, j int) bool {
- return ele[k].Position < ele[j].Position
- })
- for _, v := range ele {
- // 根据不同的Type拼接不同的内容
- htmlContent += htmlPrefix
- switch v.Type {
- case ElementsTypeText:
- htmlContent += v.RichContent
- case ElementsTypeImage:
- htmlContent += fmt.Sprint(`<img src="`, v.Src, `" class="fr-fic fr-dib fr-draggable">`)
- case ElementsTypeChart:
- if isEnglish {
- // 英文研报图表src多加一个fromPage=en, 表格暂时没有区分
- 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)
- break
- }
- htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId)
- case ElementsTypeSheet:
- 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)
- }
- htmlContent += htmlSuffix
- }
- // 每页中间插入一个换行符, 最后一页不插入
- currentPage := i + 1
- if currentPage != pageLen {
- htmlContent += htmlPrefix + htmlBr + htmlSuffix
- }
- }
- }
- htm = htmlContent
- return
- }
- // ResetPPTReport 重置PPT关联的报告(如删除关联的报告后)
- func ResetPPTReport(reportId int, isEnglish bool) (err error) {
- defer func() {
- if err != nil {
- utils.FileLog.Info("%s", err.Error())
- go alarm_msg.SendAlarmMsg("重置PPT关联报告失败, ResetPPTReport Err: "+err.Error(), 3)
- }
- }()
- // 英文报告
- if isEnglish {
- en, e := ppt_english.GetPptEnglishByReportId(reportId)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("获取英文PPT失败, Err: " + e.Error())
- return
- }
- if en != nil {
- updateCols := []string{"ReportId", "ReportCode"}
- en.ReportId = 0
- en.ReportCode = ""
- if e = en.Update(updateCols); e != nil {
- err = errors.New("更新英文PPT关联报告失败, Err: " + e.Error())
- return
- }
- }
- return
- }
- // 中文报告
- item, e := models.GetPptV2ByReportId(reportId)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("获取PPT失败, Err: " + e.Error())
- return
- }
- if item != nil {
- updateCols := []string{"ReportId", "ReportCode"}
- item.ReportId = 0
- item.ReportCode = ""
- if e = item.Update(updateCols); e != nil {
- err = errors.New("更新PPT关联报告失败, Err: " + e.Error())
- return
- }
- }
- return
- }
- // UpdatePptEditing 更新PPT编辑状态
- func UpdatePptEditing(pptId, status, userId int, userName string, isEn bool) (ret ppt_english.PPTEditingCache, err error) {
- if pptId <= 0 {
- return
- }
- cacheKey := ""
- if isEn {
- cacheKey = fmt.Sprint(utils.CACHE_EN_PPT_EDITING, pptId)
- } else {
- cacheKey = fmt.Sprint(utils.CACHE_PPT_EDITING, pptId)
- }
- // 完成编辑
- if status == 2 {
- _ = utils.Rc.Delete(cacheKey)
- return
- }
- // 读取缓存中的结果
- var editor ppt_english.PPTEditingCache
- strCache, _ := utils.Rc.RedisString(cacheKey)
- fmt.Println(strCache)
- if strCache != "" {
- e := json.Unmarshal([]byte(strCache), &editor)
- if e != nil {
- err = fmt.Errorf("解析缓存内容失败: %s", e.Error())
- return
- }
- }
- // 标记编辑中
- if status == 1 {
- // 无人编辑, 写入缓存
- if !editor.IsEditing {
- ret.IsEditing = true
- ret.AdminId = userId
- ret.Editor = userName
- ret.Tips = fmt.Sprintf("当前%s正在编辑PPT", userName)
- b, _ := json.Marshal(ret)
- utils.Rc.SetNX(cacheKey, string(b), 3*time.Minute)
- return
- }
- // 有人编辑
- if editor.IsEditing {
- // 编辑用户与当前用户不一致, 返回编辑用户, 一致则更新缓存
- if userId == editor.AdminId {
- b, _ := json.Marshal(editor)
- utils.Rc.Do("SETEX", cacheKey, int64(180), string(b))
- }
- ret = editor
- return
- }
- } else {
- // 默认查询
- ret = editor
- }
- return
- }
|