package controllers import ( "encoding/json" "eta/eta_hub/models" "eta/eta_hub/services" "eta/eta_hub/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "strconv" "strings" "time" ) // SmartReportController 智能研报 type SmartReportController struct { BaseAuthController } // List // @Title 报告列表 // @Description 报告列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码" // @Param TimeType query string false "筛选的时间类别: publish_time-发布时间, modify_time-更新时间" // @Param StartDate query string false "开始时间" // @Param EndDate query string false "结束时间" // @Param Frequency query string false "频度" // @Param ClassifyIdFirst query int false "一级分类ID" // @Param ClassifyIdSecond query int false "二级分类ID" // @Param State query int false "发布状态: 1-待发布; 2-已发布" // @Param Keyword query string false "搜索关键词" // @Success 200 {object} models.SmartReportListResp // @router /list [get] func (this *SmartReportController) List() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() type SmartReportListReq struct { PageSize int `form:"PageSize"` CurrentIndex int `form:"CurrentIndex"` TimeType string `form:"TimeType"` StartDate string `form:"StartDate"` EndDate string `form:"EndDate"` Frequency string `form:"Frequency"` ClassifyIdFirst int `form:"ClassifyIdFirst"` ClassifyIdSecond int `form:"ClassifyIdSecond"` State int `form:"State"` Keyword string `form:"Keyword"` } params := new(SmartReportListReq) if e := this.ParseForm(params); e != nil { br.Msg = "获取失败" br.ErrMsg = "入参解析失败, Err: " + e.Error() return } if params.TimeType == "" { params.TimeType = "publish_time" } if params.TimeType != "publish_time" && params.TimeType != "modify_time" { br.Msg = "请选择正确的时间类型" return } // 更新时间指的是内容更新时间 if params.TimeType == "modify_time" { params.TimeType = "content_modify_time" } var condition string var pars []interface{} // 筛选项 { keyword := strings.TrimSpace(params.Keyword) if keyword != "" { kw := fmt.Sprint("%", keyword, "%") condition += fmt.Sprintf(` AND (title LIKE ? OR admin_real_name LIKE ? OR last_modify_admin_name LIKE ?)`) pars = append(pars, kw, kw, kw) } if params.StartDate != "" && params.EndDate != "" { st := fmt.Sprintf("%s 00:00:00", params.StartDate) ed := fmt.Sprintf("%s 23:59:59", params.EndDate) condition += fmt.Sprintf(` AND %s >= ? AND %s <= ?`, params.TimeType, params.TimeType) pars = append(pars, st, ed) } if params.Frequency != "" { condition += ` AND frequency = ?` pars = append(pars, params.Frequency) } if params.ClassifyIdFirst > 0 { condition += ` AND classify_id_first = ?` pars = append(pars, params.ClassifyIdFirst) } if params.ClassifyIdSecond > 0 { condition += ` AND classify_id_second = ?` pars = append(pars, params.ClassifyIdSecond) } if params.State > 0 { condition += ` AND state = ?` pars = append(pars, params.State) } } resp := new(models.SmartReportListResp) reportOB := new(models.SmartReport) total, e := reportOB.GetCountByCondition(condition, pars) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取报告总数失败, Err:" + e.Error() return } if total <= 0 { page := paging.GetPaging(params.CurrentIndex, params.PageSize, total) resp.Paging = page br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp return } // 分页列表 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) // 列表查询过滤掉富文本内容 fields := []string{ "smart_report_id", "report_code", "classify_id_first", "classify_name_first", "classify_id_second", "classify_name_second", "add_type", "title", "abstract", "author", "frequency", "stage", "video_url", "video_name", "video_play_seconds", "video_size", "detail_img_url", "detail_pdf_url", "admin_id", "admin_real_name", "state", "publish_time", "pre_publish_time", "pre_msg_send", "msg_is_send", "msg_send_time", "create_time", "modify_time", "last_modify_admin_id", "last_modify_admin_name", "content_modify_time", "pv", "uv", } list, e := reportOB.GetPageItemsByCondition(condition, pars, fields, "", startSize, params.PageSize) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取报告分页列表失败, Err:" + e.Error() return } resp.List = list page := paging.GetPaging(params.CurrentIndex, params.PageSize, total) resp.Paging = page br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // Detail // @Title 详情 // @Description 详情 // @Param SmartReportId query int true "智能研报ID" // @Success 200 {object} models.SmartReportItem // @router /detail [get] func (this *SmartReportController) Detail() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() reportId, _ := this.GetInt("SmartReportId") if reportId <= 0 { br.Msg = "参数有误" br.ErrMsg = "报告ID有误" return } ob := new(models.SmartReport) item, e := ob.GetItemById(reportId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "报告不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取研报失败, Err: " + e.Error() return } // 报告状态 2待审核 3已驳回 4已审批 if !utils.InArrayByInt([]int{models.SmartReportStatePublished, models.SmartReportStateRejected, models.SmartReportStateApprovaled}, item.State) { br.Msg = "报告状态错误" br.ErrMsg = "报告状态错误" return } resp := models.FormatSmartReport2Item(item) br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // Publish // @Title 发布/取消发布 // @Description 发布/取消发布 // @Param request body models.SmartReportPublishReq true "type json string" // @Success 200 string "操作成功" // @router /publish [post] func (this *SmartReportController) Publish() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() var req models.SmartReportPublishReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.SmartReportIds == "" { br.Msg = "参数有误" br.ErrMsg = "报告ID为空" return } if req.PublishState != models.SmartReportStateRejected && req.PublishState != models.SmartReportStateApprovaled { br.Msg = "参数有误" return } reportArr := strings.Split(req.SmartReportIds, ",") for _, v := range reportArr { vint, err := strconv.Atoi(v) if err != nil { br.Msg = "参数错误" br.ErrMsg = "参数错误,Err:" + err.Error() return } ob := new(models.SmartReport) item, e := ob.GetItemById(vint) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "报告不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取研报失败, Err: " + e.Error() return } cols := []string{"State", "ModifyTime"} item.State = req.PublishState item.ModifyTime = time.Now().Local() if e = item.Update(cols); e != nil { br.Msg = "操作失败" br.ErrMsg = "更新研报失败, Err: " + e.Error() return } // ES更新报告 go func() { _ = services.SmartReportElasticUpsert(item.SmartReportId, req.PublishState) }() } br.Ret = 200 br.Success = true br.Msg = "操作成功" }