|
@@ -1,17 +1,25 @@
|
|
|
package user
|
|
|
|
|
|
import (
|
|
|
+ "errors"
|
|
|
"eta/eta_mini_ht_api/common/component/cache"
|
|
|
+ logger "eta/eta_mini_ht_api/common/component/log"
|
|
|
"eta/eta_mini_ht_api/common/exception"
|
|
|
+ "eta/eta_mini_ht_api/common/utils/page"
|
|
|
"eta/eta_mini_ht_api/controllers"
|
|
|
+ permissionService "eta/eta_mini_ht_api/domian/config"
|
|
|
+ reportDomian "eta/eta_mini_ht_api/domian/report"
|
|
|
chartService "eta/eta_mini_ht_api/service/media"
|
|
|
+ "eta/eta_mini_ht_api/service/report"
|
|
|
"eta/eta_mini_ht_api/service/user"
|
|
|
userService "eta/eta_mini_ht_api/service/user"
|
|
|
+ "gorm.io/gorm"
|
|
|
+ "sync"
|
|
|
)
|
|
|
|
|
|
// BookMarkController Operations about bookmark
|
|
|
type BookMarkController struct {
|
|
|
- controllers.BaseController
|
|
|
+ controllers.ListController
|
|
|
redis *cache.RedisCache
|
|
|
}
|
|
|
|
|
@@ -20,8 +28,9 @@ const (
|
|
|
Report = "report"
|
|
|
)
|
|
|
|
|
|
-func (u *BookMarkController) Prepare() {
|
|
|
- u.redis = cache.GetInstance()
|
|
|
+func (bk *BookMarkController) Prepare() {
|
|
|
+ bk.ListController.Prepare()
|
|
|
+ bk.redis = cache.GetInstance()
|
|
|
}
|
|
|
|
|
|
type BookMarkReq struct {
|
|
@@ -38,27 +47,27 @@ type BookMarkReq struct {
|
|
|
// @Description 收藏
|
|
|
// @Success 200 {object} controllers.BaseResponse
|
|
|
// @router /bookMark [post]
|
|
|
-func (u *BookMarkController) BookMark() {
|
|
|
- controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
- result = u.InitWrapData("收藏失败")
|
|
|
+func (bk *BookMarkController) BookMark() {
|
|
|
+ controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
+ result = bk.InitWrapData("收藏失败")
|
|
|
bookMark := new(BookMarkReq)
|
|
|
- u.GetPostParams(bookMark)
|
|
|
+ bk.GetPostParams(bookMark)
|
|
|
var userInfo user.User
|
|
|
- userInfo = u.Data["user"].(user.User)
|
|
|
+ userInfo = bk.Data["user"].(user.User)
|
|
|
if bookMark.SourceType == "" {
|
|
|
- u.FailedResult("收藏失败", result)
|
|
|
+ bk.FailedResult("收藏失败", result)
|
|
|
err = exception.New(exception.IllegalSourceType)
|
|
|
return
|
|
|
}
|
|
|
if bookMark.SourceId == 0 {
|
|
|
- u.FailedResult("收藏失败", result)
|
|
|
+ bk.FailedResult("收藏失败", result)
|
|
|
err = exception.New(exception.IllegalSourceId)
|
|
|
return
|
|
|
}
|
|
|
err = userService.BookMark(userInfo.Id, bookMark.SourceId, bookMark.SourceType)
|
|
|
if err != nil {
|
|
|
err = exception.NewWithException(exception.FeedBackError, err.Error())
|
|
|
- u.FailedResult("收藏失败", result)
|
|
|
+ bk.FailedResult("收藏失败", result)
|
|
|
return
|
|
|
}
|
|
|
//将图表加入es
|
|
@@ -70,7 +79,7 @@ func (u *BookMarkController) BookMark() {
|
|
|
UniqueCode: bookMark.UniqueCode,
|
|
|
})
|
|
|
}
|
|
|
- u.SuccessResult("收藏成功", nil, result)
|
|
|
+ bk.SuccessResult("收藏成功", nil, result)
|
|
|
return
|
|
|
})
|
|
|
}
|
|
@@ -80,30 +89,30 @@ func (u *BookMarkController) BookMark() {
|
|
|
// @Description 取消收藏
|
|
|
// @Success 200 {object} controllers.BaseResponse
|
|
|
// @router /unBookMark [post]
|
|
|
-func (u *BookMarkController) UnBookMark() {
|
|
|
- controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
- result = u.InitWrapData("取消收藏失败")
|
|
|
+func (bk *BookMarkController) UnBookMark() {
|
|
|
+ controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
+ result = bk.InitWrapData("取消收藏失败")
|
|
|
bookMark := new(BookMarkReq)
|
|
|
- u.GetPostParams(bookMark)
|
|
|
+ bk.GetPostParams(bookMark)
|
|
|
var userInfo user.User
|
|
|
- userInfo = u.Data["user"].(user.User)
|
|
|
+ userInfo = bk.Data["user"].(user.User)
|
|
|
if bookMark.SourceType == "" {
|
|
|
- u.FailedResult("取消收藏失败", result)
|
|
|
+ bk.FailedResult("取消收藏失败", result)
|
|
|
err = exception.New(exception.IllegalSourceType)
|
|
|
return
|
|
|
}
|
|
|
if bookMark.SourceId == 0 {
|
|
|
- u.FailedResult("取消收藏失败", result)
|
|
|
+ bk.FailedResult("取消收藏失败", result)
|
|
|
err = exception.New(exception.IllegalSourceId)
|
|
|
return
|
|
|
}
|
|
|
err = userService.UnBookMark(userInfo.Id, bookMark.SourceId, bookMark.SourceType)
|
|
|
if err != nil {
|
|
|
err = exception.NewWithException(exception.FeedBackError, err.Error())
|
|
|
- u.FailedResult("取消收藏失败", result)
|
|
|
+ bk.FailedResult("取消收藏失败", result)
|
|
|
return
|
|
|
}
|
|
|
- u.SuccessResult("收藏成功", nil, result)
|
|
|
+ bk.SuccessResult("收藏成功", nil, result)
|
|
|
return
|
|
|
})
|
|
|
}
|
|
@@ -117,32 +126,289 @@ type CheckBookMarkResp struct {
|
|
|
// @Description 取消收藏
|
|
|
// @Success 200 {object} controllers.BaseResponse
|
|
|
// @router /checkBookMark [post]
|
|
|
-func (u *BookMarkController) CheckBookMark() {
|
|
|
- controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
- result = u.InitWrapData("取消收藏失败")
|
|
|
+func (bk *BookMarkController) CheckBookMark() {
|
|
|
+ controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
+ result = bk.InitWrapData("取消收藏失败")
|
|
|
bookMark := new(BookMarkReq)
|
|
|
- u.GetPostParams(bookMark)
|
|
|
+ bk.GetPostParams(bookMark)
|
|
|
var userInfo user.User
|
|
|
- userInfo = u.Data["user"].(user.User)
|
|
|
+ userInfo = bk.Data["user"].(user.User)
|
|
|
if bookMark.SourceType == "" {
|
|
|
- u.FailedResult("获取是否收藏失败", result)
|
|
|
+ bk.FailedResult("获取是否收藏失败", result)
|
|
|
err = exception.New(exception.IllegalSourceType)
|
|
|
return
|
|
|
}
|
|
|
if bookMark.SourceId == 0 {
|
|
|
- u.FailedResult("获取是否收藏失败", result)
|
|
|
+ bk.FailedResult("获取是否收藏失败", result)
|
|
|
err = exception.New(exception.IllegalSourceId)
|
|
|
return
|
|
|
}
|
|
|
isBookMarked, err := userService.CheckBookMarkStatus(userInfo.Id, bookMark.SourceId, bookMark.SourceType)
|
|
|
if err != nil {
|
|
|
err = exception.NewWithException(exception.FeedBackError, err.Error())
|
|
|
- u.FailedResult("获取是否收藏失败", result)
|
|
|
+ bk.FailedResult("获取是否收藏失败", result)
|
|
|
return
|
|
|
}
|
|
|
- u.SuccessResult("获取是否收藏成功", &CheckBookMarkResp{
|
|
|
+ bk.SuccessResult("获取是否收藏成功", &CheckBookMarkResp{
|
|
|
IsBookMarked: isBookMarked,
|
|
|
}, result)
|
|
|
return
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+// BookMarkSearch 搜索收藏列表
|
|
|
+// @Description 搜索报告列表
|
|
|
+// @Success 200 {object}
|
|
|
+// @router /bookMarkSearch [get]
|
|
|
+func (bk *BookMarkController) BookMarkSearch(key string) {
|
|
|
+ controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
+ result = bk.InitWrapData("分页搜索报告列表失败")
|
|
|
+ if key == "" {
|
|
|
+ err = exception.New(exception.SearchKeyEmptyError)
|
|
|
+ bk.FailedResult("分页搜索报告列表失败", result)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ userInfo := bk.Data["user"].(user.User)
|
|
|
+ pageRes := page.Page{
|
|
|
+ Current: bk.PageInfo.Current,
|
|
|
+ PageSize: bk.PageInfo.PageSize,
|
|
|
+ }
|
|
|
+ //获取当前可以被搜索的报告原始ID
|
|
|
+ //先要限制查询的id范围
|
|
|
+ var reportIds []int
|
|
|
+ var mappingRiskLevel string
|
|
|
+ var userRiskStatus string
|
|
|
+ pageRes.Total, pageRes.LatestId, reportIds, _, mappingRiskLevel, userRiskStatus, err = report.RangeSearch(key, true, userInfo.Id)
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("获取报告原始ID列表失败:%v", err)
|
|
|
+ bk.FailedResult("分页搜索报告列表失败", result)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(reportIds) == 0 {
|
|
|
+ reports := new(page.PageResult)
|
|
|
+ reports.Data = []reportDomian.ReportDTO{}
|
|
|
+ reports.Page = pageRes
|
|
|
+ logger.Info("没有可以查询的报告列表")
|
|
|
+ bk.SuccessResult("分页搜索报告列表成功", reports, result)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if bk.PageInfo.LatestId == 0 {
|
|
|
+ //pageRes.Total, pageRes.LatestId = report.SearchMaxReportId(key)
|
|
|
+ bk.PageInfo.LatestId = pageRes.LatestId
|
|
|
+ bk.PageInfo.Total = pageRes.Total
|
|
|
+ } else {
|
|
|
+ pageRes.LatestId = bk.PageInfo.LatestId
|
|
|
+ pageRes.Total = bk.PageInfo.Total
|
|
|
+ }
|
|
|
+ pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
|
|
|
+ list := make([]reportDomian.ReportDTO, 0)
|
|
|
+ if pageRes.LatestId > 0 {
|
|
|
+ list, err = report.SearchReportList(key, reportIds, bk.PageInfo, true, userInfo.Id, mappingRiskLevel, userRiskStatus)
|
|
|
+ if err != nil {
|
|
|
+ bk.FailedResult("分页搜索报告列表失败", result)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ reports := new(page.PageResult)
|
|
|
+ reports.Data = list
|
|
|
+ reports.Page = pageRes
|
|
|
+ bk.SuccessResult("分页搜索报告列表成功", reports, result)
|
|
|
+ return
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+type BookMarkListReq struct {
|
|
|
+ SourceType string `json:"source_type"`
|
|
|
+}
|
|
|
+
|
|
|
+type BookMarkChart struct {
|
|
|
+ ChartName string `json:"chartName"`
|
|
|
+ ChartImage string `json:"chartImage"`
|
|
|
+ UniqueCode string `json:"uniqueCode"`
|
|
|
+ ChartInfoId int `json:"chartInfoId"`
|
|
|
+}
|
|
|
+type BookMarkReport struct {
|
|
|
+ ChartName string `json:"chartName"`
|
|
|
+ ChartImage string `json:"chartImage"`
|
|
|
+ UniqueCode string `json:"uniqueCode"`
|
|
|
+ ChartInfoId int `json:"chartInfoId"`
|
|
|
+}
|
|
|
+
|
|
|
+// BookMarkList 获取收藏列表
|
|
|
+// @Description 获取收藏列表
|
|
|
+// @Success 200 {object}
|
|
|
+// @router /bookMarkList [get]
|
|
|
+func (bk *BookMarkController) BookMarkList(sourceType string) {
|
|
|
+ controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
+ result = bk.InitWrapData("分页查询收藏列表失败")
|
|
|
+ pageRes := page.Page{
|
|
|
+ Current: bk.PageInfo.Current,
|
|
|
+ PageSize: bk.PageInfo.PageSize,
|
|
|
+ }
|
|
|
+ if sourceType == "" || (sourceType != Report && sourceType != Chart) {
|
|
|
+ err = exception.New(exception.IllegalSourceType)
|
|
|
+ bk.FailedResult("分页查询收藏列表失败", result)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ userInfo := bk.Data["user"].(user.User)
|
|
|
+ var sourceIds []int
|
|
|
+ pageRes.Total, sourceIds, err = user.GetTotalBookMarkPageBySourceType(userInfo.Id, sourceType)
|
|
|
+ //隐藏品种信息未设置风险等级的报告
|
|
|
+ if sourceType == Report {
|
|
|
+ pageRes.Total, sourceIds, err = report.FilterReportIds(sourceIds)
|
|
|
+ }
|
|
|
+ if pageRes.Total == 0 {
|
|
|
+ bookMarks := new(page.PageResult)
|
|
|
+ bookMarks.Data = []interface{}{}
|
|
|
+ bookMarks.Page = pageRes
|
|
|
+ bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ pageRes.Total = bk.PageInfo.Total
|
|
|
+ pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
|
|
|
+ switch sourceType {
|
|
|
+ case Report:
|
|
|
+ var list []BookMarkReport
|
|
|
+ list, err = getReportList(bk.PageInfo, userInfo.Id, sourceIds)
|
|
|
+ if err != nil {
|
|
|
+ err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
|
|
|
+ bk.FailedResult("分页查询收藏列表失败", result)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ bookMarks := new(page.PageResult)
|
|
|
+ bookMarks.Data = list
|
|
|
+ bookMarks.Page = pageRes
|
|
|
+ bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
|
|
|
+ return
|
|
|
+ case Chart:
|
|
|
+ var list []BookMarkChart
|
|
|
+ list, err = getChartList(bk.PageInfo, userInfo.Id)
|
|
|
+ if err != nil {
|
|
|
+ err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
|
|
|
+ bk.FailedResult("分页查询收藏列表失败", result)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ bookMarks := new(page.PageResult)
|
|
|
+ bookMarks.Data = list
|
|
|
+ bookMarks.Page = pageRes
|
|
|
+ bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
|
|
|
+ return
|
|
|
+ default:
|
|
|
+ err = exception.New(exception.IllegalSourceType)
|
|
|
+ bk.FailedResult("分页查询收藏列表失败", result)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func getReportList(info page.PageInfo, templateUserId int, sourceIds []int) (list []BookMarkReport, err error) {
|
|
|
+ sourceIds, err = userService.GetBookMarkPageRangeBySourceType(templateUserId, info, Report, sourceIds)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ userProfile, userErr := user.GetUserProfile(templateUserId)
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ var mappingRiskLevel, userRiskStatus string
|
|
|
+ userRiskStatus = userProfile.RiskLevelStatus
|
|
|
+ if userProfile.RiskLevel != "" {
|
|
|
+ var mapping permissionService.CustomerProductRiskMappingDTO
|
|
|
+ mapping, err = permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("查询产品风险等级映射失败:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ mappingRiskLevel = mapping.ProductRiskLevel
|
|
|
+ }
|
|
|
+ reports, err := report.GetReportListById(templateUserId, sourceIds, mappingRiskLevel, userRiskStatus)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ // 创建一个切片来存储结果,长度与 sourceIds 相同
|
|
|
+ list = make([]BookMarkReport, len(sourceIds))
|
|
|
+ // 使用 WaitGroup 来等待所有 goroutine 完成
|
|
|
+ var wg sync.WaitGroup
|
|
|
+ wg.Add(len(sourceIds))
|
|
|
+ // 使用 Mutex 来保护对 list 的写操作
|
|
|
+ var mu sync.Mutex
|
|
|
+ // 并发获取数据
|
|
|
+ for index, sourceId := range sourceIds {
|
|
|
+ go func(index int, id int) {
|
|
|
+ defer wg.Done()
|
|
|
+ var data chartService.ChartInfo
|
|
|
+ data, err = chartService.GetChartById(id)
|
|
|
+ reportInfo := convertToBookMarkChart(data)
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("获取数据失败: %v", err)
|
|
|
+ }
|
|
|
+ // 使用 Mutex 保护对 list 的写操作
|
|
|
+ mu.Lock()
|
|
|
+ list[index] = reportInfo
|
|
|
+ mu.Unlock()
|
|
|
+ }(index, sourceId)
|
|
|
+
|
|
|
+ }
|
|
|
+ // 等待所有 goroutine 完成
|
|
|
+ wg.Wait()
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func getChartList(info page.PageInfo, templateUserId int) (list []BookMarkChart, err error) {
|
|
|
+ sourceIds, err := userService.GetBookMarkPageBySourceType(templateUserId, info, Chart)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 创建一个切片来存储结果,长度与 sourceIds 相同
|
|
|
+ list = make([]BookMarkChart, len(sourceIds))
|
|
|
+ // 使用 WaitGroup 来等待所有 goroutine 完成
|
|
|
+ var wg sync.WaitGroup
|
|
|
+ wg.Add(len(sourceIds))
|
|
|
+ // 使用 Mutex 来保护对 list 的写操作
|
|
|
+ var mu sync.Mutex
|
|
|
+ // 并发获取数据
|
|
|
+ for index, sourceId := range sourceIds {
|
|
|
+ go func(index int, id int) {
|
|
|
+ defer wg.Done()
|
|
|
+ var data chartService.ChartInfo
|
|
|
+ data, err = chartService.GetChartById(id)
|
|
|
+ chartInfo := convertToBookMarkChart(data)
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("获取数据失败: %v", err)
|
|
|
+ }
|
|
|
+ // 使用 Mutex 保护对 list 的写操作
|
|
|
+ mu.Lock()
|
|
|
+ list[index] = chartInfo
|
|
|
+ mu.Unlock()
|
|
|
+ }(index, sourceId)
|
|
|
+
|
|
|
+ }
|
|
|
+ // 等待所有 goroutine 完成
|
|
|
+ wg.Wait()
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func convertToBookMarkChart(chart chartService.ChartInfo) BookMarkChart {
|
|
|
+ return BookMarkChart{
|
|
|
+ ChartName: chart.ChartName,
|
|
|
+ ChartImage: chart.ChartImage,
|
|
|
+ UniqueCode: chart.UniqueCode,
|
|
|
+ ChartInfoId: chart.ChartInfoId,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func convertToBookMarkReport(chart chartService.ChartInfo) BookMarkReport {
|
|
|
+ return BookMarkReport{
|
|
|
+ ChartName: chart.ChartName,
|
|
|
+ ChartImage: chart.ChartImage,
|
|
|
+ UniqueCode: chart.UniqueCode,
|
|
|
+ ChartInfoId: chart.ChartInfoId,
|
|
|
+ }
|
|
|
+}
|