package controllers import ( "encoding/json" "hongze/hongze_admin/models" "hongze/hongze_admin/services" "hongze/hongze_admin/utils" "html" "os" "path" "strconv" "time" ) //报告 type ReportController struct { BaseAuthController } // @Title 获取报告列表接口 // @Description 获取报告列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param StartDate query string true "开始时间" // @Param EndDate query string true "结束时间" // @Param Frequency query string true "频度" // @Param ClassifyNameFirst query string true "一级分类名称" // @Param ClassifyNameSecond query string true "二级分类名称" // @Param State query int true "状态" // @Param KeyWord query string true "搜索关键词" // @Param PublishSort query string true "desc:降序,asc 升序(预留)" // @Success 200 {object} models.ReportListResp // @router /list [get] func (this *ReportController) ListReport() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") startDate := this.GetString("StartDate") endDate := this.GetString("EndDate") frequency := this.GetString("Frequency") classifyNameFirst := this.GetString("ClassifyNameFirst") classifyNameSecond := this.GetString("ClassifyNameSecond") state, _ := this.GetInt("State") keyWord := this.GetString("KeyWord") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = utils.StartIndex(currentIndex, pageSize) var condition string var pars []interface{} if keyWord != "" { condition += ` AND (title LIKE '%` + keyWord + `%' OR author LIKE '%` + keyWord + `%' ) ` } if startDate != "" { condition += ` AND create_time >= ? ` pars = append(pars, startDate) } if endDate != "" { condition += ` AND create_time <= ? ` pars = append(pars, endDate) } if frequency != "" { condition += ` AND frequency = ? ` pars = append(pars, frequency) } if classifyNameFirst != "" { condition += ` AND classify_name_first = ? ` pars = append(pars, classifyNameFirst) } if classifyNameSecond != "" { condition += ` AND classify_name_second = ? ` pars = append(pars, classifyNameSecond) } if state > 0 { condition += ` AND state = ? ` pars = append(pars, state) } total, err := models.GetReportListCount(condition, pars) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } list, err := models.GetReportList(condition, pars, startSize, pageSize) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } page := models.GetPaging(currentIndex, pageSize, total) resp := new(models.ReportListResp) resp.Paging = page resp.List = list br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // @Title 发布报告接口 // @Description 发布报告 // @Param request body models.PublishReq true "type json string" // @Success 200 Ret=200 发布成功 // @router /publish [post] func (this *ReportController) PublishReport() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.PublishReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ReportIds != "" { br.Msg = "参数错误" br.ErrMsg = "参数错误,报告id不可为空" return } err = models.PublishReport(req.ReportIds) if err != nil { br.Msg = "发布失败" br.ErrMsg = "发布失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "发布成功" } // @Title 取消发布报告接口 // @Description 取消发布报告 // @Param request body models.PublishReq true "type json string" // @Success 200 Ret=200 发布成功 // @router /publish/cancle [post] func (this *ReportController) PublishCancleReport() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.PublishReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ReportIds != "" { br.Msg = "参数错误" br.ErrMsg = "参数错误,报告id不可为空" return } err = models.PublishCancleReport(req.ReportIds) if err != nil { br.Msg = "取消发布失败" br.ErrMsg = "取消发布失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "取消发布成功" } // @Title 删除报告接口 // @Description 删除报告 // @Param request body models.DeleteReq true "type json string" // @Success 200 Ret=200 发布成功 // @router /delete [post] func (this *ReportController) Delete() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.DeleteReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ReportIds != "" { br.Msg = "参数错误" br.ErrMsg = "参数错误,报告id不可为空" return } err = models.DeleteReport(req.ReportIds) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "删除成功" } // @Title 新增报告接口 // @Description 新增报告 // @Param request body models.AddReq true "type json string" // @Success 200 {object} models.AddResp // @router /add [post] func (this *ReportController) Add() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.AddReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } var contentSub string if req.Content != "" { contentSub, err = services.GetReportContentSub(req.Content) if err != nil { utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers) } } maxStage, err := models.GetReportStage(req.ClassifyIdFirst, req.ClassifyIdSecond) if err != nil { br.Msg = "期数获取失败!" br.ErrMsg = "期数获取失败,Err:" + err.Error() return } item := new(models.Report) item.AddType = req.AddType item.ClassifyIdFirst = req.ClassifyIdFirst item.ClassifyNameFirst = req.ClassifyNameFirst item.ClassifyIdSecond = req.ClassifyIdSecond item.ClassifyNameSecond = req.ClassifyNameSecond item.Title = req.Title item.Abstract = req.Abstract item.Author = req.Author item.Frequency = req.Frequency item.State = req.State item.Content = html.EscapeString(req.Content) item.Stage = maxStage + 1 item.ContentSub = html.EscapeString(contentSub) item.CreateTime = req.CreateTime newReportId, err := models.AddReport(item) if err != nil { br.Msg = "保存失败" br.ErrMsg = "保存失败,Err:" + err.Error() return } //处理权限 { permissionItems, err := models.GetPermission(req.ClassifyNameSecond) if err != nil { utils.SendEmail(utils.APPNAME+"失败提醒", "获取权限失败,Err:"+err.Error(), utils.EmailSendToUsers) } for _, v := range permissionItems { err = models.AddChartPermissionChapterMapping(v.ChartPermissionId, newReportId) if err != nil { utils.SendEmail(utils.APPNAME+"失败提醒", "新增权限失败,Err:"+err.Error()+strconv.FormatInt(newReportId, 10), utils.EmailSendToUsers) } } } resp := new(models.AddResp) resp.ReportId = newReportId br.Ret = 200 br.Success = true br.Msg = "保存成功" br.Data = resp } // @Title 编辑报告接口 // @Description 编辑报告 // @Param request body models.EditReq true "type json string" // @Success 200 {object} models.EditResp // @router /edit [post] func (this *ReportController) Edit() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.EditReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } var contentSub string if req.Content != "" { contentSub, err = services.GetReportContentSub(req.Content) if err != nil { utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers) } } item := new(models.Report) item.ClassifyIdFirst = req.ClassifyIdFirst item.ClassifyNameFirst = req.ClassifyNameFirst item.ClassifyIdSecond = req.ClassifyIdSecond item.ClassifyNameSecond = req.ClassifyNameSecond item.Title = req.Title item.Abstract = req.Abstract item.Author = req.Author item.Frequency = req.Frequency item.State = req.State item.Content = html.EscapeString(req.Content) item.ContentSub = html.EscapeString(contentSub) item.CreateTime = req.CreateTime err = models.EditReport(item, req.ReportId) if err != nil { br.Msg = "保存失败" br.ErrMsg = "保存失败,Err:" + err.Error() return } resp := new(models.EditResp) resp.ReportId = req.ReportId br.Ret = 200 br.Success = true br.Msg = "保存成功" br.Data = resp } // @Title 获取报告详情接口 // @Description 获取报告详情 // @Param request body models.ReportDetailReq true "type json string" // @Success 200 {object} models.Report // @router /detail [get] func (this *ReportController) Detail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.ReportDetailReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ReportId <= 0 { br.Msg = "参数错误" return } item, err := models.GetReportById(req.ReportId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } item.Content = html.UnescapeString(item.Content) item.ContentSub = html.UnescapeString(item.ContentSub) br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = item } // @Title 图片上传 // @Description 图片上传接口 // @Param file query file true "文件" // @Success 200 新增成功 // @router /upload [post] func (this *ReportController) Upload() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() f, h, err := this.GetFile("file") if err != nil { br.Msg = "获取资源信息失败" br.ErrMsg = "获取资源信息失败,Err:" + err.Error() return } ext := path.Ext(h.Filename) dateDir := time.Now().Format("20060102") uploadDir := utils.STATIC_DIR + "hongze/" + dateDir err = os.MkdirAll(uploadDir, 777) if err != nil { br.Msg = "存储目录创建失败" br.ErrMsg = "存储目录创建失败,Err:" + err.Error() return } randStr := utils.GetRandStringNoSpecialChar(28) fileName := randStr + ext fpath := uploadDir + "/" + fileName defer f.Close() //关闭上传文件 err = this.SaveToFile("file", fpath) if err != nil { br.Msg = "文件上传失败" br.ErrMsg = "文件上传失败,Err:" + err.Error() return } //上传到阿里云 resourceUrl, err := services.UploadAliyun(fileName, fpath) if err != nil { br.Msg = "文件上传失败" br.ErrMsg = "文件上传失败,Err:" + err.Error() return } defer func() { os.Remove(fpath) }() item := new(models.Resource) item.ResourceUrl = resourceUrl item.ResourceType = 1 item.CreateTime = time.Now() newId, err := models.AddResource(item) if err != nil { br.Msg = "资源上传失败" br.ErrMsg = "资源上传失败,Err:" + err.Error() return } resp := new(models.ResourceResp) resp.Id = newId resp.ResourceUrl = resourceUrl br.Msg = "上传成功" br.Ret = 200 br.Success = true br.Data = resp return } // @Title 根据分类获取最近一次报告详情接口 // @Description 根据分类获取最近一次报告详情 // @Param request body models.ClassifyIdDetailReq true "type json string" // @Success 200 {object} models.Report // @router /classifyIdDetail [get] func (this *ReportController) ClassifyIdDetail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.ClassifyIdDetailReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } item, err := models.GetReportDetailByClassifyId(req.ClassifyIdFirst, req.ClassifyIdSecond) if err != nil { br.Msg = "获取失败!" br.ErrMsg = "获取失败,Err:" + err.Error() return } item.Content = html.UnescapeString(item.Content) item.ContentSub = html.UnescapeString(item.ContentSub) br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = item } // @Title 模板消息推送接口 // @Description 模板消息推送 // @Param request body models.SendTemplateMsgReq true "type json string" // @Success 200 Ret=200 推送成功 // @router /sendTemplateMsg [post] func (this *ReportController) SendTemplateMsg() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.SendTemplateMsgReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ReportId <= 0 { br.Msg = "参数错误" br.ErrMsg = "参数错误" return } go services.SendWxTemplateMsg(req.ReportId) err = models.ModifyReportMsgIsSend(req.ReportId) if err != nil { br.Msg = "发送失败" br.ErrMsg = "发送失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "发送成功" } // @Title 获取报告作者接口 // @Description 获取报告作者 // @Success 200 {object} models.ReportAuthorResp // @router /author [get] func (this *ReportController) Author() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() items, err := models.GetReportAuthor() if err != nil { br.Msg = "获取失败!" br.ErrMsg = "获取失败,Err:" + err.Error() return } resp := new(models.ReportAuthorResp) resp.List = items br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp }