english_report.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. package english_report
  2. import (
  3. "encoding/json"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-playground/validator/v10"
  6. "hongze/hongze_yb_en_api/controller/resp"
  7. "hongze/hongze_yb_en_api/global"
  8. "hongze/hongze_yb_en_api/models/base"
  9. "hongze/hongze_yb_en_api/models/business_conf"
  10. "hongze/hongze_yb_en_api/models/english_classify"
  11. "hongze/hongze_yb_en_api/models/english_report"
  12. "hongze/hongze_yb_en_api/models/english_video"
  13. "hongze/hongze_yb_en_api/services"
  14. elasticService "hongze/hongze_yb_en_api/services/elastic"
  15. english_report_service "hongze/hongze_yb_en_api/services/english_report"
  16. "hongze/hongze_yb_en_api/utils"
  17. "html"
  18. "strconv"
  19. )
  20. type EnglishReportController struct{}
  21. func (er *EnglishReportController) List(c *gin.Context) {
  22. req := new(english_report.ReportListReq)
  23. err := c.BindQuery(&req)
  24. if err != nil {
  25. errs, ok := err.(validator.ValidationErrors)
  26. if !ok {
  27. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  28. return
  29. }
  30. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  31. return
  32. }
  33. page := new(base.Page)
  34. page.SetPageSize(req.PageSize)
  35. page.SetCurrent(req.Current)
  36. item := new(english_report.Report)
  37. enClassify := new(english_classify.Classify)
  38. condition := " state IN (2, 6)"
  39. var pars []interface{}
  40. var total int64
  41. var tmpList []*english_report.Report
  42. list := make([]*english_report.ReportListItem, 0)
  43. if req.ClassifyIdFirst > 0 {
  44. condition += " and classify_id_first = ?"
  45. pars = append(pars, req.ClassifyIdFirst)
  46. }
  47. if req.ClassifyIdSecond > 0 {
  48. condition += " and classify_id_second = ?"
  49. pars = append(pars, req.ClassifyIdSecond)
  50. }
  51. if req.ClassifyIdRoot > 0 && req.ClassifyIdFirst == 0 && req.ClassifyIdSecond == 0 {
  52. //查询顶级分类下的所有二级分类
  53. secondClassify, err := enClassify.GetSecondChild(req.ClassifyIdRoot)
  54. if err != nil {
  55. resp.FailMsg("获取分类失败", "获取分类信息失败,Err:"+err.Error(), c)
  56. return
  57. }
  58. var classifyIdFirstSlice []int
  59. for _, v := range secondClassify {
  60. classifyIdFirstSlice = append(classifyIdFirstSlice, v.Id)
  61. }
  62. condition += " and classify_id_first in (?)"
  63. pars = append(pars, classifyIdFirstSlice)
  64. }
  65. page.AddOrderItem(base.OrderItem{Column: "publish_time", Asc: false})
  66. page.AddOrderItem(base.OrderItem{Column: "id", Asc: false})
  67. total, tmpList, err = item.SelectPage(page, condition, pars)
  68. if err != nil {
  69. resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
  70. return
  71. }
  72. if len(tmpList) > 0 {
  73. var thirdIds []int
  74. for _, v := range tmpList {
  75. thirdIds = append(thirdIds, v.ClassifyIdSecond)
  76. }
  77. //查询分类详情
  78. nameList, err := enClassify.GetEnglishClassifyFullNameByIds(thirdIds)
  79. if err != nil {
  80. resp.FailMsg("获取分类失败", "获取分类信息失败,Err:"+err.Error(), c)
  81. return
  82. }
  83. classifyMap := make(map[int]*english_classify.EnglishClassifyFullName)
  84. for _, v := range nameList {
  85. classifyMap[v.Id] = v
  86. }
  87. for _, v := range tmpList {
  88. classifyInfo := new(english_classify.EnglishClassifyFullName)
  89. if cv, ok := classifyMap[v.ClassifyIdSecond]; ok {
  90. classifyInfo = cv
  91. }
  92. tmp := &english_report.ReportListItem{
  93. Id: v.Id,
  94. AddType: v.AddType,
  95. ClassifyIdFirst: v.ClassifyIdFirst,
  96. ClassifyNameFirst: v.ClassifyNameFirst,
  97. ClassifyIdSecond: v.ClassifyIdSecond,
  98. ClassifyNameSecond: v.ClassifyNameSecond,
  99. Title: v.Title,
  100. Abstract: v.Abstract,
  101. Author: v.Author,
  102. Frequency: v.Frequency,
  103. PublishTime: utils.TimeTransferString(utils.FormatDateTime, v.PublishTime),
  104. Stage: v.Stage,
  105. Overview: v.Overview,
  106. VideoUrl: v.VideoUrl,
  107. VideoName: v.VideoName,
  108. VideoPlaySeconds: v.VideoPlaySeconds,
  109. ReportCode: v.ReportCode,
  110. ShareUrl: "https://share.hzinsights.com/reportEn?code=" + v.ReportCode,
  111. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  112. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  113. ClassifyIdRoot: classifyInfo.RootId,
  114. ClassifyNameRoot: classifyInfo.RootName,
  115. }
  116. list = append(list, tmp)
  117. }
  118. }
  119. page.SetTotal(total)
  120. baseData := new(base.BaseData)
  121. baseData.SetPage(page)
  122. baseData.SetList(list)
  123. resp.OkData("获取成功", baseData, c)
  124. }
  125. func (er *EnglishReportController) Classify(c *gin.Context) {
  126. /*req := new(english_classify.ClassifyReq)
  127. err := c.BindQuery(&req)
  128. if err != nil {
  129. errs, ok := err.(validator.ValidationErrors)
  130. if !ok {
  131. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  132. return
  133. }
  134. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  135. return
  136. }*/
  137. item := new(english_classify.Classify)
  138. parentList, err := item.GetParent()
  139. if err != nil {
  140. resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
  141. return
  142. }
  143. childList, err := item.GetChild()
  144. if err != nil {
  145. resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
  146. return
  147. }
  148. rootMap := make(map[int][]*english_classify.ClassifyListItem, 0)
  149. secondMap := make(map[int][]*english_classify.ClassifyListItem, 0)
  150. for _, v := range childList {
  151. if v.ParentId != v.RootId { //三级分类
  152. tmp := &english_classify.ClassifyListItem{
  153. Id: v.Id,
  154. ClassifyName: v.ClassifyName,
  155. Sort: v.Sort,
  156. ParentId: v.ParentId,
  157. RootId: v.RootId,
  158. ClassifyLabel: v.ClassifyLabel,
  159. ShowType: v.ShowType,
  160. ClassifyType: v.ClassifyType,
  161. IsShow: v.IsShow,
  162. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  163. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  164. }
  165. secondMap[v.ParentId] = append(secondMap[v.ParentId], tmp)
  166. }
  167. }
  168. for _, v := range childList {
  169. if v.ParentId == v.RootId { //二级分类
  170. tmp := &english_classify.ClassifyListItem{
  171. Id: v.Id,
  172. ClassifyName: v.ClassifyName,
  173. Sort: v.Sort,
  174. ParentId: v.ParentId,
  175. RootId: v.RootId,
  176. ClassifyLabel: v.ClassifyLabel,
  177. ShowType: v.ShowType,
  178. ClassifyType: v.ClassifyType,
  179. IsShow: v.IsShow,
  180. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  181. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  182. }
  183. if child, ok := secondMap[v.Id]; ok {
  184. tmp.Child = child
  185. } else {
  186. continue
  187. }
  188. rootMap[v.ParentId] = append(rootMap[v.ParentId], tmp)
  189. }
  190. }
  191. list := make([]*english_classify.ClassifyListItem, 0)
  192. for _, v := range parentList {
  193. tmp := &english_classify.ClassifyListItem{
  194. Id: v.Id,
  195. ClassifyName: v.ClassifyName,
  196. Sort: v.Sort,
  197. ParentId: v.ParentId,
  198. RootId: v.RootId,
  199. ClassifyLabel: v.ClassifyLabel,
  200. ShowType: v.ShowType,
  201. ClassifyType: v.ClassifyType,
  202. IsShow: v.IsShow,
  203. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  204. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  205. }
  206. if child, ok := rootMap[v.Id]; ok {
  207. tmp.Child = child
  208. } else {
  209. continue
  210. }
  211. list = append(list, tmp)
  212. }
  213. baseData := new(base.BaseOnlyData)
  214. baseData.SetList(list)
  215. resp.OkData("获取成功", baseData, c)
  216. }
  217. func (er *EnglishReportController) FilterByEs(c *gin.Context) {
  218. req := new(english_report.ReportSearchReq)
  219. err := c.BindQuery(&req)
  220. if err != nil {
  221. errs, ok := err.(validator.ValidationErrors)
  222. if !ok {
  223. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  224. return
  225. }
  226. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  227. return
  228. }
  229. page := new(base.Page)
  230. page.SetPageSize(req.PageSize)
  231. page.SetCurrent(req.Current)
  232. if req.KeyWord == "" {
  233. resp.FailMsg("请输入搜索词", "", c)
  234. return
  235. }
  236. from := (req.Current - 1) * req.PageSize
  237. reportList := make([]*english_report.SearchEnglishReportItem, 0)
  238. searchResp, total, err := elasticService.SearchESEnglishReport(req.KeyWord, from, req.PageSize)
  239. if err != nil {
  240. resp.FailMsg("报告搜索失败", "报告搜索失败,Err:"+err.Error(), c)
  241. return
  242. }
  243. var tempList []*english_report.ElasticEnglishReportDetail
  244. if searchResp.Hits != nil {
  245. for _, v := range searchResp.Hits.Hits {
  246. itemJson, tmpErr := v.Source.MarshalJSON()
  247. if tmpErr != nil {
  248. resp.FailMsg("报告搜索失败 解析出错", "报告搜索失败,Err:"+tmpErr.Error(), c)
  249. return
  250. }
  251. reportItem := new(english_report.ElasticEnglishReportDetail)
  252. tmpErr = json.Unmarshal(itemJson, &reportItem)
  253. if tmpErr != nil {
  254. resp.FailMsg("报告搜索失败 解析出错", "报告搜索失败,Err:"+tmpErr.Error(), c)
  255. return
  256. }
  257. if len(v.Highlight["Abstract"]) > 0 {
  258. reportItem.Abstract = v.Highlight["Abstract"][0]
  259. }
  260. if reportItem.ReportId > 0 {
  261. if len(v.Highlight["BodyContent"]) > 0 {
  262. reportItem.ContentSub = v.Highlight["BodyContent"][0]
  263. }
  264. reportItem.ContentSub = "<div style=\"-webkit-line-clamp: 3;-webkit-box-orient: vertical;display: -webkit-box;overflow: hidden;text-overflow: ellipsis;\">" + reportItem.ContentSub + "</div>"
  265. }
  266. tempList = append(tempList, reportItem)
  267. }
  268. }
  269. videoIds := make([]uint, 0)
  270. videoMap := make(map[uint]*english_video.EnglishVideo)
  271. videoItem := new(english_video.EnglishVideo)
  272. var classifyThirdIds []int
  273. if len(tempList) > 0 {
  274. for _, reportItem := range tempList {
  275. if reportItem.VideoId > 0 {
  276. videoIds = append(videoIds, reportItem.VideoId)
  277. }
  278. classifyThirdIds = append(classifyThirdIds, reportItem.ClassifyIdSecond)
  279. }
  280. }
  281. enClassify := new(english_classify.Classify)
  282. //查询分类详情
  283. nameList, err := enClassify.GetEnglishClassifyFullNameByIds(classifyThirdIds)
  284. if err != nil {
  285. resp.FailMsg("获取分类失败", "获取分类信息失败,Err:"+err.Error(), c)
  286. return
  287. }
  288. classifyMap := make(map[int]*english_classify.EnglishClassifyFullName)
  289. for _, v := range nameList {
  290. classifyMap[v.Id] = v
  291. }
  292. if len(videoIds) > 0 {
  293. videoList, e := videoItem.GetVideosByIds(videoIds)
  294. if e != nil {
  295. resp.FailMsg("查询线上路演列表出错", "查询线上路演列表出错,Err:"+e.Error(), c)
  296. return
  297. }
  298. for _, v := range videoList {
  299. videoMap[v.Id] = v
  300. }
  301. }
  302. if len(tempList) > 0 {
  303. for _, reportItem := range tempList {
  304. temp := new(english_report.SearchEnglishReportItem)
  305. temp.Title = reportItem.Title
  306. temp.Abstract = reportItem.Abstract
  307. temp.Author = reportItem.Author
  308. temp.CreateTime = reportItem.CreateTime
  309. temp.ContentSub = reportItem.ContentSub
  310. temp.PublishTime = reportItem.PublishTime
  311. temp.ReportCode = reportItem.ReportCode
  312. classifyInfo := new(english_classify.EnglishClassifyFullName)
  313. if cv, ok := classifyMap[reportItem.ClassifyIdSecond]; ok {
  314. classifyInfo = cv
  315. }
  316. if reportItem.ReportId > 0 {
  317. temp.Id = reportItem.ReportId
  318. temp.ClassifyIdFirst = reportItem.ClassifyIdFirst
  319. temp.ClassifyNameFirst = reportItem.ClassifyNameFirst
  320. temp.ClassifyIdSecond = reportItem.ClassifyIdSecond
  321. temp.ClassifyNameSecond = reportItem.ClassifyNameSecond
  322. temp.Stage, _ = strconv.Atoi(reportItem.StageStr)
  323. temp.ClassifyIdRoot = classifyInfo.RootId
  324. temp.ClassifyNameRoot = classifyInfo.RootName
  325. } else if reportItem.VideoId > 0 {
  326. if videoTemp, ok := videoMap[reportItem.VideoId]; ok {
  327. temp.ReportType = 1
  328. temp.Id = videoTemp.Id
  329. temp.ClassifyIdFirst = videoTemp.ClassifyIdFirst
  330. temp.ClassifyNameFirst = videoTemp.ClassifyNameFirst
  331. temp.ClassifyIdSecond = videoTemp.ClassifyIdSecond
  332. temp.ClassifyNameSecond = videoTemp.ClassifyNameSecond
  333. temp.VideoUrl = videoTemp.VideoUrl
  334. temp.VideoCoverUrl = videoTemp.VideoCoverUrl
  335. temp.VideoSeconds = videoTemp.VideoSeconds
  336. temp.ClassifyIdRoot = classifyInfo.RootId
  337. temp.ClassifyNameRoot = classifyInfo.RootName
  338. }
  339. }
  340. reportList = append(reportList, temp)
  341. }
  342. }
  343. page.SetTotal(total)
  344. baseData := new(base.BaseData)
  345. baseData.SetPage(page)
  346. baseData.SetList(reportList)
  347. resp.OkData("搜索成功", baseData, c)
  348. }
  349. func (er *EnglishReportController) Detail(c *gin.Context) {
  350. req := new(english_report.ReportDetailReq)
  351. err := c.BindQuery(&req)
  352. if err != nil {
  353. errs, ok := err.(validator.ValidationErrors)
  354. if !ok {
  355. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  356. return
  357. }
  358. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  359. return
  360. }
  361. reportItem := new(english_report.Report)
  362. reportItem, err = reportItem.GetEnglishReportByCode(req.ReportCode)
  363. if err != nil {
  364. resp.FailMsg("该报告已删除", "报告查询失败,Err:"+err.Error(), c)
  365. return
  366. }
  367. // 英文研报1.1改-研报未登录也可以查看部分报告, 登录则校验权限
  368. var authOk bool
  369. userInfo := services.GetInfoByClaims(c)
  370. if userInfo.Id > 0 {
  371. // 正式客户, 校验权限
  372. if userInfo.Status == 1 {
  373. ok, e := english_report_service.CheckUserReportAuthByCompanyAndClassify(int(userInfo.CompanyId), reportItem.ClassifyIdSecond)
  374. if e != nil {
  375. resp.FailMsg("获取失败", "报告权限校验失败, Err: "+e.Error(), c)
  376. return
  377. }
  378. authOk = ok
  379. }
  380. // 临时客户, 可查看所有
  381. if userInfo.Status == 2 {
  382. authOk = true
  383. }
  384. }
  385. content, subContent := ``, ``
  386. if authOk {
  387. subContent = html.UnescapeString(reportItem.ContentSub)
  388. content = html.UnescapeString(reportItem.Content)
  389. }
  390. // 记录PV
  391. err = reportItem.UpdatePvByReportCode(req.ReportCode)
  392. if err != nil {
  393. resp.FailMsg("更新失败", "更新失败,Err:"+err.Error(), c)
  394. return
  395. }
  396. // 查询分类
  397. classifyItem := new(english_classify.Classify)
  398. fullClassify := new(english_classify.EnglishClassifyFullName)
  399. fullClassifyList, err := classifyItem.GetEnglishClassifyFullNameByIds([]int{reportItem.ClassifyIdSecond})
  400. if err != nil {
  401. resp.FailMsg("查询分类信息失败", "查询分类信息失败,Err:"+err.Error(), c)
  402. return
  403. }
  404. if len(fullClassifyList) > 0 {
  405. fullClassify = fullClassifyList[0]
  406. }
  407. // 记录邮箱
  408. if userInfo.Id > 0 {
  409. go english_report_service.DealEmail(reportItem, req.ReportCode, int(userInfo.Id))
  410. }
  411. reportDetail := &english_report.ReportDetail{
  412. Id: reportItem.Id,
  413. AddType: reportItem.AddType,
  414. ClassifyIdFirst: reportItem.ClassifyIdFirst,
  415. ClassifyNameFirst: reportItem.ClassifyNameFirst,
  416. ClassifyIdSecond: reportItem.ClassifyIdSecond,
  417. ClassifyNameSecond: reportItem.ClassifyNameSecond,
  418. Title: reportItem.Title,
  419. Abstract: reportItem.Abstract,
  420. Author: reportItem.Author,
  421. Overview: reportItem.Overview,
  422. Content: content,
  423. ContentSub: subContent,
  424. Frequency: reportItem.Frequency,
  425. PublishTime: utils.TimeTransferString(utils.FormatDateTime, reportItem.PublishTime),
  426. Stage: reportItem.Stage,
  427. VideoUrl: reportItem.VideoUrl,
  428. VideoName: reportItem.VideoName,
  429. VideoPlaySeconds: reportItem.VideoPlaySeconds,
  430. VideoSize: reportItem.VideoSize,
  431. ReportCode: reportItem.ReportCode,
  432. CreateTime: utils.TimeTransferString(utils.FormatDateTime, reportItem.CreateTime),
  433. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, reportItem.ModifyTime),
  434. KeyTakeaways: reportItem.KeyTakeaways,
  435. ClassifyIdRoot: fullClassify.RootId,
  436. ClassifyNameRoot: fullClassify.RootName,
  437. }
  438. baseData := new(english_report.ReportDetailResp)
  439. baseData.Report = reportDetail
  440. baseData.AuthOk = authOk
  441. // 配置信息
  442. obj := business_conf.BusinessConf{}
  443. conf, e := obj.GetBusinessConf()
  444. if e != nil {
  445. resp.FailMsg("获取失败", "获取免责声明失败,Err:"+e.Error(), c)
  446. return
  447. }
  448. {
  449. // 免责声明
  450. if conf[business_conf.BusinessConfDisclaimerEn] != "" {
  451. baseData.Disclaimer = conf[business_conf.BusinessConfDisclaimerEn]
  452. }
  453. // 报告Logo
  454. if v, ok := conf[business_conf.BusinessConfReportEnLogoShow]; ok {
  455. if v == `true` {
  456. if reportLogo, ok := conf[business_conf.BusinessConfReportLogo]; ok {
  457. baseData.ReportLogo = reportLogo
  458. }
  459. }
  460. }
  461. }
  462. // 授权token
  463. if conf[business_conf.BusinessConfIsOpenChartExpired] == `true` {
  464. tokenMap := make(map[string]string)
  465. baseData.Report.Content = english_report_service.HandleReportContent(baseData.Report.Content, "add", tokenMap)
  466. baseData.Report.ContentSub = english_report_service.HandleReportContent(baseData.Report.ContentSub, "add", tokenMap)
  467. }
  468. resp.OkData("查询成功", baseData, c)
  469. }