package controllers import ( "eta/eta_mini_api/models" "eta/eta_mini_api/models/response" "eta/eta_mini_api/services" "eta/eta_mini_api/utils" "sort" "time" "github.com/rdlucklib/rdluck_tools/paging" ) type ReportController struct { BaseAuthController } // @Title 研报详情 // @Description 研报详情接口 // @Param ReportId query int true "报告id" // @Success 200 {object} models.ReportDetailResp // @router /detail [get] func (this *ReportController) Detail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() reportId, _ := this.GetInt("ReportId") if reportId <= 0 { br.Msg = "报告不存在" return } user := this.User if user == nil { // 如果没有用户没有绑定信息,单独处理 resp, er := services.GetReportDetailNoUser(reportId) if er != nil { br.Msg = "查询报告详情失败" br.ErrMsg = "查询报告失败,系统异常,Err:" + er.Error() return } if resp.Ret != 200 { br.Msg = resp.Msg br.ErrMsg = resp.ErrMsg return } br.Msg = "查询成功" br.Data = resp.Data br.Success = true br.Ret = 200 return } result, err := services.GetReportDetail(reportId, user.UserId) if err != nil { br.Msg = "查询报告详情失败" br.ErrMsg = "查询报告失败,系统异常,Err:" + err.Error() return } if result.Ret == 403 { br.Msg = result.Msg br.ErrMsg = result.ErrMsg return } if result.Ret == 200 && result.Data.Report == nil { // 后台没有这个报告,那么尝试删除我的收藏 models.DeleteMyReportByUserIdAndReportId(user.UserId, reportId, utils.ReportTypeEta) br.Msg = "该报告已删除或不存在,请刷新页面" return } count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, reportId, utils.ReportTypeEta) if err != nil { br.Msg = "查询收藏数量失败" br.ErrMsg = "查询收藏数量失败,Err:" + err.Error() return } if count > 0 { result.Data.IsCollect = true } else { result.Data.IsCollect = false } if user.UserId != 0 { result.Data.IsSignIn = true } br.Msg = "查询成功" br.Success = true br.Ret = 200 br.Data = result.Data } // @Title 研报列表 // @Description 研报列表 // @Param ChartPermissionId query int true "品种ID" // @Param Level query int true "品种层级" // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param RangeType query string true "范围类型,1-一天内,2-一周内,3-半年内" // @Param ClassifyId query int true "分类id" // @Success 200 {object} response.ReportList // @router /list [get] func (this *ReportController) List() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") chartPermissionId, _ := this.GetInt("ChartPermissionId") level, _ := this.GetInt("Level") rangeType, _ := this.GetInt("RangeType") classifyId, _ := this.GetInt("ClassifyId") reports, err := services.GetReportList(chartPermissionId, level, rangeType, classifyId, currentIndex, pageSize) if err != nil { br.Msg = "研报列表查询失败" br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error() return } if reports.Ret != 200 { br.Msg = reports.Msg br.ErrMsg = reports.ErrMsg return } br.Data = reports.Data br.Msg = "查询成功" br.Ret = 200 br.Success = true } // @Title pdf研报列表 // @Description pdf研报列表 // @Param ChartPermissionId query int true "品种ID" // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param RangeType query int true "范围类型,1-一天内,2-一周内,3-半年内" // @Param ClassifyId query int true "二级分类id" // @Success 200 {object} response.ReportList // @router /pdf/list [get] func (this *ReportController) PdfList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") chartPermissionId, _ := this.GetInt("ChartPermissionId") classifyId, _ := this.GetInt("ClassifyId") rangeType, _ := this.GetInt("RangeType") if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } if chartPermissionId <= 0 { br.Msg = "请输入品种ID" return } var condition string var pars []interface{} switch rangeType { case 1: condition += ` AND DATE(publish_time)=DATE(NOW()) ` case 2: condition += ` AND DATE(publish_time) BETWEEN DATE_SUB(NOW(),INTERVAL 1 WEEK) AND NOW() ` case 3: condition += ` AND DATE(publish_time) BETWEEN DATE_SUB(NOW(),INTERVAL 6 MONTH) AND NOW() ` } startSize := utils.StartIndex(currentIndex, pageSize) if classifyId != 0 { condition += ` AND classify_id_second=?` pars = append(pars, classifyId) } else if chartPermissionId != 0 { resp, err := services.GetClassifyListByChartPermission(chartPermissionId) if err != nil { br.Msg = "获取分类失败" br.ErrMsg = "获取分类失败,系统异常,Err:" + err.Error() return } if resp.Ret != 200 { br.Msg = resp.Msg br.ErrMsg = resp.ErrMsg return } classifyList := resp.Data condition += ` AND classify_id_second IN (` + utils.GetOrmReplaceHolder(len(classifyList)) + `)` for _, item := range classifyList { pars = append(pars, item.Id) } } total, err := models.GetReportPdfCountByCondition(condition, pars) if err != nil { br.Msg = "研报列表查询失败" br.ErrMsg = "研报列表统计查询失败,系统异常,Err:" + err.Error() return } reportPdfList, err := models.GetReportPdfListByCondition(condition, pars, startSize, pageSize) if err != nil { br.Msg = "研报列表查询失败" br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error() return } page := paging.GetPaging(currentIndex, pageSize, total) resp := new(response.ReportPdfResp) resp.List = reportPdfList resp.Paging = page br.Msg = "查询成功" br.Ret = 200 br.Success = true br.Data = resp } // @Title pdf研报详情 // @Description pdf研报详情 // @Param ReportPdfId query int true "品种ID" // @Success 200 {object} response.ReportList // @router /pdf/detail [get] func (this *ReportController) PdfDetail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User // 有效期是否到期 var vailStatus int if user.Status == utils.UserStatusFormal || user.Status == utils.UserStatusNo { if user.ValidEndTime.After(time.Now()) && user.ValidStartTime.Before(time.Now()) { vailStatus = 2 } else { // 不在有效期时间则设置为过期, 将用户状态禁用 vailStatus = 1 user.Status = utils.UserStatusNo } } reportPdfId, _ := this.GetInt("ReportPdfId") if reportPdfId <= 0 { br.Msg = "研报不存在或已删除" return } reportPdf, err := models.GetReportPdfById(reportPdfId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "研报不存在或已删除" return } br.Msg = "研报不存在或已删除" br.ErrMsg = "研报不存在或已删除,系统异常,Err:" + err.Error() return } permissionResp, err := services.GetChartPermissionListByClassifyId(reportPdf.ClassifyIdSecond, 0) if err != nil { br.Msg = "研报不存在或已删除" br.ErrMsg = "获取权限失败,系统异常,Err:" + err.Error() return } if permissionResp.Ret != 200 { br.Msg = permissionResp.Msg br.ErrMsg = permissionResp.ErrMsg return } var IsHas bool var IsPublic bool permissionMap := make(map[int]struct{}) for _, v := range permissionResp.Data { if v.ChartPermissionId == 1 { IsPublic = true } permissionMap[v.ChartPermissionId] = struct{}{} } resp := new(response.ReportPdfDetailResp) if !IsPublic { // 如果是私有报告,用户权限过期直接返回有效期已过 if vailStatus == 1 { resp.Report = reportPdf resp.Status = utils.ReportPermissionStatusExpired br.Ret = 200 br.Data = resp br.Msg = "用户权限不足" return } // 如果被禁用或是潜在用户,直接返回无阅读报告权限 if user.Status == utils.UserStatusNo || user.Status == utils.UserStatusPotential { resp.Report = reportPdf resp.Status = utils.ReportPermissionStatusNo br.Ret = 200 br.Data = resp br.Msg = "用户权限不足" return } userChartPermissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId) if err != nil { br.Msg = "获取用户权限失败" br.ErrMsg = "获取用户权限失败,Err:" + err.Error() return } if len(userChartPermissionIds) == 0 { resp.Report = reportPdf resp.Status = utils.ReportPermissionStatusNo br.Ret = 200 br.Data = resp br.Msg = "用户权限不足" return } for _, v := range userChartPermissionIds { if _, ok := permissionMap[v]; ok { IsHas = true break } } if !IsHas { resp.Report = reportPdf resp.Status = utils.ReportPermissionStatusNoPermission br.Ret = 200 br.Data = resp br.Msg = "用户权限不足" return } } else { reportPdf.IsPublic = IsPublic } count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, reportPdf.ReportPdfId, utils.ReportTypePdf) if err != nil { br.Msg = "获取研报详情失败" br.ErrMsg = "获取用户收藏记录失败,Err:" + err.Error() return } if count > 0 { reportPdf.IsCollect = true } else { reportPdf.IsCollect = false } resp.Report = reportPdf resp.Status = utils.ReportPermissionStatusHas br.Msg = "查询成功" br.Ret = 200 br.Success = true br.Data = resp } // @Title 今日研报列表 // @Description 今日研报列表 // @Success 200 {object} models.ReportDetailResp // @router /daily/list [get] func (this *ReportController) DailyList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() resp, err := services.GetReportDailyList() if err != nil { br.Msg = "研报列表查询失败" br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error() return } if resp.Ret != 200 { br.Msg = resp.Msg br.ErrMsg = resp.ErrMsg return } reportPdfList, err := models.GetReportPdfDailyList() if err != nil { br.Msg = "研报列表查询失败" br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error() return } reportList := resp.Data for _, v := range reportList.List { v.ReportType = utils.ReportTypeEta } var classifyIds []int for _, v := range reportPdfList { classifyIds = append(classifyIds, v.ClassifyIdSecond) } classifyResp, err := services.GetFirstChartPermission(classifyIds) if err != nil { br.Msg = "研报列表查询失败" br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error() return } if classifyResp.Ret != 200 { br.Msg = classifyResp.Msg br.ErrMsg = classifyResp.ErrMsg return } classifyPermissionMap := make(map[int][]string) for _, v := range classifyResp.Data { classifyPermissionMap[v.ClassifyId] = v.PermissionNames } for _, v := range reportPdfList { reportList.List = append(reportList.List, &models.ReportView{ Id: v.ReportPdfId, ClassifyIdFirst: v.ClassifyIdFirst, ClassifyNameFirst: v.ClassifyNameFirst, ClassifyIdSecond: v.ClassifyIdSecond, ClassifyNameSecond: v.ClassifyNameSecond, PermissionNames: classifyPermissionMap[v.ClassifyIdSecond], PdfUrl: v.PdfUrl, Title: v.Title, Abstract: v.Abstract, Stage: v.Stage, Author: v.Author, ReportType: utils.ReportTypePdf, PublishTime: v.PublishTime.Format(utils.FormatDate), ModifyTime: v.ModifyTime, }) } br.Data = reportList br.Msg = "查询成功" br.Ret = 200 br.Success = true } // @Title 最新研报列表 // @Description 最新研报列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Success 200 {object} models.ReportDetailResp // @router /recent/list [get] func (this *ReportController) RecentList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") if pageSize <= 0 { pageSize = 3 } resp, err := services.GetReportRecentList(currentIndex, pageSize) if err != nil { br.Msg = "研报列表查询失败" br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error() return } // 查询已发布的pdf condition := ` AND state=1 ` reportPdfList, err := models.GetReportPdfListByCondition(condition, []interface{}{}, 0, 3) if err != nil { br.Msg = "研报列表查询失败" br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error() return } reportList := resp.Data for _, v := range reportList.List { v.ReportType = utils.ReportTypeEta } var classifyIds []int for _, v := range reportPdfList { classifyIds = append(classifyIds, v.ClassifyIdSecond) } classifyResp, err := services.GetFirstChartPermission(classifyIds) if err != nil { br.Msg = "研报列表查询失败" br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error() return } if classifyResp.Ret != 200 { br.Msg = classifyResp.Msg br.ErrMsg = classifyResp.ErrMsg return } classifyPermissionMap := make(map[int][]string) for _, v := range classifyResp.Data { classifyPermissionMap[v.ClassifyId] = v.PermissionNames } for _, v := range reportPdfList { reportList.List = append(reportList.List, &models.ReportView{ Id: v.ReportPdfId, ClassifyIdFirst: v.ClassifyIdFirst, ClassifyNameFirst: v.ClassifyNameFirst, ClassifyIdSecond: v.ClassifyIdSecond, ClassifyNameSecond: v.ClassifyNameSecond, PermissionNames: classifyPermissionMap[v.ClassifyIdSecond], PdfUrl: v.PdfUrl, Title: v.Title, Abstract: v.Abstract, Stage: v.Stage, Author: v.Author, ReportType: utils.ReportTypePdf, PublishTime: v.PublishTime.Format(utils.FormatDateTime), ModifyTime: v.ModifyTime, }) } sort.Sort(models.ByPublishTimeReportView(reportList.List)) endIdx := len(reportList.List) if endIdx > 3 { endIdx = 3 } br.Data = reportList.List[:endIdx] br.Msg = "查询成功" br.Ret = 200 br.Success = true } // @Title 研报列表 // @Description 研报列表 // @Param ReportId query int true "报告id" // @Param chartPermissionId query int true "品种ID" // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Success 200 {object} models.ReportDetailResp // @router /search [get] func (this *ReportController) Search() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() keyWord := this.GetString("KeyWord") pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") if pageSize <= 0 { pageSize = utils.PageSize30 } if keyWord == "" { br.Msg = "请输入关键字" return } reports, err := services.SearchReport(keyWord, currentIndex, pageSize) if err != nil { br.Msg = "研报列表查询失败" br.ErrMsg = "研报列表查询失败,系统异常,Err:" + err.Error() return } if reports.Ret != 200 { br.Msg = reports.Msg br.ErrMsg = reports.ErrMsg return } resp := new(response.ReportSearchViewResp) list := make([]*response.ReportSearchListView, 0) for _, v := range reports.Data.List { tmpReport := &response.ReportSearchListView{ ReportId: v.ReportId, ReportChapterId: v.ReportChapterId, ClassifyIdFirst: v.ClassifyIdFirst, ClassifyNameFirst: v.ClassifyNameFirst, ClassifyIdSecond: v.ClassifyIdSecond, ClassifyNameSecond: v.ClassifyNameSecond, ReportChapterTypeId: v.ReportChapterTypeId, PublishTime: v.PublishTime.Format(utils.FormatDate), Title: v.Title, ContentSub: v.ContentSub, Abstract: v.Abstract, Stage: v.Stage, Author: v.Author, } if v.PublishTime.IsZero() { tmpReport.PublishTime = "" } list = append(list, tmpReport) } resp.List = list resp.Paging = reports.Data.Paging br.Data = resp br.Msg = "查询成功" br.Ret = 200 br.Success = true }