123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- package services
- import (
- "encoding/json"
- "errors"
- "fmt"
- "hongze/hongze_ETA_mobile_api/models"
- "hongze/hongze_ETA_mobile_api/models/system"
- "hongze/hongze_ETA_mobile_api/services/alarm_msg"
- "hongze/hongze_ETA_mobile_api/utils"
- "html"
- "strconv"
- "strings"
- "time"
- )
- // CreateNewEnglishReport 生成英文研报
- func CreateNewEnglishReport(req models.AddEnglishReportReq, adminInfo *system.Admin) (newReportId int64, reportCode string, err error) {
- var contentSub string
- if req.Content != "" {
- content, e := FilterReportContentBr(req.Content)
- if e != nil {
- err = errors.New("内容去除前后空格失败, Err: " + e.Error())
- return
- }
- req.Content = content
- contentSub, e = GetReportContentSub(req.Content)
- if e != nil {
- go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+e.Error(), 3)
- err = errors.New("获取报告内容前几段失败, Err: " + e.Error())
- return
- }
- }
- maxStage, e := models.GetEnglishReportStage(req.ClassifyIdFirst, req.ClassifyIdSecond)
- if e != nil {
- err = errors.New("期数获取失败, Err: " + e.Error())
- return
- }
- item := new(models.EnglishReport)
- item.AddType = req.AddType
- item.ClassifyIdFirst = req.ClassifyIdFirst
- item.ClassifyNameFirst = req.ClassifyNameFirst
- item.ClassifyIdSecond = req.ClassifyIdSecond
- item.ClassifyNameSecond = req.ClassifyNameSecond
- item.Title = req.Title
- item.Abstract = req.Abstract
- item.Author = req.Author
- item.Frequency = req.Frequency
- item.State = req.State
- item.Content = html.EscapeString(req.Content)
- item.Stage = maxStage + 1
- item.ContentSub = html.EscapeString(contentSub)
- item.CreateTime = req.CreateTime
- item.ModifyTime = time.Now()
- item.AdminId = adminInfo.AdminId
- item.AdminRealName = adminInfo.RealName
- newReportId, e = models.AddEnglishReport(item)
- if e != nil {
- err = errors.New("新增报告失败, Err: " + e.Error())
- return
- }
- reportCode = utils.MD5(strconv.Itoa(int(newReportId)))
- //修改唯一编码
- {
- go models.ModifyEnglishReportCode(newReportId, reportCode)
- }
- return
- }
- // IEnglishEmailSend 英文研报-邮件推送接口
- type IEnglishEmailSend interface {
- NewClient() (err error)
- SendEmail(item *EnglishReportSendEmailRequest) (ok bool, result string, err error)
- BatchSendEmail(list []*EnglishReportSendEmailRequest) (results []*EnglishReportSendEmailResult, err error)
- }
- // EnglishReportSendEmailRequest 英文研报-推送邮件请求体
- type EnglishReportSendEmailRequest struct {
- ReportId int `description:"英文报告ID"`
- EmailId int `description:"邮箱ID"`
- Email string `description:"邮箱地址"`
- Subject string `description:"邮件主题"`
- FromAlias string `description:"发信人昵称"`
- ReportTitle string `description:"报告标题"`
- ReportAbstract string `description:"报告摘要"`
- ReportContent string `description:"报告内容"`
- ReportShareLink string `description:"报告分享链接"`
- ReportTime string `description:"报告时间"`
- HtmlBody string `description:"模板内容主体"`
- }
- // EnglishReportSendEmailResult 英文研报-推送邮件响应体
- type EnglishReportSendEmailResult struct {
- ReportId int `description:"英文报告ID"`
- EmailId int `description:"邮箱ID"`
- Email string `description:"邮箱地址"`
- Ok bool `description:"是否推送成功"`
- SendData string `description:"请求数据-JSON"`
- ResultData string `description:"推送结果-JSON"`
- Source int `description:"服务来源:1-阿里云;2-腾讯云"`
- }
- // BatchSendAliEnglishReportEmail 批量推送英文研报邮件
- func BatchSendAliEnglishReportEmail(list []*EnglishReportSendEmailRequest) (err error) {
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("阿里云群发英文研报邮件失败, Err: "+err.Error(), 3)
- }
- }()
- if len(list) == 0 {
- return
- }
- requestMap := make(map[int]*EnglishReportSendEmailRequest, 0)
- for i := range list {
- requestMap[list[i].EmailId] = list[i]
- }
- // 请求阿里云接口批量推送
- aliEmail := new(AliyunEmail)
- resultList, e := aliEmail.BatchSendEmail(list)
- if e != nil {
- err = e
- return
- }
- // 返回的结果更新日志
- resendList := make([]*EnglishReportSendEmailRequest, 0)
- failLogIds := make([]int, 0)
- updateCols := []string{"SendData", "Result", "SendStatus", "ErrMsg"}
- for i := range resultList {
- var cond string
- var pars []interface{}
- cond = ` AND is_deleted = 0 AND report_id = ? AND email_id = ? AND source = ? AND send_status = ?`
- pars = append(pars, resultList[i].ReportId, resultList[i].EmailId, models.EnglishReportEmailLogSourceAli, models.EnglishReportEmailLogStatusIng)
- l, e := models.GetEnglishReportEmailLog(cond, pars)
- if e != nil {
- continue
- }
- l.SendData = resultList[i].SendData
- l.Result = resultList[i].ResultData
- if resultList[i].Ok {
- l.SendStatus = models.EnglishReportEmailLogStatusSuccess
- } else {
- l.SendStatus = models.EnglishReportEmailLogStatusFail
- failLogIds = append(failLogIds, l.Id)
- if requestMap[resultList[i].EmailId] != nil {
- resendList = append(resendList, requestMap[resultList[i].EmailId])
- }
- // 取出错误信息
- r := new(AliyunEmailResult)
- if e = json.Unmarshal([]byte(resultList[i].ResultData), &r); e != nil {
- continue
- }
- rd := new(AliyunEmailResultData)
- res := strings.Replace(r.Data, `\`, ``, -1)
- if e = json.Unmarshal([]byte(res), &rd); e != nil {
- continue
- }
- l.ErrMsg = rd.Message
- }
- if e = l.Update(updateCols); e != nil {
- continue
- }
- }
- // 推送失败的重新腾讯云, 若腾讯云也失败将不再自动重推, 用户手动去重推
- if len(resendList) > 0 && len(failLogIds) > 0 {
- _ = ResendTencentEnglishReportEmail(resendList, failLogIds)
- }
- return
- }
- // ResendTencentEnglishReportEmail 腾讯云邮件重新推送
- func ResendTencentEnglishReportEmail(resendList []*EnglishReportSendEmailRequest, failLogIds []int) (err error) {
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("腾讯云重发英文研报邮件失败, Err: "+err.Error(), 3)
- }
- }()
- if len(resendList) == 0 || len(failLogIds) == 0 {
- return
- }
- // 标记原有日志为已删除
- if len(failLogIds) > 0 {
- if e := models.DeleteEnglishReportEmailLogByIds(failLogIds); e != nil {
- err = errors.New("删除原邮件日志失败, Err: " + e.Error())
- return
- }
- }
- // 写入新的日志
- nowTime := time.Now().Local()
- logData := make([]*models.EnglishReportEmailLog, 0)
- for i := range resendList {
- sendByte, e := json.Marshal(resendList[i])
- if e != nil {
- err = errors.New("sendByte json.Marshal Err, Err: " + e.Error())
- return
- }
- logData = append(logData, &models.EnglishReportEmailLog{
- ReportId: resendList[i].ReportId,
- EmailId: resendList[i].EmailId,
- Email: resendList[i].Email,
- SendData: string(sendByte),
- Source: models.EnglishReportEmailLogSourceTencent,
- SendStatus: models.EnglishReportEmailLogStatusIng,
- CreateTime: nowTime,
- })
- }
- emailLog := new(models.EnglishReportEmailLog)
- if e := emailLog.InsertMulti(logData); e != nil {
- err = errors.New("批量写入群发邮件日志失败, Err: " + e.Error())
- return
- }
- // 请求腾讯云
- tecentEmail := new(TencentEmail)
- resultList, e := tecentEmail.BatchSendEmail(resendList)
- if e != nil {
- err = e
- return
- }
- updateCols := []string{"SendData", "Result", "SendStatus", "ErrMsg"}
- for i := range resultList {
- var cond string
- var pars []interface{}
- cond = ` AND is_deleted = 0 AND report_id = ? AND email_id = ? AND source = ? AND send_status = ?`
- pars = append(pars, resultList[i].ReportId, resultList[i].EmailId, models.EnglishReportEmailLogSourceTencent, models.EnglishReportEmailLogStatusIng)
- l, e := models.GetEnglishReportEmailLog(cond, pars)
- if e != nil {
- continue
- }
- l.SendData = resultList[i].SendData
- l.Result = resultList[i].ResultData
- if resultList[i].Ok {
- l.SendStatus = models.EnglishReportEmailLogStatusSuccess
- } else {
- l.SendStatus = models.EnglishReportEmailLogStatusFail
- r := new(TencentEmailResult)
- if e = json.Unmarshal([]byte(resultList[i].ResultData), &r); e != nil {
- continue
- }
- l.ErrMsg = r.Message
- }
- if e = l.Update(updateCols); e != nil {
- continue
- }
- }
- return
- }
- // UpdateEnglishReportEs 更新英文报告/章节Es
- func UpdateEnglishReportEs(reportId int, publishState int) (err error) {
- if reportId <= 0 {
- return
- }
- reportInfo, err := models.GetEnglishReportById(reportId)
- if err != nil {
- return
- }
- // 新增报告ES
- esReport := &models.ElasticEnglishReportDetail{
- Id: strconv.Itoa(reportInfo.Id),
- ReportId: reportInfo.Id,
- Title: reportInfo.Title,
- Abstract: reportInfo.Abstract,
- BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)),
- PublishTime: reportInfo.PublishTime,
- CreateTime: reportInfo.CreateTime,
- ReportCode: reportInfo.ReportCode,
- PublishState: publishState,
- Author: reportInfo.Author,
- Frequency: reportInfo.Frequency,
- ClassifyIdFirst: reportInfo.ClassifyIdFirst,
- ClassifyNameFirst: reportInfo.ClassifyNameFirst,
- ClassifyIdSecond: reportInfo.ClassifyIdSecond,
- ClassifyNameSecond: reportInfo.ClassifyNameSecond,
- StageStr: strconv.Itoa(reportInfo.Stage),
- Overview: utils.TrimHtml(html.UnescapeString(reportInfo.Overview)),
- ContentSub: utils.TrimHtml(html.UnescapeString(reportInfo.ContentSub)),
- }
- docId := fmt.Sprintf("%d", reportInfo.Id)
- if err = EsAddOrEditEnglishReport(utils.EsEnglishReportIndexName, docId, esReport); err != nil {
- return
- }
- return
- }
- // UpdateEnglishVideoEs 更新英文线上路演Es
- func UpdateEnglishVideoEs(videoId int, publishState int) (err error) {
- if videoId <= 0 {
- return
- }
- videoInfo, err := models.GetEnglishVideoById(videoId)
- if err != nil {
- return
- }
- // 新增报告ES
- esReport := &models.ElasticEnglishReportDetail{
- Id: "v" + strconv.Itoa(videoInfo.Id),
- VideoId: videoInfo.Id,
- Title: videoInfo.Title,
- Abstract: videoInfo.Abstract,
- BodyContent: "",
- PublishTime: videoInfo.PublishTime,
- CreateTime: videoInfo.CreateTime,
- ReportCode: videoInfo.VideoCode,
- PublishState: publishState,
- Author: videoInfo.Author,
- Frequency: "",
- ClassifyIdFirst: videoInfo.ClassifyIdFirst,
- ClassifyNameFirst: videoInfo.ClassifyNameFirst,
- ClassifyIdSecond: videoInfo.ClassifyIdSecond,
- ClassifyNameSecond: videoInfo.ClassifyNameSecond,
- StageStr: "",
- Overview: utils.TrimHtml(html.UnescapeString(videoInfo.Overview)),
- ContentSub: "",
- }
- docId := fmt.Sprintf("v%d", videoInfo.Id)
- if err = EsAddOrEditEnglishReport(utils.EsEnglishReportIndexName, docId, esReport); err != nil {
- return
- }
- return
- }
- func UpdateEnglishReportClassifyId(oldItem, newItem, newParent *models.EnglishClassify, classifyId int) (err error) {
- //一级分类改为二级分类
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("英文报告分类改名-同步更新报告表字段及权限表关键词失败3, Err:"+err.Error(), 3)
- }
- }()
- //如果二级分类变更为一级分类
- //如果二级分类更换了父级分类,则更新报告中的父级分类ID
- //如果一级分类更换了名称,则更新所有报告中的一级分类名称
- //如果二级分类更换了名称,则更新所有报告中的二级分类名称
- if oldItem.ParentId != newItem.ParentId {
- parentClassifyName := ""
- parentId := 0
- //二级分类改为一级分类, 或者二级分类的一级分类有变更
- if newItem.ParentId > 0 {
- parentClassifyName = newParent.ClassifyName
- parentId = newParent.Id
- }
- // 更新报告表分类字段
- var condition string
- var pars []interface{}
- condition += ` AND classify_id_second = ? `
- pars = append(pars, classifyId)
- list, e := models.GetEnglishReportByCondition(condition, pars)
- if e != nil {
- err = e
- return
- }
- if len(list) > 0 {
- //二级分类改为一级分类
- if oldItem.ParentId > 0 && newItem.ParentId == 0 {
- //查询该二级分类下是否存在关联报告,如果存在则不允许变更
- err = fmt.Errorf("该分类有关联的报告,不允许变更为一级分类")
- return
- }
- }
- var idSlice []string
- for _, report := range list {
- idSlice = append(idSlice, strconv.Itoa(report.Id))
- }
- ids := strings.Join(idSlice, ",")
- if ids != "" {
- if err = models.UpdateEnglishReportByClassifyId(parentClassifyName, newItem.ClassifyName, parentId, oldItem.Id, ids); err != nil {
- return
- }
- }
- } else if oldItem.ClassifyName != newItem.ClassifyName {
- if oldItem.ParentId > 0 {
- //只对二级分类名称作了修改
- // 更新报告表分类字段
- if err = models.UpdateEnglishReportSecondClassifyNameByClassifyId(classifyId, newItem.ClassifyName); err != nil {
- return
- }
- } else if oldItem.ParentId == 0 {
- //只对一级分类名称作了修改
- // 更新报告表分类字段
- if err = models.UpdateEnglishReportFirstClassifyNameByClassifyId(classifyId, newItem.ClassifyName); err != nil {
- return
- }
- }
- }
- return
- }
- func UpdateEnglishVideoClassifyId(oldItem, newItem, newParent *models.EnglishClassify, classifyId int) (err error) {
- //一级分类改为二级分类
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("英文报告分类改名-同步更新报告表字段及权限表关键词失败3, Err:"+err.Error(), 3)
- }
- }()
- //如果二级分类变更为一级分类
- //如果二级分类更换了父级分类,则更新报告中的父级分类ID
- //如果一级分类更换了名称,则更新所有报告中的一级分类名称
- //如果二级分类更换了名称,则更新所有报告中的二级分类名称
- if oldItem.ParentId != newItem.ParentId {
- parentClassifyName := ""
- parentId := 0
- //二级分类改为一级分类, 或者二级分类的一级分类有变更
- if newItem.ParentId > 0 {
- parentClassifyName = newParent.ClassifyName
- parentId = newParent.Id
- }
- // 更新报告表分类字段
- var condition string
- var pars []interface{}
- condition += ` AND classify_id_second = ? `
- pars = append(pars, classifyId)
- list, e := models.GetEnglishVideoByCondition(condition, pars)
- if e != nil {
- err = e
- return
- }
- if len(list) > 0 {
- //二级分类改为一级分类
- if oldItem.ParentId > 0 && newItem.ParentId == 0 {
- //查询该二级分类下是否存在关联报告,如果存在则不允许变更
- err = fmt.Errorf("该分类有关联的视频,不允许变更为一级分类")
- return
- }
- }
- var idSlice []string
- for _, report := range list {
- idSlice = append(idSlice, strconv.Itoa(report.Id))
- }
- ids := strings.Join(idSlice, ",")
- if ids != "" {
- if err = models.UpdateEnglishVideoByClassifyId(parentClassifyName, newItem.ClassifyName, parentId, oldItem.Id, ids); err != nil {
- return
- }
- }
- } else if oldItem.ClassifyName != newItem.ClassifyName {
- if oldItem.ParentId > 0 {
- //只对二级分类名称作了修改
- // 更新报告表分类字段
- if err = models.UpdateEnglishVideoSecondClassifyNameByClassifyId(classifyId, newItem.ClassifyName); err != nil {
- return
- }
- } else if oldItem.ParentId == 0 {
- //只对一级分类名称作了修改
- // 更新报告表分类字段
- if err = models.UpdateEnglishVideoFirstClassifyNameByClassifyId(classifyId, newItem.ClassifyName); err != nil {
- return
- }
- }
- }
- return
- }
- // UpdateAllPublishedEnglishReportToEs 更新所有已发布的报告ES
- func UpdateAllPublishedEnglishReportToEs() (err error) {
- // 获取所有已发布的报告
- var condition string
- var pars []interface{}
- condition = ` AND state = 2 `
- reportList, err := models.GetEnglishReportByCondition(condition, pars)
- count := 0
- failCount := 0
- for i := 0; i < len(reportList); i++ {
- if err = UpdateEnglishReportEs(reportList[i].Id, 2); err != nil {
- fmt.Printf("更新失败, report_id: %d, Err: %s\n", reportList[i].Id, err.Error())
- failCount += 1
- } else {
- count += 1
- }
- }
- fmt.Printf("报告总数:%d, 更新成功数: %d, 更新失败数: %d", len(reportList), count, failCount)
- return
- }
- // UpdateEnReportEditMark 更新英文研报当前更新状态
- // status 枚举值 1:编辑中,0:完成编辑, 2:只做查询
- func UpdateEnReportEditMark(reportId, nowUserId, status int, nowUserName string) (ret models.MarkReportResp, err error) {
- //更新标记key
- key := fmt.Sprint(`crm:enReport:edit:`, reportId)
- opUserId, e := utils.Rc.RedisInt(key)
- var opUser models.MarkReportItem
- if e != nil {
- opUserInfoStr, tErr := utils.Rc.RedisString(key)
- if tErr == nil {
- tErr = json.Unmarshal([]byte(opUserInfoStr), &opUser)
- if tErr == nil {
- opUserId = opUser.AdminId
- }
- }
- }
- if opUserId > 0 && opUserId != nowUserId {
- editor := opUser.Editor
- if editor == "" {
- //查询账号的用户姓名
- otherInfo, e := system.GetSysAdminById(opUserId)
- if e != nil {
- err = fmt.Errorf("查询其他编辑者信息失败")
- return
- }
- editor = otherInfo.RealName
- }
- ret.Status = 1
- ret.Msg = fmt.Sprintf("当前%s正在编辑报告", editor)
- ret.Editor = editor
- return
- }
- if status == 1 {
- nowUser := &models.MarkReportItem{AdminId: nowUserId, Editor: nowUserName}
- bt, e := json.Marshal(nowUser)
- if e != nil {
- err = fmt.Errorf("格式化编辑者信息失败")
- return
- }
- if opUserId > 0 {
- utils.Rc.Do("SETEX", key, int64(180), string(bt)) //3分钟缓存
- } else {
- utils.Rc.SetNX(key, string(bt), time.Second*60*3) //3分钟缓存
- }
- } else if status == 0 {
- //清除编辑缓存
- _ = utils.Rc.Delete(key)
- }
- return
- }
- // 取消英文策略报告一键同步
- func EnglishPolicyReportSyncCancel(reportInfo *models.EnglishReportDetail) (err error) {
- var policyReport *models.EnglishPolicyReportDetail
- if reportInfo.FromReportId > 0 {
- //查询报告是否存在
- policyReport, err = models.GetEnglishPolicyReportById(reportInfo.FromReportId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = fmt.Errorf("报告不存在!")
- return
- }
- err = fmt.Errorf("获取报告信息失败,Err:%v", err)
- return
- }
- //判断报告是否已同步
- if policyReport.State == 1 {
- err = fmt.Errorf("报告已取消同步,无需重复取消")
- return
- }
- if err = models.SyncCancelEnglishPolicyReport(policyReport.Id); err != nil {
- err = fmt.Errorf("报告已取消同步失败 Err %v", err)
- return
- }
- }
- return
- }
|