Browse Source

fix:分类条件提哦啊见筛选

Roc 5 months ago
parent
commit
9247c90ae4

+ 5 - 17
controllers/document_manage/document_manage_controller.go

@@ -236,22 +236,10 @@ func (this *DocumentManageController) RuiSiReportList() {
 		return
 	}
 
-	classifyIdFirstString := this.GetString("classifyIdFirstList")
-	var classifyIdFirstList []string
-	if strings.TrimSpace(classifyIdFirstString) != "" {
-		classifyIdFirstList = strings.Split(classifyIdFirstString, ",")
-	}
-
-	classifyIdSecondString := this.GetString("classifyIdSecondList")
-	var classifyIdSecondList []string
-	if strings.TrimSpace(classifyIdSecondString) != "" {
-		classifyIdSecondList = strings.Split(classifyIdSecondString, ",")
-	}
-
-	classifyIdThirdString := this.GetString("classifyIdThirdList")
-	var classifyIdThirdList []string
-	if strings.TrimSpace(classifyIdThirdString) != "" {
-		classifyIdThirdList = strings.Split(classifyIdThirdString, ",")
+	classifyIdListStr := this.GetString("ClassifyIdList")
+	var classifyIdList []string
+	if strings.TrimSpace(classifyIdListStr) != "" {
+		classifyIdList = strings.Split(classifyIdListStr, ",")
 	}
 
 	keyword := this.GetString("Keyword")
@@ -266,7 +254,7 @@ func (this *DocumentManageController) RuiSiReportList() {
 	if currentIndex <= 0 {
 		currentIndex = 1
 	}
-	RuiSiReportPage, err := document_manage_service.RuiSiReportList(classifyIdFirstList, classifyIdSecondList, classifyIdThirdList, keyword, orderField, orderType, currentIndex, pageSize)
+	RuiSiReportPage, err := document_manage_service.RuiSiReportListV2(classifyIdList, keyword, orderField, orderType, currentIndex, pageSize)
 	if err != nil {
 		br.Msg = "获取报告列表失败"
 		br.ErrMsg = "获取报告列表失败,Err:" + err.Error()

+ 18 - 0
models/classify.go

@@ -599,3 +599,21 @@ func GetClassifyListByIdList(classifyIdList []int) (items []*Classify, err error
 	_, err = o.Raw(sql, classifyIdList).QueryRows(&items)
 	return
 }
+
+// GetClassifyListByIdStrList
+// @Description: 根据指标ID列表,获取分类列表
+// @author: Roc
+// @datetime 2024-06-27 15:23:57
+// @param classifyIdList []int
+// @return items []*Classify
+// @return err error
+func GetClassifyListByIdStrList(classifyIdStrList []string) (items []*Classify, err error) {
+	num := len(classifyIdStrList)
+	if num <= 0 {
+		return
+	}
+	sql := `SELECT * FROM classify WHERE id IN (` + utils.GetOrmInReplace(num) + `) `
+	o := orm.NewOrmUsingDB("rddp")
+	_, err = o.Raw(sql, classifyIdStrList).QueryRows(&items)
+	return
+}

+ 141 - 1
models/report.go

@@ -1452,7 +1452,6 @@ func InsertMultiReport(items []*Report) (err error) {
 	return
 }
 
-
 func GetReportListByCollectCount(classifyIdFirst, classifyIdSecond, classifyIdThird []string, keyword, author string, state int) (count int, err error) {
 	o := orm.NewOrmUsingDB("rddp")
 	var params []interface{}
@@ -1685,3 +1684,144 @@ func GetReportListByCollectList(classifyIdFirst, classifyIdSecond, classifyIdThi
 	_, err = o.Raw(sql, params...).QueryRows(&items)
 	return items, err
 }
+
+func GetReportListByCollectCountV2(classifyIdFirst, classifyIdSecond, classifyIdThird []string, keyword, author string, state int) (count int, err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	var params []interface{}
+
+	// SQL 主体
+	sql := `
+		SELECT COUNT(DISTINCT t.id) AS totalCount
+		FROM (
+			SELECT b.id
+			FROM report AS b
+			WHERE 1 = 1
+	`
+
+	// 处理关键词
+	if keyword != "" {
+		sql += " AND (b.title LIKE ? OR b.admin_real_name LIKE ?)"
+		params = append(params, "%"+keyword+"%", "%"+keyword+"%")
+	}
+
+	if author != "" {
+		sql += " AND b.author = ? "
+		params = append(params, author)
+	}
+
+	if state > 0 {
+		sql += " AND b.state = ? "
+		params = append(params, state)
+	}
+
+	// 分类id判断
+	classifyIdSqlList := make([]string, 0)
+	// 处理 classifyIdFirst
+	if len(classifyIdFirst) > 0 {
+		classifyIdSqlList = append(classifyIdSqlList, " ( b.classify_id_first IN ( "+utils.GetOrmInReplace(len(classifyIdFirst))+" ) AND classify_id_second = 0 AND classify_id_third = 0 ) ")
+		for _, id := range classifyIdFirst {
+			params = append(params, id)
+		}
+	}
+	// 处理 classifyIdSecond
+	if len(classifyIdSecond) > 0 {
+		classifyIdSqlList = append(classifyIdSqlList, " ( b.classify_id_second IN ( "+utils.GetOrmInReplace(len(classifyIdSecond))+" ) AND classify_id_third = 0) ")
+		for _, id := range classifyIdSecond {
+			params = append(params, id)
+		}
+	}
+
+	// 处理 classifyIdThird
+	if len(classifyIdThird) > 0 {
+		classifyIdSqlList = append(classifyIdSqlList, " ( b.classify_id_third IN ( "+utils.GetOrmInReplace(len(classifyIdThird))+" ) ) ")
+		for _, id := range classifyIdThird {
+			params = append(params, id)
+		}
+	}
+
+	if len(classifyIdSqlList) > 0 {
+		sql += ` AND (` + strings.Join(classifyIdSqlList, " OR ") + `) `
+	}
+
+	sql += ") AS t"
+
+	// 执行 SQL 查询获取总数
+	err = o.Raw(sql, params...).QueryRow(&count)
+	return count, err
+}
+
+func GetReportListByCollectListV2(classifyIdFirst, classifyIdSecond, classifyIdThird []string, keyword, orderField, orderType string, startSize, pageSize int, author string, state int) (items []*ReportList, err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	var params []interface{}
+
+	// SQL 主体
+	sql := `
+		SELECT DISTINCT t.id, t.title, t.author, t.modify_time, t.publish_time,t.classify_id_first,t.classify_name_first,t.classify_id_second,t.classify_name_second,t.classify_id_third,t.classify_name_third
+		FROM (
+			SELECT b.id, b.title, b.author, b.modify_time, b.publish_time,b.classify_id_first,b.classify_name_first,b.classify_id_second,b.classify_name_second,b.classify_id_third,b.classify_name_third
+			FROM report AS b
+			WHERE 1 = 1
+	`
+
+	// 处理关键词
+	if keyword != "" {
+		sql += " AND (b.title LIKE ? OR b.admin_real_name LIKE ?)"
+		params = append(params, "%"+keyword+"%", "%"+keyword+"%")
+	}
+
+	if author != "" {
+		sql += " AND b.author = ? "
+		params = append(params, author)
+	}
+
+	if state > 0 {
+		sql += " AND b.state = ? "
+		params = append(params, state)
+	}
+
+	// 分类id判断
+	classifyIdSqlList := make([]string, 0)
+	// 处理 classifyIdFirst
+	if len(classifyIdFirst) > 0 {
+		classifyIdSqlList = append(classifyIdSqlList, " ( b.classify_id_first IN ( "+utils.GetOrmInReplace(len(classifyIdFirst))+" ) AND classify_id_second = 0 AND classify_id_third = 0 ) ")
+		for _, id := range classifyIdFirst {
+			params = append(params, id)
+		}
+	}
+	// 处理 classifyIdSecond
+	if len(classifyIdSecond) > 0 {
+		classifyIdSqlList = append(classifyIdSqlList, " ( b.classify_id_second IN ( "+utils.GetOrmInReplace(len(classifyIdSecond))+" ) AND classify_id_third = 0) ")
+		for _, id := range classifyIdSecond {
+			params = append(params, id)
+		}
+	}
+
+	// 处理 classifyIdThird
+	if len(classifyIdThird) > 0 {
+		classifyIdSqlList = append(classifyIdSqlList, " ( b.classify_id_third IN ( "+utils.GetOrmInReplace(len(classifyIdThird))+" ) ) ")
+		for _, id := range classifyIdThird {
+			params = append(params, id)
+		}
+	}
+
+	if len(classifyIdSqlList) > 0 {
+		sql += ` AND (` + strings.Join(classifyIdSqlList, " OR ") + `) `
+	}
+
+	sql += ") AS t "
+
+	// 排序处理
+	if orderField != "" && orderType != "" {
+		sql += " ORDER BY " + orderField + " " + orderType
+	} else {
+		sql += " ORDER BY t.modify_time DESC, t.publish_time DESC "
+	}
+
+	// 分页
+	sql += " LIMIT ?, ?"
+	params = append(params, (startSize-1)*pageSize, pageSize)
+
+	// 执行 SQL 查询
+	_, err = o.Raw(sql, params...).QueryRows(&items)
+	return items, err
+}

+ 53 - 0
services/document_manage_service/document_manage_service.go

@@ -9,6 +9,7 @@ import (
 	"github.com/beego/beego/v2/core/logs"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"html"
+	"strconv"
 )
 
 func DocumentClassifyList(userId int) ([]models.ClassifyVO, error) {
@@ -416,3 +417,55 @@ func DocumentDelete(outsideReportId int) error {
 	}
 	return nil
 }
+
+func RuiSiReportListV2(classifyIdList []string, keyword, orderField, orderType string, startSize, pageSize int) (*models.ReportListResp, error) {
+	logs.Info("RuiSiReportList")
+
+	allClassifyList, err := models.GetClassifyListByIdStrList(classifyIdList)
+	if err != nil {
+		return nil, err
+	}
+	classifyIdFirst := make([]string, 0)
+	classifyIdSecond := make([]string, 0)
+	classifyIdThird := make([]string, 0)
+	for _, v := range allClassifyList {
+		switch v.Level {
+		case 1:
+			classifyIdFirst = append(classifyIdFirst, strconv.Itoa(v.Id))
+		case 2:
+			classifyIdSecond = append(classifyIdSecond, strconv.Itoa(v.Id))
+		case 3:
+			classifyIdThird = append(classifyIdThird, strconv.Itoa(v.Id))
+		}
+	}
+	// 作者为 全球市场战略研究中心 PCI Research
+	author := "全球市场战略研究中心 PCI Research"
+	// 已发布的报告
+	state := 2
+
+	count, err := models.GetReportListByCollectCountV2(classifyIdFirst, classifyIdSecond, classifyIdThird, keyword, author, state)
+	if err != nil {
+		return nil, err
+	}
+	reportPage := models.ReportListResp{}
+
+	page := paging.GetPaging(startSize, pageSize, count)
+	if count <= 0 {
+		reportPage.Paging = page
+		return &reportPage, nil
+	}
+
+	reportList, err := models.GetReportListByCollectListV2(classifyIdFirst, classifyIdSecond, classifyIdThird, keyword, orderField, orderType, startSize, pageSize, author, state)
+	if err != nil {
+		return nil, err
+	}
+
+	for _, report := range reportList {
+		report.ModifyTime = report.ModifyTime.UTC()
+	}
+
+	reportPage.Paging = page
+	reportPage.List = reportList
+
+	return &reportPage, nil
+}