123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- 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/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "html"
- "strconv"
- "strings"
- "time"
- )
- type ReportController struct {
- BaseAuthController
- }
- type ReportOpenController struct {
- BaseCommonController
- }
- // List
- // @Title 研报列表
- // @Description 研报列表
- // @Param request body request.ReportListForm true "type json string"
- // @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()
- }()
- params := new(request.ReportListForm)
- if e := this.ParseForm(params); e != nil {
- br.Msg = "参数解析异常"
- br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
- return
- }
- var (
- reportCond, outsideCond string
- reportPars, outsidePars []interface{}
- )
- // 仅取常规和智能布局的报告
- reportCond += ` AND r.report_layout IN (1,2)`
- params.Keywords = strings.TrimSpace(params.Keywords)
- if params.Keywords != "" {
- kw := fmt.Sprint("%", params.Keywords, "%")
- reportCond += ` AND r.title LIKE ?`
- reportPars = append(reportPars, kw)
- outsideCond += ` AND o.title LIKE ?`
- outsidePars = append(outsidePars, kw)
- }
- // 分类名称map
- classifyOb := new(models.MiniClassify)
- classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
- return
- }
- classifyMapping := make(map[int]*models.MiniClassify)
- for _, v := range classifies {
- classifyMapping[v.Id] = v
- }
- if params.ClassifyId > 0 {
- classify := classifyMapping[params.ClassifyId]
- if classify == nil || (classify != nil && classify.Enabled != 1) {
- br.Msg = "分类不存在,请刷新页面"
- return
- }
- switch classify.Level {
- case 1:
- reportCond += ` AND r.classify_id_first = ?`
- case 2:
- reportCond += ` AND r.classify_id_second = ?`
- case 3:
- reportCond += ` AND r.classify_id_third = ?`
- default:
- br.Msg = "分类异常"
- return
- }
- reportPars = append(reportPars, params.ClassifyId)
- // 获取子分类IDs
- childIds, e := classifyOb.GetChildClassifyIdsByParentId(params.ClassifyId)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取子分类失败, %v", e)
- return
- }
- if len(childIds) > 0 {
- outsideCond += fmt.Sprintf(` AND o.classify_id IN (%s)`, utils.GetOrmInReplace(len(childIds)))
- outsidePars = append(outsidePars, childIds)
- } else {
- outsideCond += ` AND o.classify_id = ?`
- outsidePars = append(outsidePars, params.ClassifyId)
- }
- }
- resp := new(response.ReportList)
- // 分页
- var startSize int
- if params.PageSize <= 0 {
- params.PageSize = utils.PageSize20
- }
- if params.CurrentIndex <= 0 {
- params.CurrentIndex = 1
- }
- startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
- total, e := models.GetReportAndOutsideReportCount(reportCond, outsideCond, reportPars, outsidePars)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取报告总数失败, %v", e)
- return
- }
- page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
- resp.Paging = page
- list, e := models.GetReportAndOutsideReportByCondition(reportCond, outsideCond, reportPars, outsidePars, startSize, params.PageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取报告列表失败, %v", e)
- return
- }
- for _, v := range list {
- t := new(models.UnionReportItem)
- t.Id = v.Id
- t.Title = v.Title
- t.Abstract = v.Abstract
- t.PublishTime = utils.TimeTransferString(utils.FormatDateTime, v.PublishTime)
- t.ReportSource = v.ReportSource
- t.ReportFile = v.ReportFile
- t.Author = v.Author
- // 报告分类名称
- if v.ReportSource == utils.ReportSourceDefault {
- if v.ClassifyIdThird > 0 {
- t.ClassifyId = v.ClassifyIdThird
- } else if v.ClassifyIdSecond > 0 {
- t.ClassifyId = v.ClassifyIdSecond
- } else {
- t.ClassifyId = v.ClassifyIdFirst
- }
- }
- if v.ReportSource == utils.ReportSourceOutside {
- t.ClassifyId = v.ClassifyIdThird
- }
- cs := classifyMapping[t.ClassifyId]
- if cs != nil {
- t.ClassifyName = cs.ClassifyName
- }
- resp.List = append(resp.List, t)
- }
- br.Data = resp
- br.Ret = 200
- br.Msg = "获取成功"
- br.Success = true
- }
- // Detail
- // @Title 研报详情H5
- // @Description 研报详情H5
- // @Param ReportId query int true "报告ID"
- // @Success 200 {object} models.ReportDetailResp
- // @router /detail [get]
- func (this *ReportOpenController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- reportId, _ := this.GetInt("ReportId")
- if reportId <= 0 {
- br.Msg = "参数有误"
- return
- }
- report, err := models.GetReportById(reportId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Ret = 200
- br.Data = new(response.ReportDetailResp)
- br.Success = true
- br.Msg = "该报告已删除或不存在"
- return
- }
- br.Msg = "该报告已删除"
- br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
- return
- }
- if report.HeadResourceId > 0 || report.EndResourceId > 0 {
- headImg, err := models.GetSmartReportResourceById(report.HeadResourceId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- utils.FileLog.Warn("版头数据获取失败,Err:" + err.Error())
- }
- endImg, err := models.GetSmartReportResourceById(report.EndResourceId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- utils.FileLog.Warn("版尾数据获取失败,Err:" + err.Error())
- }
- if headImg != nil {
- report.HeadResource = headImg
- }
- if endImg != nil {
- report.EndResource = endImg
- }
- }
- if report.HasChapter == 1 {
- chapterList, err := models.GetReportChapterList(report.Id)
- if err != nil {
- br.Msg = "该报告已删除"
- br.ErrMsg = "获取章节列表失败,Err:" + err.Error()
- return
- }
- for _, v := range chapterList {
- v.Content = html.UnescapeString(v.Content)
- }
- report.ChapterContent = chapterList
- }
- report.ContentSub = html.UnescapeString(report.ContentSub)
- report.Content = html.UnescapeString(report.Content)
- if report == nil {
- br.Msg = "报告不存在"
- return
- }
- resp := new(response.ReportDetailResp)
- resp.Report = report
- br.Data = resp
- br.Ret = 200
- br.Msg = "获取成功"
- br.Success = true
- }
- // OutsideDetail
- // @Title 研报详情H5-文档管理库
- // @Description 研报详情H5-文档管理库
- // @Param ReportId query int true "报告ID"
- // @Success 200 {object} models.OutsideReportItem
- // @router /outside_detail [get]
- func (this *ReportOpenController) OutsideDetail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- reportId, _ := this.GetInt("ReportId")
- if reportId <= 0 {
- br.Msg = "参数有误"
- return
- }
- outsideReport, e := models.GetOutsideReportById(reportId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "报告不存在,请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("获取外部报告失败, %v", e)
- return
- }
- br.Data = outsideReport.Format2Item()
- br.Ret = 200
- br.Msg = "获取成功"
- br.Success = true
- }
- // ReadRecord
- // @Title 新增报告阅读记录
- // @Description 新增报告阅读记录
- // @Param request body request.ReportReadRecordReq true "type json string"
- // @Success 200 操作成功
- // @router /read_record [post]
- func (this *ReportController) ReadRecord() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- users := this.User
- if users == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 403
- return
- }
- var req request.ReportReadRecordReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常"
- br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
- return
- }
- if req.ReportId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
- return
- }
- if req.ReportSource != utils.ReportSourceDefault && req.ReportSource != utils.ReportSourceOutside {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, ReportSource: %d", req.ReportSource)
- return
- }
- // 获取报告分类信息
- var (
- title string
- classifyIdFirst int
- classifyIdSecond int
- classifyIdThird int
- classifyNameFirst string
- classifyNameSecond string
- classifyNameThird string
- )
- if req.ReportSource == utils.ReportSourceDefault {
- reportOb := new(models.Report)
- report, e := reportOb.GetItemById(req.ReportId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "报告不存在,请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("获取报告失败, %v", e)
- return
- }
- title = report.Title
- classifyIdFirst = report.ClassifyIdFirst
- classifyIdSecond = report.ClassifyIdSecond
- classifyIdThird = report.ClassifyIdThird
- classifyNameFirst = report.ClassifyNameFirst
- classifyNameSecond = report.ClassifyNameSecond
- classifyNameThird = report.ClassifyNameThird
- }
- if req.ReportSource == utils.ReportSourceOutside {
- outsideReport, e := models.GetOutsideReportById(req.ReportId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "报告不存在,请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("获取外部报告失败, %v", e)
- return
- }
- title = outsideReport.Title
- // 根据分类层级取出所有分类ID和名称
- classifyOb := new(models.MiniClassify)
- classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("获取报告分类失败, %v", e)
- return
- }
- classifyMapping := make(map[int]*models.MiniClassify)
- for _, v := range classifies {
- classifyMapping[v.Id] = v
- }
- thisClassify := classifyMapping[outsideReport.ClassifyId]
- if thisClassify == nil {
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("报告分类不存在, ClassifyId: %d", outsideReport.ClassifyId)
- return
- }
- levelArr := strings.Split(thisClassify.LevelPath, ",")
- if len(levelArr) <= 0 {
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("报告分类层级异常, ClassifyId: %d, LevelPath: %s", thisClassify.Id, thisClassify.LevelPath)
- return
- }
- if len(levelArr) > 0 {
- classifyIdFirst, _ = strconv.Atoi(levelArr[0])
- firstClassify := classifyMapping[classifyIdFirst]
- if firstClassify != nil {
- classifyNameFirst = firstClassify.ClassifyName
- }
- // 测试环境统一放在了一个一级分类下
- if classifyNameFirst == "" && utils.RunMode == "debug" {
- classifyNameFirst = "金瑞小程序分类(勿删)"
- }
- }
- if len(levelArr) > 1 {
- classifyIdSecond, _ = strconv.Atoi(levelArr[1])
- secondClassify := classifyMapping[classifyIdSecond]
- if secondClassify != nil {
- classifyNameSecond = secondClassify.ClassifyName
- }
- }
- if len(levelArr) > 2 {
- classifyIdThird, _ = strconv.Atoi(levelArr[2])
- thirdClassify := classifyMapping[classifyIdThird]
- if thirdClassify != nil {
- classifyNameThird = thirdClassify.ClassifyName
- }
- }
- }
- // 记录不存在则新增,存在则更新结束阅读时间
- var recordId int
- nowTime := time.Now().Local()
- if req.RecordId <= 0 {
- newRecord := &models.UserReadRecord{
- UserId: users.UserId,
- ReportId: req.ReportId,
- ReportTitle: title,
- ClassifyIdFirst: classifyIdFirst,
- ClassifyNameFirst: classifyNameFirst,
- ClassifyIdSecond: classifyIdSecond,
- ClassifyNameSecond: classifyNameSecond,
- ClassifyIdThird: classifyIdThird,
- ClassifyNameThird: classifyNameThird,
- StartTimestamp: int(nowTime.Unix()),
- CreateTime: nowTime,
- ReportSource: req.ReportSource,
- }
- if e := newRecord.Create(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("新增阅读记录失败, %v", e)
- return
- }
- recordId = newRecord.Id
- } else {
- recordOb := new(models.UserReadRecord)
- readRecord, e := recordOb.GetItemById(req.RecordId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "阅读记录不存在"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("获取阅读记录失败, %v", e)
- return
- }
- readRecord.EndTimestamp = int(nowTime.Unix())
- if e = readRecord.Update([]string{recordOb.Cols().EndTimestamp}); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("更新阅读记录失败, %v", e)
- return
- }
- recordId = readRecord.Id
- // 更新用户阅读次数及最后一次阅读时间
- usersOb := new(models.Users)
- if e = usersOb.UpdateUserReadTimes(users.UserId); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("更新用户阅读次数失败, %v", e)
- return
- }
- }
- resp := new(response.UserReadRecordResp)
- resp.RecordId = recordId
- br.Data = resp
- br.Ret = 200
- br.Msg = "操作成功"
- br.Success = true
- }
|