smart_report.go 40 KB

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