Browse Source

已购模块,权限回撤

xiexiaoyuan 3 years ago
parent
commit
4c44cd57ad
3 changed files with 44 additions and 132 deletions
  1. 14 36
      models/tables/rddp/report/query.go
  2. 26 79
      services/report/report.go
  3. 4 17
      services/report/report_view_log.go

+ 14 - 36
models/tables/rddp/report/query.go

@@ -5,8 +5,7 @@ import (
 	"hongze/hongze_yb/utils"
 )
 
-// GetLatestClassReportsByClassifyIdSeconds 根据用户已购买的分类权限查询个分类最新的报告
-func GetLatestClassReportsByClassifyIdSeconds(classifyIdSeconds []int) (reportList []*Report, err error) {
+func GetLatestClassReportsByIDs(reportIDs []int) (reportList []*Report, err error) {
 	sql := `SELECT
 	max( publish_time ) as publish_time,
 	classify_id_first,
@@ -20,18 +19,26 @@ FROM
 	report 
 WHERE
 	state = 2 
-	AND classify_id_second IN ?
+	AND id IN ?
 GROUP BY
 	classify_id_first`
-	err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds).Scan(&reportList).Error
+	err = global.MYSQL["rddp"].Raw(sql, reportIDs).Scan(&reportList).Error
 	return
 }
 
-// GetReportsByClassifyIdSecondsAndDate 根据时间和报告分类筛选出合适的记录
-func GetReportsByClassifyIdSecondsAndDate( classifyIdSeconds []int, publishTime string) (reportList []*Report, err error) {
+// GetReportsByIDsAndDate 根据时间和报告ID筛选出合适的记录
+func GetReportsByIDsAndDate( ids []int, publishTime string) (reportList []*Report, err error) {
 	err = global.MYSQL["rddp"].Model(Report{}).
 		Select("id, classify_name_first").
-		Where("classify_id_second in (?) and state = 2 and publish_time > ? ", classifyIdSeconds, publishTime).Scan(&reportList).Error
+		Where("id in (?) and state = 2 and publish_time > ? ", ids, publishTime).Scan(&reportList).Error
+	return
+}
+
+// GetReportsByIDsAndDateAndClass 根据时间和报告ID筛选出合适的记录
+func GetReportsByIDsAndDateAndClass( ids []int, classifyNameFirst string, publishTime string) (reportList []*Report, err error) {
+	err = global.MYSQL["rddp"].Model(Report{}).
+		Select("id, classify_name_first").
+		Where("id in (?) and state = 2 and classify_name_first = ? and publish_time > ? ", ids, classifyNameFirst, publishTime).Scan(&reportList).Error
 	return
 }
 
@@ -56,27 +63,6 @@ func GetListCountByIDsAndClassifyIdFirst( ids []int, classifyIdFirst int) (total
 	return
 }
 
-// GetListByClassifyIdSecondsAndClassifyIdFirst 分页查询
-func GetListByClassifyIdSecondsAndClassifyIdFirst( classifyIdSeconds []int, classifyIdFirst int,  offset , limit int) (reportList []*Report, err error) {
-	err = global.MYSQL["rddp"].Model(Report{}).
-		Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
-		Where("classify_id_second in (?) and classify_id_first=? and state = 2 ", classifyIdSeconds, classifyIdFirst).
-		Order("publish_time desc, id desc").
-		Offset(offset).
-		Limit(limit).
-		Scan(&reportList).Error
-	return
-}
-
-// GetListCountByClassifyIdSecondsAndClassifyIdFirst
-func GetListCountByClassifyIdSecondsAndClassifyIdFirst( classifyIdSeconds []int, classifyIdFirst int) (total int64, err error) {
-	err = global.MYSQL["rddp"].Model(Report{}).
-		Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
-		Where("classify_id_second in (?) and classify_id_first=? and state = 2 ", classifyIdSeconds, classifyIdFirst).
-		Count(&total).Error
-	return
-}
-
 // GetListByClassifyIdFirst 按照类型分页查询
 func GetListByClassifyIdFirst(classifyIdFirst int,  offset , limit int) (reportList []*Report, err error) {
 	err = global.MYSQL["rddp"].Model(Report{}).
@@ -149,7 +135,6 @@ func GetListByClassifyIdSecond(classifyIdSecond int,  offset , limit int) (repor
 }
 
 
-
 // GetListCountByClassifyIdSecond 按照二级分类总条数
 func GetListCountByClassifyIdSecond(classifyIdSecond int) (total int64, err error) {
 	err = global.MYSQL["rddp"].Model(Report{}).
@@ -161,13 +146,6 @@ func GetListCountByClassifyIdSecond(classifyIdSecond int) (total int64, err erro
 	return
 }
 
-// GetReportListByCondition 获取报告列表
-func GetReportListByCondition(condition string, pars []interface{}) (list []*Report, err error) {
-	err = global.MYSQL["rddp"].Select("id").Model(Report{}).Where(condition, pars...).
-		Scan(&list).Error
-	return
-}
-
 
 // GetReportList 获取报告列表
 func GetReportList(condition string, pars []interface{}, offset , limit int) (list []*Report, err error) {

+ 26 - 79
services/report/report.go

@@ -24,7 +24,11 @@ func GetLatestClassReport(permissionIds []int, userId uint64) (purchaseList []*p
 			global.LOG.Critical(fmt.Sprintf("GetLatestClassReport: userId=%d, err:%s, errMsg:%s", userId, err.Error(), errMsg))
 		}
 	}()
-
+	//获取所有和权限绑定的报告
+	reportIds, err := GetReportIdsByPermissionIds(permissionIds)
+	if err != nil {
+		return
+	}
 	//获取最新的晨报
 	dayReport, err := report.GetLatestDay()
 	if err != nil {
@@ -46,41 +50,13 @@ func GetLatestClassReport(permissionIds []int, userId uint64) (purchaseList []*p
 	}
 
 	var reports []*report.Report
-	// 有权限的二级分类
-	var classifyIdSeconds []int
 	// 获取晨报和周报以外的其他报告
-	{
-		//获取有权限的二级分类
-		var classifyNameSeconds []string
-		chartPermissions, tErr := chart_permission_search_key_word_mapping.GetChartPermissionByFrom("rddp")
-		if tErr != nil {
-			errMsg = tErr.Error()
-			err = errors.New("分类权限查询出错")
-			return
-		}
-		if len(chartPermissions) > 0 {
-			for _, v := range chartPermissions {
-				for _, myPerId := range permissionIds {
-					if v.ChartPermissionId == myPerId {
-						classifyNameSeconds = append(classifyNameSeconds, v.KeyWord)
-						break
-					}
-				}
-			}
-		}
-		//获取所有二级分类的id
-		classifyIdSeconds, tErr = classify.GetIdsByClassifyName(classifyNameSeconds)
-		if tErr != nil {
-			errMsg = tErr.Error()
-			err = errors.New("二级分类查询出错")
-			return
-		}
-		reports, err = report.GetLatestClassReportsByClassifyIdSeconds(classifyIdSeconds)
-		if err != nil {
-			return
-		}
+	reports, err = report.GetLatestClassReportsByIDs(reportIds)
+	if err != nil {
+		return
 	}
 
+
 	if dayReport != nil {
 		reports = append(reports, dayReport)
 
@@ -90,7 +66,7 @@ func GetLatestClassReport(permissionIds []int, userId uint64) (purchaseList []*p
 	}
 
 	//获取未读数
-	unReadItem, tErr := GetUnRead(classifyIdSeconds, userId)
+	unReadItem, tErr := GetUnRead(reportIds, userId)
 	if tErr != nil {
 		err = tErr
 		return
@@ -139,7 +115,7 @@ func GetPurchaseDetail(permissionIds []int, userId uint64, classifyIdFirst int,
 		}
 	}()
 	var reports []*report.Report
-	var weekReportIds []int
+	var reportIds []int
 
 	classifyInfo, err := classify.GetByClassifyId(classifyIdFirst)
 	if err != nil {
@@ -155,13 +131,15 @@ func GetPurchaseDetail(permissionIds []int, userId uint64, classifyIdFirst int,
 	classifyNameFirst := classifyInfo.ClassifyName
 	offset := (pageIndex - 1) * pageSize
 	var total int64
-	var classifyIdSeconds []int
 	if classifyNameFirst == "晨报" {
 		//分类获取
 		reports, err = report.GetListByClassifyIdFirst(classifyIdFirst, offset, pageSize)
 		if err != nil {
 			return
 		}
+		for _, v := range reports {
+			reportIds = append(reportIds, v.Id)
+		}
 		total, err = report.GetListCountByClassifyIdFirst(classifyIdFirst)
 		if err != nil {
 			errMsg = err.Error()
@@ -174,57 +152,26 @@ func GetPurchaseDetail(permissionIds []int, userId uint64, classifyIdFirst int,
 			if tErr != nil {
 				return
 			}
-			weekReportIds, err = report_chapter.GetReportIdsByTypeIdsAndClass(newTypeIds, classifyNameFirst)
+			reportIds, err = report_chapter.GetReportIdsByTypeIdsAndClass(newTypeIds, classifyNameFirst)
+		} else {
+			reportIds, err = GetReportIdsByPermissionIds(permissionIds)
+			if err != nil {
+				return
+			}
+		}
+
+		if len(reportIds) > 0 {
 			//分类获取
-			reports, err = report.GetListByIDsAndClassifyIdFirst(weekReportIds, classifyIdFirst, offset, pageSize)
+			reports, err = report.GetListByIDsAndClassifyIdFirst(reportIds, classifyIdFirst, offset, pageSize)
 			if err != nil {
 				return
 			}
-			total, err = report.GetListCountByIDsAndClassifyIdFirst(weekReportIds, classifyIdFirst)
+			total, err = report.GetListCountByIDsAndClassifyIdFirst(reportIds, classifyIdFirst)
 			if err != nil {
 				errMsg = err.Error()
 				err = errors.New("查询报告总数出错")
 				return
 			}
-		} else {
-			//获取有权限的二级分类
-			var classifyNameSeconds []string
-			chartPermissions, tErr := chart_permission_search_key_word_mapping.GetChartPermissionByFrom("rddp")
-			if tErr != nil {
-				errMsg = tErr.Error()
-				err = errors.New("分类权限查询出错")
-				return
-			}
-			if len(chartPermissions) > 0 {
-				for _, v := range chartPermissions {
-					for _, myPerId := range permissionIds {
-						if v.ChartPermissionId == myPerId {
-							classifyNameSeconds = append(classifyNameSeconds, v.KeyWord)
-							break
-						}
-					}
-				}
-			}
-			//获取所有二级分类的id
-			classifyIdSeconds, tErr = classify.GetIdsByClassifyNameAndParentId(classifyNameSeconds, classifyIdFirst)
-			if tErr != nil {
-				errMsg = tErr.Error()
-				err = errors.New("二级分类查询出错")
-				return
-			}
-			if len(classifyIdSeconds) > 0 {
-				//分类获取
-				reports, err = report.GetListByClassifyIdSecondsAndClassifyIdFirst(classifyIdSeconds, classifyIdFirst, offset, pageSize)
-				if err != nil {
-					return
-				}
-				total, err = report.GetListCountByClassifyIdSecondsAndClassifyIdFirst(classifyIdSeconds, classifyIdFirst)
-				if err != nil {
-					errMsg = err.Error()
-					err = errors.New("查询报告总数出错")
-					return
-				}
-			}
 		}
 	}
 	var list []*purchase.Detail
@@ -259,7 +206,7 @@ func GetPurchaseDetail(permissionIds []int, userId uint64, classifyIdFirst int,
 	}
 	ret.List = list
 	ret.Paging = response.GetPaging(pageIndex, pageSize, int(total))
-	_, tErr := BatchInsertReportView(weekReportIds, userId, classifyNameFirst, classifyIdSeconds)
+	_, tErr := BatchInsertReportView(reportIds, userId, classifyNameFirst)
 	if tErr != nil {
 		err = tErr
 		return

+ 4 - 17
services/report/report_view_log.go

@@ -8,9 +8,9 @@ import (
 
 
 // GetUnRead 获取未读数
-func GetUnRead(classifyIdSeconds []int, userId uint64) (unReadItem map[string]int, err error) {
+func GetUnRead(reportIds []int, userId uint64) (unReadItem map[string]int, err error) {
 	firstDay := "2022-01-01"
-	newReports, err := report.GetReportsByClassifyIdSecondsAndDate(classifyIdSeconds, firstDay)
+	newReports, err := report.GetReportsByIDsAndDate(reportIds, firstDay)
 	if err != nil {
 		return
 	}
@@ -50,22 +50,9 @@ func GetUnRead(classifyIdSeconds []int, userId uint64) (unReadItem map[string]in
 }
 
 // BatchInsertReportView 批量新增报告已读记录
-func BatchInsertReportView(weekReportIds []int, userId uint64, classifyNameFirst string, classifyIdSeconds []int) (num int, err error)  {
+func BatchInsertReportView(reportIds []int, userId uint64, classifyNameFirst string) (num int, err error)  {
 	firstDay := "2022-01-01"
-	var newReports []*report.Report
-	pars := make([]interface{}, 0)
-
-	condition := "state = 2 and classify_name_first = ? and publish_time > ? "
-	pars = append(pars, classifyNameFirst, firstDay)
-	if classifyNameFirst == "晨报" {
-	} else if classifyNameFirst == "周报" {
-		condition += " and id in (?)"
-		pars = append(pars, weekReportIds)
-	} else {
-		condition += " and classify_id_second in (?)"
-		pars = append(pars, classifyIdSeconds)
-	}
-	newReports, err = report.GetReportListByCondition(condition, pars)
+	newReports, err := report.GetReportsByIDsAndDateAndClass(reportIds, classifyNameFirst, firstDay)
 	if err != nil {
 		return
 	}