package controllers import ( "encoding/json" "eta/eta_mini_crm_ht/models" "eta/eta_mini_crm_ht/models/request" "eta/eta_mini_crm_ht/models/response" "eta/eta_mini_crm_ht/services" "eta/eta_mini_crm_ht/services/elastic" "eta/eta_mini_crm_ht/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "os" "path" "strconv" "strings" "time" ) type ImagePDFController struct { BaseAuthController } // Author // @Title 获取报告作者接口 // @Description 获取报告作者 // @Success 200 {object} models.ReportAuthorResp // @router /author [get] func (this *ImagePDFController) Author() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() items, err := models.GetReportAuthorList() if err != nil { br.Msg = "获取失败!" br.ErrMsg = "获取失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = items } // Add // @Title 添加研报 // @Description 添加研报 // @Param request body request.ReportPdfAddReq true "type json string" // @Success 200 {object} models.ReportAuthorResp // @router /add [post] func (this *ImagePDFController) Add() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.ReportPdfAddReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyIdFirst <= 0 && req.ClassifyIdSecond <= 0 && req.ClassifyIdThird <= 0 { br.Msg = "请选择研报所属分类" return } if req.PdfName == "" { br.Msg = "pdf名称为空" return } var nameFirst, nameSecond, nameThird *models.ClassifyView var err error if req.ClassifyIdFirst > 0 { nameFirst, err = models.GetClassifyById(req.ClassifyIdFirst) if err != nil { br.Msg = "添加失败" br.ErrMsg = "一级类名获取失败,Err:" + err.Error() return } } else { br.Msg = "该分类不存在或已删除,请刷新重试" return } if req.ClassifyIdSecond > 0 { nameSecond, err = models.GetClassifyById(req.ClassifyIdSecond) if err != nil { br.Msg = "添加失败" br.ErrMsg = "二级类名获取失败,Err:" + err.Error() return } } if req.ClassifyIdThird > 0 { nameThird, err = models.GetClassifyById(req.ClassifyIdThird) if err != nil { br.Msg = "添加失败" br.ErrMsg = "三级类名获取失败,Err:" + err.Error() return } } pdf := &models.ReportPdf{ PdfUrl: req.PdfUrl, PdfName: req.PdfName, Title: req.Title, Author: req.Author, Abstract: req.Abstract, ClassifyIdFirst: req.ClassifyIdFirst, ClassifyNameFirst: nameFirst.ClassifyName, PublishTime: time.Now(), ModifyTime: time.Now(), SysUserId: this.SysUser.SysUserId, SysRealName: this.SysUser.SysRealName, State: utils.ReportStatusUp, } if nameSecond != nil { pdf.ClassifyIdSecond = nameSecond.Id pdf.ClassifyNameSecond = nameSecond.ClassifyName } if nameThird != nil { pdf.ClassifyIdThird = nameThird.Id pdf.ClassifyNameThird = nameThird.ClassifyName } insertId, err := pdf.Insert() if err != nil { br.Msg = "添加失败" br.ErrMsg = "pdf研报新增失败,Err:" + err.Error() return } pdf.ReportPdfId = int(insertId) // 添加es go func(reportPdf *models.ReportPdf) { reportpdfView := reportPdf.ToView() docId := strconv.Itoa(reportpdfView.ReportPdfId) err = elastic.EsAddOrEditReportPdf(utils.MINI_REPORT_INDEX_NAME, docId, reportpdfView) if err != nil { utils.FileLog.Info("pdf研报es新增失败,Err:" + err.Error()) return } utils.FileLog.Info("pdf研报es新增成功, pdfId:" + docId) }(pdf) br.Msg = "添加成功" br.Ret = 200 br.Success = true } // @Title 上传pdf研报 // @Description 上传pdf研报 // @Param File query file true "文件" // @Success 200 {object} models.ReportAuthorResp // @router /uploadPdf [post] func (this *ImagePDFController) UploadPdf() { 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 } defer f.Close() ext := path.Ext(h.Filename) if ext != ".pdf" { br.Msg = "文件格式不正确" return } size, err := strconv.Atoi(utils.UPLOAD_PDF_SIZE) if err != nil { size = 15 } if h.Size > 1024*1024*int64(size) { br.Msg = "文件大小不能超过15M" return } dateDir := time.Now().Format("20060102") uploadDir := utils.STATIC_DIR + "dongwu/" + dateDir err = os.MkdirAll(uploadDir, utils.DIR_MOD) if err != nil { br.Msg = "存储目录创建失败" br.ErrMsg = "存储目录创建失败,Err:" + err.Error() return } randStr := utils.GetRandStringNoSpecialChar(28) fileName := randStr + ext fpath := uploadDir + "/" + fileName err = this.SaveToFile("File", fpath) if err != nil { br.Msg = "文件上传失败" br.ErrMsg = "文件上传失败,Err:" + err.Error() return } pdfUploadDir := utils.RESOURCE_DIR + "pdf/" savePdfToOssPath := pdfUploadDir + time.Now().Format("200601/20060102/") pptName := utils.GetRandStringNoSpecialChar(28) savePdfToOssPath += pptName + ".pdf" defer func() { _ = os.Remove(fpath) }() ossClient := services.NewOssClient() if ossClient == nil { br.Msg = "文件上传失败" br.ErrMsg = "初始化OSS服务失败" return } pdfUrl, err := ossClient.UploadFile("", fpath, savePdfToOssPath) if err != nil { br.Msg = "文件上传失败" br.ErrMsg = "文件上传失败,Err:" + err.Error() return } base := path.Base(h.Filename) resp := new(response.ReportPdfUploadResp) resp.Url = pdfUrl resp.FileName = base br.Data = resp br.Msg = "上传成功" br.Ret = 200 br.Success = true } // List // @Title pdf研报列表 // @Description pdf研报列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开" // @Param State query int true "研报状态, 1:已发布 2:未发布" // @Param PublishStartDate query string true "发布开始时间" // @Param PublishEndDate query string true "发布结束时间" // @Param ModifyStartDate query string true "更新开始时间" // @Param ModifyEndDate query string true "更新结束时间" // @Param KeyWord query string true "报告标题/创建人" // @Param SortParam query string true "排序字段" // @Param SortType query string true "排序方式" // @Success 200 {object} models.ReportAuthorResp // @router /list [get] func (this *ImagePDFController) List() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") classifyIds := this.GetString("ClassifyIds") state, _ := this.GetInt("State") publishStartDate := this.GetString("PublishStartDate") publishEndDate := this.GetString("PublishEndDate") modifyStartDate := this.GetString("ModifyStartDate") modifyEndDate := this.GetString("ModifyEndDate") keyWord := this.GetString("KeyWord") sortParam := this.GetString("SortParam") sortType := this.GetString("SortType") var condition string var pars []interface{} if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } if classifyIds != "" { classifyArr := strings.Split(classifyIds, ",") condition += " AND classify_id_second in (" + utils.GetOrmReplaceHolder(len(classifyArr)) + ")" pars = append(pars, classifyArr) } switch state { case utils.ReportStatusUp: condition += " AND state = ?" pars = append(pars, state) case utils.ReportStatusDown: condition += " AND state = ?" pars = append(pars, state) } if publishStartDate != "" && publishEndDate != "" { condition += " AND publish_time >= ?" publishStartTime, err := time.Parse(utils.FormatDate, publishStartDate) if err != nil { br.Msg = "日期格式有误" br.ErrMsg = "日期格式有误,Err:" + err.Error() return } publishStartDateStr := publishStartTime.Format(utils.FormatDateTime) pars = append(pars, publishStartDateStr) condition += " AND publish_time <= ?" publishEndTime, err := time.Parse(utils.FormatDate, publishEndDate) if err != nil { br.Msg = "日期格式有误" br.ErrMsg = "日期格式有误,Err:" + err.Error() return } publishEndTime = publishEndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second) publishEndDateStr := publishEndTime.Format(utils.FormatDateTime) pars = append(pars, publishEndDateStr) } if modifyStartDate != "" && modifyEndDate != "" { condition += " AND modify_time >= ?" modifyStartTime, err := time.Parse(utils.FormatDate, modifyStartDate) if err != nil { br.Msg = "日期格式有误" br.ErrMsg = "日期格式有误,Err:" + err.Error() return } modifyStartDateStr := modifyStartTime.Format(utils.FormatDateTime) pars = append(pars, modifyStartDateStr) condition += " AND modify_time <= ?" modifyEndTime, err := time.Parse(utils.FormatDate, modifyEndDate) if err != nil { br.Msg = "日期格式有误" br.ErrMsg = "日期格式有误,Err:" + err.Error() return } modifyEndTime = modifyEndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second) modifyEndDateStr := modifyEndTime.Format(utils.FormatDateTime) pars = append(pars, modifyEndDateStr) } if keyWord != "" { condition += ` AND (title like ? OR sys_real_name like ?) ` pars = utils.GetLikeKeywordPars(pars, keyWord, 2) } var sortCondition string if sortParam != "" && sortType != "" { sortCondition = " ORDER BY " var param, sort string switch sortParam { case "PublishTime": param = "publish_time" case "ModifyTime": param = "modify_time" } switch sortType { case "asc": sort = " ASC " case "desc": sort = " DESC " } if param != "" && sort != "" { sortCondition += param + " " + sort } else { sortCondition = "" } } if sortCondition == "" { sortCondition = ` ORDER BY modify_time DESC ` } total, err := models.GetReportPdfCountByCondition(condition, pars) if err != nil { br.Msg = "获取研报列表失败" br.ErrMsg = "获取研报列表统计失败,Err:" + err.Error() return } startSize := utils.StartIndex(currentIndex, pageSize) reportList, err := models.GetReportPdfByCondition(condition, sortCondition, pars, startSize, pageSize) if err != nil { br.Msg = "获取研报列表失败" br.ErrMsg = "获取研报列表失败,Err:" + err.Error() return } page := paging.GetPaging(currentIndex, pageSize, total) resp := new(response.ReportPdfListResp) resp.List = reportList resp.Paging = page br.Ret = 200 br.Success = true br.Data = resp br.Msg = "获取成功" } // Edit // @Title 编辑研报 // @Description 编辑研报 // @Param request body request.ReportPdfEditReq true "type json string" // @Success 200 {object} models.ReportAuthorResp // @router /edit [post] func (this *ImagePDFController) Edit() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.ReportPdfEditReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数错误" br.ErrMsg = "参数错误,Err:" + err.Error() return } if req.ClassifyIdFirst <= 0 { br.Msg = "请选择研报所属的一级分类" return } if req.ClassifyIdSecond <= 0 { br.Msg = "请选择研报所属的二级分类" return } if req.PdfUrl == "" { br.Msg = "请上传研报文件" return } if req.PdfName == "" { 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 } nameFirst, err := models.GetClassifyById(req.ClassifyIdFirst) if err != nil { br.Msg = "文件编辑失败" br.ErrMsg = "一级类名获取失败,Err:" + err.Error() return } nameSecond, err := models.GetClassifyById(req.ClassifyIdSecond) if err != nil { br.Msg = "文件编辑失败" br.ErrMsg = "二级类名获取失败,Err:" + err.Error() return } if reportPdf.PdfName != req.PdfName || reportPdf.Title != req.Title || reportPdf.ClassifyIdFirst != req.ClassifyIdFirst || reportPdf.ClassifyIdSecond != req.ClassifyIdSecond || reportPdf.Author != req.Author || reportPdf.Abstract != req.Abstract || reportPdf.PdfUrl != req.PdfUrl { reportPdf.Title = req.Title reportPdf.PdfName = req.PdfName reportPdf.ClassifyIdFirst = req.ClassifyIdFirst reportPdf.ClassifyIdSecond = req.ClassifyIdSecond reportPdf.ClassifyNameFirst = nameFirst.ClassifyName reportPdf.ClassifyNameSecond = nameSecond.ClassifyName reportPdf.Author = req.Author reportPdf.Abstract = req.Abstract reportPdf.PdfUrl = req.PdfUrl reportPdf.ModifyTime = time.Now() err = reportPdf.Update([]string{"pdf_name", "title", "classify_id_first", "classify_id_second", "classify_name_first", "classify_name_second", "author", "abstract", "pdf_url", "modify_time"}) if err != nil { br.Msg = "文件更新失败" br.ErrMsg = "文件更新失败,Err:" + err.Error() return } // 编辑es go func(reportPdf *models.ReportPdf) { reportpdfView := reportPdf.ToView() docId := strconv.Itoa(reportpdfView.ReportPdfId) err = elastic.EsAddOrEditReportPdf(utils.MINI_REPORT_INDEX_NAME, docId, reportpdfView) if err != nil { utils.FileLog.Info("pdf研报es编辑失败,Err:" + err.Error()) return } utils.FileLog.Info("pdf研报es编辑成功, pdfId:" + docId) }(reportPdf) } br.Msg = "研报编辑成功" br.Ret = 200 br.Success = true } // Publish // @Title 发布研报 // @Description 发布研报 // @Param ReportPdfId query string true "pdf研报id" // @Success 200 {object} models.BaseResponse // @router /publish [get] func (this *ImagePDFController) Publish() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() ReportPdfId, _ := this.GetInt("ReportPdfId") reportPdf, err := models.GetReportPdfById(ReportPdfId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "研报不存在或已删除,请刷新页面" return } br.Msg = "获取研报失败" br.ErrMsg = "获取研报失败,系统错误,Err:" + err.Error() return } if reportPdf.State == utils.ReportStatusUp { br.Msg = "研报已发布" return } reportPdf.State = utils.ReportStatusUp reportPdf.PublishTime = time.Now() err = reportPdf.Update([]string{"state", "publish_time"}) if err != nil { br.Msg = "发布研报失败" br.ErrMsg = "发布研报失败,系统错误,Err:" + err.Error() return } // 修改es go func(reportPdf *models.ReportPdf) { reportpdfView := reportPdf.ToView() docId := strconv.Itoa(reportpdfView.ReportPdfId) err = elastic.EsAddOrEditReportPdf(utils.MINI_REPORT_INDEX_NAME, docId, reportpdfView) if err != nil { utils.FileLog.Info("pdf研报es发布失败,Err:" + err.Error()) return } utils.FileLog.Info("pdf研报es发布成功, pdfId:" + docId) }(reportPdf) br.Msg = "发布研报成功" br.Ret = 200 br.Success = true } // PublishCancel // @Title 取消发布研报 // @Description 取消发布研报 // @Param ReportPdfId query string true "pdf研报id" // @Success 200 {object} models.BaseResponse // @router /publishCancel [get] func (this *ImagePDFController) PublishCancel() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() ReportPdfId, _ := this.GetInt("ReportPdfId") reportPdf, err := models.GetReportPdfById(ReportPdfId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "研报不存在或已删除,请刷新页面" return } br.Msg = "获取研报失败" br.ErrMsg = "获取研报失败,系统错误,Err:" + err.Error() return } if reportPdf.State == utils.ReportStatusDown { br.Msg = "研报已撤销" return } reportPdf.State = utils.ReportStatusDown err = reportPdf.Update([]string{"state"}) if err != nil { br.Msg = "发布研报失败" br.ErrMsg = "发布研报失败,系统错误,Err:" + err.Error() return } // 修改es go func(reportPdf *models.ReportPdf) { reportpdfView := reportPdf.ToView() docId := strconv.Itoa(reportpdfView.ReportPdfId) err = elastic.EsAddOrEditReportPdf(utils.MINI_REPORT_INDEX_NAME, docId, reportpdfView) if err != nil { utils.FileLog.Info("pdf研报es取消发布失败,Err:" + err.Error()) return } utils.FileLog.Info("pdf研报es取消发布成功, pdfId:" + docId) }(reportPdf) br.Msg = "撤销研报成功" br.Ret = 200 br.Success = true } // delete // @Title 删除研报 // @Description 删除研报 // @Param ReportPdfId query string true "pdf研报id" // @Success 200 {object} models.BaseResponse // @router /delete [get] func (this *ImagePDFController) Delete() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() ReportPdfId, _ := this.GetInt("ReportPdfId") reportPdf, err := models.GetReportPdfById(ReportPdfId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "研报不存在或已删除,请刷新页面" return } br.Msg = "获取研报失败" br.ErrMsg = "获取研报失败,系统错误,Err:" + err.Error() return } if reportPdf.State == utils.ReportStatusUp { br.Msg = "研报已发布,不可以删除" return } reportPdf.State = utils.ReportStatusDown err = reportPdf.Delete() if err != nil { br.Msg = "研报删除失败" br.ErrMsg = "研报删除失败,系统错误,Err:" + err.Error() return } // 删除es go func(reportPdf *models.ReportPdf) { reportpdfView := reportPdf.ToView() docId := strconv.Itoa(reportpdfView.ReportPdfId) err = elastic.EsDeleteData(utils.MINI_REPORT_INDEX_NAME, docId) if err != nil { utils.FileLog.Info("pdf研报es删除失败,Err:" + err.Error()) return } utils.FileLog.Info("pdf研报es删除成功, pdfId:" + docId) }(reportPdf) br.Msg = "删除研报成功" br.Ret = 200 br.Success = true } // Detail // @Title 研报详情 // @Description 研报详情 // @Param ReportPdfId query string true "pdf研报id" // @Success 200 {object} models.BaseResponse // @router /detail [get] func (this *ImagePDFController) Detail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() ReportPdfId, _ := this.GetInt("ReportPdfId") reportPdf, err := models.GetReportPdfById(ReportPdfId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "研报不存在或已删除,请刷新页面" return } br.Msg = "获取研报失败" br.ErrMsg = "获取研报失败,系统错误,Err:" + err.Error() return } br.Data = reportPdf br.Ret = 200 br.Success = true br.Msg = "获取研报成功" } const ( img = "image" video = "video" audio = "audio" img_ext = ".jpg|.jpeg|.png|.gif|.bmp|.webp" ) // @Title 上传图片 // @Description 上传图片 // @Param File query file true "文件" // @Success 200 {object} models.ReportAuthorResp // @router /uploadFile [post] func (this *ImagePDFController) UploadImage() { 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 } defer f.Close() size, err := strconv.Atoi(utils.UPLOAD_IMG_SIZE) if err != nil { size = 15 } if h.Size > 1024*int64(size) { br.Msg = fmt.Sprintf("图片大小不能超过%dK", size) return } dateDir := time.Now().Format("20060102") uploadDir := utils.STATIC_DIR + "ht/" + dateDir err = os.MkdirAll(uploadDir, utils.DIR_MOD) if err != nil { br.Msg = "存储目录创建失败" br.ErrMsg = "存储目录创建失败,Err:" + err.Error() return } randStr := utils.GetRandStringNoSpecialChar(28) fileName := randStr fpath := uploadDir + "/" + fileName err = this.SaveToFile("File", fpath) if err != nil { br.Msg = "图片上传失败" br.ErrMsg = "图片上传失败,Err:" + err.Error() return } pdfUploadDir := utils.RESOURCE_DIR + "analyst/" savePdfToOssPath := pdfUploadDir + time.Now().Format("200601/20060102/") imgName := utils.GetRandStringNoSpecialChar(28) savePdfToOssPath += imgName defer func() { _ = os.Remove(fpath) }() ossClient := services.NewOssClient() if ossClient == nil { br.Msg = "图片上传失败" br.ErrMsg = "初始化OSS服务失败" return } imgUrl, err := ossClient.UploadFile("", fpath, savePdfToOssPath) if err != nil { br.Msg = "图片上传失败" br.ErrMsg = "图片上传失败,Err:" + err.Error() return } base := path.Base(h.Filename) resp := new(response.MediaUploadResp) resp.Url = imgUrl resp.FileName = base br.Data = resp br.Msg = "上传成功" br.Ret = 200 br.Success = true }