smart_report.go 39 KB

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