浏览代码

fix:兼容研报分类改版

zqbao 8 月之前
父节点
当前提交
7177eec2d5
共有 5 个文件被更改,包括 147 次插入16 次删除
  1. 82 12
      controllers/report.go
  2. 8 0
      models/base.go
  3. 1 0
      models/classify.go
  4. 43 4
      models/report_pdf.go
  5. 13 0
      services/classify.go

+ 82 - 12
controllers/report.go

@@ -164,7 +164,6 @@ func (this *ReportController) PdfList() {
 		return
 	}
 	var condition string
-	var pars []interface{}
 	switch rangeType {
 	case 1:
 		condition += ` AND DATE(publish_time)=DATE(NOW()) `
@@ -175,11 +174,42 @@ func (this *ReportController) PdfList() {
 	}
 
 	startSize := utils.StartIndex(currentIndex, pageSize)
-
+	var leafClassifyIds []int
+	var leafClassifyIdMap map[int]struct{}
+	var classifyMap map[int]*models.ClassifyView
 	if classifyId != 0 {
-		condition += ` AND classify_id_second=?`
-		pars = append(pars, classifyId)
-	} else if chartPermissionId != 0 {
+		classifyResp, err := services.GetAllClassify()
+		if err != nil {
+			br.Msg = "获取数据失败"
+			br.ErrMsg = "获取数据失败,Err:" + err.Error()
+			return
+		}
+		if classifyResp.Ret != 200 {
+			br.Msg = classifyResp.Msg
+			br.ErrMsg = classifyResp.ErrMsg
+			return
+		}
+		classifyList := classifyResp.Data
+		classifyMap = make(map[int]*models.ClassifyView)
+		isHas := false
+		for _, v := range classifyList {
+			if v.Id == classifyId && classifyId != 0 {
+				isHas = true
+			}
+			classifyMap[v.Id] = v
+		}
+		if !isHas && classifyId != 0 {
+			br.Msg = "分类不存在"
+			return
+		}
+		leafClassifyIds = getLeafClassifyIds(classifyMap, classifyId)
+		leafClassifyIdMap = make(map[int]struct{})
+		for _, v := range leafClassifyIds {
+			leafClassifyIdMap[v] = struct{}{}
+		}
+	}
+	var permissionClassifyList []int
+	if chartPermissionId != 0 {
 		resp, err := services.GetClassifyListByChartPermission(chartPermissionId)
 		if err != nil {
 			br.Msg = "获取分类失败"
@@ -200,20 +230,42 @@ func (this *ReportController) PdfList() {
 			br.Data = resp
 			return
 		}
-
-		condition += ` AND classify_id_second IN (` + utils.GetOrmReplaceHolder(len(classifyList)) + `)`
 		for _, item := range classifyList {
-			pars = append(pars, item.Id)
+			permissionClassifyList = append(permissionClassifyList, item.Id)
+		}
+	}
+	queryClassifyIds := make([]int, 0)
+	if classifyId > 0 {
+		for _, v := range permissionClassifyList {
+			if _, ok := leafClassifyIdMap[v]; ok {
+				queryClassifyIds = append(queryClassifyIds, v)
+			}
+		}
+	} else {
+		queryClassifyIds = permissionClassifyList
+	}
+	firstClassifyIds := make([]int, 0)
+	secondClassifyIds := make([]int, 0)
+	thirdClassifyIds := make([]int, 0)
+	for _, v := range queryClassifyIds {
+		switch classifyMap[v].Level {
+		case 1:
+			firstClassifyIds = append(firstClassifyIds, v)
+		case 2:
+			secondClassifyIds = append(secondClassifyIds, v)
+		case 3:
+			thirdClassifyIds = append(thirdClassifyIds, v)
 		}
 	}
-	total, err := models.GetReportPdfCountByCondition(condition, pars)
+
+	total, err := models.GetReportPdfCountByCondition(firstClassifyIds, secondClassifyIds, thirdClassifyIds, condition)
 	if err != nil {
 		br.Msg = "研报列表查询失败"
 		br.ErrMsg = "研报列表统计查询失败,系统异常,Err:" + err.Error()
 		return
 
 	}
-	reportPdfList, err := models.GetReportPdfListByCondition(condition, pars, startSize, pageSize)
+	reportPdfList, err := models.GetReportPdfListByCondition(firstClassifyIds, secondClassifyIds, thirdClassifyIds, condition, startSize, pageSize)
 	if err != nil {
 		br.Msg = "研报列表查询失败"
 		br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error()
@@ -231,6 +283,25 @@ func (this *ReportController) PdfList() {
 	br.Data = resp
 }
 
+func getLeafClassifyIds(classifyMap map[int]*models.ClassifyView, keyId int) []int {
+	var leafClassifyIds []int
+	curClassify := classifyMap[keyId]
+	if curClassify.HasChild == 0 {
+		leafClassifyIds = append(leafClassifyIds, curClassify.Id)
+		return leafClassifyIds
+	}
+	for _, v := range classifyMap {
+		if v.ParentId == curClassify.Id {
+			if v.HasChild == 0 {
+				leafClassifyIds = append(leafClassifyIds, v.Id)
+			} else {
+				leafClassifyIds = append(leafClassifyIds, getLeafClassifyIds(classifyMap, v.Id)...)
+			}
+		}
+	}
+	return leafClassifyIds
+}
+
 // @Title pdf研报详情
 // @Description pdf研报详情
 // @Param   ReportPdfId   query   int  true       "品种ID"
@@ -485,8 +556,7 @@ func (this *ReportController) RecentList() {
 		return
 	}
 	// 查询已发布的pdf
-	condition := ` AND state=1 `
-	reportPdfList, err := models.GetReportPdfListByCondition(condition, []interface{}{}, 0, 3)
+	reportPdfList, err := models.GetRecentReportPdfList(0, 3)
 	if err != nil {
 		br.Msg = "研报列表查询失败"
 		br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error()

+ 8 - 0
models/base.go

@@ -14,3 +14,11 @@ type BaseResponse struct {
 func (r *BaseResponse) Init() *BaseResponse {
 	return &BaseResponse{Ret: 403, IsSendEmail: true}
 }
+
+type BaseResponseT[T any] struct {
+	Ret     int
+	Msg     string
+	ErrMsg  string
+	ErrCode string
+	Data    T
+}

+ 1 - 0
models/classify.go

@@ -20,6 +20,7 @@ type ClassifyView struct {
 	Sort         int             `json:"-"`
 	ParentId     int             `description:"父级分类id"`
 	Level        int             `description:"分类层级"`
+	HasChild     int             `description:"是否有子分类0:下面没有子分类,1:下面有子分类"`
 	Child        []*ClassifyView `description:"子分类"`
 }
 

+ 43 - 4
models/report_pdf.go

@@ -1,6 +1,8 @@
 package models
 
 import (
+	"eta/eta_mini_api/utils"
+	"fmt"
 	"time"
 
 	"github.com/beego/beego/v2/client/orm"
@@ -16,6 +18,8 @@ type ReportPdf struct {
 	ClassifyNameFirst  string    `description:"一级分类名称"`
 	ClassifyIdSecond   int       `description:"二级分类id"`
 	ClassifyNameSecond string    `description:"二级分类名称"`
+	ClassifyIdThird    int       `description:"三级分类id"`
+	ClassifyNameThird  string    `description:"三级分类名称"`
 	Stage              int       `description:"期数"`
 	PublishTime        time.Time `description:"发布时间"`
 	ModifyTime         time.Time `description:"更新时间"`
@@ -36,6 +40,8 @@ type ReportPdfView struct {
 	ClassifyNameFirst  string `description:"一级分类名称"`
 	ClassifyIdSecond   int    `description:"二级分类id"`
 	ClassifyNameSecond string `description:"二级分类名称"`
+	ClassifyIdThird    int    `description:"三级分类id"`
+	ClassifyNameThird  string `description:"三级分类名称"`
 	Stage              int    `description:"期数"`
 	PublishTime        string `description:"发布时间"`
 	ModifyTime         string `description:"更新时间"`
@@ -48,14 +54,34 @@ type ReportPdfView struct {
 	IsCollect          bool   `description:"是否收藏"`
 }
 
-func GetReportPdfListByCondition(condition string, pars []interface{}, startSize, pageSize int) (reportPdfs []*ReportPdf, err error) {
+func GetRecentReportPdfList(startSize, pageSize int) (items []*ReportPdf, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT * FROM report_pdf WHERE state=1 ORDER BY publish_time DESC LIMIT ?,?`
+	_, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
+	return
+}
+
+func GetReportPdfListByCondition(firstClassifyIds, secondClassifyIds, thirdClassifyIds []int, condition string, startSize, pageSize int) (reportPdfs []*ReportPdf, err error) {
+	if len(firstClassifyIds) == 0 && len(secondClassifyIds) == 0 && len(thirdClassifyIds) == 0 {
+		return
+	}
 	o := orm.NewOrm()
 	sql := `SELECT * FROM report_pdf WHERE 1=1 `
+	if len(firstClassifyIds) > 0 {
+		sql += fmt.Sprintf(" OR a.classify_id_first IN (%s) ", utils.GetOrmReplaceHolder(len(firstClassifyIds)))
+	}
+	if len(secondClassifyIds) > 0 {
+		sql += fmt.Sprintf(" OR a.classify_id_second IN (%s) ", utils.GetOrmReplaceHolder(len(secondClassifyIds)))
+	}
+	if len(thirdClassifyIds) > 0 {
+		sql += fmt.Sprintf(" OR a.classify_id_third IN (%s) ", utils.GetOrmReplaceHolder(len(thirdClassifyIds)))
+	}
+
 	if condition != "" {
 		sql += condition
 	}
 	sql += ` ORDER BY publish_time DESC LIMIT ?,?`
-	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&reportPdfs)
+	_, err = o.Raw(sql, startSize, pageSize).QueryRows(&reportPdfs)
 	return
 }
 
@@ -66,13 +92,26 @@ func GetReportPdfDailyList() (reportPdfs []*ReportPdf, err error) {
 	return
 }
 
-func GetReportPdfCountByCondition(condition string, pars []interface{}) (count int, err error) {
+func GetReportPdfCountByCondition(firstClassifyIds, secondClassifyIds, thirdClassifyIds []int, condition string) (count int, err error) {
+	if len(firstClassifyIds) == 0 && len(secondClassifyIds) == 0 && len(thirdClassifyIds) == 0 {
+		return
+	}
 	o := orm.NewOrm()
 	sql := `SELECT COUNT(*) AS count FROM report_pdf WHERE 1=1 `
+
+	if len(firstClassifyIds) > 0 {
+		sql += fmt.Sprintf(" OR a.classify_id_first IN (%s) ", utils.GetOrmReplaceHolder(len(firstClassifyIds)))
+	}
+	if len(secondClassifyIds) > 0 {
+		sql += fmt.Sprintf(" OR a.classify_id_second IN (%s) ", utils.GetOrmReplaceHolder(len(secondClassifyIds)))
+	}
+	if len(thirdClassifyIds) > 0 {
+		sql += fmt.Sprintf(" OR a.classify_id_third IN (%s) ", utils.GetOrmReplaceHolder(len(thirdClassifyIds)))
+	}
 	if condition != "" {
 		sql += condition
 	}
-	err = o.Raw(sql, pars).QueryRow(&count)
+	err = o.Raw(sql, firstClassifyIds, secondClassifyIds, thirdClassifyIds).QueryRow(&count)
 	return
 }
 

+ 13 - 0
services/classify.go

@@ -33,3 +33,16 @@ func GetFirstChartPermission(classifyIds []int) (resp *models.ChartPermissionRes
 	}
 	return
 }
+
+func GetAllClassify() (resp *models.BaseResponseT[[]*models.ClassifyView], err error) {
+	url := utils.ETA_MINI_BRIDGE_URL + "/classify/list"
+	body, err := HttpGet(url)
+	if err != nil {
+		return
+	}
+	err = json.Unmarshal(body, &resp)
+	if err != nil {
+		return
+	}
+	return
+}