smart_report.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. package smart_report
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/smart_report"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services"
  9. "eta/eta_api/services/data"
  10. smartReportService "eta/eta_api/services/smart_report"
  11. "eta/eta_api/utils"
  12. "fmt"
  13. "github.com/kgiannakakis/mp3duration/src/mp3duration"
  14. "github.com/rdlucklib/rdluck_tools/paging"
  15. "html"
  16. "io/ioutil"
  17. "os"
  18. "path"
  19. "strings"
  20. "time"
  21. )
  22. // SmartReportController 智能研报
  23. type SmartReportController struct {
  24. controllers.BaseAuthController
  25. }
  26. // Add
  27. // @Title 新增
  28. // @Description 新增
  29. // @Param request body smart_report.SmartReportAddReq true "type json string"
  30. // @Success 200 {object} smart_report.SmartReportItem
  31. // @router /add [post]
  32. func (this *SmartReportController) Add() {
  33. br := new(models.BaseResponse).Init()
  34. defer func() {
  35. if br.ErrMsg == "" {
  36. br.IsSendEmail = false
  37. }
  38. this.Data["json"] = br
  39. this.ServeJSON()
  40. }()
  41. sysUser := this.SysUser
  42. if sysUser == nil {
  43. br.Msg = "请登录"
  44. br.ErrMsg = "请登录,SysUser Is Empty"
  45. br.Ret = 408
  46. return
  47. }
  48. var req smart_report.SmartReportAddReq
  49. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  50. if err != nil {
  51. br.Msg = "参数解析异常!"
  52. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  53. return
  54. }
  55. if req.AddType != 1 && req.AddType != 2 {
  56. br.Msg = "请选择新增方式"
  57. return
  58. }
  59. if req.ClassifyIdFirst <= 0 || req.ClassifyIdSecond <= 0 {
  60. br.Msg = "请选择分类"
  61. return
  62. }
  63. req.Title = strings.TrimSpace(req.Title)
  64. if req.Title == "" {
  65. br.Msg = "请输入标题"
  66. return
  67. }
  68. reportOB := new(smart_report.SmartReport)
  69. stageMax, e := reportOB.GetMaxStageByClassifyId(req.ClassifyIdSecond)
  70. if e != nil {
  71. br.Msg = "操作失败"
  72. br.ErrMsg = "获取期数失败, Err: " + e.Error()
  73. return
  74. }
  75. stageMax += 1
  76. item := new(smart_report.SmartReport)
  77. item.AddType = req.AddType
  78. item.ClassifyIdFirst = req.ClassifyIdFirst
  79. item.ClassifyNameFirst = req.ClassifyNameFirst
  80. item.ClassifyIdSecond = req.ClassifyIdSecond
  81. item.ClassifyNameSecond = req.ClassifyNameSecond
  82. item.Title = req.Title
  83. item.Abstract = req.Abstract
  84. item.Author = req.Author
  85. item.Frequency = req.Frequency
  86. item.Stage = stageMax
  87. item.AdminId = sysUser.AdminId
  88. item.AdminRealName = sysUser.RealName
  89. item.LastModifyAdminId = sysUser.AdminId
  90. item.LastModifyAdminName = sysUser.RealName
  91. item.State = smart_report.SmartReportStateWaitPublish
  92. item.CreateTime = time.Now().Local()
  93. item.ModifyTime = time.Now().Local()
  94. item.HeadImg = req.HeadImg
  95. item.EndImg = req.EndImg
  96. item.CanvasColor = req.CanvasColor
  97. // 继承报告
  98. if req.AddType == 2 {
  99. ob := new(smart_report.SmartReport)
  100. cond := ` AND classify_id_first = ? AND classify_id_second = ?`
  101. pars := make([]interface{}, 0)
  102. pars = append(pars, req.ClassifyIdFirst, req.ClassifyIdSecond)
  103. lastReport, e := ob.GetItemByCondition(cond, pars, "stage DESC")
  104. if e != nil && e.Error() != utils.ErrNoRow() {
  105. br.Msg = "获取失败"
  106. br.ErrMsg = "获取往期研报失败, Err: " + e.Error()
  107. return
  108. }
  109. if lastReport != nil {
  110. item.Content = lastReport.Content
  111. item.ContentSub = lastReport.ContentSub
  112. item.ContentStruct = lastReport.ContentStruct
  113. }
  114. }
  115. if e = item.Create(); e != nil {
  116. br.Msg = "操作失败"
  117. br.ErrMsg = "新增研报失败, Err: " + e.Error()
  118. return
  119. }
  120. uniqueCode := utils.MD5(fmt.Sprint("smart_", item.SmartReportId))
  121. item.ReportCode = uniqueCode
  122. if e = item.Update([]string{"ReportCode"}); e != nil {
  123. br.Msg = "操作失败"
  124. br.ErrMsg = "更新研报编码失败, Err: " + e.Error()
  125. return
  126. }
  127. resp := smart_report.FormatSmartReport2Item(item)
  128. recordItem := &models.ReportStateRecord{
  129. ReportId: item.SmartReportId,
  130. ReportType: 2,
  131. State: smart_report.SmartReportStateWaitPublish,
  132. AdminId: this.SysUser.AdminId,
  133. AdminName: this.SysUser.AdminName,
  134. CreateTime: time.Now(),
  135. }
  136. go func() {
  137. _, _ = models.AddReportStateRecord(recordItem)
  138. }()
  139. br.Ret = 200
  140. br.Success = true
  141. br.Msg = "操作成功"
  142. br.Data = resp
  143. }
  144. // Edit
  145. // @Title 编辑
  146. // @Description 编辑
  147. // @Param request body smart_report.SmartReportEditReq true "type json string"
  148. // @Success 200 {object} smart_report.SmartReportItem
  149. // @router /edit [post]
  150. func (this *SmartReportController) Edit() {
  151. br := new(models.BaseResponse).Init()
  152. defer func() {
  153. if br.ErrMsg == "" {
  154. br.IsSendEmail = false
  155. }
  156. this.Data["json"] = br
  157. this.ServeJSON()
  158. }()
  159. sysUser := this.SysUser
  160. if sysUser == nil {
  161. br.Msg = "请登录"
  162. br.ErrMsg = "请登录,SysUser Is Empty"
  163. br.Ret = 408
  164. return
  165. }
  166. var req smart_report.SmartReportEditReq
  167. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  168. if err != nil {
  169. br.Msg = "参数解析异常!"
  170. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  171. return
  172. }
  173. if req.SmartReportId <= 0 {
  174. br.Msg = "参数有误"
  175. br.ErrMsg = "报告ID为空"
  176. return
  177. }
  178. if req.ClassifyIdFirst <= 0 || req.ClassifyIdSecond <= 0 {
  179. br.Msg = "请选择分类"
  180. return
  181. }
  182. req.Title = strings.TrimSpace(req.Title)
  183. if req.Title == "" {
  184. br.Msg = "请输入标题"
  185. return
  186. }
  187. var subContent string
  188. if req.Content != "" {
  189. req.Content = html.EscapeString(req.Content)
  190. // 前两个章节
  191. sub, e := services.GetReportContentSub(req.Content)
  192. if e != nil {
  193. br.Msg = "操作失败"
  194. br.ErrMsg = "读取报告前两个章节内容失败, Err: " + e.Error()
  195. return
  196. }
  197. subContent = html.EscapeString(sub)
  198. }
  199. req.ContentStruct = html.EscapeString(req.ContentStruct)
  200. ob := new(smart_report.SmartReport)
  201. item, e := ob.GetItemById(req.SmartReportId)
  202. if e != nil {
  203. if e.Error() == utils.ErrNoRow() {
  204. br.Msg = "报告不存在, 请刷新页面"
  205. return
  206. }
  207. br.Msg = "操作失败"
  208. br.ErrMsg = "获取研报失败, Err: " + e.Error()
  209. return
  210. }
  211. if item.State == 2 {
  212. br.Msg = "报告已发布, 请取消发布后编辑"
  213. return
  214. }
  215. // 内容是否变更, 只有内容产生变更时, 才更新最后更新人和内容更新时间字段
  216. contentModify := false
  217. if item.ClassifyIdFirst != req.ClassifyIdFirst ||
  218. item.ClassifyIdSecond != req.ClassifyIdSecond ||
  219. item.Title != req.Title ||
  220. item.Abstract != req.Abstract ||
  221. item.Frequency != req.Frequency ||
  222. item.Author != req.Author ||
  223. utils.MD5(item.Content) != utils.MD5(req.Content) {
  224. contentModify = true
  225. }
  226. cols := []string{"ClassifyIdFirst", "ClassifyNameFirst", "ClassifyIdSecond", "ClassifyNameSecond", "Title", "Abstract", "Author",
  227. "Frequency", "Content", "ContentSub", "ContentStruct", "ModifyTime", "HeadImg", "EndImg", "CanvasColor"}
  228. item.ClassifyIdFirst = req.ClassifyIdFirst
  229. item.ClassifyNameFirst = req.ClassifyNameFirst
  230. item.ClassifyIdSecond = req.ClassifyIdSecond
  231. item.ClassifyNameSecond = req.ClassifyNameSecond
  232. item.Title = req.Title
  233. item.Abstract = req.Abstract
  234. item.Author = req.Author
  235. item.Frequency = req.Frequency
  236. item.Content = req.Content
  237. item.ContentSub = subContent
  238. item.ContentStruct = req.ContentStruct
  239. item.ModifyTime = time.Now().Local()
  240. item.HeadImg = req.HeadImg
  241. item.EndImg = req.EndImg
  242. item.CanvasColor = req.CanvasColor
  243. if contentModify {
  244. //fmt.Println(contentModify)
  245. item.LastModifyAdminId = sysUser.AdminId
  246. item.LastModifyAdminName = sysUser.RealName
  247. item.ContentModifyTime = time.Now().Local()
  248. cols = append(cols, "LastModifyAdminId", "LastModifyAdminName", "ContentModifyTime")
  249. }
  250. if e := item.Update(cols); e != nil {
  251. br.Msg = "操作失败"
  252. br.ErrMsg = "更新报告失败, Err: " + e.Error()
  253. return
  254. }
  255. resp := smart_report.FormatSmartReport2Item(item)
  256. br.Ret = 200
  257. br.Success = true
  258. br.Msg = "操作成功"
  259. br.Data = resp
  260. }
  261. // Remove
  262. // @Title 删除
  263. // @Description 删除
  264. // @Param request body smart_report.SmartReportRemoveReq true "type json string"
  265. // @Success 200 string "操作成功"
  266. // @router /remove [post]
  267. func (this *SmartReportController) Remove() {
  268. br := new(models.BaseResponse).Init()
  269. defer func() {
  270. if br.ErrMsg == "" {
  271. br.IsSendEmail = false
  272. }
  273. this.Data["json"] = br
  274. this.ServeJSON()
  275. }()
  276. sysUser := this.SysUser
  277. if sysUser == nil {
  278. br.Msg = "请登录"
  279. br.ErrMsg = "请登录,SysUser Is Empty"
  280. br.Ret = 408
  281. return
  282. }
  283. var req smart_report.SmartReportRemoveReq
  284. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  285. if err != nil {
  286. br.Msg = "参数解析异常!"
  287. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  288. return
  289. }
  290. if req.SmartReportId <= 0 {
  291. br.Msg = "参数有误"
  292. br.ErrMsg = "报告ID为空"
  293. return
  294. }
  295. ob := new(smart_report.SmartReport)
  296. item, e := ob.GetItemById(req.SmartReportId)
  297. if e != nil {
  298. if e.Error() == utils.ErrNoRow() {
  299. br.Ret = 200
  300. br.Success = true
  301. br.Msg = "操作成功"
  302. return
  303. }
  304. br.Msg = "操作失败"
  305. br.ErrMsg = "获取研报失败, Err: " + e.Error()
  306. return
  307. }
  308. if e = item.Del(); e != nil {
  309. br.Msg = "操作失败"
  310. br.ErrMsg = "删除报告失败, Err: " + e.Error()
  311. return
  312. }
  313. // ES更新报告为未发布
  314. go func() {
  315. _ = smartReportService.SmartReportElasticUpsert(item.SmartReportId, 1)
  316. }()
  317. br.Ret = 200
  318. br.Success = true
  319. br.Msg = "操作成功"
  320. }
  321. // Detail
  322. // @Title 详情
  323. // @Description 详情
  324. // @Param SmartReportId query int true "智能研报ID"
  325. // @Success 200 {object} smart_report.SmartReportItem
  326. // @router /detail [get]
  327. func (this *SmartReportController) Detail() {
  328. br := new(models.BaseResponse).Init()
  329. defer func() {
  330. if br.ErrMsg == "" {
  331. br.IsSendEmail = false
  332. }
  333. this.Data["json"] = br
  334. this.ServeJSON()
  335. }()
  336. sysUser := this.SysUser
  337. if sysUser == nil {
  338. br.Msg = "请登录"
  339. br.ErrMsg = "请登录,SysUser Is Empty"
  340. br.Ret = 408
  341. return
  342. }
  343. reportId, _ := this.GetInt("SmartReportId")
  344. if reportId <= 0 {
  345. br.Msg = "参数有误"
  346. br.ErrMsg = "报告ID有误"
  347. return
  348. }
  349. ob := new(smart_report.SmartReport)
  350. item, e := ob.GetItemById(reportId)
  351. if e != nil {
  352. if e.Error() == utils.ErrNoRow() {
  353. br.Msg = "报告不存在, 请刷新页面"
  354. return
  355. }
  356. br.Msg = "操作失败"
  357. br.ErrMsg = "获取研报失败, Err: " + e.Error()
  358. return
  359. }
  360. resp := smart_report.FormatSmartReport2Item(item)
  361. br.Ret = 200
  362. br.Success = true
  363. br.Msg = "获取成功"
  364. br.Data = resp
  365. }
  366. // Publish
  367. // @Title 发布/取消发布
  368. // @Description 发布/取消发布
  369. // @Param request body smart_report.SmartReportPublishReq true "type json string"
  370. // @Success 200 string "操作成功"
  371. // @router /publish [post]
  372. func (this *SmartReportController) Publish() {
  373. br := new(models.BaseResponse).Init()
  374. defer func() {
  375. if br.ErrMsg == "" {
  376. br.IsSendEmail = false
  377. }
  378. this.Data["json"] = br
  379. this.ServeJSON()
  380. }()
  381. sysUser := this.SysUser
  382. if sysUser == nil {
  383. br.Msg = "请登录"
  384. br.ErrMsg = "请登录,SysUser Is Empty"
  385. br.Ret = 408
  386. return
  387. }
  388. var req smart_report.SmartReportPublishReq
  389. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  390. if err != nil {
  391. br.Msg = "参数解析异常!"
  392. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  393. return
  394. }
  395. if req.SmartReportId <= 0 {
  396. br.Msg = "参数有误"
  397. br.ErrMsg = "报告ID为空"
  398. return
  399. }
  400. if req.PublishState != smart_report.SmartReportStateWaitPublish && req.PublishState != smart_report.SmartReportStatePublished {
  401. br.Msg = "参数有误"
  402. return
  403. }
  404. ob := new(smart_report.SmartReport)
  405. item, e := ob.GetItemById(req.SmartReportId)
  406. if e != nil {
  407. if e.Error() == utils.ErrNoRow() {
  408. br.Msg = "报告不存在, 请刷新页面"
  409. return
  410. }
  411. br.Msg = "操作失败"
  412. br.ErrMsg = "获取研报失败, Err: " + e.Error()
  413. return
  414. }
  415. // 图表刷新状态
  416. refreshResult := data.CheckBatchChartRefreshResult("report", item.SmartReportId, 0)
  417. if !refreshResult {
  418. br.Msg = "图表刷新未完成,请稍后操作"
  419. br.ErrMsg = "图表刷新未完成,请稍后操作"
  420. br.IsSendEmail = false
  421. return
  422. }
  423. cols := []string{"State", "ModifyTime"}
  424. item.State = req.PublishState
  425. item.ModifyTime = time.Now().Local()
  426. if req.PublishState == smart_report.SmartReportStatePublished {
  427. cols = append(cols, "PublishTime")
  428. item.PublishTime = time.Now().Local()
  429. // 写入队列
  430. var queue smart_report.Report2ImgQueueReq
  431. queue.ReportType = 2
  432. queue.ReportCode = item.ReportCode
  433. _ = utils.Rc.LPush(utils.CACHE_CREATE_REPORT_IMGPDF_QUEUE, queue)
  434. }
  435. // 取消发布时同时清除掉Img和PDF的文件地址
  436. if req.PublishState == smart_report.SmartReportStateWaitPublish {
  437. cols = append(cols, "DetailImgUrl", "DetailPdfUrl")
  438. item.DetailImgUrl = ""
  439. item.DetailPdfUrl = ""
  440. }
  441. if e = item.Update(cols); e != nil {
  442. br.Msg = "操作失败"
  443. br.ErrMsg = "更新研报失败, Err: " + e.Error()
  444. return
  445. }
  446. recordItem := &models.ReportStateRecord{
  447. ReportId: req.SmartReportId,
  448. ReportType: 2,
  449. State: req.PublishState,
  450. AdminId: this.SysUser.AdminId,
  451. AdminName: this.SysUser.AdminName,
  452. CreateTime: time.Now(),
  453. }
  454. go func() {
  455. _, _ = models.AddReportStateRecord(recordItem)
  456. }()
  457. // 生成音频
  458. if req.PublishState == smart_report.SmartReportStatePublished && item.VideoUrl == "" {
  459. go smartReportService.SmartReportBuildVideoAndUpdate(item)
  460. }
  461. // ES更新报告
  462. go func() {
  463. _ = smartReportService.SmartReportElasticUpsert(item.SmartReportId, req.PublishState)
  464. }()
  465. br.Ret = 200
  466. br.Success = true
  467. br.Msg = "操作成功"
  468. }
  469. // PrePublish
  470. // @Title 设置定时发布
  471. // @Description 设置定时发布
  472. // @Param request body smart_report.SmartReportPrePublishReq true "type json string"
  473. // @Success 200 string "操作成功"
  474. // @router /pre_publish [post]
  475. func (this *SmartReportController) PrePublish() {
  476. br := new(models.BaseResponse).Init()
  477. defer func() {
  478. if br.ErrMsg == "" {
  479. br.IsSendEmail = false
  480. }
  481. this.Data["json"] = br
  482. this.ServeJSON()
  483. }()
  484. sysUser := this.SysUser
  485. if sysUser == nil {
  486. br.Msg = "请登录"
  487. br.ErrMsg = "请登录,SysUser Is Empty"
  488. br.Ret = 408
  489. return
  490. }
  491. var req smart_report.SmartReportPrePublishReq
  492. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  493. if err != nil {
  494. br.Msg = "参数解析异常!"
  495. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  496. return
  497. }
  498. reportId := req.SmartReportId
  499. if reportId == 0 {
  500. br.Msg = "参数错误"
  501. br.ErrMsg = "参数错误,报告id不可为空"
  502. return
  503. }
  504. if req.PrePublishTime == "" {
  505. br.Msg = "发布时间不能为空"
  506. return
  507. }
  508. if req.PreMsgSend != 0 && req.PreMsgSend != 1 {
  509. br.Msg = "参数错误"
  510. br.ErrMsg = "是否发送模版消息标识错误"
  511. return
  512. }
  513. preTime, e := time.ParseInLocation(utils.FormatDateTime, req.PrePublishTime, time.Local)
  514. if e != nil {
  515. br.Msg = "发布时间格式错误"
  516. br.ErrMsg = "发布时间格式错误,Err:" + e.Error()
  517. return
  518. }
  519. if preTime.Before(time.Now()) {
  520. br.Msg = "发布时间不允许选择过去时间"
  521. return
  522. }
  523. if preTime.Before(time.Now().Add(2 * time.Minute)) {
  524. br.Msg = "发布时间距离当前时间太近了"
  525. return
  526. }
  527. reportOB := new(smart_report.SmartReport)
  528. item, e := reportOB.GetItemById(reportId)
  529. if e != nil {
  530. if e.Error() == utils.ErrNoRow() {
  531. br.Msg = "报告不存在, 请刷新页面"
  532. return
  533. }
  534. br.Msg = "获取报告失败"
  535. br.ErrMsg = "获取报告失败, Err:" + e.Error()
  536. return
  537. }
  538. if item.Content == "" {
  539. br.Msg = "报告内容为空, 不可发布"
  540. return
  541. }
  542. if item.State == 2 {
  543. br.Msg = "报告已发布, 不可设置定时发布"
  544. return
  545. }
  546. item.PrePublishTime = preTime
  547. item.PreMsgSend = req.PreMsgSend
  548. cols := []string{"PrePublishTime", "PreMsgSend"}
  549. if e = item.Update(cols); e != nil {
  550. br.Msg = "操作失败"
  551. br.ErrMsg = "更新报告预发布失败, Err: " + e.Error()
  552. return
  553. }
  554. br.Ret = 200
  555. br.Success = true
  556. br.Msg = "操作成功"
  557. }
  558. // SendMsg
  559. // @Title 消息推送
  560. // @Description 消息推送
  561. // @Param request body models.SendTemplateMsgReq true "type json string"
  562. // @Success 200 Ret=200 推送成功
  563. // @router /send_msg [post]
  564. func (this *SmartReportController) SendMsg() {
  565. br := new(models.BaseResponse).Init()
  566. defer func() {
  567. if br.ErrMsg == "" {
  568. br.IsSendEmail = false
  569. }
  570. this.Data["json"] = br
  571. this.ServeJSON()
  572. }()
  573. sysUser := this.SysUser
  574. if sysUser == nil {
  575. br.Msg = "请登录"
  576. br.ErrMsg = "请登录,SysUser Is Empty"
  577. br.Ret = 408
  578. return
  579. }
  580. var req smart_report.SmartReportSendMsgReq
  581. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  582. if err != nil {
  583. br.Msg = "参数解析异常!"
  584. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  585. return
  586. }
  587. if req.SmartReportId <= 0 {
  588. br.Msg = "参数有误"
  589. br.ErrMsg = fmt.Sprintf("参数有误, SmartReportId: %d", req.SmartReportId)
  590. return
  591. }
  592. // 避免重复推送
  593. {
  594. redisKey := fmt.Sprint(utils.CACHE_SMART_REPORT_SEND_MSG, req.SmartReportId)
  595. ok := utils.Rc.SetNX(redisKey, 1, time.Second*300)
  596. if !ok {
  597. br.Msg = "报告已推送, 请勿重复推送"
  598. return
  599. }
  600. defer func() {
  601. _ = utils.Rc.Delete(redisKey)
  602. }()
  603. }
  604. reportOB := new(smart_report.SmartReport)
  605. item, e := reportOB.GetItemById(req.SmartReportId)
  606. if e != nil {
  607. if e.Error() == utils.ErrNoRow() {
  608. br.Msg = "报告不存在, 请刷新页面"
  609. return
  610. }
  611. br.Msg = "操作失败"
  612. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  613. return
  614. }
  615. if item.MsgIsSend == 1 {
  616. br.Msg = "消息已推送,请勿重复操作"
  617. return
  618. }
  619. if item.State != 2 {
  620. br.Msg = "报告未发布, 不可推送"
  621. return
  622. }
  623. item.MsgIsSend = 1
  624. item.MsgSendTime = time.Now().Local()
  625. item.ModifyTime = time.Now().Local()
  626. if e = item.Update([]string{"MsgIsSend", "MsgSendTime", "ModifyTime"}); e != nil {
  627. br.Msg = "操作失败"
  628. br.ErrMsg = "更新报告推送状态失败, Err: " + e.Error()
  629. return
  630. }
  631. br.Ret = 200
  632. br.Success = true
  633. br.Msg = "操作成功"
  634. br.IsAddLog = true
  635. }
  636. // SaveContent
  637. // @Title 保存草稿
  638. // @Description 保存草稿
  639. // @Param request body smart_report.SmartReportSaveContentReq true "type json string"
  640. // @Success 200 {object} smart_report.SmartReportSaveContentResp
  641. // @router /save_content [post]
  642. func (this *SmartReportController) SaveContent() {
  643. br := new(models.BaseResponse).Init()
  644. defer func() {
  645. if br.ErrMsg == "" {
  646. br.IsSendEmail = false
  647. }
  648. this.Data["json"] = br
  649. this.ServeJSON()
  650. }()
  651. sysUser := this.SysUser
  652. if sysUser == nil {
  653. br.Msg = "请登录"
  654. br.ErrMsg = "请登录,SysUser Is Empty"
  655. br.Ret = 408
  656. return
  657. }
  658. var req smart_report.SmartReportSaveContentReq
  659. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  660. if err != nil {
  661. br.Msg = "参数解析异常!"
  662. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  663. return
  664. }
  665. resp := new(smart_report.SmartReportSaveContentResp)
  666. if req.SmartReportId <= 0 {
  667. br.Ret = 200
  668. br.Success = true
  669. br.Msg = "操作成功"
  670. br.Data = resp
  671. return
  672. }
  673. reportOB := new(smart_report.SmartReport)
  674. item, _ := reportOB.GetItemById(req.SmartReportId)
  675. if item == nil {
  676. br.Ret = 200
  677. br.Success = true
  678. br.Msg = "操作成功"
  679. br.Data = resp
  680. return
  681. }
  682. if item.State == smart_report.SmartReportStatePublished {
  683. br.Msg = "报告已发布, 不允许编辑"
  684. return
  685. }
  686. // 更新编辑状态
  687. adminIdName := make(map[int]string)
  688. admins, e := system.GetSysAdminList(``, make([]interface{}, 0), []string{}, "")
  689. if e != nil {
  690. br.Msg = "获取失败"
  691. br.ErrMsg = "获取系统用户列表失败, Err: " + e.Error()
  692. return
  693. }
  694. for _, ad := range admins {
  695. adminIdName[ad.AdminId] = ad.RealName
  696. }
  697. editing, e := smartReportService.UpdateSmartReportEditing(req.SmartReportId, 1, sysUser.AdminId, sysUser.RealName, adminIdName)
  698. if e != nil {
  699. br.Msg = e.Error()
  700. return
  701. }
  702. if editing.Status == 1 {
  703. br.Msg = editing.Msg
  704. return
  705. }
  706. // 内容有改动
  707. if req.NoChange != 1 && req.Content != "" && req.ContentStruct != "" {
  708. req.Content = html.EscapeString(req.Content)
  709. req.ContentStruct = html.EscapeString(req.ContentStruct)
  710. sub, e := services.GetReportContentSub(req.Content)
  711. if e != nil {
  712. br.Msg = "操作失败"
  713. br.ErrMsg = "读取报告前两个章节内容失败, Err: " + e.Error()
  714. return
  715. }
  716. subContent := html.EscapeString(sub)
  717. item.Content = req.Content
  718. item.ContentSub = subContent
  719. item.ContentStruct = req.ContentStruct
  720. item.ContentModifyTime = time.Now().Local()
  721. item.LastModifyAdminId = sysUser.AdminId
  722. item.LastModifyAdminName = sysUser.RealName
  723. item.ModifyTime = time.Now().Local()
  724. item.HeadImg = req.HeadImg
  725. item.EndImg = req.EndImg
  726. item.CanvasColor = req.CanvasColor
  727. cols := []string{"Content", "ContentSub", "ContentStruct", "ContentModifyTime", "LastModifyAdminId", "LastModifyAdminName", "ModifyTime", "HeadImg", "EndImg", "CanvasColor"}
  728. if e = item.Update(cols); e != nil {
  729. br.Msg = "操作失败"
  730. br.ErrMsg = "更新报告内容失败"
  731. return
  732. }
  733. go func() {
  734. saveLog := new(smart_report.SmartReportSaveLog)
  735. saveLog.SmartReportId = item.SmartReportId
  736. saveLog.Content = item.Content
  737. saveLog.ContentSub = item.ContentSub
  738. saveLog.ContentStruct = item.ContentStruct
  739. saveLog.AdminId = sysUser.AdminId
  740. saveLog.AdminName = sysUser.RealName
  741. saveLog.CreateTime = time.Now().Local()
  742. _ = saveLog.Create()
  743. }()
  744. }
  745. resp.SmartReportId = item.SmartReportId
  746. br.Ret = 200
  747. br.Success = true
  748. br.Msg = "保存成功"
  749. br.Data = resp
  750. }
  751. // MarkEditStatus
  752. // @Title 标记报告编辑状态
  753. // @Description 标记报告编辑状态接口
  754. // @Param request body request.MarkEditEnReport true "type json string"
  755. // @Success 200 标记成功 ;202 标记成功
  756. // @router /mark_edit [post]
  757. func (this *SmartReportController) MarkEditStatus() {
  758. br := new(models.BaseResponse).Init()
  759. defer func() {
  760. if br.ErrMsg == "" {
  761. br.IsSendEmail = false
  762. }
  763. this.Data["json"] = br
  764. this.ServeJSON()
  765. }()
  766. sysUser := this.SysUser
  767. if sysUser == nil {
  768. br.Msg = "请登录"
  769. br.ErrMsg = "请登录,SysUser Is Empty"
  770. br.Ret = 408
  771. return
  772. }
  773. var req smart_report.SmartReportMarkEditReq
  774. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  775. if err != nil {
  776. br.Msg = "参数解析异常!"
  777. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  778. return
  779. }
  780. if req.SmartReportId <= 0 {
  781. br.Msg = "缺少报告Id"
  782. return
  783. }
  784. if req.Status <= 0 {
  785. br.Msg = "标记状态异常"
  786. return
  787. }
  788. // 获取系统用户列表-用于匹配编辑中的用户
  789. adminIdName := make(map[int]string)
  790. admins, e := system.GetSysAdminList(``, make([]interface{}, 0), []string{}, "")
  791. if e != nil {
  792. br.Msg = "获取失败"
  793. br.ErrMsg = "获取系统用户列表失败, Err: " + e.Error()
  794. return
  795. }
  796. for _, ad := range admins {
  797. adminIdName[ad.AdminId] = ad.RealName
  798. }
  799. data, e := smartReportService.UpdateSmartReportEditing(req.SmartReportId, req.Status, sysUser.AdminId, sysUser.RealName, adminIdName)
  800. if e != nil {
  801. br.Msg = e.Error()
  802. return
  803. }
  804. br.Ret = 200
  805. br.Success = true
  806. br.Msg = "标记成功"
  807. br.Data = data
  808. }
  809. // List
  810. // @Title 报告列表
  811. // @Description 报告列表
  812. // @Param PageSize query int true "每页数据条数"
  813. // @Param CurrentIndex query int true "当前页页码"
  814. // @Param TimeType query string false "筛选的时间类别: publish_time-发布时间, modify_time-更新时间"
  815. // @Param StartDate query string false "开始时间"
  816. // @Param EndDate query string false "结束时间"
  817. // @Param Frequency query string false "频度"
  818. // @Param ClassifyIdFirst query int false "一级分类ID"
  819. // @Param ClassifyIdSecond query int false "二级分类ID"
  820. // @Param State query int false "发布状态: 1-待发布; 2-已发布"
  821. // @Param Keyword query string false "搜索关键词"
  822. // @Success 200 {object} smart_report.SmartReportListResp
  823. // @router /list [get]
  824. func (this *SmartReportController) List() {
  825. br := new(models.BaseResponse).Init()
  826. defer func() {
  827. if br.ErrMsg == "" {
  828. br.IsSendEmail = false
  829. }
  830. this.Data["json"] = br
  831. this.ServeJSON()
  832. }()
  833. sysUser := this.SysUser
  834. if sysUser == nil {
  835. br.Msg = "请登录"
  836. br.ErrMsg = "请登录,SysUser Is Empty"
  837. br.Ret = 408
  838. return
  839. }
  840. type SmartReportListReq struct {
  841. PageSize int `form:"PageSize"`
  842. CurrentIndex int `form:"CurrentIndex"`
  843. TimeType string `form:"TimeType"`
  844. StartDate string `form:"StartDate"`
  845. EndDate string `form:"EndDate"`
  846. Frequency string `form:"Frequency"`
  847. ClassifyIdFirst int `form:"ClassifyIdFirst"`
  848. ClassifyIdSecond int `form:"ClassifyIdSecond"`
  849. State int `form:"State"`
  850. Keyword string `form:"Keyword"`
  851. }
  852. params := new(SmartReportListReq)
  853. if e := this.ParseForm(params); e != nil {
  854. br.Msg = "获取失败"
  855. br.ErrMsg = "入参解析失败, Err: " + e.Error()
  856. return
  857. }
  858. if params.TimeType == "" {
  859. params.TimeType = "publish_time"
  860. }
  861. if params.TimeType != "publish_time" && params.TimeType != "modify_time" {
  862. br.Msg = "请选择正确的时间类型"
  863. return
  864. }
  865. // 更新时间指的是内容更新时间
  866. if params.TimeType == "modify_time" {
  867. params.TimeType = "content_modify_time"
  868. }
  869. var condition string
  870. var pars []interface{}
  871. // 筛选项
  872. {
  873. keyword := strings.TrimSpace(params.Keyword)
  874. if keyword != "" {
  875. kw := fmt.Sprint("%", keyword, "%")
  876. condition += fmt.Sprintf(` AND (title LIKE ? OR admin_real_name LIKE ? OR last_modify_admin_name LIKE ?)`)
  877. pars = append(pars, kw, kw, kw)
  878. }
  879. if params.StartDate != "" && params.EndDate != "" {
  880. st := fmt.Sprintf("%s 00:00:00", params.StartDate)
  881. ed := fmt.Sprintf("%s 23:59:59", params.EndDate)
  882. condition += fmt.Sprintf(` AND %s >= ? AND %s <= ?`, params.TimeType, params.TimeType)
  883. pars = append(pars, st, ed)
  884. }
  885. if params.Frequency != "" {
  886. condition += ` AND frequency = ?`
  887. pars = append(pars, params.Frequency)
  888. }
  889. if params.ClassifyIdFirst > 0 {
  890. condition += ` AND classify_id_first = ?`
  891. pars = append(pars, params.ClassifyIdFirst)
  892. }
  893. if params.ClassifyIdSecond > 0 {
  894. condition += ` AND classify_id_second = ?`
  895. pars = append(pars, params.ClassifyIdSecond)
  896. }
  897. if params.State > 0 {
  898. condition += ` AND state = ?`
  899. pars = append(pars, params.State)
  900. }
  901. }
  902. resp := new(smart_report.SmartReportListResp)
  903. reportOB := new(smart_report.SmartReport)
  904. total, e := reportOB.GetCountByCondition(condition, pars)
  905. if e != nil {
  906. br.Msg = "获取失败"
  907. br.ErrMsg = "获取报告总数失败, Err:" + e.Error()
  908. return
  909. }
  910. if total <= 0 {
  911. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  912. resp.Paging = page
  913. br.Ret = 200
  914. br.Success = true
  915. br.Msg = "获取成功"
  916. br.Data = resp
  917. return
  918. }
  919. // 分页列表
  920. var startSize int
  921. if params.PageSize <= 0 {
  922. params.PageSize = utils.PageSize20
  923. }
  924. if params.CurrentIndex <= 0 {
  925. params.CurrentIndex = 1
  926. }
  927. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  928. // 列表查询过滤掉富文本内容
  929. fields := []string{
  930. "smart_report_id", "report_code", "classify_id_first", "classify_name_first", "classify_id_second", "classify_name_second", "add_type",
  931. "title", "abstract", "author", "frequency", "stage", "video_url", "video_name", "video_play_seconds", "video_size", "detail_img_url", "detail_pdf_url",
  932. "admin_id", "admin_real_name", "state", "publish_time", "pre_publish_time", "pre_msg_send", "msg_is_send", "msg_send_time", "create_time", "modify_time",
  933. "last_modify_admin_id", "last_modify_admin_name", "content_modify_time", "pv", "uv", "head_img", "end_img", "canvas_color",
  934. }
  935. list, e := reportOB.GetPageItemsByCondition(condition, pars, fields, "", startSize, params.PageSize)
  936. if e != nil {
  937. br.Msg = "获取失败"
  938. br.ErrMsg = "获取报告分页列表失败, Err:" + e.Error()
  939. return
  940. }
  941. // 获取系统用户列表-用于匹配编辑中的用户
  942. adminIdName := make(map[int]string)
  943. admins, e := system.GetSysAdminList(``, make([]interface{}, 0), []string{}, "")
  944. if e != nil {
  945. br.Msg = "获取失败"
  946. br.ErrMsg = "获取系统用户列表失败, Err: " + e.Error()
  947. return
  948. }
  949. for _, ad := range admins {
  950. adminIdName[ad.AdminId] = ad.RealName
  951. }
  952. for _, v := range list {
  953. item := smart_report.FormatSmartReport2Item(v)
  954. mark, e := smartReportService.UpdateSmartReportEditing(v.SmartReportId, 2, sysUser.AdminId, sysUser.RealName, adminIdName)
  955. if e != nil {
  956. br.Msg = "获取失败"
  957. br.ErrMsg = "查询编辑中标记失败, Err:" + e.Error()
  958. return
  959. }
  960. if mark.Status == 0 {
  961. item.CanEdit = true
  962. } else {
  963. item.Editor = mark.Editor
  964. }
  965. resp.List = append(resp.List, item)
  966. }
  967. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  968. resp.Paging = page
  969. br.Ret = 200
  970. br.Success = true
  971. br.Msg = "获取成功"
  972. br.Data = resp
  973. }
  974. // DetailImg
  975. // @Title 生成长图
  976. // @Description 生成长图
  977. // @Param SmartReportId query int true "智能研报ID"
  978. // @Success 200 {object} smart_report.SmartReportSaveContentResp
  979. // @router /detail_img [get]
  980. func (this *SmartReportController) DetailImg() {
  981. br := new(models.BaseResponse).Init()
  982. defer func() {
  983. if br.ErrMsg == "" {
  984. br.IsSendEmail = false
  985. }
  986. this.Data["json"] = br
  987. this.ServeJSON()
  988. }()
  989. sysUser := this.SysUser
  990. if sysUser == nil {
  991. br.Msg = "请登录"
  992. br.ErrMsg = "请登录,SysUser Is Empty"
  993. br.Ret = 408
  994. return
  995. }
  996. reportId, _ := this.GetInt("SmartReportId", 0)
  997. if reportId <= 0 {
  998. br.Msg = "参数有误"
  999. br.ErrMsg = fmt.Sprintf("参数有误, SmartReportId: %d", reportId)
  1000. return
  1001. }
  1002. reportOB := new(smart_report.SmartReport)
  1003. item, e := reportOB.GetItemById(reportId)
  1004. if e != nil {
  1005. if e.Error() == utils.ErrNoRow() {
  1006. br.Msg = "报告不存在, 请刷新页面"
  1007. return
  1008. }
  1009. br.Msg = "操作失败"
  1010. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  1011. return
  1012. }
  1013. if item.DetailImgUrl != "" {
  1014. br.Data = item.DetailImgUrl
  1015. br.Ret = 200
  1016. br.Success = true
  1017. br.Msg = "获取成功"
  1018. return
  1019. }
  1020. // 写入队列
  1021. var queue smart_report.Report2ImgQueueReq
  1022. queue.ReportType = 2
  1023. queue.ReportCode = item.ReportCode
  1024. _ = utils.Rc.LPush(utils.CACHE_CREATE_REPORT_IMGPDF_QUEUE, queue)
  1025. br.Msg = "图片正在生成中, 请稍后下载"
  1026. return
  1027. }
  1028. // LastPublishedReport
  1029. // @Title 上期已发布的报告
  1030. // @Description 上期已发布的报告
  1031. // @Param ClassifyIdFirst query int false "一级分类ID"
  1032. // @Param ClassifyIdSecond query int false "二级分类ID"
  1033. // @Success 200 {object} smart_report.SmartReportItem
  1034. // @router /last_published_report [get]
  1035. func (this *SmartReportController) LastPublishedReport() {
  1036. br := new(models.BaseResponse).Init()
  1037. defer func() {
  1038. if br.ErrMsg == "" {
  1039. br.IsSendEmail = false
  1040. }
  1041. this.Data["json"] = br
  1042. this.ServeJSON()
  1043. }()
  1044. sysUser := this.SysUser
  1045. if sysUser == nil {
  1046. br.Msg = "请登录"
  1047. br.ErrMsg = "请登录,SysUser Is Empty"
  1048. br.Ret = 408
  1049. return
  1050. }
  1051. firstId, _ := this.GetInt("ClassifyIdFirst")
  1052. secondId, _ := this.GetInt("ClassifyIdSecond")
  1053. ob := new(smart_report.SmartReport)
  1054. cond := ` AND classify_id_first = ? AND classify_id_second = ?`
  1055. pars := make([]interface{}, 0)
  1056. pars = append(pars, firstId, secondId)
  1057. item, e := ob.GetItemByCondition(cond, pars, "stage DESC")
  1058. if e != nil && e.Error() != utils.ErrNoRow() {
  1059. br.Msg = "获取失败"
  1060. br.ErrMsg = "获取研报失败, Err: " + e.Error()
  1061. return
  1062. }
  1063. resp := smart_report.FormatSmartReport2Item(item)
  1064. br.Ret = 200
  1065. br.Success = true
  1066. br.Msg = "获取成功"
  1067. br.Data = resp
  1068. }
  1069. // VoiceUpload
  1070. // @Title 音频上传
  1071. // @Description 音频上传接口
  1072. // @Param file query file true "文件"
  1073. // @Param SmartReportId query int true "报告ID"
  1074. // @Success Ret=200 上传成功
  1075. // @router /voice_upload [post]
  1076. func (this *SmartReportController) VoiceUpload() {
  1077. br := new(models.BaseResponse).Init()
  1078. defer func() {
  1079. if br.ErrMsg == "" {
  1080. br.IsSendEmail = false
  1081. }
  1082. this.Data["json"] = br
  1083. this.ServeJSON()
  1084. }()
  1085. reportId, _ := this.GetInt("SmartReportId")
  1086. if reportId <= 0 {
  1087. br.Msg = "参数有误"
  1088. br.ErrMsg = fmt.Sprintf("参数有误, SmartReportId: %d", reportId)
  1089. return
  1090. }
  1091. f, h, err := this.GetFile("file")
  1092. if err != nil {
  1093. br.Msg = "获取资源信息失败"
  1094. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  1095. return
  1096. }
  1097. defer func() {
  1098. _ = f.Close()
  1099. }()
  1100. reportOb := new(smart_report.SmartReport)
  1101. item, e := reportOb.GetItemById(reportId)
  1102. if e != nil {
  1103. if e.Error() == utils.ErrNoRow() {
  1104. br.Msg = "报告不存在, 请刷新页面"
  1105. return
  1106. }
  1107. br.Msg = "操作失败"
  1108. br.ErrMsg = "获取报告信息失败, Err:" + err.Error()
  1109. return
  1110. }
  1111. ext := path.Ext(h.Filename)
  1112. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  1113. if e = os.MkdirAll(uploadDir, 0766); e != nil {
  1114. br.Msg = "存储目录创建失败"
  1115. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  1116. return
  1117. }
  1118. ossFileName := utils.GetRandStringNoSpecialChar(28) + ext
  1119. filePath := uploadDir + "/" + ossFileName
  1120. if e = this.SaveToFile("file", filePath); e != nil {
  1121. br.Msg = "文件保存失败"
  1122. br.ErrMsg = "文件保存失败, Err:" + e.Error()
  1123. return
  1124. }
  1125. defer func() {
  1126. _ = os.Remove(filePath)
  1127. }()
  1128. ossDir := "static/audio/"
  1129. resourceUrl := ``
  1130. //上传到阿里云 和 minio
  1131. if utils.ObjectStorageClient == "minio" {
  1132. resourceUrl, e = services.UploadMinIoToDir(ossFileName, filePath, ossDir, "")
  1133. if e != nil {
  1134. br.Msg = "文件上传失败"
  1135. br.ErrMsg = "文件上传失败, Err:" + e.Error()
  1136. return
  1137. }
  1138. } else {
  1139. resourceUrl, e = services.UploadAliyunToDir(ossFileName, filePath, ossDir, "")
  1140. if e != nil {
  1141. br.Msg = "文件上传失败"
  1142. br.ErrMsg = "文件上传失败, Err:" + e.Error()
  1143. return
  1144. }
  1145. }
  1146. resource := new(models.Resource)
  1147. resource.ResourceUrl = resourceUrl
  1148. resource.ResourceType = 2
  1149. resource.CreateTime = time.Now()
  1150. newId, err := models.AddResource(resource)
  1151. if err != nil {
  1152. br.Msg = "资源上传失败"
  1153. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  1154. return
  1155. }
  1156. //fmt.Println(filePath)
  1157. playSeconds, err := mp3duration.Calculate(filePath)
  1158. if playSeconds <= 0 {
  1159. playSeconds, err = utils.GetVideoPlaySeconds(filePath)
  1160. if err != nil {
  1161. br.Msg = "获取音频时间失败"
  1162. br.ErrMsg = "获取音频时间失败,Err:" + err.Error()
  1163. return
  1164. }
  1165. }
  1166. createTime := item.CreateTime.Format("0102")
  1167. videoName := item.Title + "(" + createTime + ")"
  1168. fileBody, err := ioutil.ReadFile(filePath)
  1169. videoSize := len(fileBody)
  1170. sizeFloat := (float64(videoSize) / float64(1024)) / float64(1024)
  1171. sizeStr := utils.SubFloatToFloatStr(sizeFloat, 2)
  1172. item.VideoUrl = resourceUrl
  1173. item.VideoName = videoName
  1174. item.VideoPlaySeconds = playSeconds
  1175. item.VideoSize = sizeStr
  1176. item.ModifyTime = time.Now().Local()
  1177. updateCols := []string{"VideoUrl", "VideoName", "VideoPlaySeconds", "VideoSize", "ModifyTime"}
  1178. if e = item.Update(updateCols); e != nil {
  1179. br.Msg = "上传失败"
  1180. br.ErrMsg = "上传失败,Err:" + e.Error()
  1181. return
  1182. }
  1183. resp := new(models.ResourceResp)
  1184. resp.Id = newId
  1185. resp.ResourceUrl = resourceUrl
  1186. br.Msg = "上传成功"
  1187. br.Ret = 200
  1188. br.Success = true
  1189. br.Data = resp
  1190. return
  1191. }