smart_report.go 43 KB

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