smart_report.go 43 KB

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