report.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. package services
  2. import (
  3. "errors"
  4. "eta/eta_task/models"
  5. "eta/eta_task/models/report"
  6. "eta/eta_task/services/alarm_msg"
  7. "eta/eta_task/utils"
  8. "fmt"
  9. "golang.org/x/net/context"
  10. "html"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // UpdateReportEs 更新报告/章节Es
  16. func UpdateReportEs(reportId int, publishState int) (err error) {
  17. if reportId <= 0 {
  18. return
  19. }
  20. reportInfo, err := models.GetReportByReportId(reportId)
  21. if err != nil {
  22. return
  23. }
  24. categories := ""
  25. if reportInfo.HasChapter == 1 {
  26. // 晨周报
  27. chapterList, tmpErr := models.GetPublishedChapterListByReportId(reportInfo.Id)
  28. if tmpErr != nil {
  29. return
  30. }
  31. if len(chapterList) > 0 {
  32. // 更新章节的es数据
  33. for _, chapterInfo := range chapterList {
  34. err = updateReportChapterEsByChapter(chapterInfo)
  35. if err != nil {
  36. return
  37. }
  38. }
  39. }
  40. } else {
  41. //if utils.BusinessCode == utils.BusinessCodeRelease || utils.BusinessCode == utils.BusinessCodeSandbox {
  42. permissionList, tmpErr := models.GetChartPermissionNameFromMappingByKeyword("rddp", reportInfo.ClassifyIdSecond)
  43. if tmpErr != nil {
  44. return
  45. }
  46. categoryArr := make([]string, 0)
  47. for i := 0; i < len(permissionList); i++ {
  48. categoryArr = append(categoryArr, permissionList[i].PermissionName)
  49. }
  50. aliasArr, _ := addCategoryAliasToArr(categoryArr)
  51. categories = strings.Join(aliasArr, ",")
  52. //}
  53. }
  54. // 最小单位的分类id
  55. minClassifyId, minClassifyName, err := getMinClassify(reportInfo)
  56. if err != nil {
  57. return
  58. }
  59. // 新增报告ES
  60. esReport := &models.ElasticReportDetail{
  61. ReportId: reportInfo.Id,
  62. ReportChapterId: 0,
  63. Title: reportInfo.Title,
  64. Abstract: reportInfo.Abstract,
  65. BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)),
  66. PublishTime: reportInfo.PublishTime.Format(utils.FormatDateTime),
  67. PublishState: publishState,
  68. Author: reportInfo.Author,
  69. ClassifyIdFirst: reportInfo.ClassifyIdFirst,
  70. ClassifyNameFirst: reportInfo.ClassifyNameFirst,
  71. ClassifyIdSecond: reportInfo.ClassifyIdSecond,
  72. ClassifyNameSecond: reportInfo.ClassifyNameSecond,
  73. ClassifyId: minClassifyId,
  74. ClassifyName: minClassifyName,
  75. Categories: categories,
  76. StageStr: strconv.Itoa(reportInfo.Stage),
  77. }
  78. docId := fmt.Sprintf("%d-%d", reportInfo.Id, 0)
  79. if err = EsAddOrEditReport(utils.EsReportIndexName, docId, esReport); err != nil {
  80. return
  81. }
  82. return
  83. }
  84. // addCategoryAliasToArr 品种别名
  85. func addCategoryAliasToArr(categoryArr []string) (aliasArr []string, err error) {
  86. aliasArr = categoryArr
  87. if len(categoryArr) > 0 {
  88. for i := 0; i < len(categoryArr); i++ {
  89. if strings.Contains(categoryArr[i], "沥青") {
  90. aliasArr = append(aliasArr, "BU")
  91. }
  92. if strings.Contains(categoryArr[i], "MEG") {
  93. aliasArr = append(aliasArr, "EG", "乙二醇")
  94. }
  95. if strings.Contains(categoryArr[i], "聚酯") {
  96. aliasArr = append(aliasArr, "长丝", "短纤", "瓶片")
  97. }
  98. if strings.Contains(categoryArr[i], "纯苯+苯乙烯") {
  99. aliasArr = append(aliasArr, "EB")
  100. }
  101. if strings.Contains(categoryArr[i], "聚乙烯") {
  102. aliasArr = append(aliasArr, "PP", "PE")
  103. }
  104. if strings.Contains(categoryArr[i], "玻璃纯碱") {
  105. aliasArr = append(aliasArr, "玻璃", "纯碱", "FG", "SA")
  106. }
  107. if strings.Contains(categoryArr[i], "甲醇") {
  108. aliasArr = append(aliasArr, "甲醇", "MA")
  109. }
  110. if strings.Contains(categoryArr[i], "橡胶") {
  111. aliasArr = append(aliasArr, "橡胶", "RU")
  112. }
  113. }
  114. }
  115. return
  116. }
  117. // updateReportChapterEsByChapter
  118. // @Description: 通过章节详情更新报告章节ES
  119. // @author: Roc
  120. // @datetime 2024-06-20 13:16:11
  121. // @param chapterInfo *models.ReportChapter
  122. // @return err error
  123. func updateReportChapterEsByChapter(chapterInfo *models.ReportChapter) (err error) {
  124. // 章节对应的品种
  125. obj := report.ReportChapterPermissionMapping{}
  126. permissionList, tmpErr := obj.GetPermissionItemListById(chapterInfo.ReportChapterId)
  127. if tmpErr != nil {
  128. return
  129. }
  130. categoryArr := make([]string, 0)
  131. if len(permissionList) > 0 {
  132. for ii := 0; ii < len(permissionList); ii++ {
  133. categoryArr = append(categoryArr, permissionList[ii].ChartPermissionName)
  134. }
  135. }
  136. aliasArr, _ := addCategoryAliasToArr(categoryArr)
  137. categories := strings.Join(aliasArr, ",")
  138. // 新增/编辑ES
  139. esChapter := &models.ElasticReportDetail{
  140. ReportId: chapterInfo.ReportId,
  141. ReportChapterId: chapterInfo.ReportChapterId,
  142. Title: chapterInfo.Title,
  143. Abstract: chapterInfo.Abstract,
  144. BodyContent: utils.TrimHtml(html.UnescapeString(chapterInfo.Content)),
  145. PublishTime: chapterInfo.PublishTime.Format(utils.FormatDateTime),
  146. PublishState: chapterInfo.PublishState,
  147. Author: chapterInfo.Author,
  148. ClassifyIdFirst: chapterInfo.ClassifyIdFirst,
  149. ClassifyNameFirst: chapterInfo.ClassifyNameFirst,
  150. ClassifyIdSecond: 0,
  151. ClassifyNameSecond: "",
  152. ClassifyId: chapterInfo.ClassifyIdFirst,
  153. ClassifyName: chapterInfo.ClassifyNameFirst,
  154. Categories: categories,
  155. StageStr: strconv.Itoa(chapterInfo.Stage),
  156. }
  157. chapterDocId := fmt.Sprintf("%d-%d", chapterInfo.ReportId, chapterInfo.ReportChapterId)
  158. if err = EsAddOrEditReport(utils.EsReportIndexName, chapterDocId, esChapter); err != nil {
  159. return
  160. }
  161. return
  162. }
  163. // PublishReport 定时发布研报-每秒
  164. func PublishReport(cont context.Context) (err error) {
  165. errMsgList := make([]string, 0)
  166. defer func() {
  167. if err != nil {
  168. go alarm_msg.SendAlarmMsg("PublishReport-定时发布研报失败, ErrMsg:\n"+err.Error(), 3)
  169. }
  170. }()
  171. now := time.Now().Format(utils.FormatDateTimeMinute)
  172. startTime := now + ":00"
  173. endTime := now + ":59"
  174. afterDate := time.Now().AddDate(0, -1, 0).Format(utils.FormatDate) //限制一下,只查询最近一个月的
  175. list, err := models.GetPrePublishedReports(startTime, endTime, afterDate)
  176. if err != nil {
  177. return
  178. }
  179. listLen := len(list)
  180. if listLen == 0 {
  181. return
  182. }
  183. // 比对时间(分钟),时间相等则发布并推送
  184. for i := 0; i < listLen; i++ {
  185. item := list[i]
  186. var publishTime time.Time
  187. if item.MsgIsSend == 1 && !item.PublishTime.IsZero() { //如果报告曾经发布过,并且已经发送过模版消息,则章节的发布时间为报告的发布时间
  188. publishTime = item.PublishTime
  189. } else {
  190. publishTime = time.Now()
  191. }
  192. //if item.HasChapter == 1 && (item.ChapterType == utils.REPORT_TYPE_DAY || item.ChapterType == utils.REPORT_TYPE_WEEK) {
  193. // continue
  194. //}
  195. var tmpErr error
  196. if item.HasChapter == 1 { // 章节类型的报告
  197. // 发布报告和章节
  198. item.State = 2
  199. item.PublishTime = publishTime
  200. item.ModifyTime = time.Now().Local()
  201. updateCols := make([]string, 0)
  202. updateCols = append(updateCols, "PublishTime", "State", "ModifyTime")
  203. tmpErr = models.PublishReportAndChapter(item, true, updateCols)
  204. } else {
  205. tmpErr = models.PublishReportById(item.Id, publishTime)
  206. }
  207. if tmpErr != nil {
  208. errMsgList = append(errMsgList, fmt.Sprint("报告发布失败,ID:", item.Id, ",ErrMsg:", tmpErr.Error()))
  209. } else {
  210. go handleByPublishReport(item)
  211. }
  212. }
  213. return
  214. }
  215. // handleByPublishReport
  216. // @Description: 报告发布
  217. // @author: Roc
  218. // @datetime 2024-06-28 13:26:51
  219. // @param item *models.Report
  220. // @param publishTime time.Time
  221. // @return err error
  222. func handleByPublishReport(item *models.Report) {
  223. recordItem := &models.ReportStateRecord{
  224. ReportId: item.Id,
  225. ReportType: 1,
  226. State: 2,
  227. AdminId: item.AdminId,
  228. AdminName: item.AdminName,
  229. CreateTime: time.Now(),
  230. }
  231. // 添加记录
  232. _, _ = models.AddReportStateRecord(recordItem)
  233. if item.HasChapter == 1 {
  234. // 生产报告章节音频
  235. _ = UpdateChaptersVideoByReportId(item)
  236. } else {
  237. // 生成音频
  238. if item.VideoUrl == "" {
  239. _ = CreateVideo(item)
  240. }
  241. }
  242. // 更新报告Es
  243. err := UpdateReportEs(item.Id, 2)
  244. if err != nil {
  245. utils.FileLog.Error("UpdateReportEs, 更新报告Es失败, ReportId:%s, Err:%s", item.Id, err.Error())
  246. }
  247. // 判断是否未发送模版消息,并且配置了立即推送模版消息的报告需要推送
  248. if utils.SendWxTemplateEnable == "1" {
  249. if item.MsgIsSend == 0 && item.PreMsgSend == 1 {
  250. _ = ReportSendTemplateMsg(item.Id)
  251. }
  252. }
  253. return
  254. }
  255. // PublishReportTest 定时发布研报-每秒
  256. /*func PublishReportTest() (err error) {
  257. defer func() {
  258. if err != nil {
  259. fmt.Println(err.Error())
  260. }
  261. }()
  262. item, err := models.GetReportById(3331)
  263. if err != nil {
  264. return
  265. }
  266. // 判断是否未发送模版消息,并且配置了立即推送模版消息的报告需要推送
  267. if item.MsgIsSend == 0 && item.PreMsgSend == 1 {
  268. err = ReportSendTemplateMsg(item.Id)
  269. if err != nil {
  270. return
  271. }
  272. }
  273. return
  274. }*/
  275. func ReportSendTemplateMsg(reportId int) (err error) {
  276. defer func() {
  277. if err != nil {
  278. msg := fmt.Sprintf("ReportSendTemplateMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportId, err.Error())
  279. utils.FileLog.Error(msg)
  280. go alarm_msg.SendAlarmMsg(msg, 3)
  281. }
  282. }()
  283. reportInfo, err := models.GetReportByReportId(reportId)
  284. if err != nil {
  285. err = errors.New("查询报告失败 Err:" + err.Error())
  286. return
  287. }
  288. if reportInfo.MsgIsSend == 1 {
  289. err = errors.New("模板消息已推送,请勿重复操作")
  290. return
  291. }
  292. videoNameDate := `(` + time.Now().Format("0102") + `)`
  293. err = models.UpdateReportPublishTime(reportId, videoNameDate)
  294. if err != nil {
  295. err = errors.New("修改发布时间失败,Err:" + err.Error())
  296. return
  297. }
  298. if reportInfo.HasChapter > 0 {
  299. err = models.UpdateReportChapterPublishTime(reportId, videoNameDate)
  300. if err != nil {
  301. err = errors.New("修改发布时间失败,Err:" + err.Error())
  302. return
  303. }
  304. }
  305. err = sendMiniProgramReportWxMsg(reportInfo)
  306. if err != nil {
  307. err = errors.New("发送失败,Err:" + err.Error())
  308. return
  309. }
  310. err = models.ModifyReportMsgIsSend(reportId)
  311. if err != nil {
  312. err = errors.New("发送失败,Err:" + err.Error())
  313. return
  314. }
  315. return
  316. }
  317. // sendMiniProgramReportWxMsg 推送报告微信模板消息-小程序链接
  318. func sendMiniProgramReportWxMsg(report *models.Report) (err error) {
  319. reportId := report.Id
  320. var msg string
  321. reportIdStr := strconv.Itoa(reportId)
  322. defer func() {
  323. if err != nil {
  324. fmt.Println("msg:", msg)
  325. utils.FileLog.Error(fmt.Sprintf("SendMiniProgramReportWxMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportIdStr, err.Error()))
  326. go alarm_msg.SendAlarmMsg("SendMiniProgramReportWxMsg发送报告模版消息失败;"+"ReportId:"+reportIdStr+",Err:"+err.Error()+";msg:"+msg, 3)
  327. //go utils.SendEmail("SendMiniProgramReportWxMsg发送报告模版消息失败"+"【"+utils.APPNAME+"】"+"【"+utils.RunMode+"】"+time.Now().Format("2006-01-02 15:04:05"), "ReportId:"+reportIdStr+";"+msg+";Err:"+err.Error(), toUser)
  328. }
  329. }()
  330. utils.FileLog.Info("%s", "services SendMsg")
  331. if report == nil {
  332. utils.FileLog.Info("报告信息不存在")
  333. return
  334. }
  335. var openIdArr []string
  336. // 如果是弘则,且报告分类是晨报,那么就所有人推送
  337. if (utils.BusinessCode == utils.BusinessCodeRelease || utils.BusinessCode == utils.BusinessCodeSandbox || utils.BusinessCode == utils.BusinessCodeDebug) && report.ClassifyNameFirst == "晨报" {
  338. // 如果是章节,那就推送所有用户
  339. openIdArr, err = models.GetOpenIdArr()
  340. if err != nil {
  341. msg = "get GetOpenIdArr err:" + err.Error()
  342. return
  343. }
  344. } else {
  345. minClassifyId, _, err := getMinClassify(report)
  346. if err != nil {
  347. msg = "获取报告的最小分类失败 err:" + err.Error()
  348. return err
  349. }
  350. // 判断分类是否存在
  351. _, err = models.GetClassifyById(minClassifyId)
  352. if err != nil {
  353. msg = "获取报告分类失败 err:" + err.Error()
  354. return err
  355. }
  356. // 获取该分类关联的openid列表
  357. openIdArr, err = models.GetOpenIdArrByClassifyId(minClassifyId)
  358. if err != nil {
  359. msg = "GetOpenIdArrByClassifyNameSecond err:" + err.Error()
  360. return err
  361. }
  362. }
  363. // TODO 弘则的文案是不是要去掉
  364. title := fmt.Sprintf("弘则%s", report.ClassifyNameFirst)
  365. if CheckTwoWeekOrMonthReport(report.ClassifyIdFirst, report.ClassifyNameFirst) {
  366. title = fmt.Sprintf("弘则%s", report.ClassifyNameSecond)
  367. }
  368. //redirectUrl := utils.TemplateRedirectUrl + strconv.Itoa(reportId)
  369. first := fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
  370. keyword1 := title
  371. keyword2 := report.Title
  372. keyword3 := report.PublishTime.Format(utils.FormatDateTime)
  373. keyword4 := report.Abstract
  374. var wxAppPath string
  375. if report.ChapterType == utils.REPORT_TYPE_WEEK {
  376. wxAppPath = fmt.Sprintf("pages-report/chapterList?reportId=%s", reportIdStr)
  377. } else {
  378. wxAppPath = fmt.Sprintf("pages-report/reportDetail?reportId=%s", reportIdStr)
  379. }
  380. sendInfo := new(SendWxTemplate)
  381. sendInfo.First = first
  382. sendInfo.Keyword1 = keyword1
  383. sendInfo.Keyword2 = keyword2
  384. sendInfo.Keyword3 = keyword3
  385. sendInfo.Keyword4 = keyword4
  386. sendInfo.TemplateId = utils.TemplateIdByProduct
  387. sendInfo.RedirectUrl = wxAppPath
  388. sendInfo.Resource = wxAppPath
  389. sendInfo.SendType = utils.TEMPLATE_MSG_REPORT
  390. sendInfo.OpenIdArr = openIdArr
  391. sendInfo.RedirectTarget = 1
  392. err = SendTemplateMsgV2(sendInfo)
  393. return
  394. }
  395. // CheckTwoWeekOrMonthReport 校验推送报告是否为双周报或者月报
  396. func CheckTwoWeekOrMonthReport(classifyId int, classifyName string) (ok bool) {
  397. if utils.RunMode == "debug" {
  398. miniStrArr := []string{
  399. "双周报", "月报",
  400. }
  401. if utils.InArrayByStr(miniStrArr, classifyName) {
  402. ok = true
  403. }
  404. } else {
  405. // 此处生产环境用ID主要是担心分类改了名字...
  406. IdArr := []int{
  407. 96, 112,
  408. }
  409. if utils.InArrayByInt(IdArr, classifyId) {
  410. ok = true
  411. }
  412. }
  413. return
  414. }
  415. // ClearReportSaveLog 每天清理两周前的报告保存日志
  416. func ClearReportSaveLog(cont context.Context) (err error) {
  417. defer func() {
  418. if err != nil {
  419. tips := fmt.Sprintf("报告日志记录, SaveReportLogs error: %s", err.Error())
  420. fmt.Println(tips)
  421. go alarm_msg.SendAlarmMsg(tips, 2)
  422. }
  423. }()
  424. twoWeek := time.Now().Local().AddDate(0, 0, -14).Format(utils.FormatDateTime)
  425. e := models.ClearReportSaveLog(twoWeek)
  426. if e != nil {
  427. err = fmt.Errorf("ClearReportSaveLog: %s", e.Error())
  428. }
  429. return
  430. }
  431. // UpdateChaptersVideoByReportId
  432. // @Description: 更新报告章节音频
  433. // @author: Roc
  434. // @datetime 2024-06-28 13:52:56
  435. // @param reportInfo *models.Report
  436. // @return err error
  437. func UpdateChaptersVideoByReportId(reportInfo *models.Report) (err error) {
  438. reportId := reportInfo.Id
  439. defer func() {
  440. if err != nil {
  441. utils.FileLog.Error("UpdateChaptersVideo, reportId:%v, Err:%s", reportId, err.Error())
  442. go alarm_msg.SendAlarmMsg(fmt.Sprintf("更新章节音频失败, 报告ID: %v; Err: "+err.Error(), reportId), 3)
  443. }
  444. }()
  445. chapterList, err := models.GetChapterListByReportId(reportId)
  446. if err != nil {
  447. return
  448. }
  449. reportContent := ""
  450. // 生成video
  451. nowTime := time.Now()
  452. updateCols := make([]string, 0)
  453. updateCols = append(updateCols, "VideoUrl", "VideoName", "VideoSize", "VideoPlaySeconds")
  454. for i := 0; i < len(chapterList); i++ {
  455. item := chapterList[i]
  456. reportContent += item.Title + `。`
  457. reportContent += item.Content + `。`
  458. // 忽略已有音频的章节
  459. if item.VideoUrl != "" && item.VideoName != "" && item.VideoSize != "" && item.VideoPlaySeconds != "" {
  460. continue
  461. }
  462. videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(item.Title, html.UnescapeString(item.Content), nowTime.Format(utils.FormatDateTime))
  463. if e != nil {
  464. err = e
  465. return
  466. }
  467. item.VideoUrl = videoUrl
  468. item.VideoName = videoName
  469. item.VideoSize = videoSize
  470. item.VideoPlaySeconds = fmt.Sprintf("%.2f", videoPlaySeconds)
  471. if e = item.UpdateChapter(updateCols); e != nil {
  472. err = e
  473. }
  474. }
  475. // 生成汇总音频
  476. {
  477. if reportInfo.VideoUrl != "" && reportInfo.VideoName != "" && reportInfo.VideoSize != "" && reportInfo.VideoPlaySeconds != "" {
  478. return
  479. }
  480. videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(reportInfo.Title, html.UnescapeString(reportContent), time.Now().Format(utils.FormatDateTime))
  481. if e != nil {
  482. err = e
  483. return
  484. }
  485. // 修改报告的音频信息
  486. err = models.ModifyReportVideoByNoVideo(reportInfo.Id, videoUrl, videoName, videoSize, videoPlaySeconds)
  487. }
  488. return
  489. }
  490. // getMinClassify
  491. // @Description: 获取最小分类ID
  492. // @author: Roc
  493. // @datetime 2024-06-20 09:23:19
  494. // @param reportInfo *models.Report
  495. // @return minClassifyId int
  496. // @return minClassifyName string
  497. // @return err error
  498. func getMinClassify(reportInfo *models.Report) (minClassifyId int, minClassifyName string, err error) {
  499. defer func() {
  500. if err != nil {
  501. utils.FileLog.Error("获取最小分类ID失败,报告ID:%d,Err:%s", reportInfo.Id, err.Error())
  502. }
  503. }()
  504. minClassifyId = reportInfo.ClassifyIdThird
  505. minClassifyName = reportInfo.ClassifyNameThird
  506. if minClassifyId <= 0 {
  507. minClassifyId = reportInfo.ClassifyIdSecond
  508. minClassifyName = reportInfo.ClassifyNameSecond
  509. }
  510. if minClassifyId <= 0 {
  511. minClassifyId = reportInfo.ClassifyIdFirst
  512. minClassifyName = reportInfo.ClassifyNameFirst
  513. }
  514. if minClassifyId <= 0 {
  515. err = errors.New("分类异常")
  516. }
  517. return
  518. }