english_report.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models"
  6. "eta/eta_mobile/models/system"
  7. "eta/eta_mobile/services/alarm_msg"
  8. "eta/eta_mobile/utils"
  9. "fmt"
  10. "html"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // CreateNewEnglishReport 生成英文研报
  16. func CreateNewEnglishReport(req models.AddEnglishReportReq, adminInfo *system.Admin) (newReportId int64, reportCode string, err error) {
  17. var contentSub string
  18. if req.Content != "" {
  19. content, e := FilterReportContentBr(req.Content)
  20. if e != nil {
  21. err = errors.New("内容去除前后空格失败, Err: " + e.Error())
  22. return
  23. }
  24. req.Content = content
  25. contentSub, e = GetReportContentSub(req.Content)
  26. if e != nil {
  27. go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+e.Error(), 3)
  28. err = errors.New("获取报告内容前几段失败, Err: " + e.Error())
  29. return
  30. }
  31. }
  32. maxStage, e := models.GetEnglishReportStage(req.ClassifyIdFirst, req.ClassifyIdSecond)
  33. if e != nil {
  34. err = errors.New("期数获取失败, Err: " + e.Error())
  35. return
  36. }
  37. item := new(models.EnglishReport)
  38. item.AddType = req.AddType
  39. item.ClassifyIdFirst = req.ClassifyIdFirst
  40. item.ClassifyNameFirst = req.ClassifyNameFirst
  41. item.ClassifyIdSecond = req.ClassifyIdSecond
  42. item.ClassifyNameSecond = req.ClassifyNameSecond
  43. item.Title = req.Title
  44. item.Abstract = req.Abstract
  45. item.Author = req.Author
  46. item.Frequency = req.Frequency
  47. item.State = req.State
  48. item.Content = html.EscapeString(req.Content)
  49. item.Stage = maxStage + 1
  50. item.ContentSub = html.EscapeString(contentSub)
  51. item.CreateTime = req.CreateTime
  52. item.ModifyTime = time.Now()
  53. item.AdminId = adminInfo.AdminId
  54. item.AdminRealName = adminInfo.RealName
  55. newReportId, e = models.AddEnglishReport(item)
  56. if e != nil {
  57. err = errors.New("新增报告失败, Err: " + e.Error())
  58. return
  59. }
  60. reportCode = utils.MD5(strconv.Itoa(int(newReportId)))
  61. //修改唯一编码
  62. {
  63. go models.ModifyEnglishReportCode(newReportId, reportCode)
  64. }
  65. return
  66. }
  67. // IEnglishEmailSend 英文研报-邮件推送接口
  68. type IEnglishEmailSend interface {
  69. NewClient() (err error)
  70. SendEmail(item *EnglishReportSendEmailRequest) (ok bool, result string, err error)
  71. BatchSendEmail(list []*EnglishReportSendEmailRequest) (results []*EnglishReportSendEmailResult, err error)
  72. }
  73. // EnglishReportSendEmailRequest 英文研报-推送邮件请求体
  74. type EnglishReportSendEmailRequest struct {
  75. ReportId int `description:"英文报告ID"`
  76. EmailId int `description:"邮箱ID"`
  77. Email string `description:"邮箱地址"`
  78. Subject string `description:"邮件主题"`
  79. FromAlias string `description:"发信人昵称"`
  80. ReportTitle string `description:"报告标题"`
  81. ReportAbstract string `description:"报告摘要"`
  82. ReportContent string `description:"报告内容"`
  83. ReportShareLink string `description:"报告分享链接"`
  84. ReportTime string `description:"报告时间"`
  85. HtmlBody string `description:"模板内容主体"`
  86. }
  87. // EnglishReportSendEmailResult 英文研报-推送邮件响应体
  88. type EnglishReportSendEmailResult struct {
  89. ReportId int `description:"英文报告ID"`
  90. EmailId int `description:"邮箱ID"`
  91. Email string `description:"邮箱地址"`
  92. Ok bool `description:"是否推送成功"`
  93. SendData string `description:"请求数据-JSON"`
  94. ResultData string `description:"推送结果-JSON"`
  95. Source int `description:"服务来源:1-阿里云;2-腾讯云"`
  96. }
  97. // BatchSendAliEnglishReportEmail 批量推送英文研报邮件
  98. func BatchSendAliEnglishReportEmail(list []*EnglishReportSendEmailRequest) (err error) {
  99. defer func() {
  100. if err != nil {
  101. go alarm_msg.SendAlarmMsg("阿里云群发英文研报邮件失败, Err: "+err.Error(), 3)
  102. }
  103. }()
  104. if len(list) == 0 {
  105. return
  106. }
  107. requestMap := make(map[int]*EnglishReportSendEmailRequest, 0)
  108. for i := range list {
  109. requestMap[list[i].EmailId] = list[i]
  110. }
  111. // 请求阿里云接口批量推送
  112. aliEmail := new(AliyunEmail)
  113. resultList, e := aliEmail.BatchSendEmail(list)
  114. if e != nil {
  115. err = e
  116. return
  117. }
  118. // 返回的结果更新日志
  119. resendList := make([]*EnglishReportSendEmailRequest, 0)
  120. failLogIds := make([]int, 0)
  121. updateCols := []string{"SendData", "Result", "SendStatus", "ErrMsg"}
  122. for i := range resultList {
  123. var cond string
  124. var pars []interface{}
  125. cond = ` AND is_deleted = 0 AND report_id = ? AND email_id = ? AND source = ? AND send_status = ?`
  126. pars = append(pars, resultList[i].ReportId, resultList[i].EmailId, models.EnglishReportEmailLogSourceAli, models.EnglishReportEmailLogStatusIng)
  127. l, e := models.GetEnglishReportEmailLog(cond, pars)
  128. if e != nil {
  129. continue
  130. }
  131. l.SendData = resultList[i].SendData
  132. l.Result = resultList[i].ResultData
  133. if resultList[i].Ok {
  134. l.SendStatus = models.EnglishReportEmailLogStatusSuccess
  135. } else {
  136. l.SendStatus = models.EnglishReportEmailLogStatusFail
  137. failLogIds = append(failLogIds, l.Id)
  138. if requestMap[resultList[i].EmailId] != nil {
  139. resendList = append(resendList, requestMap[resultList[i].EmailId])
  140. }
  141. // 取出错误信息
  142. r := new(AliyunEmailResult)
  143. if e = json.Unmarshal([]byte(resultList[i].ResultData), &r); e != nil {
  144. continue
  145. }
  146. rd := new(AliyunEmailResultData)
  147. res := strings.Replace(r.Data, `\`, ``, -1)
  148. if e = json.Unmarshal([]byte(res), &rd); e != nil {
  149. continue
  150. }
  151. l.ErrMsg = rd.Message
  152. }
  153. if e = l.Update(updateCols); e != nil {
  154. continue
  155. }
  156. }
  157. // 推送失败的重新腾讯云, 若腾讯云也失败将不再自动重推, 用户手动去重推
  158. if len(resendList) > 0 && len(failLogIds) > 0 {
  159. _ = ResendTencentEnglishReportEmail(resendList, failLogIds)
  160. }
  161. return
  162. }
  163. // ResendTencentEnglishReportEmail 腾讯云邮件重新推送
  164. func ResendTencentEnglishReportEmail(resendList []*EnglishReportSendEmailRequest, failLogIds []int) (err error) {
  165. defer func() {
  166. if err != nil {
  167. go alarm_msg.SendAlarmMsg("腾讯云重发英文研报邮件失败, Err: "+err.Error(), 3)
  168. }
  169. }()
  170. if len(resendList) == 0 || len(failLogIds) == 0 {
  171. return
  172. }
  173. // 标记原有日志为已删除
  174. if len(failLogIds) > 0 {
  175. if e := models.DeleteEnglishReportEmailLogByIds(failLogIds); e != nil {
  176. err = errors.New("删除原邮件日志失败, Err: " + e.Error())
  177. return
  178. }
  179. }
  180. // 写入新的日志
  181. nowTime := time.Now().Local()
  182. logData := make([]*models.EnglishReportEmailLog, 0)
  183. for i := range resendList {
  184. sendByte, e := json.Marshal(resendList[i])
  185. if e != nil {
  186. err = errors.New("sendByte json.Marshal Err, Err: " + e.Error())
  187. return
  188. }
  189. logData = append(logData, &models.EnglishReportEmailLog{
  190. ReportId: resendList[i].ReportId,
  191. EmailId: resendList[i].EmailId,
  192. Email: resendList[i].Email,
  193. SendData: string(sendByte),
  194. Source: models.EnglishReportEmailLogSourceTencent,
  195. SendStatus: models.EnglishReportEmailLogStatusIng,
  196. CreateTime: nowTime,
  197. })
  198. }
  199. emailLog := new(models.EnglishReportEmailLog)
  200. if e := emailLog.InsertMulti(logData); e != nil {
  201. err = errors.New("批量写入群发邮件日志失败, Err: " + e.Error())
  202. return
  203. }
  204. // 请求腾讯云
  205. tecentEmail := new(TencentEmail)
  206. resultList, e := tecentEmail.BatchSendEmail(resendList)
  207. if e != nil {
  208. err = e
  209. return
  210. }
  211. updateCols := []string{"SendData", "Result", "SendStatus", "ErrMsg"}
  212. for i := range resultList {
  213. var cond string
  214. var pars []interface{}
  215. cond = ` AND is_deleted = 0 AND report_id = ? AND email_id = ? AND source = ? AND send_status = ?`
  216. pars = append(pars, resultList[i].ReportId, resultList[i].EmailId, models.EnglishReportEmailLogSourceTencent, models.EnglishReportEmailLogStatusIng)
  217. l, e := models.GetEnglishReportEmailLog(cond, pars)
  218. if e != nil {
  219. continue
  220. }
  221. l.SendData = resultList[i].SendData
  222. l.Result = resultList[i].ResultData
  223. if resultList[i].Ok {
  224. l.SendStatus = models.EnglishReportEmailLogStatusSuccess
  225. } else {
  226. l.SendStatus = models.EnglishReportEmailLogStatusFail
  227. r := new(TencentEmailResult)
  228. if e = json.Unmarshal([]byte(resultList[i].ResultData), &r); e != nil {
  229. continue
  230. }
  231. l.ErrMsg = r.Message
  232. }
  233. if e = l.Update(updateCols); e != nil {
  234. continue
  235. }
  236. }
  237. return
  238. }
  239. // UpdateEnglishReportEs 更新英文报告/章节Es
  240. func UpdateEnglishReportEs(reportId int, publishState int) (err error) {
  241. if reportId <= 0 {
  242. return
  243. }
  244. reportInfo, err := models.GetEnglishReportById(reportId)
  245. if err != nil {
  246. return
  247. }
  248. // 新增报告ES
  249. esReport := &models.ElasticEnglishReportDetail{
  250. Id: strconv.Itoa(reportInfo.Id),
  251. ReportId: reportInfo.Id,
  252. Title: reportInfo.Title,
  253. Abstract: reportInfo.Abstract,
  254. BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)),
  255. PublishTime: reportInfo.PublishTime,
  256. CreateTime: reportInfo.CreateTime,
  257. ReportCode: reportInfo.ReportCode,
  258. PublishState: publishState,
  259. Author: reportInfo.Author,
  260. Frequency: reportInfo.Frequency,
  261. ClassifyIdFirst: reportInfo.ClassifyIdFirst,
  262. ClassifyNameFirst: reportInfo.ClassifyNameFirst,
  263. ClassifyIdSecond: reportInfo.ClassifyIdSecond,
  264. ClassifyNameSecond: reportInfo.ClassifyNameSecond,
  265. StageStr: strconv.Itoa(reportInfo.Stage),
  266. Overview: utils.TrimHtml(html.UnescapeString(reportInfo.Overview)),
  267. ContentSub: utils.TrimHtml(html.UnescapeString(reportInfo.ContentSub)),
  268. }
  269. docId := fmt.Sprintf("%d", reportInfo.Id)
  270. if err = EsAddOrEditEnglishReport(utils.EsEnglishReportIndexName, docId, esReport); err != nil {
  271. return
  272. }
  273. return
  274. }
  275. // UpdateEnglishVideoEs 更新英文线上路演Es
  276. func UpdateEnglishVideoEs(videoId int, publishState int) (err error) {
  277. if videoId <= 0 {
  278. return
  279. }
  280. videoInfo, err := models.GetEnglishVideoById(videoId)
  281. if err != nil {
  282. return
  283. }
  284. // 新增报告ES
  285. esReport := &models.ElasticEnglishReportDetail{
  286. Id: "v" + strconv.Itoa(videoInfo.Id),
  287. VideoId: videoInfo.Id,
  288. Title: videoInfo.Title,
  289. Abstract: videoInfo.Abstract,
  290. BodyContent: "",
  291. PublishTime: videoInfo.PublishTime,
  292. CreateTime: videoInfo.CreateTime,
  293. ReportCode: videoInfo.VideoCode,
  294. PublishState: publishState,
  295. Author: videoInfo.Author,
  296. Frequency: "",
  297. ClassifyIdFirst: videoInfo.ClassifyIdFirst,
  298. ClassifyNameFirst: videoInfo.ClassifyNameFirst,
  299. ClassifyIdSecond: videoInfo.ClassifyIdSecond,
  300. ClassifyNameSecond: videoInfo.ClassifyNameSecond,
  301. StageStr: "",
  302. Overview: utils.TrimHtml(html.UnescapeString(videoInfo.Overview)),
  303. ContentSub: "",
  304. }
  305. docId := fmt.Sprintf("v%d", videoInfo.Id)
  306. if err = EsAddOrEditEnglishReport(utils.EsEnglishReportIndexName, docId, esReport); err != nil {
  307. return
  308. }
  309. return
  310. }
  311. func UpdateEnglishReportClassifyId(oldItem, newItem, newParent *models.EnglishClassify, classifyId int) (err error) {
  312. //一级分类改为二级分类
  313. defer func() {
  314. if err != nil {
  315. go alarm_msg.SendAlarmMsg("英文报告分类改名-同步更新报告表字段及权限表关键词失败3, Err:"+err.Error(), 3)
  316. }
  317. }()
  318. //如果二级分类变更为一级分类
  319. //如果二级分类更换了父级分类,则更新报告中的父级分类ID
  320. //如果一级分类更换了名称,则更新所有报告中的一级分类名称
  321. //如果二级分类更换了名称,则更新所有报告中的二级分类名称
  322. if oldItem.ParentId != newItem.ParentId {
  323. parentClassifyName := ""
  324. parentId := 0
  325. //二级分类改为一级分类, 或者二级分类的一级分类有变更
  326. if newItem.ParentId > 0 {
  327. parentClassifyName = newParent.ClassifyName
  328. parentId = newParent.Id
  329. }
  330. // 更新报告表分类字段
  331. var condition string
  332. var pars []interface{}
  333. condition += ` AND classify_id_second = ? `
  334. pars = append(pars, classifyId)
  335. list, e := models.GetEnglishReportByCondition(condition, pars)
  336. if e != nil {
  337. err = e
  338. return
  339. }
  340. if len(list) > 0 {
  341. //二级分类改为一级分类
  342. if oldItem.ParentId > 0 && newItem.ParentId == 0 {
  343. //查询该二级分类下是否存在关联报告,如果存在则不允许变更
  344. err = fmt.Errorf("该分类有关联的报告,不允许变更为一级分类")
  345. return
  346. }
  347. }
  348. var idSlice []string
  349. for _, report := range list {
  350. idSlice = append(idSlice, strconv.Itoa(report.Id))
  351. }
  352. ids := strings.Join(idSlice, ",")
  353. if ids != "" {
  354. if err = models.UpdateEnglishReportByClassifyId(parentClassifyName, newItem.ClassifyName, parentId, oldItem.Id, ids); err != nil {
  355. return
  356. }
  357. }
  358. } else if oldItem.ClassifyName != newItem.ClassifyName {
  359. if oldItem.ParentId > 0 {
  360. //只对二级分类名称作了修改
  361. // 更新报告表分类字段
  362. if err = models.UpdateEnglishReportSecondClassifyNameByClassifyId(classifyId, newItem.ClassifyName); err != nil {
  363. return
  364. }
  365. } else if oldItem.ParentId == 0 {
  366. //只对一级分类名称作了修改
  367. // 更新报告表分类字段
  368. if err = models.UpdateEnglishReportFirstClassifyNameByClassifyId(classifyId, newItem.ClassifyName); err != nil {
  369. return
  370. }
  371. }
  372. }
  373. return
  374. }
  375. func UpdateEnglishVideoClassifyId(oldItem, newItem, newParent *models.EnglishClassify, classifyId int) (err error) {
  376. //一级分类改为二级分类
  377. defer func() {
  378. if err != nil {
  379. go alarm_msg.SendAlarmMsg("英文报告分类改名-同步更新报告表字段及权限表关键词失败3, Err:"+err.Error(), 3)
  380. }
  381. }()
  382. //如果二级分类变更为一级分类
  383. //如果二级分类更换了父级分类,则更新报告中的父级分类ID
  384. //如果一级分类更换了名称,则更新所有报告中的一级分类名称
  385. //如果二级分类更换了名称,则更新所有报告中的二级分类名称
  386. if oldItem.ParentId != newItem.ParentId {
  387. parentClassifyName := ""
  388. parentId := 0
  389. //二级分类改为一级分类, 或者二级分类的一级分类有变更
  390. if newItem.ParentId > 0 {
  391. parentClassifyName = newParent.ClassifyName
  392. parentId = newParent.Id
  393. }
  394. // 更新报告表分类字段
  395. var condition string
  396. var pars []interface{}
  397. condition += ` AND classify_id_second = ? `
  398. pars = append(pars, classifyId)
  399. list, e := models.GetEnglishVideoByCondition(condition, pars)
  400. if e != nil {
  401. err = e
  402. return
  403. }
  404. if len(list) > 0 {
  405. //二级分类改为一级分类
  406. if oldItem.ParentId > 0 && newItem.ParentId == 0 {
  407. //查询该二级分类下是否存在关联报告,如果存在则不允许变更
  408. err = fmt.Errorf("该分类有关联的视频,不允许变更为一级分类")
  409. return
  410. }
  411. }
  412. var idSlice []string
  413. for _, report := range list {
  414. idSlice = append(idSlice, strconv.Itoa(report.Id))
  415. }
  416. ids := strings.Join(idSlice, ",")
  417. if ids != "" {
  418. if err = models.UpdateEnglishVideoByClassifyId(parentClassifyName, newItem.ClassifyName, parentId, oldItem.Id, ids); err != nil {
  419. return
  420. }
  421. }
  422. } else if oldItem.ClassifyName != newItem.ClassifyName {
  423. if oldItem.ParentId > 0 {
  424. //只对二级分类名称作了修改
  425. // 更新报告表分类字段
  426. if err = models.UpdateEnglishVideoSecondClassifyNameByClassifyId(classifyId, newItem.ClassifyName); err != nil {
  427. return
  428. }
  429. } else if oldItem.ParentId == 0 {
  430. //只对一级分类名称作了修改
  431. // 更新报告表分类字段
  432. if err = models.UpdateEnglishVideoFirstClassifyNameByClassifyId(classifyId, newItem.ClassifyName); err != nil {
  433. return
  434. }
  435. }
  436. }
  437. return
  438. }
  439. // UpdateAllPublishedEnglishReportToEs 更新所有已发布的报告ES
  440. func UpdateAllPublishedEnglishReportToEs() (err error) {
  441. // 获取所有已发布的报告
  442. var condition string
  443. var pars []interface{}
  444. condition = ` AND state = 2 `
  445. reportList, err := models.GetEnglishReportByCondition(condition, pars)
  446. count := 0
  447. failCount := 0
  448. for i := 0; i < len(reportList); i++ {
  449. if err = UpdateEnglishReportEs(reportList[i].Id, 2); err != nil {
  450. fmt.Printf("更新失败, report_id: %d, Err: %s\n", reportList[i].Id, err.Error())
  451. failCount += 1
  452. } else {
  453. count += 1
  454. }
  455. }
  456. fmt.Printf("报告总数:%d, 更新成功数: %d, 更新失败数: %d", len(reportList), count, failCount)
  457. return
  458. }
  459. // UpdateEnReportEditMark 更新英文研报当前更新状态
  460. // status 枚举值 1:编辑中,0:完成编辑, 2:只做查询
  461. func UpdateEnReportEditMark(reportId, nowUserId, status int, nowUserName string) (ret models.MarkReportResp, err error) {
  462. //更新标记key
  463. key := fmt.Sprint(`crm:enReport:edit:`, reportId)
  464. opUserId, e := utils.Rc.RedisInt(key)
  465. var opUser models.MarkReportItem
  466. if e != nil {
  467. opUserInfoStr, tErr := utils.Rc.RedisString(key)
  468. if tErr == nil {
  469. tErr = json.Unmarshal([]byte(opUserInfoStr), &opUser)
  470. if tErr == nil {
  471. opUserId = opUser.AdminId
  472. }
  473. }
  474. }
  475. if opUserId > 0 && opUserId != nowUserId {
  476. editor := opUser.Editor
  477. if editor == "" {
  478. //查询账号的用户姓名
  479. otherInfo, e := system.GetSysAdminById(opUserId)
  480. if e != nil {
  481. err = fmt.Errorf("查询其他编辑者信息失败")
  482. return
  483. }
  484. editor = otherInfo.RealName
  485. }
  486. ret.Status = 1
  487. ret.Msg = fmt.Sprintf("当前%s正在编辑报告", editor)
  488. ret.Editor = editor
  489. return
  490. }
  491. if status == 1 {
  492. nowUser := &models.MarkReportItem{AdminId: nowUserId, Editor: nowUserName}
  493. bt, e := json.Marshal(nowUser)
  494. if e != nil {
  495. err = fmt.Errorf("格式化编辑者信息失败")
  496. return
  497. }
  498. if opUserId > 0 {
  499. utils.Rc.Do("SETEX", key, int64(180), string(bt)) //3分钟缓存
  500. } else {
  501. utils.Rc.SetNX(key, string(bt), time.Second*60*3) //3分钟缓存
  502. }
  503. } else if status == 0 {
  504. //清除编辑缓存
  505. _ = utils.Rc.Delete(key)
  506. }
  507. return
  508. }
  509. // 取消英文策略报告一键同步
  510. func EnglishPolicyReportSyncCancel(reportInfo *models.EnglishReportDetail) (err error) {
  511. var policyReport *models.EnglishPolicyReportDetail
  512. if reportInfo.FromReportId > 0 {
  513. //查询报告是否存在
  514. policyReport, err = models.GetEnglishPolicyReportById(reportInfo.FromReportId)
  515. if err != nil {
  516. if err.Error() == utils.ErrNoRow() {
  517. err = fmt.Errorf("报告不存在!")
  518. return
  519. }
  520. err = fmt.Errorf("获取报告信息失败,Err:%v", err)
  521. return
  522. }
  523. //判断报告是否已同步
  524. if policyReport.State == 1 {
  525. err = fmt.Errorf("报告已取消同步,无需重复取消")
  526. return
  527. }
  528. if err = models.SyncCancelEnglishPolicyReport(policyReport.Id); err != nil {
  529. err = fmt.Errorf("报告已取消同步失败 Err %v", err)
  530. return
  531. }
  532. }
  533. return
  534. }