smart_report.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. reportId, _ := this.GetInt("SmartReportId")
  1038. if reportId <= 0 {
  1039. br.Msg = "参数有误"
  1040. br.ErrMsg = fmt.Sprintf("参数有误, SmartReportId: %d", reportId)
  1041. return
  1042. }
  1043. f, h, err := this.GetFile("file")
  1044. if err != nil {
  1045. br.Msg = "获取资源信息失败"
  1046. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  1047. return
  1048. }
  1049. defer func() {
  1050. _ = f.Close()
  1051. }()
  1052. reportOb := new(smart_report.SmartReport)
  1053. item, e := reportOb.GetItemById(reportId)
  1054. if e != nil {
  1055. if e.Error() == utils.ErrNoRow() {
  1056. br.Msg = "报告不存在, 请刷新页面"
  1057. return
  1058. }
  1059. br.Msg = "操作失败"
  1060. br.ErrMsg = "获取报告信息失败, Err:" + err.Error()
  1061. return
  1062. }
  1063. ext := path.Ext(h.Filename)
  1064. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  1065. if e = os.MkdirAll(uploadDir, 0766); e != nil {
  1066. br.Msg = "存储目录创建失败"
  1067. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  1068. return
  1069. }
  1070. ossFileName := utils.GetRandStringNoSpecialChar(28) + ext
  1071. filePath := uploadDir + "/" + ossFileName
  1072. if e = this.SaveToFile("file", filePath); e != nil {
  1073. br.Msg = "文件保存失败"
  1074. br.ErrMsg = "文件保存失败, Err:" + e.Error()
  1075. return
  1076. }
  1077. defer func() {
  1078. _ = os.Remove(filePath)
  1079. }()
  1080. ossDir := "static/audio/"
  1081. resourceUrl := ``
  1082. //上传到阿里云 和 minio
  1083. if utils.ObjectStorageClient == "minio" {
  1084. resourceUrl, e = services.UploadMinIoToDir(ossFileName, filePath, ossDir, "")
  1085. if e != nil {
  1086. br.Msg = "文件上传失败"
  1087. br.ErrMsg = "文件上传失败, Err:" + e.Error()
  1088. return
  1089. }
  1090. } else {
  1091. resourceUrl, e = services.UploadAliyunToDir(ossFileName, filePath, ossDir, "")
  1092. if e != nil {
  1093. br.Msg = "文件上传失败"
  1094. br.ErrMsg = "文件上传失败, Err:" + e.Error()
  1095. return
  1096. }
  1097. }
  1098. resource := new(models.Resource)
  1099. resource.ResourceUrl = resourceUrl
  1100. resource.ResourceType = 2
  1101. resource.CreateTime = time.Now()
  1102. newId, err := models.AddResource(resource)
  1103. if err != nil {
  1104. br.Msg = "资源上传失败"
  1105. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  1106. return
  1107. }
  1108. //fmt.Println(filePath)
  1109. playSeconds, err := mp3duration.Calculate(filePath)
  1110. if playSeconds <= 0 {
  1111. playSeconds, err = utils.GetVideoPlaySeconds(filePath)
  1112. if err != nil {
  1113. br.Msg = "获取音频时间失败"
  1114. br.ErrMsg = "获取音频时间失败,Err:" + err.Error()
  1115. return
  1116. }
  1117. }
  1118. createTime := item.CreateTime.Format("0102")
  1119. videoName := item.Title + "(" + createTime + ")"
  1120. fileBody, err := ioutil.ReadFile(filePath)
  1121. videoSize := len(fileBody)
  1122. sizeFloat := (float64(videoSize) / float64(1024)) / float64(1024)
  1123. sizeStr := utils.SubFloatToFloatStr(sizeFloat, 2)
  1124. item.VideoUrl = resourceUrl
  1125. item.VideoName = videoName
  1126. item.VideoPlaySeconds = playSeconds
  1127. item.VideoSize = sizeStr
  1128. item.ModifyTime = time.Now().Local()
  1129. updateCols := []string{"VideoUrl", "VideoName", "VideoPlaySeconds", "VideoSize", "ModifyTime"}
  1130. if e = item.Update(updateCols); e != nil {
  1131. br.Msg = "上传失败"
  1132. br.ErrMsg = "上传失败,Err:" + e.Error()
  1133. return
  1134. }
  1135. resp := new(models.ResourceResp)
  1136. resp.Id = newId
  1137. resp.ResourceUrl = resourceUrl
  1138. br.Msg = "上传成功"
  1139. br.Ret = 200
  1140. br.Success = true
  1141. br.Data = resp
  1142. return
  1143. }