smart_report.go 45 KB

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