image_back.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/models/request"
  6. "eta/eta_mini_crm_ht/models/response"
  7. "eta/eta_mini_crm_ht/services"
  8. "eta/eta_mini_crm_ht/services/elastic"
  9. "eta/eta_mini_crm_ht/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. "os"
  13. "path"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type ImagePDFController struct {
  19. BaseAuthController
  20. }
  21. // Author
  22. // @Title 获取报告作者接口
  23. // @Description 获取报告作者
  24. // @Success 200 {object} models.ReportAuthorResp
  25. // @router /author [get]
  26. func (this *ImagePDFController) Author() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. items, err := models.GetReportAuthorList()
  33. if err != nil {
  34. br.Msg = "获取失败!"
  35. br.ErrMsg = "获取失败,Err:" + err.Error()
  36. return
  37. }
  38. br.Ret = 200
  39. br.Success = true
  40. br.Msg = "获取成功"
  41. br.Data = items
  42. }
  43. // Add
  44. // @Title 添加研报
  45. // @Description 添加研报
  46. // @Param request body request.ReportPdfAddReq true "type json string"
  47. // @Success 200 {object} models.ReportAuthorResp
  48. // @router /add [post]
  49. func (this *ImagePDFController) Add() {
  50. br := new(models.BaseResponse).Init()
  51. defer func() {
  52. this.Data["json"] = br
  53. this.ServeJSON()
  54. }()
  55. var req request.ReportPdfAddReq
  56. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  57. br.Msg = "参数解析失败"
  58. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  59. return
  60. }
  61. if req.ClassifyIdFirst <= 0 && req.ClassifyIdSecond <= 0 && req.ClassifyIdThird <= 0 {
  62. br.Msg = "请选择研报所属分类"
  63. return
  64. }
  65. if req.PdfName == "" {
  66. br.Msg = "pdf名称为空"
  67. return
  68. }
  69. var nameFirst, nameSecond, nameThird *models.ClassifyView
  70. var err error
  71. if req.ClassifyIdFirst > 0 {
  72. nameFirst, err = models.GetClassifyById(req.ClassifyIdFirst)
  73. if err != nil {
  74. br.Msg = "添加失败"
  75. br.ErrMsg = "一级类名获取失败,Err:" + err.Error()
  76. return
  77. }
  78. } else {
  79. br.Msg = "该分类不存在或已删除,请刷新重试"
  80. return
  81. }
  82. if req.ClassifyIdSecond > 0 {
  83. nameSecond, err = models.GetClassifyById(req.ClassifyIdSecond)
  84. if err != nil {
  85. br.Msg = "添加失败"
  86. br.ErrMsg = "二级类名获取失败,Err:" + err.Error()
  87. return
  88. }
  89. }
  90. if req.ClassifyIdThird > 0 {
  91. nameThird, err = models.GetClassifyById(req.ClassifyIdThird)
  92. if err != nil {
  93. br.Msg = "添加失败"
  94. br.ErrMsg = "三级类名获取失败,Err:" + err.Error()
  95. return
  96. }
  97. }
  98. pdf := &models.ReportPdf{
  99. PdfUrl: req.PdfUrl,
  100. PdfName: req.PdfName,
  101. Title: req.Title,
  102. Author: req.Author,
  103. Abstract: req.Abstract,
  104. ClassifyIdFirst: req.ClassifyIdFirst,
  105. ClassifyNameFirst: nameFirst.ClassifyName,
  106. PublishTime: time.Now(),
  107. ModifyTime: time.Now(),
  108. SysUserId: this.SysUser.SysUserId,
  109. SysRealName: this.SysUser.SysRealName,
  110. State: utils.ReportStatusUp,
  111. }
  112. if nameSecond != nil {
  113. pdf.ClassifyIdSecond = nameSecond.Id
  114. pdf.ClassifyNameSecond = nameSecond.ClassifyName
  115. }
  116. if nameThird != nil {
  117. pdf.ClassifyIdThird = nameThird.Id
  118. pdf.ClassifyNameThird = nameThird.ClassifyName
  119. }
  120. insertId, err := pdf.Insert()
  121. if err != nil {
  122. br.Msg = "添加失败"
  123. br.ErrMsg = "pdf研报新增失败,Err:" + err.Error()
  124. return
  125. }
  126. pdf.ReportPdfId = int(insertId)
  127. // 添加es
  128. go func(reportPdf *models.ReportPdf) {
  129. reportpdfView := reportPdf.ToView()
  130. docId := strconv.Itoa(reportpdfView.ReportPdfId)
  131. err = elastic.EsAddOrEditReportPdf(utils.MINI_REPORT_INDEX_NAME, docId, reportpdfView)
  132. if err != nil {
  133. utils.FileLog.Info("pdf研报es新增失败,Err:" + err.Error())
  134. return
  135. }
  136. utils.FileLog.Info("pdf研报es新增成功, pdfId:" + docId)
  137. }(pdf)
  138. br.Msg = "添加成功"
  139. br.Ret = 200
  140. br.Success = true
  141. }
  142. // @Title 上传pdf研报
  143. // @Description 上传pdf研报
  144. // @Param File query file true "文件"
  145. // @Success 200 {object} models.ReportAuthorResp
  146. // @router /uploadPdf [post]
  147. func (this *ImagePDFController) UploadPdf() {
  148. br := new(models.BaseResponse).Init()
  149. defer func() {
  150. this.Data["json"] = br
  151. this.ServeJSON()
  152. }()
  153. f, h, err := this.GetFile("File")
  154. if err != nil {
  155. br.Msg = "获取资源信息失败"
  156. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  157. return
  158. }
  159. defer f.Close()
  160. ext := path.Ext(h.Filename)
  161. if ext != ".pdf" {
  162. br.Msg = "文件格式不正确"
  163. return
  164. }
  165. size, err := strconv.Atoi(utils.UPLOAD_PDF_SIZE)
  166. if err != nil {
  167. size = 15
  168. }
  169. if h.Size > 1024*1024*int64(size) {
  170. br.Msg = "文件大小不能超过15M"
  171. return
  172. }
  173. dateDir := time.Now().Format("20060102")
  174. uploadDir := utils.STATIC_DIR + "dongwu/" + dateDir
  175. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  176. if err != nil {
  177. br.Msg = "存储目录创建失败"
  178. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  179. return
  180. }
  181. randStr := utils.GetRandStringNoSpecialChar(28)
  182. fileName := randStr + ext
  183. fpath := uploadDir + "/" + fileName
  184. err = this.SaveToFile("File", fpath)
  185. if err != nil {
  186. br.Msg = "文件上传失败"
  187. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  188. return
  189. }
  190. pdfUploadDir := utils.RESOURCE_DIR + "pdf/"
  191. savePdfToOssPath := pdfUploadDir + time.Now().Format("200601/20060102/")
  192. pptName := utils.GetRandStringNoSpecialChar(28)
  193. savePdfToOssPath += pptName + ".pdf"
  194. defer func() {
  195. _ = os.Remove(fpath)
  196. }()
  197. ossClient := services.NewOssClient()
  198. if ossClient == nil {
  199. br.Msg = "文件上传失败"
  200. br.ErrMsg = "初始化OSS服务失败"
  201. return
  202. }
  203. pdfUrl, err := ossClient.UploadFile("", fpath, savePdfToOssPath)
  204. if err != nil {
  205. br.Msg = "文件上传失败"
  206. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  207. return
  208. }
  209. base := path.Base(h.Filename)
  210. resp := new(response.ReportPdfUploadResp)
  211. resp.Url = pdfUrl
  212. resp.FileName = base
  213. br.Data = resp
  214. br.Msg = "上传成功"
  215. br.Ret = 200
  216. br.Success = true
  217. }
  218. // List
  219. // @Title pdf研报列表
  220. // @Description pdf研报列表
  221. // @Param PageSize query int true "每页数据条数"
  222. // @Param CurrentIndex query int true "当前页页码,从1开始"
  223. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  224. // @Param State query int true "研报状态, 1:已发布 2:未发布"
  225. // @Param PublishStartDate query string true "发布开始时间"
  226. // @Param PublishEndDate query string true "发布结束时间"
  227. // @Param ModifyStartDate query string true "更新开始时间"
  228. // @Param ModifyEndDate query string true "更新结束时间"
  229. // @Param KeyWord query string true "报告标题/创建人"
  230. // @Param SortParam query string true "排序字段"
  231. // @Param SortType query string true "排序方式"
  232. // @Success 200 {object} models.ReportAuthorResp
  233. // @router /list [get]
  234. func (this *ImagePDFController) List() {
  235. br := new(models.BaseResponse).Init()
  236. defer func() {
  237. this.Data["json"] = br
  238. this.ServeJSON()
  239. }()
  240. pageSize, _ := this.GetInt("PageSize")
  241. currentIndex, _ := this.GetInt("CurrentIndex")
  242. classifyIds := this.GetString("ClassifyIds")
  243. state, _ := this.GetInt("State")
  244. publishStartDate := this.GetString("PublishStartDate")
  245. publishEndDate := this.GetString("PublishEndDate")
  246. modifyStartDate := this.GetString("ModifyStartDate")
  247. modifyEndDate := this.GetString("ModifyEndDate")
  248. keyWord := this.GetString("KeyWord")
  249. sortParam := this.GetString("SortParam")
  250. sortType := this.GetString("SortType")
  251. var condition string
  252. var pars []interface{}
  253. if pageSize <= 0 {
  254. pageSize = utils.PageSize20
  255. }
  256. if currentIndex <= 0 {
  257. currentIndex = 1
  258. }
  259. if classifyIds != "" {
  260. classifyArr := strings.Split(classifyIds, ",")
  261. condition += " AND classify_id_second in (" + utils.GetOrmReplaceHolder(len(classifyArr)) + ")"
  262. pars = append(pars, classifyArr)
  263. }
  264. switch state {
  265. case utils.ReportStatusUp:
  266. condition += " AND state = ?"
  267. pars = append(pars, state)
  268. case utils.ReportStatusDown:
  269. condition += " AND state = ?"
  270. pars = append(pars, state)
  271. }
  272. if publishStartDate != "" && publishEndDate != "" {
  273. condition += " AND publish_time >= ?"
  274. publishStartTime, err := time.Parse(utils.FormatDate, publishStartDate)
  275. if err != nil {
  276. br.Msg = "日期格式有误"
  277. br.ErrMsg = "日期格式有误,Err:" + err.Error()
  278. return
  279. }
  280. publishStartDateStr := publishStartTime.Format(utils.FormatDateTime)
  281. pars = append(pars, publishStartDateStr)
  282. condition += " AND publish_time <= ?"
  283. publishEndTime, err := time.Parse(utils.FormatDate, publishEndDate)
  284. if err != nil {
  285. br.Msg = "日期格式有误"
  286. br.ErrMsg = "日期格式有误,Err:" + err.Error()
  287. return
  288. }
  289. publishEndTime = publishEndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  290. publishEndDateStr := publishEndTime.Format(utils.FormatDateTime)
  291. pars = append(pars, publishEndDateStr)
  292. }
  293. if modifyStartDate != "" && modifyEndDate != "" {
  294. condition += " AND modify_time >= ?"
  295. modifyStartTime, err := time.Parse(utils.FormatDate, modifyStartDate)
  296. if err != nil {
  297. br.Msg = "日期格式有误"
  298. br.ErrMsg = "日期格式有误,Err:" + err.Error()
  299. return
  300. }
  301. modifyStartDateStr := modifyStartTime.Format(utils.FormatDateTime)
  302. pars = append(pars, modifyStartDateStr)
  303. condition += " AND modify_time <= ?"
  304. modifyEndTime, err := time.Parse(utils.FormatDate, modifyEndDate)
  305. if err != nil {
  306. br.Msg = "日期格式有误"
  307. br.ErrMsg = "日期格式有误,Err:" + err.Error()
  308. return
  309. }
  310. modifyEndTime = modifyEndTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  311. modifyEndDateStr := modifyEndTime.Format(utils.FormatDateTime)
  312. pars = append(pars, modifyEndDateStr)
  313. }
  314. if keyWord != "" {
  315. condition += ` AND (title like ? OR sys_real_name like ?) `
  316. pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  317. }
  318. var sortCondition string
  319. if sortParam != "" && sortType != "" {
  320. sortCondition = " ORDER BY "
  321. var param, sort string
  322. switch sortParam {
  323. case "PublishTime":
  324. param = "publish_time"
  325. case "ModifyTime":
  326. param = "modify_time"
  327. }
  328. switch sortType {
  329. case "asc":
  330. sort = " ASC "
  331. case "desc":
  332. sort = " DESC "
  333. }
  334. if param != "" && sort != "" {
  335. sortCondition += param + " " + sort
  336. } else {
  337. sortCondition = ""
  338. }
  339. }
  340. if sortCondition == "" {
  341. sortCondition = ` ORDER BY modify_time DESC `
  342. }
  343. total, err := models.GetReportPdfCountByCondition(condition, pars)
  344. if err != nil {
  345. br.Msg = "获取研报列表失败"
  346. br.ErrMsg = "获取研报列表统计失败,Err:" + err.Error()
  347. return
  348. }
  349. startSize := utils.StartIndex(currentIndex, pageSize)
  350. reportList, err := models.GetReportPdfByCondition(condition, sortCondition, pars, startSize, pageSize)
  351. if err != nil {
  352. br.Msg = "获取研报列表失败"
  353. br.ErrMsg = "获取研报列表失败,Err:" + err.Error()
  354. return
  355. }
  356. page := paging.GetPaging(currentIndex, pageSize, total)
  357. resp := new(response.ReportPdfListResp)
  358. resp.List = reportList
  359. resp.Paging = page
  360. br.Ret = 200
  361. br.Success = true
  362. br.Data = resp
  363. br.Msg = "获取成功"
  364. }
  365. // Edit
  366. // @Title 编辑研报
  367. // @Description 编辑研报
  368. // @Param request body request.ReportPdfEditReq true "type json string"
  369. // @Success 200 {object} models.ReportAuthorResp
  370. // @router /edit [post]
  371. func (this *ImagePDFController) Edit() {
  372. br := new(models.BaseResponse).Init()
  373. defer func() {
  374. this.Data["json"] = br
  375. this.ServeJSON()
  376. }()
  377. var req request.ReportPdfEditReq
  378. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  379. br.Msg = "参数错误"
  380. br.ErrMsg = "参数错误,Err:" + err.Error()
  381. return
  382. }
  383. if req.ClassifyIdFirst <= 0 {
  384. br.Msg = "请选择研报所属的一级分类"
  385. return
  386. }
  387. if req.ClassifyIdSecond <= 0 {
  388. br.Msg = "请选择研报所属的二级分类"
  389. return
  390. }
  391. if req.PdfUrl == "" {
  392. br.Msg = "请上传研报文件"
  393. return
  394. }
  395. if req.PdfName == "" {
  396. br.Msg = "请填写研报名称"
  397. return
  398. }
  399. reportPdf, err := models.GetReportPdfById(req.ReportPdfId)
  400. if err != nil {
  401. if err.Error() == utils.ErrNoRow() {
  402. br.Msg = "研报不存在或已删除,请刷新页面"
  403. return
  404. }
  405. br.Msg = "获取研报失败"
  406. br.ErrMsg = "获取研报失败,系统错误,Err:" + err.Error()
  407. return
  408. }
  409. nameFirst, err := models.GetClassifyById(req.ClassifyIdFirst)
  410. if err != nil {
  411. br.Msg = "文件编辑失败"
  412. br.ErrMsg = "一级类名获取失败,Err:" + err.Error()
  413. return
  414. }
  415. nameSecond, err := models.GetClassifyById(req.ClassifyIdSecond)
  416. if err != nil {
  417. br.Msg = "文件编辑失败"
  418. br.ErrMsg = "二级类名获取失败,Err:" + err.Error()
  419. return
  420. }
  421. 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 {
  422. reportPdf.Title = req.Title
  423. reportPdf.PdfName = req.PdfName
  424. reportPdf.ClassifyIdFirst = req.ClassifyIdFirst
  425. reportPdf.ClassifyIdSecond = req.ClassifyIdSecond
  426. reportPdf.ClassifyNameFirst = nameFirst.ClassifyName
  427. reportPdf.ClassifyNameSecond = nameSecond.ClassifyName
  428. reportPdf.Author = req.Author
  429. reportPdf.Abstract = req.Abstract
  430. reportPdf.PdfUrl = req.PdfUrl
  431. reportPdf.ModifyTime = time.Now()
  432. 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"})
  433. if err != nil {
  434. br.Msg = "文件更新失败"
  435. br.ErrMsg = "文件更新失败,Err:" + err.Error()
  436. return
  437. }
  438. // 编辑es
  439. go func(reportPdf *models.ReportPdf) {
  440. reportpdfView := reportPdf.ToView()
  441. docId := strconv.Itoa(reportpdfView.ReportPdfId)
  442. err = elastic.EsAddOrEditReportPdf(utils.MINI_REPORT_INDEX_NAME, docId, reportpdfView)
  443. if err != nil {
  444. utils.FileLog.Info("pdf研报es编辑失败,Err:" + err.Error())
  445. return
  446. }
  447. utils.FileLog.Info("pdf研报es编辑成功, pdfId:" + docId)
  448. }(reportPdf)
  449. }
  450. br.Msg = "研报编辑成功"
  451. br.Ret = 200
  452. br.Success = true
  453. }
  454. // Publish
  455. // @Title 发布研报
  456. // @Description 发布研报
  457. // @Param ReportPdfId query string true "pdf研报id"
  458. // @Success 200 {object} models.BaseResponse
  459. // @router /publish [get]
  460. func (this *ImagePDFController) Publish() {
  461. br := new(models.BaseResponse).Init()
  462. defer func() {
  463. this.Data["json"] = br
  464. this.ServeJSON()
  465. }()
  466. ReportPdfId, _ := this.GetInt("ReportPdfId")
  467. reportPdf, err := models.GetReportPdfById(ReportPdfId)
  468. if err != nil {
  469. if err.Error() == utils.ErrNoRow() {
  470. br.Msg = "研报不存在或已删除,请刷新页面"
  471. return
  472. }
  473. br.Msg = "获取研报失败"
  474. br.ErrMsg = "获取研报失败,系统错误,Err:" + err.Error()
  475. return
  476. }
  477. if reportPdf.State == utils.ReportStatusUp {
  478. br.Msg = "研报已发布"
  479. return
  480. }
  481. reportPdf.State = utils.ReportStatusUp
  482. reportPdf.PublishTime = time.Now()
  483. err = reportPdf.Update([]string{"state", "publish_time"})
  484. if err != nil {
  485. br.Msg = "发布研报失败"
  486. br.ErrMsg = "发布研报失败,系统错误,Err:" + err.Error()
  487. return
  488. }
  489. // 修改es
  490. go func(reportPdf *models.ReportPdf) {
  491. reportpdfView := reportPdf.ToView()
  492. docId := strconv.Itoa(reportpdfView.ReportPdfId)
  493. err = elastic.EsAddOrEditReportPdf(utils.MINI_REPORT_INDEX_NAME, docId, reportpdfView)
  494. if err != nil {
  495. utils.FileLog.Info("pdf研报es发布失败,Err:" + err.Error())
  496. return
  497. }
  498. utils.FileLog.Info("pdf研报es发布成功, pdfId:" + docId)
  499. }(reportPdf)
  500. br.Msg = "发布研报成功"
  501. br.Ret = 200
  502. br.Success = true
  503. }
  504. // PublishCancel
  505. // @Title 取消发布研报
  506. // @Description 取消发布研报
  507. // @Param ReportPdfId query string true "pdf研报id"
  508. // @Success 200 {object} models.BaseResponse
  509. // @router /publishCancel [get]
  510. func (this *ImagePDFController) PublishCancel() {
  511. br := new(models.BaseResponse).Init()
  512. defer func() {
  513. this.Data["json"] = br
  514. this.ServeJSON()
  515. }()
  516. ReportPdfId, _ := this.GetInt("ReportPdfId")
  517. reportPdf, err := models.GetReportPdfById(ReportPdfId)
  518. if err != nil {
  519. if err.Error() == utils.ErrNoRow() {
  520. br.Msg = "研报不存在或已删除,请刷新页面"
  521. return
  522. }
  523. br.Msg = "获取研报失败"
  524. br.ErrMsg = "获取研报失败,系统错误,Err:" + err.Error()
  525. return
  526. }
  527. if reportPdf.State == utils.ReportStatusDown {
  528. br.Msg = "研报已撤销"
  529. return
  530. }
  531. reportPdf.State = utils.ReportStatusDown
  532. err = reportPdf.Update([]string{"state"})
  533. if err != nil {
  534. br.Msg = "发布研报失败"
  535. br.ErrMsg = "发布研报失败,系统错误,Err:" + err.Error()
  536. return
  537. }
  538. // 修改es
  539. go func(reportPdf *models.ReportPdf) {
  540. reportpdfView := reportPdf.ToView()
  541. docId := strconv.Itoa(reportpdfView.ReportPdfId)
  542. err = elastic.EsAddOrEditReportPdf(utils.MINI_REPORT_INDEX_NAME, docId, reportpdfView)
  543. if err != nil {
  544. utils.FileLog.Info("pdf研报es取消发布失败,Err:" + err.Error())
  545. return
  546. }
  547. utils.FileLog.Info("pdf研报es取消发布成功, pdfId:" + docId)
  548. }(reportPdf)
  549. br.Msg = "撤销研报成功"
  550. br.Ret = 200
  551. br.Success = true
  552. }
  553. // delete
  554. // @Title 删除研报
  555. // @Description 删除研报
  556. // @Param ReportPdfId query string true "pdf研报id"
  557. // @Success 200 {object} models.BaseResponse
  558. // @router /delete [get]
  559. func (this *ImagePDFController) Delete() {
  560. br := new(models.BaseResponse).Init()
  561. defer func() {
  562. this.Data["json"] = br
  563. this.ServeJSON()
  564. }()
  565. ReportPdfId, _ := this.GetInt("ReportPdfId")
  566. reportPdf, err := models.GetReportPdfById(ReportPdfId)
  567. if err != nil {
  568. if err.Error() == utils.ErrNoRow() {
  569. br.Msg = "研报不存在或已删除,请刷新页面"
  570. return
  571. }
  572. br.Msg = "获取研报失败"
  573. br.ErrMsg = "获取研报失败,系统错误,Err:" + err.Error()
  574. return
  575. }
  576. if reportPdf.State == utils.ReportStatusUp {
  577. br.Msg = "研报已发布,不可以删除"
  578. return
  579. }
  580. reportPdf.State = utils.ReportStatusDown
  581. err = reportPdf.Delete()
  582. if err != nil {
  583. br.Msg = "研报删除失败"
  584. br.ErrMsg = "研报删除失败,系统错误,Err:" + err.Error()
  585. return
  586. }
  587. // 删除es
  588. go func(reportPdf *models.ReportPdf) {
  589. reportpdfView := reportPdf.ToView()
  590. docId := strconv.Itoa(reportpdfView.ReportPdfId)
  591. err = elastic.EsDeleteData(utils.MINI_REPORT_INDEX_NAME, docId)
  592. if err != nil {
  593. utils.FileLog.Info("pdf研报es删除失败,Err:" + err.Error())
  594. return
  595. }
  596. utils.FileLog.Info("pdf研报es删除成功, pdfId:" + docId)
  597. }(reportPdf)
  598. br.Msg = "删除研报成功"
  599. br.Ret = 200
  600. br.Success = true
  601. }
  602. // Detail
  603. // @Title 研报详情
  604. // @Description 研报详情
  605. // @Param ReportPdfId query string true "pdf研报id"
  606. // @Success 200 {object} models.BaseResponse
  607. // @router /detail [get]
  608. func (this *ImagePDFController) Detail() {
  609. br := new(models.BaseResponse).Init()
  610. defer func() {
  611. this.Data["json"] = br
  612. this.ServeJSON()
  613. }()
  614. ReportPdfId, _ := this.GetInt("ReportPdfId")
  615. reportPdf, err := models.GetReportPdfById(ReportPdfId)
  616. if err != nil {
  617. if err.Error() == utils.ErrNoRow() {
  618. br.Msg = "研报不存在或已删除,请刷新页面"
  619. return
  620. }
  621. br.Msg = "获取研报失败"
  622. br.ErrMsg = "获取研报失败,系统错误,Err:" + err.Error()
  623. return
  624. }
  625. br.Data = reportPdf
  626. br.Ret = 200
  627. br.Success = true
  628. br.Msg = "获取研报成功"
  629. }
  630. const (
  631. img = "image"
  632. video = "video"
  633. audio = "audio"
  634. img_ext = ".jpg|.jpeg|.png|.gif|.bmp|.webp"
  635. )
  636. // @Title 上传图片
  637. // @Description 上传图片
  638. // @Param File query file true "文件"
  639. // @Success 200 {object} models.ReportAuthorResp
  640. // @router /uploadFile [post]
  641. func (this *ImagePDFController) UploadImage() {
  642. br := new(models.BaseResponse).Init()
  643. defer func() {
  644. this.Data["json"] = br
  645. this.ServeJSON()
  646. }()
  647. f, h, err := this.GetFile("File")
  648. if err != nil {
  649. br.Msg = "获取资源信息失败"
  650. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  651. return
  652. }
  653. defer f.Close()
  654. size, err := strconv.Atoi(utils.UPLOAD_IMG_SIZE)
  655. if err != nil {
  656. size = 15
  657. }
  658. if h.Size > 1024*int64(size) {
  659. br.Msg = fmt.Sprintf("图片大小不能超过%dK", size)
  660. return
  661. }
  662. dateDir := time.Now().Format("20060102")
  663. uploadDir := utils.STATIC_DIR + "ht/" + dateDir
  664. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  665. if err != nil {
  666. br.Msg = "存储目录创建失败"
  667. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  668. return
  669. }
  670. randStr := utils.GetRandStringNoSpecialChar(28)
  671. fileName := randStr
  672. fpath := uploadDir + "/" + fileName
  673. err = this.SaveToFile("File", fpath)
  674. if err != nil {
  675. br.Msg = "图片上传失败"
  676. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  677. return
  678. }
  679. pdfUploadDir := utils.RESOURCE_DIR + "analyst/"
  680. savePdfToOssPath := pdfUploadDir + time.Now().Format("200601/20060102/")
  681. imgName := utils.GetRandStringNoSpecialChar(28)
  682. savePdfToOssPath += imgName
  683. defer func() {
  684. _ = os.Remove(fpath)
  685. }()
  686. ossClient := services.NewOssClient()
  687. if ossClient == nil {
  688. br.Msg = "图片上传失败"
  689. br.ErrMsg = "初始化OSS服务失败"
  690. return
  691. }
  692. imgUrl, err := ossClient.UploadFile("", fpath, savePdfToOssPath)
  693. if err != nil {
  694. br.Msg = "图片上传失败"
  695. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  696. return
  697. }
  698. base := path.Base(h.Filename)
  699. resp := new(response.MediaUploadResp)
  700. resp.Url = imgUrl
  701. resp.FileName = base
  702. br.Data = resp
  703. br.Msg = "上传成功"
  704. br.Ret = 200
  705. br.Success = true
  706. }