smart_report.go 32 KB

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