smart_report.go 33 KB

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