smart_report.go 43 KB

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