smart_report.go 41 KB

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