package report import ( logger "eta_mini_ht_api/common/component/log" "eta_mini_ht_api/common/exception" "eta_mini_ht_api/common/utils/date" "eta_mini_ht_api/common/utils/page" reportService "eta_mini_ht_api/domian/report" "sync" "time" ) type PublishRankedReport struct { Id int OrgId int Title string Abstract string PermissionNames interface{} PublishedTime string } type HotRankedReport struct { Id int OrgId int Count int Title string PublishedTime string } type PermissionNode struct { ID int `json:"id"` Name string `json:"name"` ParentID int `json:"parentId"` Children []*PermissionNode `json:"children,omitempty"` } type RecordCount struct { UserId int Mobile string ReportId int IpAddress string Location string Referer string Additional string } func GetTotalPageCount() (total int64) { return reportService.GetTotalPageCount() } // GetReportPage 分页获取报告列表 func GetReportPage(pageInfo page.PageInfo) (list []reportService.ReportDTO, err error) { list, err = reportService.GetReportPage(pageInfo) if err != nil { err = exception.New(exception.QueryReportPageFailed) } return } func CountReport(count RecordCount) error { dto := convertToRecordCountDTO(count) return reportService.CountReport(dto) } func GetRandedReportByWeeklyHot(limit int) (reports []HotRankedReport, err error) { end := time.Now() begin := date.GetBeginOfTheWeek(end, time.Monday) hotReports := reportService.GetHotReports(begin.Format(time.DateOnly), end.Format(time.DateOnly), limit) if len(hotReports) > 0 { var dtoList []reportService.ReportDTO var ids []int for i := 0; i < len(hotReports); i++ { ids = append(ids, hotReports[i].ReportId) } dtoList, err = reportService.GetListByCondition("id", ids) if err != nil { logger.Error("获取本周最热研报列表失败:%v", err) err = exception.New(exception.GetHotRandListFailed) return } reports = make([]HotRankedReport, len(ids)) for i := 0; i < len(dtoList); i++ { report := convertToHotRankedReport(dtoList[i]) for j := 0; j < len(ids); j++ { if ids[j] == report.Id { reports[j] = report break } } } } else { reports = []HotRankedReport{} } return } func GetRandedReportByPublishTime() (reports []PublishRankedReport, err error) { dtoList, err := reportService.GetListOrderByCondition("published_time", 3, reportService.DESC) if err != nil { logger.Error("获取最新发布的研报列表失败:%v", err) err = exception.New(exception.GetPublishedRandListFailed) return } //并发获取研报的标签 var wg sync.WaitGroup wg.Add(len(dtoList)) for i := 0; i < len(dtoList); i++ { go func(report *reportService.ReportDTO) { defer wg.Done() report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source) }(&dtoList[i]) } wg.Wait() reports = convertToPublishRankedReportList(dtoList) return } func getReportPermissionNames(id int, source string) (labels []string) { permissions := reportService.GetReportPermissionsById(id, source) for _, permission := range permissions { labels = append(labels, permission.Name) } return } func GetPermissionList() (root *PermissionNode, err error) { list, err := reportService.GetPermissionList() if err != nil { logger.Error("获取品种列表失败:%v", err) err = exception.New(exception.GetPermissionListFailed) return } root = &PermissionNode{ ID: 0, ParentID: 0, } assemblePermissionNode(list, root, 0, 2) return } func assemblePermissionNode(list []reportService.PermissionDTO, node *PermissionNode, current int, level int) { if node != nil && current < level { for _, permission := range list { if permission.ParentID == node.ID { childNode := &PermissionNode{ ID: permission.ID, Name: permission.Name, ParentID: permission.ParentID, } node.Children = append(node.Children, childNode) assemblePermissionNode(list, childNode, current+1, level) } } } } func convertToHotRankedReport(dto reportService.ReportDTO) (report HotRankedReport) { report = HotRankedReport{ Id: dto.ReportID, OrgId: dto.OrgId, PublishedTime: dto.PublishedTime, Title: dto.Title, } //publishDate, err := time.Parse(time.DateTime, report.PublishedTime) //if err == nil { // report.PublishedTime = publishDate.Format(time.DateOnly) //} return } func convertToPublishRankedReportList(dtoList []reportService.ReportDTO) (reports []PublishRankedReport) { reports = []PublishRankedReport{} for _, dto := range dtoList { report := PublishRankedReport{ Id: dto.ReportID, OrgId: dto.OrgId, PublishedTime: dto.PublishedTime, Abstract: dto.Abstract, Title: dto.Title, PermissionNames: dto.PermissionNames, } //publishDate, err := time.Parse(time.DateTime, report.PublishedTime) //if err == nil { // report.PublishedTime = publishDate.Format(time.DateOnly) //} reports = append(reports, report) } return } func convertToRecordCountDTO(record RecordCount) (dto reportService.RecordCountDTO) { return reportService.RecordCountDTO{ UserId: record.UserId, Mobile: record.Mobile, ReportId: record.ReportId, IpAddress: record.IpAddress, Location: record.Location, Referer: record.Referer, Additional: record.Additional, } }