package services import ( "context" "eta/eta_task/models" "eta/eta_task/services/alarm_msg" "eta/eta_task/utils" "fmt" "html" "strconv" "time" ) // PublishSmartReport 定时发布智能研报 func PublishSmartReport(cont context.Context) (err error) { defer func() { if err != nil { go alarm_msg.SendAlarmMsg("PublishSmartReport-定时发布智能研报失败, ErrMsg:\n"+err.Error(), 3) } }() now := time.Now().Format(utils.FormatDateTimeMinute) startTime := now + ":00" endTime := now + ":59" list, e := models.GetPrePublishSmartReports(startTime, endTime) if e != nil { err = fmt.Errorf("GetPrePublishSmartReports err: %s", e.Error()) return } listLen := len(list) if listLen == 0 { return } // 比对时间(分钟), 时间相等则发布并推送 for i := 0; i < listLen; i++ { item := list[i] var publishTime time.Time // 如果报告曾经发布过,并且已经发送过模版消息,则发布时间为原发布时间 if item.MsgIsSend == 1 && !item.PublishTime.IsZero() { publishTime = item.PublishTime } else { publishTime = time.Now() } // 发布报告, 同时更新消息推送状态, 消息推送目前仅为预留功能, 后面小程序需要售卖的时候再细化消息推送 if err = models.PublishSmartReportById(item.SmartReportId, publishTime); err != nil { return } // 写入队列 var queue models.Report2ImgQueueReq queue.ReportType = 2 queue.ReportCode = item.ReportCode _ = utils.Rc.LPush(utils.CACHE_CREATE_REPORT_IMGPDF_QUEUE, queue) // 生成音频, 更新ES go func() { if item.VideoUrl == "" { SmartReportBuildVideoAndUpdate(item) } _ = SmartReportElasticUpsert(item.SmartReportId, 2) }() recordItem := &models.ReportStateRecord{ ReportId: item.SmartReportId, ReportType: 2, State: 2, AdminId: item.AdminId, AdminName: item.AdminRealName, CreateTime: time.Now(), } go func() { _, _ = models.AddReportStateRecord(recordItem) }() } return } // SmartReportBuildVideoAndUpdate 生成音频 func SmartReportBuildVideoAndUpdate(item *models.SmartReport) { if item == nil { return } var err error defer func() { if err != nil { tips := fmt.Sprintf("智能研报-音频生成, errMsg: %s", err.Error()) go alarm_msg.SendAlarmMsg(tips, 2) } }() videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(item.Title, item.Content, time.Now().Local().Format(utils.FormatDateTime)) if e != nil { err = fmt.Errorf("create audio err: %s", e.Error()) return } item.VideoUrl = videoUrl item.VideoName = videoName item.VideoSize = videoSize item.VideoPlaySeconds = fmt.Sprintf("%.2f", videoPlaySeconds) item.ModifyTime = time.Now().Local() cols := []string{"VideoUrl", "VideoName", "VideoSize", "VideoPlaySeconds", "ModifyTime"} if e = item.Update(cols); e != nil { err = fmt.Errorf("smart report update err: %s", e.Error()) return } } // SmartReportElasticUpsert 新增/编辑报告es func SmartReportElasticUpsert(smartReportId int, state int) (err error) { if smartReportId <= 0 { return } reportOB := new(models.SmartReport) item, e := reportOB.GetItemById(smartReportId) if e != nil { if e.Error() == utils.ErrNoRow() { // 可能被删了就直接忽略掉 return } err = fmt.Errorf("获取报告失败, Err: %s", e.Error()) return } esReport := new(models.ElasticSmartReport) esReport.SmartReportId = item.SmartReportId esReport.Title = item.Title esReport.Abstract = item.Abstract esReport.BodyContent = utils.TrimHtml(html.UnescapeString(item.Content)) esReport.PublishTime = item.PublishTime.Format(utils.FormatDateTime) esReport.PublishState = state esReport.Author = item.Author esReport.ClassifyIdFirst = item.ClassifyIdFirst esReport.ClassifyNameFirst = item.ClassifyNameFirst esReport.ClassifyIdSecond = item.ClassifyIdSecond esReport.ClassifyNameSecond = item.ClassifyNameSecond esReport.StageStr = strconv.Itoa(item.Stage) esReport.Frequency = item.Frequency if err = EsAddOrEditSmartReport(utils.SmartReportIndexName, strconv.Itoa(item.SmartReportId), esReport); err != nil { return } return }