package controllers import ( "encoding/json" "eta/eta_mini_api/models" "eta/eta_mini_api/models/request" "eta/eta_mini_api/models/response" "eta/eta_mini_api/services" "eta/eta_mini_api/utils" "fmt" "time" "github.com/rdlucklib/rdluck_tools/paging" ) type MyReportController struct { BaseAuthController } // @Title 获取我的报告列表 // @Description 获取我的报告列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Success 200 {object} models.MyReportResp // @router /list [get] func (this *MyReportController) List() { br := new(models.BaseResponse).Init() defer func() { if err := recover(); err != nil { fmt.Println(err) } this.Data["json"] = br this.ServeJSON() }() user := this.User pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") if pageSize <= 0 { pageSize = utils.PageSize30 } if currentIndex <= 0 { currentIndex = 1 } if user.Status != utils.UserStatusFormal { br.Msg = "该用户没有收藏权限" return } total, err := models.GetMyReportListCountByUserId(user.UserId) if err != nil { br.Msg = "获取我的报告列表失败" br.ErrMsg = "获取我的报告列表失败,系统异常,Err:" + err.Error() return } resp := new(response.MyReportListResp) startSize := utils.StartIndex(currentIndex, pageSize) reportList, err := models.GetMyReportListByUserId(user.UserId, startSize, pageSize) if err != nil { br.Msg = "获取我的报告列表失败" br.ErrMsg = "获取我的报告列表失败,系统异常,Err:" + err.Error() return } page := paging.GetPaging(currentIndex, pageSize, total) resp.List = reportList resp.Paging = page br.Data = resp br.Success = true br.Msg = "获取我的报告列表成功" br.Ret = 200 } // @Title 收藏研报 // @Description 收藏研报 // @Success 200 {object} models.BaseResponse // @Failure 403 {object} models.BaseResponse // @router /collect [post] func (this *MyReportController) Collect() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.MyReportCollectReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } user := this.User if user.Status != utils.UserStatusFormal { br.Msg = "用户没有权限收藏" return } count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportId, utils.ReportTypeEta) if err != nil { br.Msg = "收藏失败" br.ErrMsg = "查询收藏数量失败,Err:" + err.Error() return } if count > 0 { br.Msg = "该研报已收藏,请重新刷新页面" return } reportResp, err := services.GetReportDetail(req.ReportId, user.UserId) if err != nil { br.Msg = "收藏失败" br.ErrMsg = "获取研报详情失败,Err:" + err.Error() return } if reportResp.Ret != 200 { br.Msg = "收藏失败" br.ErrMsg = "获取研报详情失败,Err:" + reportResp.ErrMsg return } if reportResp.Data.Report == nil { br.Msg = "该研报已删除或不存在,请刷新页面" return } if reportResp.Data.Status != utils.ReportPermissionStatusHas { br.Msg = "用户权限不足,不能收藏" return } report := reportResp.Data.Report publishTime, err := time.Parse(utils.FormatDateTime, report.PublishTime) if err != nil { br.Msg = "时间格式不对" return } myChart := &models.MyReport{ UserId: user.UserId, ReportId: req.ReportId, Title: report.Title, Abstract: report.Abstract, Author: report.Author, PublishTime: publishTime, Stage: report.Stage, CreateTime: time.Now(), ReportType: utils.ReportTypeEta, } err = myChart.Insert() if err != nil { br.Msg = "收藏失败" br.ErrMsg = "收藏失败,Err:" + err.Error() return } br.Msg = "收藏成功" br.Success = true br.Ret = 200 } // @Title 收藏pdf研报 // @Description 收藏pdf研报 // @Success 200 {object} models.BaseResponse // @Failure 403 {object} models.BaseResponse // @router /pdf/collect [post] func (this *MyReportController) PdfCollect() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.MyReportPdfCollectReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } user := this.User if user.Status != utils.UserStatusFormal { br.Msg = "用户没有权限收藏" return } count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportPdfId, utils.ReportTypePdf) if err != nil { br.Msg = "收藏失败" br.ErrMsg = "查询收藏数量失败,Err:" + err.Error() return } if count > 0 { br.Msg = "该研报已收藏,请重新刷新页面" return } reportPdf, err := models.GetReportPdfById(req.ReportPdfId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "研报不存在或已删除" return } br.Msg = "收藏失败" br.ErrMsg = "获取研报详情失败,Err:" + err.Error() return } reportClassifyId := services.GetReportPdfClassify(reportPdf) chartPermissionResp, err := services.GetChartPermissionListByClassifyId(reportClassifyId, 0) if err != nil { br.Msg = "收藏失败" br.ErrMsg = "获取研报详情失败,Err:" + err.Error() return } if chartPermissionResp.Ret != 200 { br.Msg = "收藏失败" br.ErrMsg = "获取研报详情失败,Err:" + chartPermissionResp.ErrMsg return } var IsPublic bool var permissionMap = make(map[int]struct{}) for _, v := range chartPermissionResp.Data { if v.IsPublic == 1 { IsPublic = true } permissionMap[v.ChartPermissionId] = struct{}{} } // 校验是否有收藏权限 var IsHas bool if !IsPublic { chartPermissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId) if err != nil { br.Msg = "收藏失败" br.ErrMsg = "获取用户权限失败,Err:" + err.Error() return } if len(chartPermissionIds) == 0 { br.Msg = "用户没有权限收藏" return } for _, v := range chartPermissionIds { if _, ok := permissionMap[v]; ok { IsHas = true break } } if !IsHas { br.Msg = "用户没有权限收藏" return } } publishTime, err := time.Parse(utils.FormatDateTime, reportPdf.PublishTime) if err != nil { br.Msg = "收藏失败" br.ErrMsg = "时间格式不对, err:" + err.Error() return } myChart := &models.MyReport{ UserId: user.UserId, ReportId: req.ReportPdfId, Title: reportPdf.Title, Abstract: reportPdf.Abstract, Author: reportPdf.Author, PublishTime: publishTime, Stage: reportPdf.Stage, CreateTime: time.Now(), ReportType: utils.ReportTypePdf, } err = myChart.Insert() if err != nil { br.Msg = "收藏失败" br.ErrMsg = "收藏失败,Err:" + err.Error() return } br.Msg = "收藏成功" br.Success = true br.Ret = 200 } // @Title 收藏pdf研报 // @Description 收藏pdf研报 // @Success 200 {object} models.BaseResponse // @Failure 403 {object} models.BaseResponse // @router /pdf/collectCancel [post] func (this *MyReportController) PdfCollectCancel() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.MyReportPdfCollectReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } user := this.User if user.Status != utils.UserStatusFormal { br.Msg = "用户没有权限收藏" return } count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportPdfId, utils.ReportTypePdf) if err != nil { br.Msg = "取消收藏失败" br.ErrMsg = "查询收藏数量失败,Err:" + err.Error() return } if count == 0 { br.Msg = "该研报已取消收藏,请重新刷新页面" return } err = models.DeleteMyReportByUserIdAndReportId(user.UserId, req.ReportPdfId, utils.ReportTypePdf) if err != nil { br.Msg = "取消收藏失败" br.ErrMsg = "取消收藏失败,Err:" + err.Error() return } br.Msg = "取消收藏成功" br.Success = true br.Ret = 200 } // @Title 取消收藏 // @Description 取消收藏 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Success 200 {object} models.BaseResponse // @Failure 403 {object} models.BaseResponse // @router /collectCancel [post] func (this *MyReportController) CollectCancel() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.MyReportCollectReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } user := this.User if user.Status != utils.UserStatusFormal { br.Msg = "用户没有权限收藏" return } count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportId, utils.ReportTypeEta) if err != nil { br.Msg = "取消收藏失败" br.ErrMsg = "获取收藏信息失败,Err:" + err.Error() return } if count == 0 { br.Msg = "该研报已取消收藏,请重新刷新页面" return } err = models.DeleteMyReportByUserIdAndReportId(user.UserId, req.ReportId, utils.ReportTypeEta) if err != nil { br.Msg = "取消收藏失败" br.ErrMsg = "取消收藏失败,Err:" + err.Error() return } br.Msg = "取消收藏成功" br.Success = true br.Ret = 200 } // @Title 是否收藏pdf研报 // @Description 是否收藏pdf研报 // @Param request body request.MyReportCollectReq true "type json string" // @Success 200 {object} models.BaseResponse // @router /pdf/isCollect [post] func (this *MyReportController) IsPdfCollect() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.MyReportPdfCollectReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } user := this.User if user.Status != utils.UserStatusFormal { br.Msg = "用户没有权限收藏" return } count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportPdfId, utils.ReportTypePdf) if err != nil { br.Msg = "收藏失败" br.ErrMsg = "查询收藏数量失败,Err:" + err.Error() return } resp := new(response.MyReportIsCollectResp) if count > 0 { resp.IsCollect = true } else { resp.IsCollect = false } br.Data = resp br.Msg = "获取成功" br.Success = true br.Ret = 200 } // @Title 是否收藏研报 // @Description 是否收藏研报 // @Param request body request.MyReportCollectReq true "type json string" // @Success 200 {object} models.BaseResponse // @router /isCollect [post] func (this *MyReportController) IsCollect() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.MyReportCollectReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } user := this.User if user.Status != utils.UserStatusFormal { br.Msg = "用户没有权限收藏" return } count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportId, utils.ReportTypeEta) if err != nil { br.Msg = "收藏失败" br.ErrMsg = "查询收藏数量失败,Err:" + err.Error() return } resp := new(response.MyReportIsCollectResp) if count > 0 { resp.IsCollect = true } else { resp.IsCollect = false } br.Data = resp br.Msg = "获取成功" br.Success = true br.Ret = 200 }