english_report.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. func CreateSaCompareES() {
  573. indexName := utils.EsSemanticAnalysisCompareIndexName
  574. mappingJson := `{
  575. "mappings": {
  576. "dynamic": true,
  577. "properties": {
  578. "ResultImg" : {
  579. "type" : "text"
  580. },
  581. "ClassifyName" : {
  582. "type" : "text",
  583. "fields" : {
  584. "keyword" : {
  585. "type" : "keyword",
  586. "ignore_above" : 256
  587. }
  588. }
  589. },
  590. "SysAdminName" : {
  591. "type" : "text"
  592. },
  593. "SysAdminId" : {
  594. "type" : "long"
  595. },
  596. "ClassifyId" : {
  597. "type" : "long"
  598. },
  599. "ModifyTime" : {
  600. "type" : "text",
  601. "fields" : {
  602. "keyword" : {
  603. "type" : "keyword",
  604. "ignore_above" : 256
  605. }
  606. }
  607. },
  608. "CreateTime" : {
  609. "type" : "text",
  610. "fields" : {
  611. "keyword" : {
  612. "type" : "keyword",
  613. "ignore_above" : 256
  614. }
  615. }
  616. },
  617. "SaCompareId" : {
  618. "type" : "long"
  619. },
  620. "Title" : {
  621. "type" : "text",
  622. "fields" : {
  623. "keyword" : {
  624. "type" : "keyword",
  625. "ignore_above" : 256
  626. }
  627. }
  628. }
  629. }
  630. }
  631. }`
  632. EsCreateIndex(indexName, mappingJson)
  633. }
  634. // UpdateEnReportEditMark 更新英文研报当前更新状态
  635. // status 枚举值 1:编辑中,0:完成编辑, 2:只做查询
  636. func UpdateEnReportEditMark(reportId, nowUserId, status int, nowUserName string) (ret models.MarkReportResp, err error) {
  637. //更新标记key
  638. key := fmt.Sprint(`crm:enReport:edit:`, reportId)
  639. opUserId, e := utils.Rc.RedisInt(key)
  640. var opUser models.MarkReportItem
  641. if e != nil {
  642. opUserInfoStr, tErr := utils.Rc.RedisString(key)
  643. if tErr == nil {
  644. tErr = json.Unmarshal([]byte(opUserInfoStr), &opUser)
  645. if tErr == nil {
  646. opUserId = opUser.AdminId
  647. }
  648. }
  649. }
  650. if opUserId > 0 && opUserId != nowUserId {
  651. editor := opUser.Editor
  652. if editor == "" {
  653. //查询账号的用户姓名
  654. otherInfo, e := system.GetSysAdminById(opUserId)
  655. if e != nil {
  656. err = fmt.Errorf("查询其他编辑者信息失败")
  657. return
  658. }
  659. editor = otherInfo.RealName
  660. }
  661. ret.Status = 1
  662. ret.Msg = fmt.Sprintf("当前%s正在编辑报告", editor)
  663. ret.Editor = editor
  664. return
  665. }
  666. if status == 1 {
  667. nowUser := &models.MarkReportItem{AdminId: nowUserId, Editor: nowUserName}
  668. bt, e := json.Marshal(nowUser)
  669. if e != nil {
  670. err = fmt.Errorf("格式化编辑者信息失败")
  671. return
  672. }
  673. if opUserId > 0 {
  674. utils.Rc.Do("SETEX", key, int64(180), string(bt)) //3分钟缓存
  675. } else {
  676. utils.Rc.SetNX(key, string(bt), time.Second*60*3) //3分钟缓存
  677. }
  678. } else if status == 0 {
  679. //清除编辑缓存
  680. _ = utils.Rc.Delete(key)
  681. }
  682. return
  683. }
  684. // 英文策略报告一键同步功能
  685. func EnglishPolicyReportSync(id int, adminInfo *system.Admin) (err error, errMsg string) {
  686. //查询报告是否存在
  687. policyReport, err := models.GetEnglishPolicyReportById(id)
  688. if err != nil {
  689. if err.Error() == utils.ErrNoRow() {
  690. err = fmt.Errorf("报告不存在!")
  691. return
  692. }
  693. errMsg = "获取报告信息失败,Err:" + err.Error()
  694. err = fmt.Errorf("获取报告信息失败")
  695. return
  696. }
  697. //判断报告是否已同步
  698. if policyReport.State == 2 {
  699. err = fmt.Errorf("报告已同步,无需重复同步")
  700. return
  701. }
  702. //新增英文研报
  703. classifyNameFirst := "Macro"
  704. classifySecondName := "China A-share Daily Check-in"
  705. // 根据分类名称查询分类ID
  706. classifyInfo, err := models.GetEnglishClassifyByClassifyNameAndParentName(classifyNameFirst, classifySecondName)
  707. if err != nil {
  708. if err.Error() == utils.ErrNoRow() {
  709. err = fmt.Errorf("报告分类不存在!")
  710. return
  711. }
  712. errMsg = "获取报告分类失败,Err:" + err.Error()
  713. err = fmt.Errorf("获取报告分类失败")
  714. return
  715. }
  716. // 获取期数
  717. maxStage, err := models.GetEnglishReportStage(classifyInfo.ParentId, classifyInfo.Id)
  718. if err != nil {
  719. errMsg = "期数获取失败,Err:" + err.Error()
  720. err = fmt.Errorf("期数获取失败")
  721. return
  722. }
  723. var contentSub string
  724. if policyReport.Content != "" {
  725. contentSub, err = GetReportContentSub(policyReport.Content)
  726. if err != nil {
  727. go alarm_msg.SendAlarmMsg("策略报告 一键同步 ContentSub 失败,Err:"+err.Error(), 3)
  728. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  729. }
  730. }
  731. item := new(models.EnglishReport)
  732. item.AddType = 1
  733. item.ClassifyIdFirst = classifyInfo.ParentId
  734. item.ClassifyNameFirst = classifyInfo.ParentClassifyName
  735. item.ClassifyIdSecond = classifyInfo.Id
  736. item.ClassifyNameSecond = classifyInfo.ClassifyName
  737. item.Title = "China A-share Daily Check-in"
  738. item.Abstract = "China A-share market and economic data review"
  739. item.Author = "Horizon Insights FICC Team"
  740. item.Frequency = policyReport.Frequency
  741. item.State = 1
  742. item.Content = policyReport.Content
  743. item.Stage = maxStage + 1
  744. item.ContentSub = html.EscapeString(contentSub)
  745. item.CreateTime = time.Now().Format(utils.FormatDateTime)
  746. item.ModifyTime = time.Now()
  747. item.Overview = policyReport.Abstract
  748. item.KeyTakeaways = policyReport.KeyTakeaways
  749. item.FromReportId = policyReport.Id
  750. item.AdminId = adminInfo.AdminId
  751. item.AdminRealName = adminInfo.RealName
  752. newReportId, err := models.AddEnglishReport(item)
  753. if err != nil {
  754. errMsg = "保存研报失败,Err:" + err.Error()
  755. err = fmt.Errorf("保存研报失败")
  756. return
  757. }
  758. reportCode := utils.MD5(strconv.Itoa(int(newReportId)))
  759. //修改唯一编码
  760. {
  761. go models.ModifyEnglishReportCode(newReportId, reportCode)
  762. }
  763. //更新策略报告状态为已同步
  764. if err = models.SyncEnglishPolicyReportById(policyReport.Id, int(newReportId)); err != nil {
  765. errMsg = "更新策略报告失败,Err:" + err.Error()
  766. err = fmt.Errorf("更新策略报告失败")
  767. return
  768. }
  769. return
  770. }
  771. // 英文策略报告批量同步功能
  772. func MultiEnglishPolicyReportSync() (err error, errMsg string) {
  773. //查询发布时间是当天的所有未同步的报告,并更新到英文研报
  774. condition := ""
  775. var pars []interface{}
  776. startDate := time.Now().Format(utils.FormatDate)
  777. condition += ` AND publish_time >= ? AND state = 1 AND (content !="" AND content is not null)`
  778. pars = append(pars, startDate)
  779. policyReports, err := models.GetEnglishPolicyReportByCondition(condition, pars)
  780. if err != nil {
  781. errMsg = "查询英文策略报告列表接口失败"
  782. return
  783. }
  784. if len(policyReports) == 0 {
  785. return
  786. }
  787. //新增英文研报
  788. classifyNameFirst := "Macro"
  789. classifySecondName := "China A-share Daily Check-in"
  790. // 根据分类名称查询分类ID
  791. classifyInfo, err := models.GetEnglishClassifyByClassifyNameAndParentName(classifyNameFirst, classifySecondName)
  792. if err != nil {
  793. if err.Error() == utils.ErrNoRow() {
  794. err = fmt.Errorf("报告分类不存在!")
  795. return
  796. }
  797. errMsg = "获取报告分类失败,Err:" + err.Error()
  798. err = fmt.Errorf("获取报告分类失败")
  799. return
  800. }
  801. // 获取期数
  802. maxStage, err := models.GetEnglishReportStage(classifyInfo.ParentId, classifyInfo.Id)
  803. if err != nil {
  804. errMsg = "期数获取失败,Err:" + err.Error()
  805. err = fmt.Errorf("期数获取失败")
  806. return
  807. }
  808. for _, policyReport := range policyReports {
  809. contentSub := ""
  810. maxStage += 1
  811. if policyReport.Content != "" {
  812. contentSub, err = GetReportContentSub(policyReport.Content)
  813. if err != nil {
  814. errMsg = "策略报告 批量同步 ContentSub 失败,Err:" + err.Error()
  815. err = fmt.Errorf("截取内容失败")
  816. return
  817. }
  818. }
  819. item := new(models.EnglishReport)
  820. item.AddType = 1
  821. item.ClassifyIdFirst = classifyInfo.ParentId
  822. item.ClassifyNameFirst = classifyInfo.ParentClassifyName
  823. item.ClassifyIdSecond = classifyInfo.Id
  824. item.ClassifyNameSecond = classifyInfo.ClassifyName
  825. item.Title = "China A-share Daily Check-in"
  826. item.Abstract = "China A-share market and economic data review"
  827. item.Author = "Horizon Insights FICC Team"
  828. item.Frequency = policyReport.Frequency
  829. item.State = 1
  830. item.Content = policyReport.Content
  831. item.Stage = maxStage
  832. item.ContentSub = html.EscapeString(contentSub)
  833. item.CreateTime = time.Now().Format(utils.FormatDateTime)
  834. item.ModifyTime = time.Now()
  835. item.Overview = policyReport.Abstract
  836. item.KeyTakeaways = policyReport.KeyTakeaways
  837. item.FromReportId = policyReport.Id
  838. newReportId, e := models.AddEnglishReport(item)
  839. if e != nil {
  840. errMsg = "保存研报失败,Err:" + e.Error()
  841. err = fmt.Errorf("保存研报失败")
  842. return
  843. }
  844. reportCode := utils.MD5(strconv.Itoa(int(newReportId)))
  845. //修改唯一编码
  846. err = models.ModifyEnglishReportCode(newReportId, reportCode)
  847. if err != nil {
  848. errMsg = "更新英文研报失败,Err:" + err.Error()
  849. err = fmt.Errorf("更新英文研报失败")
  850. return
  851. }
  852. //更新策略报告状态为已同步
  853. if err = models.SyncEnglishPolicyReportById(policyReport.Id, int(newReportId)); err != nil {
  854. errMsg = "更新策略报告失败,Err:" + err.Error()
  855. err = fmt.Errorf("更新策略报告失败")
  856. return
  857. }
  858. }
  859. return
  860. }
  861. // 取消英文策略报告一键同步
  862. func EnglishPolicyReportSyncCancel(reportInfo *models.EnglishReportDetail) (err error) {
  863. var policyReport *models.EnglishPolicyReportDetail
  864. if reportInfo.FromReportId > 0 {
  865. //查询报告是否存在
  866. policyReport, err = models.GetEnglishPolicyReportById(reportInfo.FromReportId)
  867. if err != nil {
  868. if err.Error() == utils.ErrNoRow() {
  869. err = fmt.Errorf("报告不存在!")
  870. return
  871. }
  872. err = fmt.Errorf("获取报告信息失败,Err:%v", err)
  873. return
  874. }
  875. //判断报告是否已同步
  876. if policyReport.State == 1 {
  877. err = fmt.Errorf("报告已取消同步,无需重复取消")
  878. return
  879. }
  880. if err = models.SyncCancelEnglishPolicyReport(policyReport.Id); err != nil {
  881. err = fmt.Errorf("报告已取消同步失败 Err %v", err)
  882. return
  883. }
  884. }
  885. return
  886. }
  887. // UpdateEnglishCompanyEnabledByCompanyId 根据英文客户ID更新状态
  888. func UpdateEnglishCompanyEnabledByCompanyId(companyId int) (err error) {
  889. defer func() {
  890. if err != nil {
  891. alarm_msg.SendAlarmMsg(fmt.Sprintf("更新英文客户状态失败, Err: %s", err.Error()), 3)
  892. }
  893. }()
  894. // 获取客户
  895. comp, e := models.GetEnglishCompanyById(companyId)
  896. if e != nil {
  897. err = fmt.Errorf("英文客户已被删除")
  898. return
  899. }
  900. // 获取联系人
  901. emailCond := ` AND company_id = ?`
  902. emailPars := make([]interface{}, 0)
  903. emailPars = append(emailPars, companyId)
  904. emailList, e := models.GetEnglishReportEmailList(emailCond, emailPars, "")
  905. if e != nil {
  906. err = fmt.Errorf("获取英文联系人列表失败, Err: %s", e.Error())
  907. return
  908. }
  909. // 无联系人, 保持原状态
  910. total := len(emailList)
  911. if total == 0 {
  912. return
  913. }
  914. // 判断状态, 是否部分禁用
  915. status := 0
  916. enabledCount := 0
  917. for _, v := range emailList {
  918. if v.Enabled == 1 {
  919. enabledCount += 1
  920. }
  921. }
  922. if enabledCount == 0 {
  923. status = models.EnglishCompanyDisabled
  924. }
  925. if total == enabledCount {
  926. status = models.EnglishCompanyEnabled
  927. }
  928. if total > enabledCount && enabledCount != 0 {
  929. status = models.EnglishCompanyHalfEnabled
  930. }
  931. // 是否需要更新
  932. if comp.Enabled == status {
  933. return
  934. }
  935. comp.Enabled = status
  936. comp.ModifyTime = time.Now().Local()
  937. if e = comp.Update([]string{"Enabled", "ModifyTime"}); e != nil {
  938. err = fmt.Errorf("更新客户状态失败, Err: %s", e.Error())
  939. return
  940. }
  941. return
  942. }