smart_report.go 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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.ContentModifyTime = time.Now().Local()
  102. item.HeadImg = req.HeadImg
  103. item.EndImg = req.EndImg
  104. item.CanvasColor = req.CanvasColor
  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 == models.ReportStatePublished || item.State == models.ReportStatePass {
  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. item.LastModifyAdminId = sysUser.AdminId
  253. item.LastModifyAdminName = sysUser.RealName
  254. item.ContentModifyTime = time.Now().Local()
  255. cols = append(cols, "LastModifyAdminId", "LastModifyAdminName", "ContentModifyTime")
  256. }
  257. if e := item.Update(cols); e != nil {
  258. br.Msg = "操作失败"
  259. br.ErrMsg = "更新报告失败, Err: " + e.Error()
  260. return
  261. }
  262. resp := smart_report.FormatSmartReport2Item(item)
  263. br.Ret = 200
  264. br.Success = true
  265. br.Msg = "操作成功"
  266. br.Data = resp
  267. }
  268. // Remove
  269. // @Title 删除
  270. // @Description 删除
  271. // @Param request body smart_report.SmartReportRemoveReq true "type json string"
  272. // @Success 200 string "操作成功"
  273. // @router /remove [post]
  274. func (this *SmartReportController) Remove() {
  275. br := new(models.BaseResponse).Init()
  276. defer func() {
  277. if br.ErrMsg == "" {
  278. br.IsSendEmail = false
  279. }
  280. this.Data["json"] = br
  281. this.ServeJSON()
  282. }()
  283. sysUser := this.SysUser
  284. if sysUser == nil {
  285. br.Msg = "请登录"
  286. br.ErrMsg = "请登录,SysUser Is Empty"
  287. br.Ret = 408
  288. return
  289. }
  290. var req smart_report.SmartReportRemoveReq
  291. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  292. if err != nil {
  293. br.Msg = "参数解析异常!"
  294. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  295. return
  296. }
  297. if req.SmartReportId <= 0 {
  298. br.Msg = "参数有误"
  299. br.ErrMsg = "报告ID为空"
  300. return
  301. }
  302. ob := new(smart_report.SmartReport)
  303. item, e := ob.GetItemById(req.SmartReportId)
  304. if e != nil {
  305. if e.Error() == utils.ErrNoRow() {
  306. br.Ret = 200
  307. br.Success = true
  308. br.Msg = "操作成功"
  309. return
  310. }
  311. br.Msg = "操作失败"
  312. br.ErrMsg = "获取研报失败, Err: " + e.Error()
  313. return
  314. }
  315. if e = item.Del(); e != nil {
  316. br.Msg = "操作失败"
  317. br.ErrMsg = "删除报告失败, Err: " + e.Error()
  318. return
  319. }
  320. // ES更新报告为未发布
  321. go func() {
  322. _ = services.SmartReportElasticUpsert(item.SmartReportId, 1)
  323. }()
  324. br.Ret = 200
  325. br.Success = true
  326. br.Msg = "操作成功"
  327. }
  328. // Detail
  329. // @Title 详情
  330. // @Description 详情
  331. // @Param SmartReportId query int true "智能研报ID"
  332. // @Success 200 {object} smart_report.SmartReportItem
  333. // @router /detail [get]
  334. func (this *SmartReportController) Detail() {
  335. br := new(models.BaseResponse).Init()
  336. defer func() {
  337. if br.ErrMsg == "" {
  338. br.IsSendEmail = false
  339. }
  340. this.Data["json"] = br
  341. this.ServeJSON()
  342. }()
  343. sysUser := this.SysUser
  344. if sysUser == nil {
  345. br.Msg = "请登录"
  346. br.ErrMsg = "请登录,SysUser Is Empty"
  347. br.Ret = 408
  348. return
  349. }
  350. reportId, _ := this.GetInt("SmartReportId")
  351. if reportId <= 0 {
  352. br.Msg = "参数有误"
  353. br.ErrMsg = "报告ID有误"
  354. return
  355. }
  356. ob := new(smart_report.SmartReport)
  357. item, e := ob.GetItemById(reportId)
  358. if e != nil {
  359. if e.Error() == utils.ErrNoRow() {
  360. br.Msg = "报告不存在, 请刷新页面"
  361. return
  362. }
  363. br.Msg = "操作失败"
  364. br.ErrMsg = "获取研报失败, Err: " + e.Error()
  365. return
  366. }
  367. resp := smart_report.FormatSmartReport2Item(item)
  368. br.Ret = 200
  369. br.Success = true
  370. br.Msg = "获取成功"
  371. br.Data = resp
  372. }
  373. // Publish
  374. // @Title 发布/取消发布
  375. // @Description 发布/取消发布
  376. // @Param request body smart_report.SmartReportPublishReq true "type json string"
  377. // @Success 200 string "操作成功"
  378. // @router /publish [post]
  379. func (this *SmartReportController) Publish() {
  380. br := new(models.BaseResponse).Init()
  381. defer func() {
  382. if br.ErrMsg == "" {
  383. br.IsSendEmail = false
  384. }
  385. this.Data["json"] = br
  386. this.ServeJSON()
  387. }()
  388. sysUser := this.SysUser
  389. if sysUser == nil {
  390. br.Msg = "请登录"
  391. br.ErrMsg = "请登录,SysUser Is Empty"
  392. br.Ret = 408
  393. return
  394. }
  395. var req smart_report.SmartReportPublishReq
  396. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  397. if err != nil {
  398. br.Msg = "参数解析异常!"
  399. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  400. return
  401. }
  402. if req.SmartReportId <= 0 {
  403. br.Msg = "参数有误"
  404. br.ErrMsg = "报告ID为空"
  405. return
  406. }
  407. if req.PublishState != smart_report.SmartReportStateWaitPublish && req.PublishState != smart_report.SmartReportStatePublished {
  408. br.Msg = "参数有误"
  409. return
  410. }
  411. operate := 0
  412. if req.PublishState == smart_report.SmartReportStateWaitPublish {
  413. operate = models.ReportOperateCancelPublish
  414. }
  415. if req.PublishState == smart_report.SmartReportStatePublished {
  416. operate = models.ReportOperatePublish
  417. }
  418. ob := new(smart_report.SmartReport)
  419. item, e := ob.GetItemById(req.SmartReportId)
  420. if e != nil {
  421. if e.Error() == utils.ErrNoRow() {
  422. br.Msg = "报告不存在, 请刷新页面"
  423. return
  424. }
  425. br.Msg = "操作失败"
  426. br.ErrMsg = "获取研报失败, Err: " + e.Error()
  427. return
  428. }
  429. // 图表刷新状态
  430. refreshResult := data.CheckBatchChartRefreshResult("report", item.SmartReportId, 0)
  431. if !refreshResult {
  432. br.Msg = "图表刷新未完成,请稍后操作"
  433. br.ErrMsg = "图表刷新未完成,请稍后操作"
  434. br.IsSendEmail = false
  435. return
  436. }
  437. // 根据审批开关及审批流判断当前报告状态
  438. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeSmart, item.ClassifyIdFirst, item.ClassifyIdSecond, operate)
  439. if e != nil {
  440. br.Msg = "操作失败"
  441. br.ErrMsg = "校验报告当前状态失败, Err: " + e.Error()
  442. return
  443. }
  444. cols := []string{"State", "ModifyTime"}
  445. item.State = state
  446. item.ModifyTime = time.Now().Local()
  447. if state == smart_report.SmartReportStatePublished {
  448. cols = append(cols, "PublishTime")
  449. item.PublishTime = time.Now().Local()
  450. // 写入队列
  451. var queue smart_report.Report2ImgQueueReq
  452. queue.ReportType = 2
  453. queue.ReportCode = item.ReportCode
  454. _ = utils.Rc.LPush(utils.CACHE_CREATE_REPORT_IMGPDF_QUEUE, queue)
  455. }
  456. // 取消发布时同时清除掉Img和PDF的文件地址, 以及预发布设置
  457. if state == smart_report.SmartReportStateWaitPublish {
  458. cols = append(cols, "DetailImgUrl", "DetailPdfUrl", "PrePublishTime")
  459. item.DetailImgUrl = ""
  460. item.DetailPdfUrl = ""
  461. item.PrePublishTime = time.Time{}
  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. ossClient := services.NewOssClient()
  1188. if ossClient == nil {
  1189. br.Msg = "上传失败"
  1190. br.ErrMsg = "初始化OSS服务失败"
  1191. return
  1192. }
  1193. resourceUrl, e = ossClient.UploadFile(ossFileName, filePath, "")
  1194. if e != nil {
  1195. br.Msg = "文件上传失败"
  1196. br.ErrMsg = "文件上传失败,Err:" + e.Error()
  1197. return
  1198. }
  1199. resource := new(models.Resource)
  1200. resource.ResourceUrl = resourceUrl
  1201. resource.ResourceType = 2
  1202. resource.CreateTime = time.Now()
  1203. newId, err := models.AddResource(resource)
  1204. if err != nil {
  1205. br.Msg = "资源上传失败"
  1206. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  1207. return
  1208. }
  1209. //fmt.Println(filePath)
  1210. playSeconds, err := mp3duration.Calculate(filePath)
  1211. if playSeconds <= 0 {
  1212. playSeconds, err = utils.GetVideoPlaySeconds(filePath)
  1213. if err != nil {
  1214. br.Msg = "获取音频时间失败"
  1215. br.ErrMsg = "获取音频时间失败,Err:" + err.Error()
  1216. return
  1217. }
  1218. }
  1219. createTime := item.CreateTime.Format("0102")
  1220. videoName := item.Title + "(" + createTime + ")"
  1221. fileBody, err := ioutil.ReadFile(filePath)
  1222. videoSize := len(fileBody)
  1223. sizeFloat := (float64(videoSize) / float64(1024)) / float64(1024)
  1224. sizeStr := utils.SubFloatToFloatStr(sizeFloat, 2)
  1225. item.VideoUrl = resourceUrl
  1226. item.VideoName = videoName
  1227. item.VideoPlaySeconds = playSeconds
  1228. item.VideoSize = sizeStr
  1229. item.ModifyTime = time.Now().Local()
  1230. updateCols := []string{"VideoUrl", "VideoName", "VideoPlaySeconds", "VideoSize", "ModifyTime"}
  1231. if e = item.Update(updateCols); e != nil {
  1232. br.Msg = "上传失败"
  1233. br.ErrMsg = "上传失败,Err:" + e.Error()
  1234. return
  1235. }
  1236. resp := new(models.ResourceResp)
  1237. resp.Id = newId
  1238. resp.ResourceUrl = resourceUrl
  1239. br.Msg = "上传成功"
  1240. br.Ret = 200
  1241. br.Success = true
  1242. br.Data = resp
  1243. return
  1244. }
  1245. // SubmitApprove
  1246. // @Title 提交审批
  1247. // @Description 提交审批接口
  1248. // @Param request body models.ReportSubmitApproveReq true "type json string"
  1249. // @Success 200 Ret=200 操作成功
  1250. // @router /approve/submit [post]
  1251. func (this *SmartReportController) SubmitApprove() {
  1252. br := new(models.BaseResponse).Init()
  1253. defer func() {
  1254. if br.ErrMsg == "" {
  1255. br.IsSendEmail = false
  1256. }
  1257. this.Data["json"] = br
  1258. this.ServeJSON()
  1259. }()
  1260. sysUser := this.SysUser
  1261. if sysUser == nil {
  1262. br.Msg = "请登录"
  1263. br.ErrMsg = "请登录,SysUser Is Empty"
  1264. return
  1265. }
  1266. var req models.ReportSubmitApproveReq
  1267. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1268. br.Msg = "参数有误"
  1269. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  1270. return
  1271. }
  1272. reportId := req.ReportId
  1273. if reportId <= 0 {
  1274. br.Msg = "参数有误"
  1275. br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
  1276. return
  1277. }
  1278. reportOb := new(smart_report.SmartReport)
  1279. reportItem, e := reportOb.GetItemById(reportId)
  1280. if e != nil {
  1281. if e.Error() == utils.ErrNoRow() {
  1282. br.Msg = "报告已被删除, 请刷新页面"
  1283. return
  1284. }
  1285. br.Msg = "操作失败"
  1286. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  1287. return
  1288. }
  1289. // 校验当前审批配置, 返回下一个状态
  1290. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeSmart, reportItem.ClassifyIdFirst, reportItem.ClassifyIdSecond, models.ReportOperateSubmitApprove)
  1291. if e != nil {
  1292. br.Msg = "操作失败"
  1293. br.ErrMsg = "校验报告状态失败, Err: " + e.Error()
  1294. return
  1295. }
  1296. // 下一个状态不为待审批时, 仅更新状态
  1297. if state != models.ReportStateWaitApprove {
  1298. reportItem.State = state
  1299. e = reportItem.Update([]string{"State"})
  1300. if e != nil {
  1301. br.Msg = "操作失败"
  1302. br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1303. return
  1304. }
  1305. br.Ret = 200
  1306. br.Success = true
  1307. br.Msg = "操作成功"
  1308. return
  1309. }
  1310. // 提交审批
  1311. approveId, e := services.SubmitReportApprove(report_approve.FlowReportTypeSmart, reportItem.SmartReportId, reportItem.Title, reportItem.ClassifyIdFirst, reportItem.ClassifyIdSecond, sysUser.AdminId, sysUser.RealName)
  1312. if e != nil {
  1313. br.Msg = "操作失败"
  1314. br.ErrMsg = "提交审批失败, Err: " + e.Error()
  1315. return
  1316. }
  1317. reportItem.ApproveId = approveId
  1318. reportItem.State = models.ReportStateWaitApprove
  1319. reportItem.ModifyTime = time.Now().Local()
  1320. e = reportItem.Update([]string{"ApproveId", "State", "ModifyTime"})
  1321. if e != nil {
  1322. br.Msg = "操作失败"
  1323. br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1324. return
  1325. }
  1326. br.Ret = 200
  1327. br.Success = true
  1328. br.Msg = "操作成功"
  1329. }
  1330. // CancelApprove
  1331. // @Title 撤销审批
  1332. // @Description 撤销审批
  1333. // @Param request body models.ReportCancelApproveReq true "type json string"
  1334. // @Success 200 Ret=200 操作成功
  1335. // @router /approve/cancel [post]
  1336. func (this *SmartReportController) CancelApprove() {
  1337. br := new(models.BaseResponse).Init()
  1338. defer func() {
  1339. if br.ErrMsg == "" {
  1340. br.IsSendEmail = false
  1341. }
  1342. this.Data["json"] = br
  1343. this.ServeJSON()
  1344. }()
  1345. sysUser := this.SysUser
  1346. if sysUser == nil {
  1347. br.Msg = "请登录"
  1348. br.ErrMsg = "请登录,SysUser Is Empty"
  1349. return
  1350. }
  1351. var req models.ReportCancelApproveReq
  1352. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1353. br.Msg = "参数有误"
  1354. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  1355. return
  1356. }
  1357. reportId := req.ReportId
  1358. if reportId <= 0 {
  1359. br.Msg = "参数有误"
  1360. br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
  1361. return
  1362. }
  1363. reportOb := new(smart_report.SmartReport)
  1364. reportItem, e := reportOb.GetItemById(reportId)
  1365. if e != nil {
  1366. if e.Error() == utils.ErrNoRow() {
  1367. br.Msg = "报告已被删除, 请刷新页面"
  1368. return
  1369. }
  1370. br.Msg = "操作失败"
  1371. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  1372. return
  1373. }
  1374. // 校验当前审批配置, 返回下一个状态
  1375. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeSmart, reportItem.ClassifyIdFirst, reportItem.ClassifyIdSecond, models.ReportOperateCancelApprove)
  1376. if e != nil {
  1377. br.Msg = "操作失败"
  1378. br.ErrMsg = "校验报告状态失败, Err: " + e.Error()
  1379. return
  1380. }
  1381. // 下一个状态不为待提交时, 仅更新状态
  1382. if state != models.ReportStateWaitSubmit {
  1383. reportItem.State = state
  1384. e = reportItem.Update([]string{"State"})
  1385. if e != nil {
  1386. br.Msg = "操作失败"
  1387. br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1388. return
  1389. }
  1390. br.Ret = 200
  1391. br.Success = true
  1392. br.Msg = "操作成功"
  1393. return
  1394. }
  1395. //if reportItem.ApproveId <= 0 {
  1396. // br.Msg = "报告审批不存在"
  1397. // br.ErrMsg = fmt.Sprintf("报告审批不存在, ApproveId: %d", reportItem.ApproveId)
  1398. // return
  1399. //}
  1400. // 撤销审批
  1401. e = services.CancelReportApprove(report_approve.FlowReportTypeSmart, reportItem.SmartReportId, reportItem.ApproveId, sysUser.AdminId, sysUser.RealName)
  1402. if e != nil {
  1403. br.Msg = "操作失败"
  1404. br.ErrMsg = "撤销审批失败, Err: " + e.Error()
  1405. return
  1406. }
  1407. //reportItem.ApproveId = 0
  1408. //reportItem.State = models.ReportStateWaitSubmit
  1409. //reportItem.ModifyTime = time.Now().Local()
  1410. //e = reportItem.UpdateReport([]string{"ApproveId", "State", "ModifyTime"})
  1411. //if e != nil {
  1412. // br.Msg = "操作失败"
  1413. // br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1414. // return
  1415. //}
  1416. br.Ret = 200
  1417. br.Success = true
  1418. br.Msg = "操作成功"
  1419. }