kobe6258 4 сар өмнө
parent
commit
522bf9c085

+ 24 - 2
common/component/es/es.go

@@ -32,8 +32,10 @@ type ESClient struct {
 type SearchType string
 
 const (
-	MatchAll                   = "match_all"
-	Match                      = "match"
+	MatchAll = "match_all"
+	Match    = "match"
+
+	CountWithDocIds            = "count_with_doc_ids"
 	Range                      = "range"
 	MatchAllByCondition        = "match_all_by_condition"
 	RangeByCondition           = "range_by_condition"
@@ -243,6 +245,26 @@ func (req *ESQueryRequest) parseJsonQuery() (queryMap map[string]interface{}) {
 			},
 		}
 		return
+	case CountWithDocIds:
+		queryMap = map[string]interface{}{
+			"query": map[string]interface{}{
+				"bool": map[string]interface{}{
+					"must": []map[string]interface{}{
+						{
+							"match": map[string]interface{}{
+								req.Column: req.Key,
+							},
+						},
+					},
+					"filter": map[string]interface{}{
+						"terms": map[string]interface{}{
+							"_id": req.DocIds,
+						},
+					},
+				},
+			},
+		}
+		return
 	case Range:
 		queryMap = map[string]interface{}{
 			"query": map[string]interface{}{

+ 3 - 3
controllers/report/report_controller.go

@@ -34,10 +34,9 @@ func (r *ReportController) Search(key string) {
 			PageSize: r.PageInfo.PageSize,
 		}
 		//获取当前可以被搜索的报告原始ID
-		var orgIds map[string][]int
 		//先要限制查询的id范围
-		pageRes.Total, pageRes.LatestId, orgIds = report.RangeSearch(isLogin(detailType), userInfo.Id)
-		reportIds, err := report.GetReportByIdListByOrgIds(orgIds)
+		var reportIds []int
+		pageRes.Total, pageRes.LatestId, reportIds, err = report.RangeSearch(key, isLogin(detailType), userInfo.Id)
 		if err != nil {
 			logger.Error("获取报告原始ID列表失败:%v", err)
 			r.FailedResult("分页搜索报告列表失败", result)
@@ -51,6 +50,7 @@ func (r *ReportController) Search(key string) {
 			r.SuccessResult("分页搜索报告列表成功", reports, result)
 			return
 		}
+
 		if r.PageInfo.LatestId == 0 {
 			//pageRes.Total, pageRes.LatestId = report.SearchMaxReportId(key)
 			r.PageInfo.LatestId = pageRes.LatestId

+ 35 - 0
domian/report/report_service.go

@@ -228,6 +228,37 @@ func SearchMaxReportId(key string) (total int64, reportId int64) {
 	}
 	return
 }
+func SearchMaxReportIdWithRange(key string, reportIds []int) (total int64) {
+	sort := []string{"reportId:desc"}
+	var docIds []string
+	for _, reportId := range reportIds {
+		docIds = append(docIds, strconv.Itoa(reportId))
+	}
+	request := CountByDocId(key, sort, docIds)
+	//同步es
+	re, err := elastic().Count(request)
+	if err != nil {
+		logger.Error("es搜索异常:%v", err)
+	}
+	count := re.Count
+	total = int64(count)
+	if total > 0 {
+		request = match(key, 0, count, sort)
+		re, err = elastic().Search(request)
+		if err != nil {
+			logger.Error("es搜索异常:%v", err)
+		}
+		hits := elastic().GetSource(re.Hits)
+		data := hits[0].Source
+		report := ReportDTO{}
+		err = json.Unmarshal(data, &report)
+		if err != nil {
+			logger.Error("获取当前最大研报id失败:%v", err)
+			return
+		}
+	}
+	return
+}
 
 func SearchReportList(key string, ids []int, from int, size int, max int64) (reports []ReportDTO, err error) {
 	//同步es
@@ -914,3 +945,7 @@ func matchRangeByDocId(key string, from int, to int, max int64, sorts []string,
 	req := new(es.ESQueryRequest)
 	return req.CreateESQueryRequest(htConfig.GetReportIndex(), ESColumn, key, from, to, sorts, es.RangeByConditionWithDocIds).Range(0, max, ESRangeColumn).ByCondition("status", "PUBLISH").WithDocs(docIds)
 }
+func CountByDocId(key string, sorts []string, docIds []string) (request *es.ESQueryRequest) {
+	req := new(es.ESQueryRequest)
+	return req.CreateESQueryRequest(htConfig.GetReportIndex(), ESColumn, key, 0, 1, sorts, es.CountWithDocIds).WithDocs(docIds)
+}

+ 12 - 228
service/report/report_service.go

@@ -263,69 +263,6 @@ func getHTReportDetail(report *reportService.ReportDTO) (url string, err error)
 }
 func GetTotalPageCountByPermissionIds(permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids map[string][]int) {
 	return getCount(permissionIds, isLogin, userId)
-	//var err error
-	////登录了需要校验风险等级,如果风险等级没做或者过期直接返回空,做了就筛选风险等级
-	//if isLogin {
-	//	userProfile, userErr := user.GetUserProfile(userId)
-	//	if userErr != nil {
-	//		if errors.Is(userErr, gorm.ErrRecordNotFound) {
-	//			err = exception.New(exception.TemplateUserNotFound)
-	//		} else {
-	//			err = exception.New(exception.TemplateUserFoundFailed)
-	//		}
-	//		logger.Error("分页查询报告列表失败:%v", err)
-	//		return
-	//	}
-	//	//获取产品风险等级
-	//	if userProfile.RiskLevel == user.RiskUnTest {
-	//		logger.Error("客户未做风险等级测评,mobile:%d", userProfile.Mobile)
-	//		return
-	//	}
-	//	if userProfile.RiskLevelStatus == user.RiskExpired {
-	//		logger.Error("客户风险等级已过期,mobile:%d", userProfile.Mobile)
-	//		return
-	//	}
-	//	mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
-	//	if mappingErr != nil {
-	//		logger.Error("查询产品风险等级映射失败:%v", mappingErr)
-	//		return
-	//	}
-	//	var permissionList []permissionService.PermissionDTO
-	//	if len(permissionIds) == 0 {
-	//		//获取所有设置风险等级的品种
-	//		permissionList, err = permissionService.GetPermissionListWithRisk()
-	//	} else {
-	//		//更具id过滤设置了风险等级的品种
-	//		permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
-	//	}
-	//	permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
-	//	if len(permissionList) == 0 {
-	//		return
-	//	}
-	//	var filterPermissionIds []int
-	//	for _, permission := range permissionList {
-	//		filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
-	//	}
-	//	return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds, mapping.ProductRiskLevel)
-	//} else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
-	//	var permissionList []permissionService.PermissionDTO
-	//	if len(permissionIds) == 0 {
-	//		//获取所有设置风险等级的品种
-	//		permissionList, err = permissionService.GetPermissionListWithRisk()
-	//	} else {
-	//		//更具id过滤设置了风险等级的品种
-	//		permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
-	//	}
-	//	if err != nil {
-	//		logger.Error("根据ID查询品种列表失败:%v", err)
-	//	}
-	//	var filterPermissionIds []int
-	//	for _, permission := range permissionList {
-	//		filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
-	//	}
-	//	//查询品种
-	//	return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds, "")
-	//}
 }
 func filterPermissionsByRisk(permissionList []permissionService.PermissionDTO, riskLevel string) (resultList []permissionService.PermissionDTO) {
 	if riskLevel != "" {
@@ -369,66 +306,6 @@ func SearchReportList(key string, Ids []int, pageInfo page.PageInfo, isLogin boo
 	var reports []reportService.ReportDTO
 	reports, err = reportService.SearchReportList(key, Ids, offset, pageInfo.PageSize, pageInfo.LatestId)
 	list, err = dealReportInfo(reports, isLogin, userId)
-	//var wg sync.WaitGroup
-	//wg.Add(len(reports))
-	//for i := 0; i < len(reports); i++ {
-	//	go func(report *reportService.ReportDTO) {
-	//		defer wg.Done()
-	//		report.Login = isLogin
-	//		report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
-	//		permissions := getReportSecondPermissions(report.OrgId, report.Source)
-	//		if len(permissions) == 0 {
-	//			return
-	//		}
-	//		riskNum := getHighestRiskLevel(permissions)
-	//		report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
-	//		var src string
-	//		src, err = mediaService.GetImageSrc(report.CoverSrc)
-	//		if err != nil {
-	//			logger.Error("获取图片地址失败:%v", err)
-	//			src = ""
-	//		} else {
-	//			report.CoverUrl = src
-	//		}
-	//		//下查询产品信息
-	//		product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
-	//		if pdErr != nil {
-	//			if errors.Is(pdErr, gorm.ErrRecordNotFound) {
-	//				report.Price = defaultProductPrice
-	//				report.IsFree = true
-	//				report.IsSubscribe = false
-	//			} else {
-	//				report.Price = defaultProductPrice
-	//				report.IsFree = false
-	//				report.IsSubscribe = false
-	//			}
-	//		} else {
-	//			report.Price = product.Price.String()
-	//			report.IsFree = false
-	//			if isLogin {
-	//				subscribe, subscribeErr := userService.GetUserSubscribe(product.Id, userId)
-	//				if subscribeErr != nil {
-	//					report.IsSubscribe = false
-	//				} else {
-	//					report.IsSubscribe = subscribe.Status == userDao.SubscribeValid
-	//				}
-	//			}
-	//			pdRiskNum, parseErr := parseRiskLevel(product.RiskLevel)
-	//			if parseErr != nil {
-	//				return
-	//			}
-	//			rpRiskNum, parseErr := parseRiskLevel(report.RiskLevel)
-	//			if parseErr != nil {
-	//				return
-	//			}
-	//			if rpRiskNum <= pdRiskNum {
-	//				report.RiskLevel = product.RiskLevel
-	//			}
-	//		}
-	//		list = append(list, *report)
-	//	}(&reports[i])
-	//}
-	//wg.Wait()
 	if err != nil {
 		err = exception.New(exception.SearchReportPageFailed)
 	}
@@ -436,47 +313,18 @@ func SearchReportList(key string, Ids []int, pageInfo page.PageInfo, isLogin boo
 }
 func RangeSearchByAnalyst(analystName string, userId int) (total int64, latestId int64, ids []int) {
 	return getCountByAnalyst(nil, true, userId, analystName)
-	////登录了需要校验风险等级,如果风险等级没做或者过期直接返回空,做了就筛选风险等级
-	//userProfile, userErr := user.GetUserProfile(userId)
-	//if userErr != nil {
-	//	if errors.Is(userErr, gorm.ErrRecordNotFound) {
-	//		err = exception.New(exception.TemplateUserNotFound)
-	//	} else {
-	//		err = exception.New(exception.TemplateUserFoundFailed)
-	//	}
-	//	logger.Error("分页查询报告列表失败:%v", err)
-	//	return
-	//}
-	////获取产品风险等级
-	//if userProfile.RiskLevel == user.RiskUnTest {
-	//	logger.Error("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
-	//	return
-	//}
-	//if userProfile.RiskLevelStatus == user.RiskExpired {
-	//	logger.Error("客户风险等级已过期,mobile:%v", userProfile.Mobile)
-	//	return
-	//}
-	//mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
-	//if mappingErr != nil {
-	//	logger.Error("查询产品风险等级映射失败:%v", mappingErr)
-	//	return
-	//}
-	//var permissionList []permissionService.PermissionDTO
-	////获取所有设置风险等级的品种
-	//permissionList, err = permissionService.GetPermissionListWithRisk()
-	//permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
-	//if len(permissionList) == 0 {
-	//	return
-	//}
-	//var filterPermissionIds []int
-	//for _, permission := range permissionList {
-	//	filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
-	//}
-
-	//return reportService.GetTotalPageCountByAnalyst(analystName, filterPermissionIds)
 }
-func RangeSearch(isLogin bool, userId int) (total int64, latestId int64, orgIds map[string][]int) {
-	return getCount(nil, isLogin, userId)
+func RangeSearch(key string, isLogin bool, userId int) (total int64, latestId int64, reportIds []int, err error) {
+	var orgIds map[string][]int
+	_, latestId, orgIds = getCount(nil, isLogin, userId)
+	reportIds, err = GetReportByIdListByOrgIds(orgIds)
+	if err != nil {
+		logger.Error("获取报告ID列表失败:%v", err)
+		err = exception.NewWithException(exception.GetReportSearchRangeFailed, err.Error())
+		return
+	}
+	total = reportService.SearchMaxReportIdWithRange(key, reportIds)
+	return
 }
 func dealReportInfo(list []reportService.ReportDTO, isLogin bool, userId int) (resultList []reportService.ReportDTO, err error) {
 	var wg sync.WaitGroup
@@ -547,76 +395,12 @@ func GetReportPage(pageInfo page.PageInfo, orgIds map[string][]int, searchAll bo
 	var list []reportService.ReportDTO
 	list, err = reportService.GetReportPageByOrgIds(pageInfo, orgIds, searchAll)
 	reports, err = dealReportInfo(list, isLogin, userId)
-	////并发获取研报的标签
-	//var wg sync.WaitGroup
-	//wg.Add(len(list))
-	//for i := 0; i < len(list); i++ {
-	//	go func(report *reportService.ReportDTO) {
-	//		defer wg.Done()
-	//		report.Login = isLogin
-	//		report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
-	//		permissions := getReportSecondPermissions(report.OrgId, report.Source)
-	//		if len(permissions) == 0 {
-	//			return
-	//		}
-	//		riskNum := getHighestRiskLevel(permissions)
-	//		report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
-	//		var src string
-	//		src, err = mediaService.GetImageSrc(report.CoverSrc)
-	//		if err != nil {
-	//			logger.Error("获取图片地址失败:%v", err)
-	//			src = ""
-	//		} else {
-	//			report.CoverUrl = src
-	//		}
-	//		//下查询产品信息
-	//		product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
-	//		if pdErr != nil {
-	//			if errors.Is(pdErr, gorm.ErrRecordNotFound) {
-	//				report.Price = defaultProductPrice
-	//				report.IsFree = true
-	//				report.IsSubscribe = false
-	//			} else {
-	//				report.Price = defaultProductPrice
-	//				report.IsFree = false
-	//				report.IsSubscribe = false
-	//			}
-	//		} else {
-	//			report.Price = product.Price.String()
-	//			report.IsFree = false
-	//			if isLogin {
-	//				subscribe, subscribeErr := userService.GetUserSubscribe(product.Id, userId)
-	//				if subscribeErr != nil {
-	//					report.IsSubscribe = false
-	//				} else {
-	//					report.IsSubscribe = subscribe.Status == userDao.SubscribeValid
-	//				}
-	//			}
-	//			pdRiskNum, parseErr := parseRiskLevel(product.RiskLevel)
-	//			if parseErr != nil {
-	//				return
-	//			}
-	//			rpRiskNum, parseErr := parseRiskLevel(report.RiskLevel)
-	//			if parseErr != nil {
-	//				return
-	//			}
-	//			if rpRiskNum <= pdRiskNum {
-	//				report.RiskLevel = product.RiskLevel
-	//			}
-	//		}
-	//		reports = append(reports, *report)
-	//	}(&list[i])
-	//}
-	//wg.Wait()
+
 	if err != nil {
 		err = exception.New(exception.QueryReportPageFailed)
 	}
 	return
 }
-
-//	func GetTotalPageCountByAnalyst(analyst string) (total int64, latestId int64) {
-//		return reportService.GetTotalPageCountByAnalyst(analyst)
-//	}
 func GetReportPageByAnalyst(pageInfo page.PageInfo, analyst string, reportIds []int) (list []reportService.ReportDTO, err error) {
 	list, err = reportService.GetReportPageByAnalyst(pageInfo, analyst, reportIds)
 	//并发获取研报的标签