report.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/company"
  7. "eta_gn/eta_api/models/report"
  8. "eta_gn/eta_api/models/system"
  9. "eta_gn/eta_api/services/alarm_msg"
  10. "eta_gn/eta_api/services/public_api"
  11. "eta_gn/eta_api/utils"
  12. "fmt"
  13. "github.com/PuerkitoBio/goquery"
  14. "html"
  15. "os"
  16. "regexp"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. func GetReportContentSub(content string) (contentSub string, err error) {
  22. content = html.UnescapeString(content)
  23. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  24. if err != nil {
  25. fmt.Println("create doc err:", err.Error())
  26. return
  27. }
  28. n := 0
  29. doc.Find("p").Each(func(i int, s *goquery.Selection) {
  30. if n >= 5 {
  31. return
  32. }
  33. n++
  34. phtml, err := s.Html()
  35. if err != nil {
  36. fmt.Println("get html err", err.Error())
  37. return
  38. }
  39. if s.Text() != "" || strings.Contains(phtml, "src") {
  40. contentSub = contentSub + "<p>" + phtml + "</p>"
  41. }
  42. })
  43. return
  44. }
  45. func UpdateChaptersVideo(ids []int) (err error) {
  46. defer func() {
  47. if err != nil {
  48. utils.FileLog.Error("UpdateChaptersVideo, chapterIds:%v, Err:%s", ids, err.Error())
  49. go alarm_msg.SendAlarmMsg(fmt.Sprintf("更新章节音频失败, 章节ID: %v; Err: "+err.Error(), ids), 3)
  50. }
  51. }()
  52. if len(ids) <= 0 {
  53. return
  54. }
  55. chapterList, err := models.GetChapterListByChapterIds(ids)
  56. if err != nil {
  57. return
  58. }
  59. nowTime := time.Now()
  60. updateCols := make([]string, 0)
  61. updateCols = append(updateCols, "VideoUrl", "VideoName", "VideoSize", "VideoPlaySeconds")
  62. for i := 0; i < len(chapterList); i++ {
  63. item := chapterList[i]
  64. if item.VideoUrl != "" && item.VideoName != "" && item.VideoSize != "" && item.VideoPlaySeconds != "" {
  65. continue
  66. }
  67. videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(item.Title, html.UnescapeString(item.Content), nowTime.Format(utils.FormatDateTime))
  68. if e != nil {
  69. err = e
  70. return
  71. }
  72. item.VideoUrl = videoUrl
  73. item.VideoName = videoName
  74. item.VideoSize = videoSize
  75. item.VideoPlaySeconds = fmt.Sprintf("%.2f", videoPlaySeconds)
  76. if e = item.UpdateChapter(updateCols); e != nil {
  77. err = e
  78. }
  79. }
  80. return
  81. }
  82. func UpdateReportEs(reportId int, publishState int) (err error) {
  83. if reportId <= 0 {
  84. return
  85. }
  86. reportInfo, err := models.GetReportByReportId(reportId)
  87. if err != nil {
  88. return
  89. }
  90. categories := ""
  91. if reportInfo.HasChapter == 1 {
  92. chapterList, tmpErr := models.GetPublishedChapterListByReportId(reportInfo.Id)
  93. if tmpErr != nil {
  94. return
  95. }
  96. if len(chapterList) > 0 {
  97. for _, chapterInfo := range chapterList {
  98. err = updateReportChapterEsByChapter(chapterInfo)
  99. if err != nil {
  100. return
  101. }
  102. }
  103. }
  104. } else {
  105. minClassifyId, _, tmpErr := getMinClassify(reportInfo)
  106. if tmpErr != nil {
  107. return
  108. }
  109. permissionList, tmpErr := models.GetChartPermissionNameFromMappingByKeyword("rddp", minClassifyId)
  110. if tmpErr != nil {
  111. return
  112. }
  113. categoryArr := make([]string, 0)
  114. for i := 0; i < len(permissionList); i++ {
  115. categoryArr = append(categoryArr, permissionList[i].PermissionName)
  116. }
  117. aliasArr, _ := addCategoryAliasToArr(categoryArr)
  118. categories = strings.Join(aliasArr, ",")
  119. }
  120. minClassifyId, minClassifyName, err := getMinClassify(reportInfo)
  121. if err != nil {
  122. return
  123. }
  124. esReport := &models.ElasticReportDetail{
  125. ReportId: reportInfo.Id,
  126. ReportChapterId: 0,
  127. Title: reportInfo.Title,
  128. Abstract: reportInfo.Abstract,
  129. BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)),
  130. PublishTime: reportInfo.PublishTime.Format(utils.FormatDateTime),
  131. PublishState: publishState,
  132. Author: reportInfo.Author,
  133. ClassifyIdFirst: reportInfo.ClassifyIdFirst,
  134. ClassifyNameFirst: reportInfo.ClassifyNameFirst,
  135. ClassifyIdSecond: reportInfo.ClassifyIdSecond,
  136. ClassifyNameSecond: reportInfo.ClassifyNameSecond,
  137. ClassifyId: minClassifyId,
  138. ClassifyName: minClassifyName,
  139. Categories: categories,
  140. StageStr: strconv.Itoa(reportInfo.Stage),
  141. }
  142. docId := fmt.Sprintf("%d-%d", reportInfo.Id, 0)
  143. if err = EsAddOrEditReport(utils.EsReportIndexName, docId, esReport); err != nil {
  144. return
  145. }
  146. return
  147. }
  148. func addCategoryAliasToArr(categoryArr []string) (aliasArr []string, err error) {
  149. aliasArr = categoryArr
  150. if len(categoryArr) > 0 {
  151. for i := 0; i < len(categoryArr); i++ {
  152. if strings.Contains(categoryArr[i], "沥青") {
  153. aliasArr = append(aliasArr, "BU")
  154. }
  155. if strings.Contains(categoryArr[i], "MEG") {
  156. aliasArr = append(aliasArr, "EG", "乙二醇")
  157. }
  158. if strings.Contains(categoryArr[i], "聚酯") {
  159. aliasArr = append(aliasArr, "长丝", "短纤", "瓶片")
  160. }
  161. if strings.Contains(categoryArr[i], "纯苯+苯乙烯") {
  162. aliasArr = append(aliasArr, "EB")
  163. }
  164. if strings.Contains(categoryArr[i], "聚乙烯") {
  165. aliasArr = append(aliasArr, "PP", "PE")
  166. }
  167. if strings.Contains(categoryArr[i], "玻璃纯碱") {
  168. aliasArr = append(aliasArr, "玻璃", "纯碱", "FG", "SA")
  169. }
  170. if strings.Contains(categoryArr[i], "甲醇") {
  171. aliasArr = append(aliasArr, "甲醇", "MA")
  172. }
  173. if strings.Contains(categoryArr[i], "橡胶") {
  174. aliasArr = append(aliasArr, "橡胶", "RU")
  175. }
  176. }
  177. }
  178. return
  179. }
  180. func UpdateReportChapterEs(reportChapterId int) (err error) {
  181. if reportChapterId <= 0 {
  182. return
  183. }
  184. chapterInfo, err := models.GetReportChapterInfoById(reportChapterId)
  185. if err != nil {
  186. return
  187. }
  188. err = updateReportChapterEsByChapter(chapterInfo)
  189. if err != nil {
  190. return
  191. }
  192. return
  193. }
  194. func updateReportChapterEsByChapter(chapterInfo *models.ReportChapter) (err error) {
  195. obj := report.ReportChapterPermissionMapping{}
  196. permissionList, tmpErr := obj.GetPermissionItemListById(chapterInfo.ReportChapterId)
  197. if tmpErr != nil {
  198. return
  199. }
  200. categoryArr := make([]string, 0)
  201. if len(permissionList) > 0 {
  202. for ii := 0; ii < len(permissionList); ii++ {
  203. categoryArr = append(categoryArr, permissionList[ii].ChartPermissionName)
  204. }
  205. }
  206. aliasArr, _ := addCategoryAliasToArr(categoryArr)
  207. categories := strings.Join(aliasArr, ",")
  208. esChapter := &models.ElasticReportDetail{
  209. ReportId: chapterInfo.ReportId,
  210. ReportChapterId: chapterInfo.ReportChapterId,
  211. Title: chapterInfo.Title,
  212. Abstract: chapterInfo.Abstract,
  213. BodyContent: utils.TrimHtml(html.UnescapeString(chapterInfo.Content)),
  214. PublishTime: chapterInfo.PublishTime.Format(utils.FormatDateTime),
  215. PublishState: chapterInfo.PublishState,
  216. Author: chapterInfo.Author,
  217. ClassifyIdFirst: chapterInfo.ClassifyIdFirst,
  218. ClassifyNameFirst: chapterInfo.ClassifyNameFirst,
  219. ClassifyIdSecond: 0,
  220. ClassifyNameSecond: "",
  221. ClassifyId: chapterInfo.ClassifyIdFirst,
  222. ClassifyName: chapterInfo.ClassifyNameFirst,
  223. Categories: categories,
  224. StageStr: strconv.Itoa(chapterInfo.Stage),
  225. }
  226. chapterDocId := fmt.Sprintf("%d-%d", chapterInfo.ReportId, chapterInfo.ReportChapterId)
  227. if err = EsAddOrEditReport(utils.EsReportIndexName, chapterDocId, esChapter); err != nil {
  228. return
  229. }
  230. return
  231. }
  232. func replaceReportBase64ToImg(content string) (newContent string, err error) {
  233. if content == "" {
  234. return
  235. }
  236. pattern := "data:([a-z]+\\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?"
  237. re, _ := regexp.Compile(pattern)
  238. matcher := re.FindAllString(content, 999)
  239. if len(matcher) > 0 {
  240. for _, v := range matcher {
  241. imgUrl, tmpErr := reportBase64ToImg(v)
  242. if tmpErr != nil {
  243. err = tmpErr
  244. return
  245. }
  246. content = strings.ReplaceAll(content, v, imgUrl)
  247. }
  248. }
  249. newContent = content
  250. return
  251. }
  252. func reportBase64ToImg(imageBase64 string) (resourceUrl string, err error) {
  253. if imageBase64 == "" {
  254. err = errors.New("图片为空")
  255. return
  256. }
  257. ext := ".png"
  258. uploadDir := "./static"
  259. randStr := utils.GetRandStringNoSpecialChar(28)
  260. fileName := randStr + ext
  261. fpath := uploadDir + "/" + fileName
  262. b, _ := regexp.MatchString(`^data:\s*image\/(\w+);base64,`, imageBase64)
  263. if !b {
  264. err = errors.New("图片格式不正确")
  265. return
  266. }
  267. re, _ := regexp.Compile(`^data:\s*image\/(\w+);base64,`)
  268. base64Str := re.ReplaceAllString(imageBase64, "")
  269. base64Str = strings.Replace(base64Str, " ", "", -1)
  270. err = utils.SaveBase64ToFile(base64Str, fpath)
  271. if err != nil {
  272. err = errors.New("图片保存失败" + err.Error())
  273. return
  274. }
  275. defer os.Remove(fpath)
  276. hzUploadDir := utils.RESOURCE_DIR + "images/"
  277. savePath := hzUploadDir + time.Now().Format("200601/20060102/")
  278. savePath += fileName
  279. ossClient := NewOssClient()
  280. if ossClient == nil {
  281. err = fmt.Errorf("初始化OSS服务失败")
  282. return
  283. }
  284. resourceUrl, err = ossClient.UploadFile(fileName, fpath, savePath)
  285. if err != nil {
  286. err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
  287. return
  288. }
  289. item := new(models.Resource)
  290. item.ResourceUrl = resourceUrl
  291. item.ResourceType = 1
  292. item.CreateTime = time.Now()
  293. _, err = models.AddResource(item)
  294. if err != nil {
  295. err = errors.New("资源上传失败" + err.Error())
  296. return
  297. }
  298. return
  299. }
  300. func PcCreateAndUploadSunCode(scene, page string) (imgUrl string, err error) {
  301. if page == "" {
  302. err = errors.New("page不能为空")
  303. return
  304. }
  305. sceneMD5 := "a=1"
  306. if scene != "" {
  307. sceneMD5 = utils.MD5(scene)
  308. }
  309. picByte, err := GetSunCode(page, sceneMD5)
  310. if err != nil {
  311. return
  312. }
  313. localPath := "./static/imgs"
  314. fileName := utils.GetRandStringNoSpecialChar(28) + ".png"
  315. fpath := fmt.Sprint(localPath, "/", fileName)
  316. f, err := os.Create(fpath)
  317. if err != nil {
  318. fmt.Println("11111")
  319. return
  320. }
  321. if _, err = f.Write(picByte); err != nil {
  322. return
  323. }
  324. defer func() {
  325. f.Close()
  326. os.Remove(fpath)
  327. }()
  328. fileDir := "yb/suncode/"
  329. savePath := fileDir + time.Now().Format("200601/20060102/") + fileName
  330. ossClient := NewOssClient()
  331. if ossClient == nil {
  332. err = fmt.Errorf("初始化OSS服务失败")
  333. return
  334. }
  335. imgUrl, err = ossClient.UploadFile(fileName, fpath, savePath)
  336. if err != nil {
  337. err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
  338. return
  339. }
  340. if err != nil {
  341. return
  342. }
  343. if scene != "" {
  344. newSuncode := &models.YbPcSuncode{
  345. Scene: scene,
  346. SceneMd5: sceneMD5,
  347. CodePage: page,
  348. SuncodeUrl: imgUrl,
  349. CreateTime: time.Now(),
  350. }
  351. err = models.AddYbPcSunCode(newSuncode)
  352. }
  353. if scene != "" {
  354. newPars := &models.YbSuncodePars{
  355. Scene: scene,
  356. SceneKey: sceneMD5,
  357. CreateTime: time.Now(),
  358. }
  359. err = models.AddYbSuncodePars(newPars)
  360. }
  361. return
  362. }
  363. func CreateNewReport(req models.AddReq, adminInfo *system.Admin) (newReportId int64, reportCode, errMsg string, err error) {
  364. contentSub := ""
  365. if req.Content != "" {
  366. e := utils.ContentXssCheck(req.Content)
  367. if e != nil {
  368. errMsg = "存在非法标签"
  369. err = errors.New("存在非法标签, Err: " + e.Error())
  370. return
  371. }
  372. contentClean, e := FilterReportContentBr(req.Content)
  373. if e != nil {
  374. errMsg = "内容去除前后空格失败"
  375. err = errors.New("内容去除前后空格失败, Err: " + e.Error())
  376. return
  377. }
  378. req.Content = contentClean
  379. sub, e := GetReportContentSub(req.Content)
  380. if e != nil {
  381. go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+e.Error(), 3)
  382. }
  383. contentSub = sub
  384. }
  385. maxStage, e := models.GetReportStage(req.ClassifyIdFirst, req.ClassifyIdSecond, req.ClassifyIdThird)
  386. if e != nil {
  387. errMsg = "期数获取失败!"
  388. err = errors.New("期数获取失败,Err:" + e.Error())
  389. return
  390. }
  391. item := new(models.Report)
  392. item.AddType = req.AddType
  393. item.ClassifyIdFirst = req.ClassifyIdFirst
  394. item.ClassifyNameFirst = req.ClassifyNameFirst
  395. item.ClassifyIdSecond = req.ClassifyIdSecond
  396. item.ClassifyNameSecond = req.ClassifyNameSecond
  397. item.Title = req.Title
  398. item.Abstract = req.Abstract
  399. item.Author = req.Author
  400. item.Frequency = req.Frequency
  401. item.State = req.State
  402. item.Content = html.EscapeString(req.Content)
  403. item.Stage = maxStage + 1
  404. item.ContentSub = html.EscapeString(contentSub)
  405. if req.CreateTime != "" {
  406. createTime, tmpErr := time.ParseInLocation(utils.FormatDateTime, req.CreateTime, time.Local)
  407. if tmpErr != nil {
  408. err = tmpErr
  409. return
  410. }
  411. item.CreateTime = createTime
  412. }
  413. item.ModifyTime = time.Now()
  414. item.ReportVersion = req.ReportVersion
  415. item.AdminId = adminInfo.AdminId
  416. item.AdminRealName = adminInfo.RealName
  417. item.ClassifyIdThird = req.ClassifyIdThird
  418. item.ClassifyNameThird = req.ClassifyNameThird
  419. if req.CollaborateType == 2 {
  420. item.HasChapter = 1
  421. item.ChapterType = ""
  422. }
  423. item.LastModifyAdminId = adminInfo.AdminId
  424. item.LastModifyAdminName = adminInfo.RealName
  425. item.ContentModifyTime = time.Now()
  426. item.NeedSplice = 1
  427. item.ContentStruct = html.EscapeString(req.ContentStruct)
  428. item.HeadImg = req.HeadImg
  429. item.EndImg = req.EndImg
  430. item.CanvasColor = req.CanvasColor
  431. item.HeadResourceId = req.HeadResourceId
  432. item.EndResourceId = req.EndResourceId
  433. item.CollaborateType = req.CollaborateType
  434. item.ReportLayout = req.ReportLayout
  435. item.IsPublicPublish = req.IsPublicPublish
  436. item.ReportCreateTime = time.Now()
  437. err, errMsg = AddReportAndChapter(item, 0, req.GrantAdminIdList)
  438. return
  439. }
  440. func FilterReportContentBr(content string) (res string, err error) {
  441. newContent := content
  442. content = html.UnescapeString(content)
  443. if content == "" {
  444. return
  445. }
  446. content = strings.Replace(content, "<p data-f-id=\"pbf\" style=\"text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sans-serif;\">Powered by <a href=\"https://www.froala.com/wysiwyg-editor?pb=1\" title=\"Froala Editor\">Froala Editor</a></p>", "", -1)
  447. defer func() {
  448. if err != nil {
  449. go alarm_msg.SendAlarmMsg("过滤报告正文前后换行符及空格失败, ErrMsg: "+err.Error(), 3)
  450. }
  451. }()
  452. configKey := "report_filter_br"
  453. conf, e := company.GetConfigDetailByCode(configKey)
  454. if e != nil {
  455. err = errors.New("获取报告过滤配置失败, Err: " + e.Error())
  456. return
  457. }
  458. if conf.ConfigValue != "1" {
  459. return content, nil
  460. }
  461. re := regexp.MustCompile(`(?is:<p(.*?)</p>)`)
  462. arr := re.FindAllString(content, -1)
  463. indexArr := re.FindAllIndex([]byte(content), -1)
  464. emptyRe := `<p[^>]*>(<br>|<br/>)+</p>`
  465. startIsBr := false
  466. countEmptyBr := 0 // 需要连续替换的空<p>总数
  467. lastBrRange := 0 // 最后一个空<p>右侧index, 用来判断是否为连续的空<p>
  468. for i := range arr {
  469. byteRange := indexArr[i]
  470. if len(byteRange) == 2 {
  471. if i == 0 && byteRange[0] == 0 {
  472. startIsBr = true
  473. }
  474. if !startIsBr {
  475. break
  476. }
  477. if lastBrRange != 0 {
  478. if lastBrRange != byteRange[0] {
  479. break
  480. }
  481. }
  482. m, e := regexp.Match(emptyRe, []byte(arr[i]))
  483. if e != nil {
  484. err = e
  485. return
  486. }
  487. if m {
  488. countEmptyBr += 1
  489. lastBrRange = byteRange[1]
  490. continue
  491. }
  492. break
  493. }
  494. }
  495. if countEmptyBr > 0 {
  496. reg, e := regexp.Compile(emptyRe)
  497. if e != nil {
  498. err = errors.New("正则解析失败, Err: " + e.Error())
  499. return
  500. }
  501. counted := 0 // 已替换数
  502. res = reg.ReplaceAllStringFunc(content, func(s string) string {
  503. counted += 1
  504. if counted <= countEmptyBr {
  505. return ""
  506. } else {
  507. return s
  508. }
  509. })
  510. if res == "" {
  511. res = newContent
  512. }
  513. } else {
  514. res = content
  515. if res == "" {
  516. res = newContent
  517. }
  518. }
  519. return
  520. }
  521. func GetEnglishReportOverview(content string) (res string, err error) {
  522. content = html.UnescapeString(content)
  523. doc, e := goquery.NewDocumentFromReader(strings.NewReader(content))
  524. if e != nil {
  525. err = errors.New("Create Doc Err: " + e.Error())
  526. return
  527. }
  528. target := "overview"
  529. label := "</strong>"
  530. start := -1
  531. end := -1
  532. doc.Find("p").Each(func(i int, s *goquery.Selection) {
  533. h, e := s.Html()
  534. if e != nil {
  535. err = errors.New("Get Html1 Err: " + e.Error())
  536. return
  537. }
  538. h = strings.ToLower(h)
  539. t := s.Text()
  540. t = strings.ToLower(t)
  541. if strings.Contains(h, label) && t != "" && strings.Contains(t, target) {
  542. start = i
  543. }
  544. if start != -1 && end == -1 && i > start && strings.Contains(h, label) {
  545. end = i
  546. }
  547. })
  548. if start != -1 && end != -1 {
  549. doc.Find("p").Each(func(i int, s *goquery.Selection) {
  550. if i > start && i < end {
  551. h, e := s.Html()
  552. if e != nil {
  553. err = errors.New("Get Html2 Err: " + e.Error())
  554. return
  555. }
  556. if strings.Contains(h, "iframe") {
  557. return
  558. }
  559. res += `<p>` + h + `</p>`
  560. }
  561. })
  562. }
  563. return
  564. }
  565. func GetReportContentSubWithoutIframe(content string) (contentSub string, err error) {
  566. content = html.UnescapeString(content)
  567. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  568. if err != nil {
  569. fmt.Println("create doc err:", err.Error())
  570. return
  571. }
  572. label := "iframe"
  573. n := 0
  574. doc.Find("p").Each(func(i int, s *goquery.Selection) {
  575. if n >= 5 {
  576. return
  577. }
  578. n++
  579. h, err := s.Html()
  580. if err != nil {
  581. fmt.Println("get html err", err.Error())
  582. return
  583. }
  584. if strings.Contains(h, label) {
  585. return
  586. }
  587. if s.Text() != "" || strings.Contains(h, "src") {
  588. contentSub = contentSub + "<p>" + h + "</p>"
  589. }
  590. })
  591. return
  592. }
  593. func UpdateReportEditMark(reportId, reportChapterId, nowUserId, status int, nowUserName, lang string) (ret models.MarkReportResp, err error) {
  594. key := fmt.Sprint(`crm:report:edit:`, reportId)
  595. if reportChapterId > 0 {
  596. key = fmt.Sprint(key, ":", reportChapterId)
  597. }
  598. ret.Status = 0
  599. ret.Msg = "无人编辑"
  600. opUserId, e := utils.Rc.RedisInt(key)
  601. var opUser models.MarkReportItem
  602. var classifyNameFirst string
  603. if e != nil {
  604. opUserInfoStr, tErr := utils.Rc.RedisString(key)
  605. if tErr == nil {
  606. tErr = json.Unmarshal([]byte(opUserInfoStr), &opUser)
  607. if tErr == nil {
  608. opUserId = opUser.AdminId
  609. }
  610. }
  611. }
  612. var reportInfo *models.ReportDetail
  613. classifyNameFirst = opUser.ReportClassifyNameFirst
  614. if reportId > 0 && status != 2 && classifyNameFirst == "" {
  615. reportInfo, err = models.GetReportById(reportId)
  616. if err != nil {
  617. err = fmt.Errorf("报告不存在")
  618. return
  619. }
  620. classifyNameFirst = reportInfo.ClassifyNameFirst
  621. }
  622. if opUserId > 0 && opUserId != nowUserId {
  623. editor := opUser.Editor
  624. if editor == "" {
  625. otherInfo, e := system.GetSysAdminById(opUserId)
  626. if e != nil {
  627. err = fmt.Errorf("查询其他编辑者信息失败")
  628. return
  629. }
  630. editor = otherInfo.RealName
  631. }
  632. ret.Status = 1
  633. if lang == utils.EnLangVersion {
  634. ret.Msg = fmt.Sprintf("%s is currently editing the report", editor)
  635. } else {
  636. ret.Msg = fmt.Sprintf("当前%s正在编辑报告", editor)
  637. }
  638. ret.Editor = editor
  639. return
  640. }
  641. if status == 1 {
  642. nowUser := &models.MarkReportItem{AdminId: nowUserId, Editor: nowUserName, ReportClassifyNameFirst: classifyNameFirst}
  643. bt, e := json.Marshal(nowUser)
  644. if e != nil {
  645. err = fmt.Errorf("格式化编辑者信息失败")
  646. return
  647. }
  648. if opUserId > 0 {
  649. utils.Rc.Do("SETEX", key, int64(60), string(bt)) //3分钟缓存
  650. } else {
  651. utils.Rc.SetNX(key, string(bt), time.Second*60*1) //3分钟缓存
  652. }
  653. } else if status == 3 {
  654. _ = utils.Rc.Delete(key)
  655. }
  656. return
  657. }
  658. func HandleVideoDecibel(chapterInfo *models.ReportChapter) {
  659. public_api.HandleVideoDecibel(chapterInfo.ReportChapterId)
  660. return
  661. }
  662. func SaveReportLogs(item *models.Report, chapters []*models.ReportChapter, adminId int, adminRealName string) {
  663. if item == nil && len(chapters) == 0 {
  664. return
  665. }
  666. var err error
  667. defer func() {
  668. if err != nil {
  669. tips := fmt.Sprintf("报告日志记录, SaveReportLogs error: %s", err.Error())
  670. go alarm_msg.SendAlarmMsg(tips, 2)
  671. }
  672. }()
  673. if item != nil {
  674. e := models.AddReportSaveLog(item.Id, item.AdminId, item.Content, item.ContentSub, item.ContentStruct, item.CanvasColor, item.AdminRealName, item.HeadResourceId, item.EndResourceId)
  675. if e != nil {
  676. err = fmt.Errorf("AddReportSaveLog: %s", e.Error())
  677. return
  678. }
  679. }
  680. if len(chapters) > 0 {
  681. e := models.MultiAddReportChaptersSaveLog(chapters, adminId, adminRealName)
  682. if e != nil {
  683. err = fmt.Errorf("MultiAddReportChaptersSaveLog: %s", e.Error())
  684. return
  685. }
  686. }
  687. return
  688. }