package english_report import ( "encoding/json" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "hongze/hongze_yb_en_api/controller/resp" "hongze/hongze_yb_en_api/global" "hongze/hongze_yb_en_api/models/base" "hongze/hongze_yb_en_api/models/business_conf" "hongze/hongze_yb_en_api/models/english_classify" "hongze/hongze_yb_en_api/models/english_report" "hongze/hongze_yb_en_api/models/english_video" "hongze/hongze_yb_en_api/services" elasticService "hongze/hongze_yb_en_api/services/elastic" english_report_service "hongze/hongze_yb_en_api/services/english_report" "hongze/hongze_yb_en_api/utils" "html" "strconv" ) type EnglishReportController struct{} func (er *EnglishReportController) List(c *gin.Context) { req := new(english_report.ReportListReq) err := c.BindQuery(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } page := new(base.Page) page.SetPageSize(req.PageSize) page.SetCurrent(req.Current) item := new(english_report.Report) enClassify := new(english_classify.Classify) condition := " state IN (2, 6)" var pars []interface{} var total int64 var tmpList []*english_report.Report list := make([]*english_report.ReportListItem, 0) if req.ClassifyIdFirst > 0 { condition += " and classify_id_first = ?" pars = append(pars, req.ClassifyIdFirst) } if req.ClassifyIdSecond > 0 { condition += " and classify_id_second = ?" pars = append(pars, req.ClassifyIdSecond) } if req.ClassifyIdRoot > 0 && req.ClassifyIdFirst == 0 && req.ClassifyIdSecond == 0 { //查询顶级分类下的所有二级分类 secondClassify, err := enClassify.GetSecondChild(req.ClassifyIdRoot) if err != nil { resp.FailMsg("获取分类失败", "获取分类信息失败,Err:"+err.Error(), c) return } var classifyIdFirstSlice []int for _, v := range secondClassify { classifyIdFirstSlice = append(classifyIdFirstSlice, v.Id) } condition += " and classify_id_first in (?)" pars = append(pars, classifyIdFirstSlice) } page.AddOrderItem(base.OrderItem{Column: "publish_time", Asc: false}) page.AddOrderItem(base.OrderItem{Column: "id", Asc: false}) total, tmpList, err = item.SelectPage(page, condition, pars) if err != nil { resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c) return } if len(tmpList) > 0 { var thirdIds []int for _, v := range tmpList { thirdIds = append(thirdIds, v.ClassifyIdSecond) } //查询分类详情 nameList, err := enClassify.GetEnglishClassifyFullNameByIds(thirdIds) if err != nil { resp.FailMsg("获取分类失败", "获取分类信息失败,Err:"+err.Error(), c) return } classifyMap := make(map[int]*english_classify.EnglishClassifyFullName) for _, v := range nameList { classifyMap[v.Id] = v } for _, v := range tmpList { classifyInfo := new(english_classify.EnglishClassifyFullName) if cv, ok := classifyMap[v.ClassifyIdSecond]; ok { classifyInfo = cv } tmp := &english_report.ReportListItem{ Id: v.Id, AddType: v.AddType, ClassifyIdFirst: v.ClassifyIdFirst, ClassifyNameFirst: v.ClassifyNameFirst, ClassifyIdSecond: v.ClassifyIdSecond, ClassifyNameSecond: v.ClassifyNameSecond, Title: v.Title, Abstract: v.Abstract, Author: v.Author, Frequency: v.Frequency, PublishTime: utils.TimeTransferString(utils.FormatDateTime, v.PublishTime), Stage: v.Stage, Overview: v.Overview, VideoUrl: v.VideoUrl, VideoName: v.VideoName, VideoPlaySeconds: v.VideoPlaySeconds, ReportCode: v.ReportCode, ShareUrl: "https://share.hzinsights.com/reportEn?code=" + v.ReportCode, CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime), ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime), ClassifyIdRoot: classifyInfo.RootId, ClassifyNameRoot: classifyInfo.RootName, } list = append(list, tmp) } } page.SetTotal(total) baseData := new(base.BaseData) baseData.SetPage(page) baseData.SetList(list) resp.OkData("获取成功", baseData, c) } func (er *EnglishReportController) Classify(c *gin.Context) { /*req := new(english_classify.ClassifyReq) err := c.BindQuery(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return }*/ item := new(english_classify.Classify) parentList, err := item.GetParent() if err != nil { resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c) return } childList, err := item.GetChild() if err != nil { resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c) return } rootMap := make(map[int][]*english_classify.ClassifyListItem, 0) secondMap := make(map[int][]*english_classify.ClassifyListItem, 0) for _, v := range childList { if v.ParentId != v.RootId { //三级分类 tmp := &english_classify.ClassifyListItem{ Id: v.Id, ClassifyName: v.ClassifyName, Sort: v.Sort, ParentId: v.ParentId, RootId: v.RootId, ClassifyLabel: v.ClassifyLabel, ShowType: v.ShowType, ClassifyType: v.ClassifyType, IsShow: v.IsShow, CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime), ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime), } secondMap[v.ParentId] = append(secondMap[v.ParentId], tmp) } } for _, v := range childList { if v.ParentId == v.RootId { //二级分类 tmp := &english_classify.ClassifyListItem{ Id: v.Id, ClassifyName: v.ClassifyName, Sort: v.Sort, ParentId: v.ParentId, RootId: v.RootId, ClassifyLabel: v.ClassifyLabel, ShowType: v.ShowType, ClassifyType: v.ClassifyType, IsShow: v.IsShow, CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime), ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime), } if child, ok := secondMap[v.Id]; ok { tmp.Child = child } else { continue } rootMap[v.ParentId] = append(rootMap[v.ParentId], tmp) } } list := make([]*english_classify.ClassifyListItem, 0) for _, v := range parentList { tmp := &english_classify.ClassifyListItem{ Id: v.Id, ClassifyName: v.ClassifyName, Sort: v.Sort, ParentId: v.ParentId, RootId: v.RootId, ClassifyLabel: v.ClassifyLabel, ShowType: v.ShowType, ClassifyType: v.ClassifyType, IsShow: v.IsShow, CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime), ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime), } if child, ok := rootMap[v.Id]; ok { tmp.Child = child } else { continue } list = append(list, tmp) } baseData := new(base.BaseOnlyData) baseData.SetList(list) resp.OkData("获取成功", baseData, c) } func (er *EnglishReportController) FilterByEs(c *gin.Context) { req := new(english_report.ReportSearchReq) err := c.BindQuery(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } page := new(base.Page) page.SetPageSize(req.PageSize) page.SetCurrent(req.Current) if req.KeyWord == "" { resp.FailMsg("请输入搜索词", "", c) return } from := (req.Current - 1) * req.PageSize reportList := make([]*english_report.SearchEnglishReportItem, 0) searchResp, total, err := elasticService.SearchESEnglishReport(req.KeyWord, from, req.PageSize) if err != nil { resp.FailMsg("报告搜索失败", "报告搜索失败,Err:"+err.Error(), c) return } var tempList []*english_report.ElasticEnglishReportDetail if searchResp.Hits != nil { for _, v := range searchResp.Hits.Hits { itemJson, tmpErr := v.Source.MarshalJSON() if tmpErr != nil { resp.FailMsg("报告搜索失败 解析出错", "报告搜索失败,Err:"+tmpErr.Error(), c) return } reportItem := new(english_report.ElasticEnglishReportDetail) tmpErr = json.Unmarshal(itemJson, &reportItem) if tmpErr != nil { resp.FailMsg("报告搜索失败 解析出错", "报告搜索失败,Err:"+tmpErr.Error(), c) return } if len(v.Highlight["Abstract"]) > 0 { reportItem.Abstract = v.Highlight["Abstract"][0] } if reportItem.ReportId > 0 { if len(v.Highlight["BodyContent"]) > 0 { reportItem.ContentSub = v.Highlight["BodyContent"][0] } reportItem.ContentSub = "
" + reportItem.ContentSub + "
" } tempList = append(tempList, reportItem) } } videoIds := make([]uint, 0) videoMap := make(map[uint]*english_video.EnglishVideo) videoItem := new(english_video.EnglishVideo) var classifyThirdIds []int if len(tempList) > 0 { for _, reportItem := range tempList { if reportItem.VideoId > 0 { videoIds = append(videoIds, reportItem.VideoId) } classifyThirdIds = append(classifyThirdIds, reportItem.ClassifyIdSecond) } } enClassify := new(english_classify.Classify) //查询分类详情 nameList, err := enClassify.GetEnglishClassifyFullNameByIds(classifyThirdIds) if err != nil { resp.FailMsg("获取分类失败", "获取分类信息失败,Err:"+err.Error(), c) return } classifyMap := make(map[int]*english_classify.EnglishClassifyFullName) for _, v := range nameList { classifyMap[v.Id] = v } if len(videoIds) > 0 { videoList, e := videoItem.GetVideosByIds(videoIds) if e != nil { resp.FailMsg("查询线上路演列表出错", "查询线上路演列表出错,Err:"+e.Error(), c) return } for _, v := range videoList { videoMap[v.Id] = v } } if len(tempList) > 0 { for _, reportItem := range tempList { temp := new(english_report.SearchEnglishReportItem) temp.Title = reportItem.Title temp.Abstract = reportItem.Abstract temp.Author = reportItem.Author temp.CreateTime = reportItem.CreateTime temp.ContentSub = reportItem.ContentSub temp.PublishTime = reportItem.PublishTime temp.ReportCode = reportItem.ReportCode classifyInfo := new(english_classify.EnglishClassifyFullName) if cv, ok := classifyMap[reportItem.ClassifyIdSecond]; ok { classifyInfo = cv } if reportItem.ReportId > 0 { temp.Id = reportItem.ReportId temp.ClassifyIdFirst = reportItem.ClassifyIdFirst temp.ClassifyNameFirst = reportItem.ClassifyNameFirst temp.ClassifyIdSecond = reportItem.ClassifyIdSecond temp.ClassifyNameSecond = reportItem.ClassifyNameSecond temp.Stage, _ = strconv.Atoi(reportItem.StageStr) temp.ClassifyIdRoot = classifyInfo.RootId temp.ClassifyNameRoot = classifyInfo.RootName } else if reportItem.VideoId > 0 { if videoTemp, ok := videoMap[reportItem.VideoId]; ok { temp.ReportType = 1 temp.Id = videoTemp.Id temp.ClassifyIdFirst = videoTemp.ClassifyIdFirst temp.ClassifyNameFirst = videoTemp.ClassifyNameFirst temp.ClassifyIdSecond = videoTemp.ClassifyIdSecond temp.ClassifyNameSecond = videoTemp.ClassifyNameSecond temp.VideoUrl = videoTemp.VideoUrl temp.VideoCoverUrl = videoTemp.VideoCoverUrl temp.VideoSeconds = videoTemp.VideoSeconds temp.ClassifyIdRoot = classifyInfo.RootId temp.ClassifyNameRoot = classifyInfo.RootName } } reportList = append(reportList, temp) } } page.SetTotal(total) baseData := new(base.BaseData) baseData.SetPage(page) baseData.SetList(reportList) resp.OkData("搜索成功", baseData, c) } func (er *EnglishReportController) Detail(c *gin.Context) { req := new(english_report.ReportDetailReq) err := c.BindQuery(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } reportItem := new(english_report.Report) reportItem, err = reportItem.GetEnglishReportByCode(req.ReportCode) if err != nil { resp.FailMsg("该报告已删除", "报告查询失败,Err:"+err.Error(), c) return } // 英文研报1.1改-研报未登录也可以查看部分报告, 登录则校验权限 var authOk bool userInfo := services.GetInfoByClaims(c) if userInfo.Id > 0 { // 正式客户, 校验权限 if userInfo.Status == 1 { ok, e := english_report_service.CheckUserReportAuthByCompanyAndClassify(int(userInfo.CompanyId), reportItem.ClassifyIdSecond) if e != nil { resp.FailMsg("获取失败", "报告权限校验失败, Err: "+e.Error(), c) return } authOk = ok } // 临时客户, 可查看所有 if userInfo.Status == 2 { authOk = true } } content, subContent := ``, `` if authOk { subContent = html.UnescapeString(reportItem.ContentSub) content = html.UnescapeString(reportItem.Content) } // 记录PV err = reportItem.UpdatePvByReportCode(req.ReportCode) if err != nil { resp.FailMsg("更新失败", "更新失败,Err:"+err.Error(), c) return } // 查询分类 classifyItem := new(english_classify.Classify) fullClassify := new(english_classify.EnglishClassifyFullName) fullClassifyList, err := classifyItem.GetEnglishClassifyFullNameByIds([]int{reportItem.ClassifyIdSecond}) if err != nil { resp.FailMsg("查询分类信息失败", "查询分类信息失败,Err:"+err.Error(), c) return } if len(fullClassifyList) > 0 { fullClassify = fullClassifyList[0] } // 记录邮箱 if userInfo.Id > 0 { go english_report_service.DealEmail(reportItem, req.ReportCode, int(userInfo.Id)) } reportDetail := &english_report.ReportDetail{ Id: reportItem.Id, AddType: reportItem.AddType, ClassifyIdFirst: reportItem.ClassifyIdFirst, ClassifyNameFirst: reportItem.ClassifyNameFirst, ClassifyIdSecond: reportItem.ClassifyIdSecond, ClassifyNameSecond: reportItem.ClassifyNameSecond, Title: reportItem.Title, Abstract: reportItem.Abstract, Author: reportItem.Author, Overview: reportItem.Overview, Content: content, ContentSub: subContent, Frequency: reportItem.Frequency, PublishTime: utils.TimeTransferString(utils.FormatDateTime, reportItem.PublishTime), Stage: reportItem.Stage, VideoUrl: reportItem.VideoUrl, VideoName: reportItem.VideoName, VideoPlaySeconds: reportItem.VideoPlaySeconds, VideoSize: reportItem.VideoSize, ReportCode: reportItem.ReportCode, CreateTime: utils.TimeTransferString(utils.FormatDateTime, reportItem.CreateTime), ModifyTime: utils.TimeTransferString(utils.FormatDateTime, reportItem.ModifyTime), KeyTakeaways: reportItem.KeyTakeaways, ClassifyIdRoot: fullClassify.RootId, ClassifyNameRoot: fullClassify.RootName, } baseData := new(english_report.ReportDetailResp) baseData.Report = reportDetail baseData.AuthOk = authOk // 配置信息 obj := business_conf.BusinessConf{} conf, e := obj.GetBusinessConf() if e != nil { resp.FailMsg("获取失败", "获取免责声明失败,Err:"+e.Error(), c) return } { // 免责声明 if conf[business_conf.BusinessConfDisclaimerEn] != "" { baseData.Disclaimer = conf[business_conf.BusinessConfDisclaimerEn] } // 报告Logo if v, ok := conf[business_conf.BusinessConfReportEnLogoShow]; ok { if v == `true` { if reportLogo, ok := conf[business_conf.BusinessConfReportLogo]; ok { baseData.ReportLogo = reportLogo } } } } // 授权token if conf[business_conf.BusinessConfIsOpenChartExpired] == `true` { tokenMap := make(map[string]string) baseData.Report.Content = english_report_service.HandleReportContent(baseData.Report.Content, "add", tokenMap) baseData.Report.ContentSub = english_report_service.HandleReportContent(baseData.Report.ContentSub, "add", tokenMap) } resp.OkData("查询成功", baseData, c) }