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 = "