smart_report.go 41 KB

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