Browse Source

优化 最新周报查询

xiexiaoyuan 2 years ago
parent
commit
0140a23130

+ 9 - 0
models/tables/rddp/report/query.go

@@ -108,6 +108,15 @@ func GetListCountByClassifyIdFirst(classifyIdFirst int) (total int64, err error)
 	return
 }
 
+// GetByReportIds 根据ids获取报告
+func GetByReportIds(ids []int) (item *Report, err error) {
+	err = global.MYSQL["rddp"].Where("id in ? and state = 2", ids).Order("publish_time desc, id desc").First(&item).Error
+	if err == utils.ErrNoRow {
+		err = nil
+	}
+	return
+}
+
 // GetByReportId 根据id获取报告
 func GetByReportId(id int) (item *Report, err error) {
 	err = global.MYSQL["rddp"].Where("id = ? and state = 2", id).First(&item).Error

+ 5 - 11
models/tables/rddp/report_chapter/query.go

@@ -5,16 +5,9 @@ import (
 	"hongze/hongze_yb/utils"
 )
 
-func GetLatestChapterByTypeIdsAndClass(typeIds []int, classifyNameFirst string) (reportChapter *ReportChapter, err error) {
+func GetLatestChaptersByTypeIdsAndClass(typeIds []int, classifyNameFirst string) (list []*ReportChapter, err error) {
 	sql := `SELECT
-	report_id,	
-	max( publish_time ) as publish_time,
-	classify_id_first,
-	classify_name_first,
-	title,
-	type_id,
-	type_name,
-	report_chapter_id
+	DISTINCT report_id
 FROM
 	report_chapter
 WHERE
@@ -23,8 +16,9 @@ WHERE
 	AND is_edit = 1
 	AND report_type = ?
 ORDER BY
-	publish_time desc`
-	err = global.MYSQL["rddp"].Model(ReportChapter{}).Raw(sql, typeIds, classifyNameFirst).First(&reportChapter).Error
+	publish_time desc, report_chapter_id desc limit 30
+`
+	err = global.MYSQL["rddp"].Model(ReportChapter{}).Raw(sql, typeIds, classifyNameFirst).Scan(&list).Error
 	return
 }
 

+ 5 - 4
services/report/report_chapter.go

@@ -27,14 +27,15 @@ func GetReportTypeIdsByPermissionIds(permissionIds []int) (ids []int, err error)
 // GetLatestWeek 获取有权限的最新的周报
 func GetLatestWeek(permissionIds []int, typeIds []int) (reportInfo *report.Report, err error) {
 	newTypeIds, err := GetWeekTypeIdsByPermissionIds(permissionIds, typeIds)
-	chapter, err := report_chapter.GetLatestChapterByTypeIdsAndClass(newTypeIds,"week")
+	chapters, err := report_chapter.GetLatestChaptersByTypeIdsAndClass(newTypeIds,"week")
 	if err != nil {
 		return
 	}
-	if chapter.ReportId == 0 {
-		return
+	var reportIds []int
+	for _, v := range chapters {
+		reportIds = append(reportIds, v.ReportId)
 	}
-	reportInfo, err = report.GetByReportId(chapter.ReportId)
+	reportInfo, err = report.GetByReportIds(reportIds)
 	return
 }