english_report.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hz_eta_api/models"
  7. "hongze/hz_eta_api/models/system"
  8. "hongze/hz_eta_api/services/alarm_msg"
  9. "hongze/hz_eta_api/utils"
  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. func CreateEnglishReportIndex() {
  460. indexName := utils.EsEnglishReportIndexName
  461. mappingJson := `{
  462. "mappings": {
  463. "dynamic": true,
  464. "properties": {
  465. "Frequency" : {
  466. "type" : "text"
  467. },
  468. "ReportCode" : {
  469. "type" : "text"
  470. },
  471. "Author" : {
  472. "type" : "text",
  473. "fields" : {
  474. "keyword" : {
  475. "type" : "keyword",
  476. "ignore_above" : 256
  477. }
  478. }
  479. },
  480. "BodyContent" : {
  481. "type" : "text",
  482. "fields" : {
  483. "keyword" : {
  484. "type" : "keyword",
  485. "ignore_above" : 256
  486. }
  487. }
  488. },
  489. "Overview" : {
  490. "type" : "text",
  491. "fields" : {
  492. "keyword" : {
  493. "type" : "keyword",
  494. "ignore_above" : 256
  495. }
  496. }
  497. },
  498. "Abstract" : {
  499. "type" : "text",
  500. "fields" : {
  501. "keyword" : {
  502. "type" : "keyword",
  503. "ignore_above" : 256
  504. }
  505. }
  506. },
  507. "ClassifyIdFirst" : {
  508. "type" : "long"
  509. },
  510. "ClassifyIdSecond" : {
  511. "type" : "long"
  512. },
  513. "ClassifyNameFirst" : {
  514. "type" : "text",
  515. "fields" : {
  516. "keyword" : {
  517. "type" : "keyword",
  518. "ignore_above" : 256
  519. }
  520. }
  521. },
  522. "ClassifyNameSecond" : {
  523. "type" : "text",
  524. "fields" : {
  525. "keyword" : {
  526. "type" : "keyword",
  527. "ignore_above" : 256
  528. }
  529. }
  530. },
  531. "PublishState" : {
  532. "type" : "long"
  533. },
  534. "PublishTime" : {
  535. "type" : "text",
  536. "fields" : {
  537. "keyword" : {
  538. "type" : "keyword",
  539. "ignore_above" : 256
  540. }
  541. }
  542. },
  543. "CreateTime" : {
  544. "type" : "text",
  545. "fields" : {
  546. "keyword" : {
  547. "type" : "keyword",
  548. "ignore_above" : 256
  549. }
  550. }
  551. },
  552. "ReportId" : {
  553. "type" : "long"
  554. },
  555. "StageStr" : {
  556. "type" : "text"
  557. },
  558. "Title" : {
  559. "type" : "text",
  560. "fields" : {
  561. "keyword" : {
  562. "type" : "keyword",
  563. "ignore_above" : 256
  564. }
  565. }
  566. }
  567. }
  568. }
  569. }`
  570. EsCreateIndex(indexName, mappingJson)
  571. }
  572. // UpdateEnReportEditMark 更新英文研报当前更新状态
  573. // status 枚举值 1:编辑中,0:完成编辑, 2:只做查询
  574. func UpdateEnReportEditMark(reportId, nowUserId, status int, nowUserName string) (ret models.MarkReportResp, err error) {
  575. //更新标记key
  576. key := fmt.Sprint(`crm:enReport:edit:`, reportId)
  577. opUserId, e := utils.Rc.RedisInt(key)
  578. var opUser models.MarkReportItem
  579. if e != nil {
  580. opUserInfoStr, tErr := utils.Rc.RedisString(key)
  581. if tErr == nil {
  582. tErr = json.Unmarshal([]byte(opUserInfoStr), &opUser)
  583. if tErr == nil {
  584. opUserId = opUser.AdminId
  585. }
  586. }
  587. }
  588. if opUserId > 0 && opUserId != nowUserId {
  589. editor := opUser.Editor
  590. if editor == "" {
  591. //查询账号的用户姓名
  592. otherInfo, e := system.GetSysAdminById(opUserId)
  593. if e != nil {
  594. err = fmt.Errorf("查询其他编辑者信息失败")
  595. return
  596. }
  597. editor = otherInfo.RealName
  598. }
  599. ret.Status = 1
  600. ret.Msg = fmt.Sprintf("当前%s正在编辑报告", editor)
  601. ret.Editor = editor
  602. return
  603. }
  604. if status == 1 {
  605. nowUser := &models.MarkReportItem{AdminId: nowUserId, Editor: nowUserName}
  606. bt, e := json.Marshal(nowUser)
  607. if e != nil {
  608. err = fmt.Errorf("格式化编辑者信息失败")
  609. return
  610. }
  611. if opUserId > 0 {
  612. utils.Rc.Do("SETEX", key, int64(180), string(bt)) //3分钟缓存
  613. } else {
  614. utils.Rc.SetNX(key, string(bt), time.Second*60*3) //3分钟缓存
  615. }
  616. } else if status == 0 {
  617. //清除编辑缓存
  618. _ = utils.Rc.Delete(key)
  619. }
  620. return
  621. }
  622. // 英文策略报告一键同步功能
  623. func EnglishPolicyReportSync(id int, adminInfo *system.Admin) (err error, errMsg string) {
  624. //查询报告是否存在
  625. policyReport, err := models.GetEnglishPolicyReportById(id)
  626. if err != nil {
  627. if err.Error() == utils.ErrNoRow() {
  628. err = fmt.Errorf("报告不存在!")
  629. return
  630. }
  631. errMsg = "获取报告信息失败,Err:" + err.Error()
  632. err = fmt.Errorf("获取报告信息失败")
  633. return
  634. }
  635. //判断报告是否已同步
  636. if policyReport.State == 2 {
  637. err = fmt.Errorf("报告已同步,无需重复同步")
  638. return
  639. }
  640. //新增英文研报
  641. classifyNameFirst := "Macro"
  642. classifySecondName := "China A-share Daily Check-in"
  643. // 根据分类名称查询分类ID
  644. classifyInfo, err := models.GetEnglishClassifyByClassifyNameAndParentName(classifyNameFirst, classifySecondName)
  645. if err != nil {
  646. if err.Error() == utils.ErrNoRow() {
  647. err = fmt.Errorf("报告分类不存在!")
  648. return
  649. }
  650. errMsg = "获取报告分类失败,Err:" + err.Error()
  651. err = fmt.Errorf("获取报告分类失败")
  652. return
  653. }
  654. // 获取期数
  655. maxStage, err := models.GetEnglishReportStage(classifyInfo.ParentId, classifyInfo.Id)
  656. if err != nil {
  657. errMsg = "期数获取失败,Err:" + err.Error()
  658. err = fmt.Errorf("期数获取失败")
  659. return
  660. }
  661. var contentSub string
  662. if policyReport.Content != "" {
  663. contentSub, err = GetReportContentSub(policyReport.Content)
  664. if err != nil {
  665. go alarm_msg.SendAlarmMsg("策略报告 一键同步 ContentSub 失败,Err:"+err.Error(), 3)
  666. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  667. }
  668. }
  669. item := new(models.EnglishReport)
  670. item.AddType = 1
  671. item.ClassifyIdFirst = classifyInfo.ParentId
  672. item.ClassifyNameFirst = classifyInfo.ParentClassifyName
  673. item.ClassifyIdSecond = classifyInfo.Id
  674. item.ClassifyNameSecond = classifyInfo.ClassifyName
  675. item.Title = "China A-share Daily Check-in"
  676. item.Abstract = "China A-share market and economic data review"
  677. item.Author = "Horizon Insights FICC Team"
  678. item.Frequency = policyReport.Frequency
  679. item.State = 1
  680. item.Content = policyReport.Content
  681. item.Stage = maxStage + 1
  682. item.ContentSub = html.EscapeString(contentSub)
  683. item.CreateTime = time.Now().Format(utils.FormatDateTime)
  684. item.ModifyTime = time.Now()
  685. item.Overview = policyReport.Abstract
  686. item.KeyTakeaways = policyReport.KeyTakeaways
  687. item.FromReportId = policyReport.Id
  688. item.AdminId = adminInfo.AdminId
  689. item.AdminRealName = adminInfo.RealName
  690. newReportId, err := models.AddEnglishReport(item)
  691. if err != nil {
  692. errMsg = "保存研报失败,Err:" + err.Error()
  693. err = fmt.Errorf("保存研报失败")
  694. return
  695. }
  696. reportCode := utils.MD5(strconv.Itoa(int(newReportId)))
  697. //修改唯一编码
  698. {
  699. go models.ModifyEnglishReportCode(newReportId, reportCode)
  700. }
  701. //更新策略报告状态为已同步
  702. if err = models.SyncEnglishPolicyReportById(policyReport.Id, int(newReportId)); err != nil {
  703. errMsg = "更新策略报告失败,Err:" + err.Error()
  704. err = fmt.Errorf("更新策略报告失败")
  705. return
  706. }
  707. return
  708. }
  709. // 英文策略报告批量同步功能
  710. func MultiEnglishPolicyReportSync() (err error, errMsg string) {
  711. //查询发布时间是当天的所有未同步的报告,并更新到英文研报
  712. condition := ""
  713. var pars []interface{}
  714. startDate := time.Now().Format(utils.FormatDate)
  715. condition += ` AND publish_time >= ? AND state = 1 AND (content !="" AND content is not null)`
  716. pars = append(pars, startDate)
  717. policyReports, err := models.GetEnglishPolicyReportByCondition(condition, pars)
  718. if err != nil {
  719. errMsg = "查询英文策略报告列表接口失败"
  720. return
  721. }
  722. if len(policyReports) == 0 {
  723. return
  724. }
  725. //新增英文研报
  726. classifyNameFirst := "Macro"
  727. classifySecondName := "China A-share Daily Check-in"
  728. // 根据分类名称查询分类ID
  729. classifyInfo, err := models.GetEnglishClassifyByClassifyNameAndParentName(classifyNameFirst, classifySecondName)
  730. if err != nil {
  731. if err.Error() == utils.ErrNoRow() {
  732. err = fmt.Errorf("报告分类不存在!")
  733. return
  734. }
  735. errMsg = "获取报告分类失败,Err:" + err.Error()
  736. err = fmt.Errorf("获取报告分类失败")
  737. return
  738. }
  739. // 获取期数
  740. maxStage, err := models.GetEnglishReportStage(classifyInfo.ParentId, classifyInfo.Id)
  741. if err != nil {
  742. errMsg = "期数获取失败,Err:" + err.Error()
  743. err = fmt.Errorf("期数获取失败")
  744. return
  745. }
  746. for _, policyReport := range policyReports {
  747. contentSub := ""
  748. maxStage += 1
  749. if policyReport.Content != "" {
  750. contentSub, err = GetReportContentSub(policyReport.Content)
  751. if err != nil {
  752. errMsg = "策略报告 批量同步 ContentSub 失败,Err:" + err.Error()
  753. err = fmt.Errorf("截取内容失败")
  754. return
  755. }
  756. }
  757. item := new(models.EnglishReport)
  758. item.AddType = 1
  759. item.ClassifyIdFirst = classifyInfo.ParentId
  760. item.ClassifyNameFirst = classifyInfo.ParentClassifyName
  761. item.ClassifyIdSecond = classifyInfo.Id
  762. item.ClassifyNameSecond = classifyInfo.ClassifyName
  763. item.Title = "China A-share Daily Check-in"
  764. item.Abstract = "China A-share market and economic data review"
  765. item.Author = "Horizon Insights FICC Team"
  766. item.Frequency = policyReport.Frequency
  767. item.State = 1
  768. item.Content = policyReport.Content
  769. item.Stage = maxStage
  770. item.ContentSub = html.EscapeString(contentSub)
  771. item.CreateTime = time.Now().Format(utils.FormatDateTime)
  772. item.ModifyTime = time.Now()
  773. item.Overview = policyReport.Abstract
  774. item.KeyTakeaways = policyReport.KeyTakeaways
  775. item.FromReportId = policyReport.Id
  776. newReportId, e := models.AddEnglishReport(item)
  777. if e != nil {
  778. errMsg = "保存研报失败,Err:" + e.Error()
  779. err = fmt.Errorf("保存研报失败")
  780. return
  781. }
  782. reportCode := utils.MD5(strconv.Itoa(int(newReportId)))
  783. //修改唯一编码
  784. err = models.ModifyEnglishReportCode(newReportId, reportCode)
  785. if err != nil {
  786. errMsg = "更新英文研报失败,Err:" + err.Error()
  787. err = fmt.Errorf("更新英文研报失败")
  788. return
  789. }
  790. //更新策略报告状态为已同步
  791. if err = models.SyncEnglishPolicyReportById(policyReport.Id, int(newReportId)); err != nil {
  792. errMsg = "更新策略报告失败,Err:" + err.Error()
  793. err = fmt.Errorf("更新策略报告失败")
  794. return
  795. }
  796. }
  797. return
  798. }
  799. // 取消英文策略报告一键同步
  800. func EnglishPolicyReportSyncCancel(reportInfo *models.EnglishReportDetail) (err error) {
  801. var policyReport *models.EnglishPolicyReportDetail
  802. if reportInfo.FromReportId > 0 {
  803. //查询报告是否存在
  804. policyReport, err = models.GetEnglishPolicyReportById(reportInfo.FromReportId)
  805. if err != nil {
  806. if err.Error() == utils.ErrNoRow() {
  807. err = fmt.Errorf("报告不存在!")
  808. return
  809. }
  810. err = fmt.Errorf("获取报告信息失败,Err:%v", err)
  811. return
  812. }
  813. //判断报告是否已同步
  814. if policyReport.State == 1 {
  815. err = fmt.Errorf("报告已取消同步,无需重复取消")
  816. return
  817. }
  818. if err = models.SyncCancelEnglishPolicyReport(policyReport.Id); err != nil {
  819. err = fmt.Errorf("报告已取消同步失败 Err %v", err)
  820. return
  821. }
  822. }
  823. return
  824. }
  825. // UpdateEnglishCompanyEnabledByCompanyId 根据英文客户ID更新状态
  826. func UpdateEnglishCompanyEnabledByCompanyId(companyId int) (err error) {
  827. defer func() {
  828. if err != nil {
  829. alarm_msg.SendAlarmMsg(fmt.Sprintf("更新英文客户状态失败, Err: %s", err.Error()), 3)
  830. }
  831. }()
  832. // 获取客户
  833. comp, e := models.GetEnglishCompanyById(companyId)
  834. if e != nil {
  835. err = fmt.Errorf("英文客户已被删除")
  836. return
  837. }
  838. // 获取联系人
  839. emailCond := ` AND company_id = ?`
  840. emailPars := make([]interface{}, 0)
  841. emailPars = append(emailPars, companyId)
  842. emailList, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  843. if e != nil {
  844. err = fmt.Errorf("获取英文联系人列表失败, Err: %s", e.Error())
  845. return
  846. }
  847. // 无联系人, 保持原状态
  848. total := len(emailList)
  849. if total == 0 {
  850. return
  851. }
  852. // 判断状态, 是否部分禁用
  853. status := 0
  854. enabledCount := 0
  855. for _, v := range emailList {
  856. if v.Enabled == 1 {
  857. enabledCount += 1
  858. }
  859. }
  860. if enabledCount == 0 {
  861. status = models.EnglishCompanyDisabled
  862. }
  863. if total == enabledCount {
  864. status = models.EnglishCompanyEnabled
  865. }
  866. if total > enabledCount && enabledCount != 0 {
  867. status = models.EnglishCompanyHalfEnabled
  868. }
  869. // 是否需要更新
  870. if comp.Enabled == status {
  871. return
  872. }
  873. comp.Enabled = status
  874. comp.ModifyTime = time.Now().Local()
  875. if e = comp.Update([]string{"Enabled", "ModifyTime"}); e != nil {
  876. err = fmt.Errorf("更新客户状态失败, Err: %s", e.Error())
  877. return
  878. }
  879. return
  880. }