english_report.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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/english_classify"
  10. "hongze/hongze_yb_en_api/models/english_report"
  11. "hongze/hongze_yb_en_api/models/english_video"
  12. elasticService "hongze/hongze_yb_en_api/services/elastic"
  13. english_report_service "hongze/hongze_yb_en_api/services/english_report"
  14. "hongze/hongze_yb_en_api/utils"
  15. "html"
  16. "strconv"
  17. )
  18. type EnglishReportController struct {}
  19. func (er *EnglishReportController) List(c *gin.Context) {
  20. req := new(english_report.ReportListReq)
  21. err := c.BindQuery(&req)
  22. if err != nil {
  23. errs, ok := err.(validator.ValidationErrors)
  24. if !ok {
  25. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  26. return
  27. }
  28. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  29. return
  30. }
  31. page := new(base.Page)
  32. page.SetPageSize(req.PageSize)
  33. page.SetCurrent(req.Current)
  34. item := new(english_report.Report)
  35. condition := " state=2"
  36. var pars []interface{}
  37. var total int64
  38. var tmpList []*english_report.Report
  39. list := make([]*english_report.ReportListItem, 0)
  40. if req.ClassifyIdFirst >0 {
  41. condition += " and classify_id_first = ?"
  42. pars = append(pars, req.ClassifyIdFirst)
  43. }
  44. if req.ClassifyIdSecond >0 {
  45. condition += " and classify_id_second = ?"
  46. pars = append(pars, req.ClassifyIdSecond)
  47. }
  48. page.AddOrderItem(base.OrderItem{Column: "publish_time", Asc: false})
  49. page.AddOrderItem(base.OrderItem{Column: "id", Asc: false})
  50. total, tmpList, err = item.SelectPage(page, condition, pars)
  51. if err != nil {
  52. resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
  53. return
  54. }
  55. if len(tmpList) > 0 {
  56. for _, v := range tmpList {
  57. tmp := &english_report.ReportListItem{
  58. Id: v.Id,
  59. AddType: v.AddType,
  60. ClassifyIdFirst: v.ClassifyIdFirst,
  61. ClassifyNameFirst: v.ClassifyNameFirst,
  62. ClassifyIdSecond: v.ClassifyIdSecond,
  63. ClassifyNameSecond: v.ClassifyNameSecond,
  64. Title: v.Title,
  65. Abstract: v.Abstract,
  66. Author: v.Author,
  67. Frequency: v.Frequency,
  68. PublishTime: utils.TimeTransferString(utils.FormatDateTime, v.PublishTime),
  69. Stage: v.Stage,
  70. VideoUrl: v.VideoUrl,
  71. VideoName: v.VideoName,
  72. VideoPlaySeconds: v.VideoPlaySeconds,
  73. ReportCode: v.ReportCode,
  74. ShareUrl: "https://share.hzinsights.com/reportEn?code=" + v.ReportCode,
  75. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  76. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  77. }
  78. list = append(list, tmp)
  79. }
  80. }
  81. page.SetTotal(total)
  82. baseData := new(base.BaseData)
  83. baseData.SetPage(page)
  84. baseData.SetList(list)
  85. resp.OkData("获取成功", baseData, c)
  86. }
  87. func (er *EnglishReportController) Classify(c *gin.Context) {
  88. req := new(english_classify.ClassifyReq)
  89. err := c.BindQuery(&req)
  90. if err != nil {
  91. errs, ok := err.(validator.ValidationErrors)
  92. if !ok {
  93. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  94. return
  95. }
  96. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  97. return
  98. }
  99. item := new(english_classify.Classify)
  100. parentList, err := item.GetParent(req.ClassifyType)
  101. if err != nil {
  102. resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
  103. return
  104. }
  105. childList, err := item.GetChild(req.ClassifyType)
  106. if err != nil {
  107. resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
  108. return
  109. }
  110. parentMap := make(map[int][]*english_classify.ClassifyItem, 0)
  111. for _, v := range childList {
  112. tmp := &english_classify.ClassifyItem{
  113. Id: v.Id,
  114. ClassifyName: v.ClassifyName,
  115. Sort: v.Sort,
  116. ParentId: v.ParentId,
  117. ClassifyLabel: v.ClassifyLabel,
  118. ShowType: v.ShowType,
  119. ClassifyType: v.ClassifyType,
  120. IsShow: v.IsShow,
  121. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  122. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  123. }
  124. parentMap[v.ParentId] = append(parentMap[v.ParentId], tmp)
  125. }
  126. list := make([]*english_classify.ClassifyListItem, 0)
  127. for _, v := range parentList {
  128. tmp := &english_classify.ClassifyListItem{
  129. Id: v.Id,
  130. ClassifyName: v.ClassifyName,
  131. Sort: v.Sort,
  132. ParentId: v.ParentId,
  133. ClassifyLabel: v.ClassifyLabel,
  134. ShowType: v.ShowType,
  135. ClassifyType: v.ClassifyType,
  136. IsShow: v.IsShow,
  137. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  138. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  139. }
  140. if child, ok := parentMap[v.Id]; ok {
  141. tmp.Child = child
  142. }else{
  143. continue
  144. }
  145. list = append(list, tmp)
  146. }
  147. baseData := new(base.BaseOnlyData)
  148. baseData.SetList(list)
  149. resp.OkData("获取成功", baseData, c)
  150. }
  151. func (er *EnglishReportController) FilterByEs(c *gin.Context) {
  152. req := new(english_report.ReportSearchReq)
  153. err := c.BindQuery(&req)
  154. if err != nil {
  155. errs, ok := err.(validator.ValidationErrors)
  156. if !ok {
  157. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  158. return
  159. }
  160. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  161. return
  162. }
  163. page := new(base.Page)
  164. page.SetPageSize(req.PageSize)
  165. page.SetCurrent(req.Current)
  166. if req.KeyWord == "" {
  167. resp.FailMsg("请输入搜索词", "", c)
  168. return
  169. }
  170. from := (req.Current - 1) * req.PageSize
  171. reportList := make([]*english_report.SearchEnglishReportItem, 0)
  172. searchResp, total, err := elasticService.SearchESEnglishReport(req.KeyWord, from, req.PageSize)
  173. if err != nil {
  174. resp.FailMsg("报告搜索失败", "报告搜索失败,Err:" + err.Error(), c)
  175. return
  176. }
  177. var tempList []*english_report.ElasticEnglishReportDetail
  178. if searchResp.Hits != nil {
  179. for _, v := range searchResp.Hits.Hits {
  180. itemJson, tmpErr := v.Source.MarshalJSON()
  181. if tmpErr != nil {
  182. resp.FailMsg("报告搜索失败 解析出错", "报告搜索失败,Err:"+tmpErr.Error(), c)
  183. return
  184. }
  185. reportItem := new(english_report.ElasticEnglishReportDetail)
  186. tmpErr = json.Unmarshal(itemJson, &reportItem)
  187. if tmpErr != nil {
  188. resp.FailMsg("报告搜索失败 解析出错", "报告搜索失败,Err:" + tmpErr.Error(), c)
  189. return
  190. }
  191. if len(v.Highlight["Title"]) > 0 {
  192. reportItem.Title = v.Highlight["Title"][0]
  193. }
  194. if reportItem.ReportId > 0 {
  195. if len(v.Highlight["BodyContent"]) > 0 {
  196. reportItem.ContentSub = v.Highlight["BodyContent"][0]
  197. }
  198. reportItem.ContentSub = "<div style=\"-webkit-line-clamp: 3;-webkit-box-orient: vertical;display: -webkit-box;overflow: hidden;text-overflow: ellipsis;\">" + reportItem.ContentSub + "</div>"
  199. }
  200. tempList = append(tempList, reportItem)
  201. }
  202. }
  203. videoIds := make([]uint, 0)
  204. videoMap := make(map[uint]*english_video.EnglishVideo)
  205. videoItem := new(english_video.EnglishVideo)
  206. if len(tempList)> 0 {
  207. for _, reportItem := range tempList {
  208. if reportItem.VideoId > 0 {
  209. videoIds = append(videoIds, reportItem.VideoId)
  210. }
  211. }
  212. }
  213. if len(videoIds) > 0 {
  214. videoList, e := videoItem.GetVideosByIds(videoIds)
  215. if e != nil {
  216. resp.FailMsg("查询线上路演列表出错", "查询线上路演列表出错,Err:" + e.Error(), c)
  217. return
  218. }
  219. for _, v := range videoList {
  220. videoMap[v.Id] = v
  221. }
  222. }
  223. if len(tempList)> 0 {
  224. for _, reportItem := range tempList {
  225. temp := new(english_report.SearchEnglishReportItem)
  226. temp.Title = reportItem.Title
  227. temp.Author = reportItem.Author
  228. temp.CreateTime = reportItem.CreateTime
  229. temp.ContentSub = reportItem.ContentSub
  230. temp.PublishTime = reportItem.PublishTime
  231. temp.ReportCode = reportItem.ReportCode
  232. if reportItem.ReportId > 0 {
  233. temp.Id = reportItem.ReportId
  234. temp.ClassifyIdFirst = reportItem.ClassifyIdFirst
  235. temp.ClassifyNameFirst = reportItem.ClassifyNameFirst
  236. temp.ClassifyIdSecond = reportItem.ClassifyIdSecond
  237. temp.ClassifyNameSecond = reportItem.ClassifyNameSecond
  238. }else if reportItem.VideoId > 0 {
  239. if videoTemp, ok := videoMap[reportItem.VideoId]; ok {
  240. temp.ReportType = 1
  241. temp.Id = videoTemp.Id
  242. temp.ClassifyIdFirst = videoTemp.ClassifyIdFirst
  243. temp.ClassifyNameFirst = videoTemp.ClassifyNameFirst
  244. temp.ClassifyIdSecond = videoTemp.ClassifyIdSecond
  245. temp.ClassifyNameSecond = videoTemp.ClassifyNameSecond
  246. temp.VideoUrl = videoTemp.VideoUrl
  247. temp.VideoCoverUrl = videoTemp.VideoCoverUrl
  248. temp.VideoSeconds = videoTemp.VideoSeconds
  249. }
  250. }
  251. reportList = append(reportList, temp)
  252. }
  253. }
  254. page.SetTotal(total)
  255. baseData := new(base.BaseData)
  256. baseData.SetPage(page)
  257. baseData.SetList(reportList)
  258. resp.OkData("搜索成功", baseData, c)
  259. }
  260. func (er *EnglishReportController) Detail(c *gin.Context) {
  261. req := new(english_report.ReportDetailReq)
  262. err := c.BindQuery(&req)
  263. if err != nil {
  264. errs, ok := err.(validator.ValidationErrors)
  265. if !ok {
  266. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  267. return
  268. }
  269. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  270. return
  271. }
  272. reportItem := new(english_report.Report)
  273. reportItem, err = reportItem.GetEnglishReportByCode(req.ReportCode)
  274. if err != nil {
  275. resp.FailMsg("该报告已删除", "报告查询失败,Err:" + err.Error(), c)
  276. return
  277. }
  278. err = reportItem.UpdatePvByReportCode(req.ReportCode)
  279. if err != nil {
  280. resp.FailMsg("更新失败", "更新失败,Err:" + err.Error(), c)
  281. return
  282. }
  283. reportItem.ContentSub = html.UnescapeString(reportItem.ContentSub)
  284. reportItem.Content = html.UnescapeString(reportItem.Content)
  285. shareEmailId := req.ShareEmail
  286. if shareEmailId == 0 {
  287. shareEmailIdStr := c.GetString("shareEmailId")
  288. shareEmailId, _ = strconv.Atoi(shareEmailIdStr)
  289. }
  290. // 记录邮箱
  291. if shareEmailId > 0 {
  292. go english_report_service.DealEmail(reportItem, req.ReportCode, shareEmailId)
  293. }
  294. reportDetail := &english_report.ReportDetail{
  295. Id: reportItem.Id,
  296. AddType: reportItem.AddType,
  297. ClassifyIdFirst: reportItem.ClassifyIdFirst,
  298. ClassifyNameFirst: reportItem.ClassifyNameFirst,
  299. ClassifyIdSecond: reportItem.ClassifyIdSecond,
  300. ClassifyNameSecond: reportItem.ClassifyNameSecond,
  301. Title: reportItem.Title,
  302. Abstract: reportItem.Abstract,
  303. Author: reportItem.Author,
  304. Overview: reportItem.Overview,
  305. Content: reportItem.Content,
  306. ContentSub: reportItem.ContentSub,
  307. Frequency: reportItem.Frequency,
  308. PublishTime: utils.TimeTransferString(utils.FormatDateTime, reportItem.PublishTime),
  309. Stage: reportItem.Stage,
  310. VideoUrl: reportItem.VideoUrl,
  311. VideoName: reportItem.VideoName,
  312. VideoPlaySeconds: reportItem.VideoPlaySeconds,
  313. VideoSize: reportItem.VideoSize,
  314. ReportCode: reportItem.ReportCode,
  315. CreateTime: utils.TimeTransferString(utils.FormatDateTime, reportItem.CreateTime),
  316. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, reportItem.ModifyTime),
  317. }
  318. baseData := new(english_report.ReportDetailResp)
  319. baseData.Report = reportDetail
  320. resp.OkData("查询成功", baseData, c)
  321. }