report.go 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. package english_report
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/company"
  7. "eta/eta_api/models/report_approve"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services"
  10. "eta/eta_api/services/alarm_msg"
  11. "eta/eta_api/services/data"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "github.com/rdlucklib/rdluck_tools/paging"
  15. "html"
  16. "strconv"
  17. "strings"
  18. "sync"
  19. "time"
  20. )
  21. // EnglishReportController 研报活动模块
  22. type EnglishReportController struct {
  23. controllers.BaseAuthController
  24. }
  25. // @Title 新增报告接口
  26. // @Description 新增报告
  27. // @Param request body models.AddEnglishReportReq true "type json string"
  28. // @Success 200 {object} models.AddEnglishReportResp
  29. // @router /add [post]
  30. func (this *EnglishReportController) Add() {
  31. br := new(models.BaseResponse).Init()
  32. defer func() {
  33. this.Data["json"] = br
  34. this.ServeJSON()
  35. }()
  36. sysUser := this.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录,SysUser Is Empty"
  40. br.Ret = 408
  41. return
  42. }
  43. var req models.AddEnglishReportReq
  44. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  45. if err != nil {
  46. br.Msg = "参数解析异常!"
  47. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  48. return
  49. }
  50. if req.Content == "" {
  51. br.Msg = "请输入内容"
  52. return
  53. }
  54. if req.Overview == "" {
  55. br.Msg = "请输入overview"
  56. return
  57. }
  58. if req.Abstract == "" {
  59. br.Msg = "请输入摘要"
  60. return
  61. }
  62. var contentSub string
  63. if req.Content != "" {
  64. req.Content = utils.ContentXssFilter(req.Content)
  65. content, e := services.FilterReportContentBr(req.Content)
  66. if e != nil {
  67. br.Msg = "内容去除前后空格失败"
  68. br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
  69. return
  70. }
  71. req.Content = content
  72. contentSub, err = services.GetReportContentSub(req.Content)
  73. if err != nil {
  74. go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+err.Error(), 3)
  75. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  76. }
  77. }
  78. maxStage, err := models.GetEnglishReportStage(req.ClassifyIdFirst, req.ClassifyIdSecond)
  79. if err != nil {
  80. br.Msg = "期数获取失败!"
  81. br.ErrMsg = "期数获取失败,Err:" + err.Error()
  82. return
  83. }
  84. // 根据审批开关及审批流判断当前报告状态
  85. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeEnglish, req.ClassifyIdFirst, req.ClassifyIdSecond, models.ReportOperateAdd)
  86. if e != nil {
  87. br.Msg = "操作失败"
  88. br.ErrMsg = "校验报告当前状态失败, Err: " + e.Error()
  89. return
  90. }
  91. item := new(models.EnglishReport)
  92. item.AddType = req.AddType
  93. item.ClassifyIdFirst = req.ClassifyIdFirst
  94. item.ClassifyNameFirst = req.ClassifyNameFirst
  95. item.ClassifyIdSecond = req.ClassifyIdSecond
  96. item.ClassifyNameSecond = req.ClassifyNameSecond
  97. item.Title = req.Title
  98. item.Abstract = req.Abstract
  99. item.Author = req.Author
  100. item.Frequency = req.Frequency
  101. item.State = state
  102. item.Content = html.EscapeString(req.Content)
  103. item.Stage = maxStage + 1
  104. item.ContentSub = html.EscapeString(contentSub)
  105. item.CreateTime = req.CreateTime
  106. item.ModifyTime = time.Now()
  107. item.Overview = req.Overview
  108. item.AdminId = sysUser.AdminId
  109. item.AdminRealName = sysUser.RealName
  110. newReportId, err := models.AddEnglishReport(item)
  111. if err != nil {
  112. br.Msg = "保存失败"
  113. br.ErrMsg = "保存失败,Err:" + err.Error()
  114. return
  115. }
  116. reportCode := utils.MD5(strconv.Itoa(int(newReportId)))
  117. //修改唯一编码
  118. {
  119. go models.ModifyEnglishReportCode(newReportId, reportCode)
  120. }
  121. resp := new(models.AddEnglishReportResp)
  122. resp.ReportId = newReportId
  123. resp.ReportCode = reportCode
  124. br.Ret = 200
  125. br.Success = true
  126. br.Msg = "保存成功"
  127. br.Data = resp
  128. }
  129. // @Title 编辑报告接口
  130. // @Description 编辑报告
  131. // @Param request body models.EditEnglishReportReq true "type json string"
  132. // @Success 200 {object} models.EditEnglishReportResp
  133. // @router /edit [post]
  134. func (this *EnglishReportController) Edit() {
  135. br := new(models.BaseResponse).Init()
  136. defer func() {
  137. this.Data["json"] = br
  138. this.ServeJSON()
  139. }()
  140. sysUser := this.SysUser
  141. if sysUser == nil {
  142. br.Msg = "请登录"
  143. br.ErrMsg = "请登录,SysUser Is Empty"
  144. br.Ret = 408
  145. return
  146. }
  147. var req models.EditEnglishReportReq
  148. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  149. if err != nil {
  150. br.Msg = "参数解析异常!"
  151. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  152. return
  153. }
  154. if req.Content == "" {
  155. br.Msg = "请输入内容"
  156. return
  157. }
  158. if req.Overview == "" {
  159. br.Msg = "请输入overview"
  160. return
  161. }
  162. if req.Abstract == "" {
  163. br.Msg = "请输入摘要"
  164. return
  165. }
  166. markStatus, err := services.UpdateEnReportEditMark(int(req.ReportId), sysUser.AdminId, 1, sysUser.RealName)
  167. if err != nil {
  168. br.Msg = err.Error()
  169. return
  170. }
  171. if markStatus.Status == 1 {
  172. br.Msg = markStatus.Msg
  173. return
  174. }
  175. var contentSub string
  176. if req.Content != "" {
  177. req.Content = utils.ContentXssFilter(req.Content)
  178. content, e := services.FilterReportContentBr(req.Content)
  179. if e != nil {
  180. br.Msg = "内容去除前后空格失败"
  181. br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
  182. return
  183. }
  184. req.Content = content
  185. contentSub, err = services.GetReportContentSub(req.Content)
  186. if err != nil {
  187. go alarm_msg.SendAlarmMsg("解析 ContentSub 失败,Err:"+err.Error(), 3)
  188. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  189. }
  190. }
  191. var stage int
  192. report, e := models.GetEnglishReportById(int(req.ReportId))
  193. if e != nil {
  194. if e.Error() == utils.ErrNoRow() {
  195. br.Msg = "报告已被删除, 请刷新页面"
  196. return
  197. }
  198. br.Msg = "操作失败"
  199. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  200. return
  201. }
  202. if report.State == models.ReportStatePublished || report.State == models.ReportStatePass {
  203. br.Msg = "该报告已发布,不允许编辑"
  204. br.ErrMsg = "该报告已发布,不允许编辑"
  205. return
  206. }
  207. if report.ClassifyNameFirst != req.ClassifyNameFirst || report.ClassifyNameSecond != req.ClassifyNameSecond {
  208. maxStage, _ := models.GetEnglishReportStageEdit(req.ClassifyIdFirst, req.ClassifyIdSecond, int(req.ReportId))
  209. maxStage = maxStage + 1
  210. stage = maxStage
  211. } else {
  212. stage = report.Stage
  213. }
  214. item := new(models.EnglishReport)
  215. item.ClassifyIdFirst = req.ClassifyIdFirst
  216. item.ClassifyNameFirst = req.ClassifyNameFirst
  217. item.ClassifyIdSecond = req.ClassifyIdSecond
  218. item.ClassifyNameSecond = req.ClassifyNameSecond
  219. item.Title = req.Title
  220. item.Abstract = req.Abstract
  221. item.Author = req.Author
  222. item.Frequency = req.Frequency
  223. item.State = report.State // 编辑不变更状态
  224. item.Stage = stage
  225. item.Content = html.EscapeString(req.Content)
  226. item.ContentSub = html.EscapeString(contentSub)
  227. item.CreateTime = req.CreateTime
  228. item.Overview = req.Overview
  229. err = models.EditEnglishReport(item, req.ReportId)
  230. if err != nil {
  231. br.Msg = "保存失败"
  232. br.ErrMsg = "保存失败,Err:" + err.Error()
  233. return
  234. }
  235. reportCode := utils.MD5(strconv.Itoa(int(req.ReportId)))
  236. resp := new(models.EditEnglishReportResp)
  237. resp.ReportId = req.ReportId
  238. resp.ReportCode = reportCode
  239. br.Ret = 200
  240. br.Success = true
  241. br.Msg = "保存成功"
  242. br.Data = resp
  243. }
  244. // @Title 获取报告详情接口
  245. // @Description 获取报告详情
  246. // @Param request body models.ReportDetailReq true "type json string"
  247. // @Success 200 {object} models.EnglishReportDetailView
  248. // @router /detail [get]
  249. func (this *EnglishReportController) Detail() {
  250. br := new(models.BaseResponse).Init()
  251. defer func() {
  252. this.Data["json"] = br
  253. this.ServeJSON()
  254. }()
  255. /*var req models.ReportDetailReq
  256. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  257. if err != nil {
  258. br.Msg = "参数解析异常!"
  259. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  260. return
  261. }
  262. if req.ReportId <= 0 {
  263. br.Msg = "参数错误"
  264. return
  265. }*/
  266. reportId, err := this.GetInt("ReportId")
  267. if err != nil {
  268. br.Msg = "获取参数失败!"
  269. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  270. return
  271. }
  272. if reportId <= 0 {
  273. br.Msg = "参数错误"
  274. return
  275. }
  276. item, err := models.GetEnglishReportById(reportId)
  277. if err != nil {
  278. if err.Error() == utils.ErrNoRow() {
  279. br.Msg = "报告已被删除"
  280. return
  281. }
  282. br.Msg = "获取失败"
  283. br.ErrMsg = "获取失败,Err:" + err.Error()
  284. return
  285. }
  286. item.Content = html.UnescapeString(item.Content)
  287. item.ContentSub = html.UnescapeString(item.ContentSub)
  288. classifyNameMap := make(map[int]*models.EnglishClassifyFullName)
  289. if item.ClassifyIdSecond > 0 {
  290. nameList, tErr := models.GetEnglishClassifyFullNameByIds([]int{item.ClassifyIdSecond})
  291. if tErr != nil {
  292. br.Msg = "获取分类名称失败"
  293. br.ErrMsg = "获取分类名称失败, ERR:" + tErr.Error()
  294. return
  295. }
  296. for _, v := range nameList {
  297. classifyNameMap[v.Id] = v
  298. }
  299. //处理分类名
  300. if n, ok := classifyNameMap[item.ClassifyIdSecond]; ok {
  301. item.ClassifyNameRoot = n.RootName
  302. item.ClassifyIdRoot = n.RootId
  303. }
  304. }
  305. br.Ret = 200
  306. br.Success = true
  307. br.Msg = "获取成功"
  308. br.Data = item
  309. }
  310. // ListReport
  311. // @Title 获取报告列表接口
  312. // @Description 获取报告列表
  313. // @Param PageSize query int true "每页数据条数"
  314. // @Param CurrentIndex query int true "当前页页码,从1开始"
  315. // @Param TimeType query string true "筛选的时间类别:publish_time(发布时间),modify_time(更新时间);approve_time(审批时间)"
  316. // @Param StartDate query string true "开始时间"
  317. // @Param EndDate query string true "结束时间"
  318. // @Param Frequency query string true "频度"
  319. // @Param ClassifyIdFirst query string true "一级分类id"
  320. // @Param ClassifyIdSecond query string true "二级分类id"
  321. // @Param State query int true "状态"
  322. // @Param KeyWord query string true "搜索关键词"
  323. // @Param PublishSort query string true "desc:降序,asc 升序(预留)"
  324. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  325. // @Success 200 {object} models.ReportListResp
  326. // @router /list [get]
  327. func (this *EnglishReportController) ListReport() {
  328. br := new(models.BaseResponse).Init()
  329. defer func() {
  330. this.Data["json"] = br
  331. this.ServeJSON()
  332. }()
  333. sysUser := this.SysUser
  334. if sysUser == nil {
  335. br.Msg = "请登录"
  336. br.ErrMsg = "请登录,SysUser Is Empty"
  337. br.Ret = 408
  338. return
  339. }
  340. pageSize, _ := this.GetInt("PageSize")
  341. currentIndex, _ := this.GetInt("CurrentIndex")
  342. timeType := this.GetString("TimeType")
  343. startDate := this.GetString("StartDate")
  344. endDate := this.GetString("EndDate")
  345. frequency := this.GetString("Frequency")
  346. classifyIdFirst, _ := this.GetInt("ClassifyIdFirst")
  347. classifyIdSecond, _ := this.GetInt("ClassifyIdSecond")
  348. classifyIdRoot, _ := this.GetInt("ClassifyIdRoot")
  349. state, _ := this.GetInt("State")
  350. keyWord := this.GetString("KeyWord")
  351. companyType := this.GetString("CompanyType")
  352. // 群发邮件状态筛选
  353. emailState, _ := this.GetInt("EmailState")
  354. var startSize int
  355. if pageSize <= 0 {
  356. pageSize = utils.PageSize20
  357. }
  358. if currentIndex <= 0 {
  359. currentIndex = 1
  360. }
  361. startSize = utils.StartIndex(currentIndex, pageSize)
  362. var condition string
  363. var pars []interface{}
  364. if keyWord != "" {
  365. condition += ` AND (title LIKE ? OR admin_real_name LIKE ? ) `
  366. pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  367. }
  368. if timeType == "" {
  369. timeType = "publish_time"
  370. }
  371. if timeType != "publish_time" && timeType != "modify_time" && timeType != "approve_time" {
  372. br.Msg = "请选择正确的时间"
  373. br.ErrMsg = "请选择正确的时间"
  374. return
  375. }
  376. if startDate != "" {
  377. condition += ` AND ` + timeType + ` >= ? `
  378. pars = append(pars, startDate)
  379. }
  380. if endDate != "" {
  381. condition += ` AND ` + timeType + ` <= ? `
  382. pars = append(pars, endDate)
  383. }
  384. if frequency != "" {
  385. condition += ` AND frequency = ? `
  386. pars = append(pars, frequency)
  387. }
  388. if classifyIdFirst != 0 {
  389. condition += ` AND classify_id_first = ? `
  390. pars = append(pars, classifyIdFirst)
  391. }
  392. if classifyIdSecond != 0 {
  393. condition += ` AND classify_id_second = ? `
  394. pars = append(pars, classifyIdSecond)
  395. }
  396. if state > 0 {
  397. condition += ` AND state = ? `
  398. pars = append(pars, state)
  399. }
  400. if classifyIdRoot > 0 && classifyIdFirst == 0 && classifyIdSecond == 0 {
  401. //查询顶级分类下的所有二级分类ID
  402. childClassify, err := models.GetEnglishSecondClassifyList([]int{classifyIdRoot})
  403. if err != nil {
  404. br.Msg = "查询子分类出错"
  405. br.ErrMsg = "查询子分类出错, Err:" + err.Error()
  406. return
  407. }
  408. var childClassifyIds []int
  409. if len(childClassify) > 0 {
  410. for _, v := range childClassify {
  411. childClassifyIds = append(childClassifyIds, v.Id)
  412. }
  413. condition += ` AND classify_id_first IN (` + utils.GetOrmInReplace(len(childClassifyIds)) + `)`
  414. pars = append(pars, childClassifyIds)
  415. } else {
  416. //一级分类下没有子分类,直接返回空列表
  417. page := paging.GetPaging(currentIndex, pageSize, 0)
  418. resp := new(models.EnglishReportListResp)
  419. resp.Paging = page
  420. resp.List = make([]*models.EnglishReportList, 0)
  421. br.Ret = 200
  422. br.Success = true
  423. br.Msg = "获取成功"
  424. br.Data = resp
  425. return
  426. }
  427. }
  428. // 未群发邮件(包含推送邮件失败的)
  429. if emailState == 1 {
  430. failIds, e := models.GetHasFailEmailLogReportIds()
  431. if e != nil {
  432. br.Msg = "获取失败"
  433. br.ErrMsg = "获取存在邮件推送失败记录的英文报告IDs失败, Err:" + e.Error()
  434. return
  435. }
  436. condition += ` AND email_state = 0`
  437. if len(failIds) > 0 {
  438. condition += ` OR id IN (` + utils.GetOrmInReplace(len(failIds)) + `)`
  439. pars = append(pars, failIds)
  440. }
  441. }
  442. // 已群发邮件
  443. if emailState == 2 {
  444. successIds, e := models.GetSuccessEmailLogReportIds()
  445. if e != nil {
  446. br.Msg = "获取失败"
  447. br.ErrMsg = "获取邮件推送记录均为成功的英文报告IDs失败, Err:" + e.Error()
  448. return
  449. }
  450. condition += ` AND email_state = 1`
  451. if len(successIds) > 0 {
  452. condition += ` AND id IN (` + utils.GetOrmInReplace(len(successIds)) + `)`
  453. pars = append(pars, successIds)
  454. }
  455. }
  456. var total int
  457. var errCount, errList, errOther error
  458. var authOk bool
  459. list := make([]*models.EnglishReportList, 0)
  460. failMap := make(map[int]bool, 0) // 有群发失败记录的研报
  461. adminMap := make(map[int]string, 0) // 编辑中的研究员姓名
  462. wg := sync.WaitGroup{}
  463. wg.Add(3)
  464. // 列表总数
  465. go func() {
  466. defer func() {
  467. wg.Done()
  468. }()
  469. t, e := models.GetEnglishReportListCount(condition, pars, companyType)
  470. if e != nil {
  471. errCount = fmt.Errorf("获取英文研报Count失败, Err: %s", e.Error())
  472. return
  473. }
  474. total = t
  475. }()
  476. // 列表数据
  477. go func() {
  478. defer func() {
  479. wg.Done()
  480. }()
  481. // 限制一下富文本字段, 列表用不到
  482. fieldArr := []string{
  483. "id", "add_type", "classify_id_first", "classify_name_first", "classify_id_second", "classify_name_second", "title", "abstract", "author",
  484. "frequency", "create_time", "modify_time", "state", "publish_time", "pre_publish_time", "stage", "msg_is_send", "report_code", "pv", "share_url",
  485. "pv_email", "email_state", "from_report_id", "key_takeaways", "admin_id", "admin_real_name", "approve_time",
  486. }
  487. items, e := models.GetEnglishReportList(condition, pars, companyType, startSize, pageSize, fieldArr)
  488. if e != nil {
  489. errList = fmt.Errorf("获取英文研报列表失败, Err: %s", e.Error())
  490. return
  491. }
  492. list = items
  493. }()
  494. // 群发权限/失败记录
  495. go func() {
  496. defer func() {
  497. wg.Done()
  498. }()
  499. // 获取邮件配置-是否有权限群发
  500. conf := new(models.EnglishReportEmailConf)
  501. authKey := "english_report_email_conf"
  502. confAuth, e := company.GetConfigDetailByCode(authKey)
  503. if e != nil {
  504. errOther = fmt.Errorf("获取群发邮件权限失败, Err: %s", e.Error())
  505. return
  506. }
  507. if confAuth.ConfigValue == "" {
  508. errOther = fmt.Errorf("群发邮件配置为空")
  509. return
  510. }
  511. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  512. errOther = fmt.Errorf("群发邮件配置有误")
  513. return
  514. }
  515. authArr := strings.Split(conf.SendAuthGroup, ",")
  516. if utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  517. authOk = true
  518. }
  519. // 是否有群发邮件失败的记录,标记红点
  520. failList, e := models.GetEnglishReportEmailLogFailList(0)
  521. if e != nil {
  522. errOther = fmt.Errorf("获取群发邮件记录失败, Err: %s", e.Error())
  523. return
  524. }
  525. for i := range failList {
  526. failMap[failList[i].ReportId] = true
  527. }
  528. // 获取admin, 用于匹配编辑中的研究员姓名
  529. admins, e := system.GetSysAdminList("", make([]interface{}, 0), []string{"admin_id", "real_name"}, "")
  530. if e != nil {
  531. errOther = fmt.Errorf("获取系统用户列表失败, Err: %s", e.Error())
  532. return
  533. }
  534. for _, a := range admins {
  535. adminMap[a.AdminId] = a.RealName
  536. }
  537. }()
  538. wg.Wait()
  539. if errCount != nil {
  540. br.Msg = "获取失败"
  541. br.ErrMsg = errCount.Error()
  542. return
  543. }
  544. if errList != nil {
  545. br.Msg = "获取失败"
  546. br.ErrMsg = errList.Error()
  547. return
  548. }
  549. if errOther != nil {
  550. br.Msg = "获取失败"
  551. br.ErrMsg = errOther.Error()
  552. return
  553. }
  554. // 查询分类信息
  555. var classifyIdSecondSlice []int
  556. for _, item := range list {
  557. classifyIdSecondSlice = append(classifyIdSecondSlice, item.ClassifyIdSecond)
  558. }
  559. classifyNameMap := make(map[int]*models.EnglishClassifyFullName)
  560. if len(classifyIdSecondSlice) > 0 {
  561. nameList, err := models.GetEnglishClassifyFullNameByIds(classifyIdSecondSlice)
  562. if err != nil {
  563. br.Msg = "获取分类名称失败"
  564. br.ErrMsg = "获取分类名称失败, ERR:" + err.Error()
  565. return
  566. }
  567. for _, v := range nameList {
  568. classifyNameMap[v.Id] = v
  569. }
  570. }
  571. for _, item := range list {
  572. if item.State == 2 {
  573. item.ShareUrl = "https://share.hzinsights.com/reportEn?code=" + item.ReportCode
  574. }
  575. item.EmailAuth = authOk
  576. item.EmailHasFail = failMap[item.Id]
  577. // 邮箱PV大于0的时候, 不展示最初版本的PV
  578. if item.PvEmail > 0 {
  579. item.Pv = 0
  580. }
  581. // 报告是否正在编辑中
  582. var opUser models.MarkReportItem
  583. key := fmt.Sprint(`crm:enReport:edit:`, item.Id)
  584. opUserId, e := utils.Rc.RedisInt(key)
  585. if e != nil {
  586. str, te := utils.Rc.RedisString(key)
  587. if te == nil {
  588. te = json.Unmarshal([]byte(str), &opUser)
  589. if te == nil {
  590. opUserId = opUser.AdminId
  591. }
  592. }
  593. }
  594. var ret models.MarkReportResp
  595. if opUserId > 0 && opUserId != sysUser.AdminId {
  596. editor := opUser.Editor
  597. if editor == "" {
  598. editor = adminMap[opUserId]
  599. }
  600. ret.Status = 1
  601. ret.Msg = fmt.Sprintf("当前%s正在编辑报告", editor)
  602. ret.Editor = editor
  603. }
  604. if ret.Status == 0 {
  605. item.CanEdit = true
  606. } else {
  607. item.Editor = ret.Editor
  608. }
  609. //处理分类名
  610. if n, ok := classifyNameMap[item.ClassifyIdSecond]; ok {
  611. if n.RootId == 0 {
  612. item.FullClassifyName = strings.Join([]string{n.ParentName, n.ClassifyName}, "/")
  613. } else {
  614. item.FullClassifyName = strings.Join([]string{n.RootName, n.ParentName, n.ClassifyName}, "/")
  615. }
  616. item.ClassifyIdRoot = n.RootId
  617. item.ClassifyNameRoot = n.RootName
  618. }
  619. }
  620. page := paging.GetPaging(currentIndex, pageSize, total)
  621. resp := new(models.EnglishReportListResp)
  622. resp.Paging = page
  623. resp.List = list
  624. br.Ret = 200
  625. br.Success = true
  626. br.Msg = "获取成功"
  627. br.Data = resp
  628. }
  629. // PublishReport
  630. // @Title 发布报告接口
  631. // @Description 发布报告
  632. // @Param request body models.PublishReq true "type json string"
  633. // @Success 200 Ret=200 发布成功
  634. // @router /publish [post]
  635. func (this *EnglishReportController) PublishReport() {
  636. br := new(models.BaseResponse).Init()
  637. defer func() {
  638. this.Data["json"] = br
  639. this.ServeJSON()
  640. }()
  641. var req models.PublishReq
  642. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  643. if err != nil {
  644. br.Msg = "参数解析异常!"
  645. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  646. return
  647. }
  648. reportIds := req.ReportIds
  649. if reportIds == "" {
  650. br.Msg = "参数错误"
  651. br.ErrMsg = "参数错误,报告id不可为空"
  652. return
  653. }
  654. reportArr := strings.Split(reportIds, ",")
  655. for _, v := range reportArr {
  656. vint, err := strconv.Atoi(v)
  657. if err != nil {
  658. br.Msg = "参数错误"
  659. br.ErrMsg = "参数错误,Err:" + err.Error()
  660. return
  661. }
  662. report, err := models.GetEnglishReportById(vint)
  663. if err != nil {
  664. br.Msg = "获取报告信息失败"
  665. br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
  666. return
  667. }
  668. if report == nil {
  669. br.Msg = "报告不存在"
  670. return
  671. }
  672. // 图表刷新状态
  673. refreshResult := data.CheckBatchChartRefreshResult("report", vint, 0)
  674. if !refreshResult {
  675. br.Msg = "图表刷新未完成,请稍后操作"
  676. br.ErrMsg = "图表刷新未完成,请稍后操作"
  677. br.IsSendEmail = false
  678. return
  679. }
  680. var tmpErr error
  681. if report.Content == "" {
  682. br.Msg = "报告内容为空,不可发布"
  683. br.ErrMsg = "报告内容为空,不需要生成,report_id:" + strconv.Itoa(report.Id)
  684. return
  685. }
  686. var publishTime string
  687. if report.PublishTime != "" {
  688. // 发布时间固定为首次发布时间
  689. publishTime = report.PublishTime
  690. } else {
  691. publishTime = time.Now().Format(utils.FormatDateTime)
  692. }
  693. // 根据审批开关及审批流判断当前报告状态
  694. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeEnglish, report.ClassifyIdFirst, report.ClassifyIdSecond, models.ReportOperatePublish)
  695. if e != nil {
  696. br.Msg = "操作失败"
  697. br.ErrMsg = "校验报告当前状态失败, Err: " + e.Error()
  698. return
  699. }
  700. if state == models.ReportStatePublished {
  701. // 发布报告
  702. if tmpErr = models.PublishEnglishReportById(report.Id, publishTime); tmpErr != nil {
  703. br.Msg = "报告发布失败"
  704. br.ErrMsg = "报告发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(report.Id)
  705. return
  706. }
  707. go func() {
  708. _ = services.UpdateEnglishReportEs(report.Id, 2)
  709. }()
  710. } else {
  711. // 从无审批切换为有审批, 状态重置
  712. if e = models.ResetEnglishReportById(report.Id, state); tmpErr != nil {
  713. br.Msg = "操作失败"
  714. br.ErrMsg = fmt.Sprintf("重置英文报告状态失败, Err: %s, ReportId: %d", e.Error(), report.Id)
  715. return
  716. }
  717. }
  718. }
  719. br.Ret = 200
  720. br.Success = true
  721. br.Msg = "发布成功"
  722. }
  723. // PrePublishReport
  724. // @Title 设置定时发布接口
  725. // @Description 设置定时发布接口
  726. // @Param request body models.PrePublishReq true "type json string"
  727. // @Success 200 Ret=200 发布成功
  728. // @router /pre_publish [post]
  729. func (this *EnglishReportController) PrePublishReport() {
  730. br := new(models.BaseResponse).Init()
  731. defer func() {
  732. this.Data["json"] = br
  733. this.ServeJSON()
  734. }()
  735. var req models.PrePublishReq
  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. reportId := req.ReportId
  743. if reportId == 0 {
  744. br.Msg = "参数错误"
  745. br.ErrMsg = "参数错误,报告id不可为空"
  746. return
  747. }
  748. if req.PrePublishTime == "" {
  749. br.Msg = "发布时间不能为空"
  750. return
  751. }
  752. prePublishTime, err := time.ParseInLocation(utils.FormatDateTime, req.PrePublishTime, time.Local)
  753. if err != nil {
  754. br.Msg = "发布时间格式错误"
  755. br.ErrMsg = "发布时间格式错误,Err:" + err.Error()
  756. return
  757. }
  758. if prePublishTime.Before(time.Now()) {
  759. br.Msg = "发布时间不允许选择过去时间"
  760. return
  761. }
  762. if prePublishTime.Before(time.Now().Add(2 * time.Minute)) {
  763. br.Msg = "发布时间距离当前时间太近了"
  764. return
  765. }
  766. report, err := models.GetEnglishReportById(reportId)
  767. if err != nil {
  768. br.Msg = "获取报告信息失败"
  769. br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
  770. return
  771. }
  772. if report == nil {
  773. br.Msg = "报告不存在"
  774. return
  775. }
  776. if report.Content == "" {
  777. br.Msg = "报告内容为空,不可发布"
  778. br.ErrMsg = "报告内容为空,不需要生成,report_id:" + strconv.Itoa(report.Id)
  779. return
  780. }
  781. if report.State == 2 {
  782. br.Msg = "报告已发布,不可设置定时发布"
  783. return
  784. }
  785. // 校验是否开启了审批流
  786. opening, e := services.CheckReportOpenApprove(report_approve.FlowReportTypeEnglish, report.ClassifyIdFirst, report.ClassifyIdSecond)
  787. if e != nil {
  788. br.Msg = "操作失败"
  789. br.ErrMsg = "校验报告是否开启审批流失败, Err: " + e.Error()
  790. return
  791. }
  792. if opening {
  793. br.Msg = "报告已开启审批流, 不可设置定时发布"
  794. return
  795. }
  796. var tmpErr error
  797. if tmpErr = models.SetPrePublishEnglishReportById(report.Id, req.PrePublishTime); tmpErr != nil {
  798. br.Msg = "设置定时发布失败"
  799. br.ErrMsg = "设置定时发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(report.Id)
  800. return
  801. }
  802. br.Ret = 200
  803. br.Success = true
  804. br.Msg = "定时发布成功"
  805. }
  806. // @Title 取消发布报告接口
  807. // @Description 取消发布报告
  808. // @Param request body models.PublishCancelReq true "type json string"
  809. // @Success 200 Ret=200 取消发布成功
  810. // @router /publish/cancel [post]
  811. func (this *EnglishReportController) PublishCancleReport() {
  812. br := new(models.BaseResponse).Init()
  813. defer func() {
  814. this.Data["json"] = br
  815. this.ServeJSON()
  816. }()
  817. var req models.PublishCancelReq
  818. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  819. if err != nil {
  820. br.Msg = "参数解析异常!"
  821. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  822. return
  823. }
  824. if req.ReportIds <= 0 {
  825. br.Msg = "参数错误"
  826. br.ErrMsg = "参数错误,报告id不可为空"
  827. return
  828. }
  829. reportInfo, err := models.GetEnglishReportById(req.ReportIds)
  830. if err != nil {
  831. br.Msg = "获取报告信息失败"
  832. br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
  833. return
  834. }
  835. // 根据审批开关及审批流判断当前报告状态
  836. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeEnglish, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, models.ReportOperateCancelPublish)
  837. if e != nil {
  838. br.Msg = "操作失败"
  839. br.ErrMsg = "校验报告当前状态失败, Err: " + e.Error()
  840. return
  841. }
  842. err = models.PublishCancelEnglishReport(req.ReportIds, state)
  843. if err != nil {
  844. br.Msg = "取消发布失败"
  845. br.ErrMsg = "取消发布失败,Err:" + err.Error()
  846. return
  847. }
  848. // 更新es里的报告状态
  849. go func() {
  850. _ = services.UpdateEnglishReportEs(req.ReportIds, 1)
  851. }()
  852. br.Ret = 200
  853. br.Success = true
  854. br.Msg = "取消发布成功"
  855. }
  856. // @Title 删除报告接口
  857. // @Description 删除报告
  858. // @Param request body models.DeleteReq true "type json string"
  859. // @Success 200 Ret=200 删除成功
  860. // @router /delete [post]
  861. func (this *EnglishReportController) Delete() {
  862. br := new(models.BaseResponse).Init()
  863. defer func() {
  864. this.Data["json"] = br
  865. this.ServeJSON()
  866. }()
  867. var req models.DeleteReq
  868. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  869. if err != nil {
  870. br.Msg = "参数解析异常!"
  871. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  872. return
  873. }
  874. if req.ReportIds <= 0 {
  875. br.Msg = "参数错误"
  876. br.ErrMsg = "参数错误,报告id不可为空"
  877. return
  878. }
  879. reportInfo, err := models.GetEnglishReportById(req.ReportIds)
  880. if err != nil {
  881. if err.Error() == utils.ErrNoRow() {
  882. br.Msg = "报告不存在"
  883. return
  884. }
  885. br.Msg = "查询报告信息失败"
  886. br.ErrMsg = "查询报告信息失败, Err" + err.Error()
  887. return
  888. }
  889. // 更新es里的报告状态
  890. _ = services.UpdateEnglishReportEs(req.ReportIds, 1)
  891. if err = models.DeleteEnglishReportAndChapter(reportInfo); err != nil {
  892. br.Msg = "删除失败"
  893. br.ErrMsg = "删除报告失败, Err: " + err.Error()
  894. return
  895. }
  896. // 重置PPT关联报告
  897. go func() {
  898. _ = services.ResetPPTReport(req.ReportIds, true)
  899. }()
  900. br.Ret = 200
  901. br.Success = true
  902. br.Msg = "删除成功"
  903. }
  904. // @Title 保存草稿
  905. // @Description 保存草稿
  906. // @Param request body models.SaveReportContent true "type json string"
  907. // @Success 200 {object} models.ReportAuthorResp
  908. // @router /saveReportContent [post]
  909. func (this *EnglishReportController) SaveReportContent() {
  910. br := new(models.BaseResponse).Init()
  911. br.IsSendEmail = false
  912. defer func() {
  913. this.Data["json"] = br
  914. this.ServeJSON()
  915. }()
  916. var req models.SaveReportContent
  917. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  918. if err != nil {
  919. br.Msg = "参数解析异常!"
  920. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  921. return
  922. }
  923. sysUser := this.SysUser
  924. if sysUser == nil {
  925. br.Msg = "请登录"
  926. br.ErrMsg = "请登录,SysUser Is Empty"
  927. br.Ret = 408
  928. return
  929. }
  930. reportId := req.ReportId
  931. noChangeFlag := req.NoChange
  932. fmt.Println("reportId:", reportId)
  933. if reportId <= 0 {
  934. br.Msg = "请输入报告ID"
  935. return
  936. }
  937. markStatus, err := services.UpdateEnReportEditMark(int(req.ReportId), sysUser.AdminId, 1, sysUser.RealName)
  938. if err != nil {
  939. br.Msg = err.Error()
  940. return
  941. }
  942. if markStatus.Status == 1 {
  943. br.Msg = markStatus.Msg
  944. return
  945. }
  946. if noChangeFlag != 1 {
  947. content := req.Content
  948. if content == "" {
  949. content = this.GetString("Content")
  950. }
  951. if content != "" {
  952. req.Content = utils.ContentXssFilter(req.Content)
  953. contentClean, e := services.FilterReportContentBr(req.Content)
  954. if e != nil {
  955. br.Msg = "内容去除前后空格失败"
  956. br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
  957. return
  958. }
  959. content = contentClean
  960. contentSub, err := services.GetReportContentSub(content)
  961. if err != nil {
  962. go alarm_msg.SendAlarmMsg("解析 ContentSub 失败,Err:"+err.Error(), 3)
  963. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  964. }
  965. content = html.EscapeString(content)
  966. contentSub = html.EscapeString(contentSub)
  967. err = models.EditEnglishReportContent(reportId, content, contentSub)
  968. if err != nil {
  969. br.Msg = "保存失败"
  970. br.ErrMsg = "保存失败,Err:" + err.Error()
  971. return
  972. }
  973. go models.AddEnglishReportSaveLog(reportId, this.SysUser.AdminId, content, contentSub, this.SysUser.AdminName)
  974. }
  975. }
  976. resp := new(models.SaveReportContentResp)
  977. resp.ReportId = reportId
  978. br.Ret = 200
  979. br.Success = true
  980. br.Msg = "保存成功成功"
  981. br.Data = resp
  982. }
  983. // @Title 根据分类获取最近一次报告详情接口
  984. // @Description 根据分类获取最近一次报告详情
  985. // @Param request body models.ClassifyIdDetailReq true "type json string"
  986. // @Success 200 {object} models.Report
  987. // @router /classifyIdDetail [get]
  988. func (this *EnglishReportController) ClassifyIdDetail() {
  989. br := new(models.BaseResponse).Init()
  990. defer func() {
  991. this.Data["json"] = br
  992. this.ServeJSON()
  993. }()
  994. //classify_id_first=34&classify_id_second=36
  995. /*var req models.ClassifyIdDetailReq
  996. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  997. if err != nil {
  998. br.Msg = "参数解析异常!"
  999. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1000. return
  1001. }*/
  1002. classifyIdFirst, _ := this.GetInt("classify_id_first")
  1003. if classifyIdFirst <= 0 {
  1004. classifyIdFirst, _ = this.GetInt("ClassifyIdFirst")
  1005. }
  1006. classifyIdSecond, _ := this.GetInt("classify_id_second")
  1007. if classifyIdSecond <= 0 {
  1008. classifyIdSecond, _ = this.GetInt("ClassifyIdSecond")
  1009. }
  1010. item, err := models.GetEnglishReportDetailByClassifyId(classifyIdFirst, classifyIdSecond)
  1011. if err != nil && err.Error() != utils.ErrNoRow() {
  1012. br.Msg = "获取失败!"
  1013. br.ErrMsg = "获取失败,Err:" + err.Error()
  1014. return
  1015. }
  1016. if item != nil {
  1017. item.Content = html.UnescapeString(item.Content)
  1018. item.ContentSub = html.UnescapeString(item.ContentSub)
  1019. classifyNameMap := make(map[int]*models.EnglishClassifyFullName)
  1020. if item.ClassifyIdSecond > 0 {
  1021. nameList, tErr := models.GetEnglishClassifyFullNameByIds([]int{item.ClassifyIdSecond})
  1022. if tErr != nil {
  1023. br.Msg = "获取分类名称失败"
  1024. br.ErrMsg = "获取分类名称失败, ERR:" + tErr.Error()
  1025. return
  1026. }
  1027. for _, v := range nameList {
  1028. classifyNameMap[v.Id] = v
  1029. }
  1030. //处理分类名
  1031. if n, ok := classifyNameMap[item.ClassifyIdSecond]; ok {
  1032. item.ClassifyNameRoot = n.RootName
  1033. item.ClassifyIdRoot = n.RootId
  1034. }
  1035. }
  1036. }
  1037. br.Ret = 200
  1038. br.Success = true
  1039. br.Msg = "获取成功"
  1040. br.Data = item
  1041. }
  1042. // Author
  1043. // @Title 获取报告作者接口
  1044. // @Description 获取报告作者
  1045. // @Param AuthorType query int true "来源类型,1:中文,2:英文"
  1046. // @Param Keyword query string true "搜索关键词"
  1047. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1048. // @Param StartDate query string true "开始时间"
  1049. // @Success 200 {object} models.ReportAuthorResp
  1050. // @router /author [get]
  1051. func (this *EnglishReportController) Author() {
  1052. br := new(models.BaseResponse).Init()
  1053. defer func() {
  1054. this.Data["json"] = br
  1055. this.ServeJSON()
  1056. }()
  1057. keyword := this.GetString("Keyword")
  1058. var condition string
  1059. var pars []interface{}
  1060. condition += ` AND author_type = 2 AND enable = 1`
  1061. if keyword != `` {
  1062. condition += ` AND report_author like ? `
  1063. pars = append(pars, utils.GetLikeKeyword(keyword))
  1064. }
  1065. _, items, err := models.GetReportAuthorList(condition, pars, 0, 10000)
  1066. if err != nil {
  1067. br.Msg = "获取失败!"
  1068. br.ErrMsg = "获取失败,Err:" + err.Error()
  1069. return
  1070. }
  1071. resp := models.ReportAuthorResp{
  1072. List: items,
  1073. }
  1074. br.Ret = 200
  1075. br.Success = true
  1076. br.Msg = "获取成功"
  1077. br.Data = resp
  1078. }
  1079. // MarkEditStatus
  1080. // @Title 标记报告编辑状态
  1081. // @Description 标记报告编辑状态接口
  1082. // @Param request body request.MarkEditEnReport true "type json string"
  1083. // @Success 200 标记成功 ;202 标记成功
  1084. // @router /mark [post]
  1085. func (this *EnglishReportController) MarkEditStatus() {
  1086. br := new(models.BaseResponse).Init()
  1087. defer func() {
  1088. this.Data["json"] = br
  1089. this.ServeJSON()
  1090. }()
  1091. sysUser := this.SysUser
  1092. if sysUser == nil {
  1093. br.Msg = "请登录"
  1094. br.ErrMsg = "请登录,SysUser Is Empty"
  1095. br.Ret = 408
  1096. return
  1097. }
  1098. var req models.MarkEditEnReport
  1099. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1100. if err != nil {
  1101. br.Msg = "参数解析异常!"
  1102. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1103. return
  1104. }
  1105. if req.ReportId <= 0 {
  1106. br.Msg = "缺少报告Id"
  1107. return
  1108. }
  1109. if req.Status <= 0 {
  1110. br.Msg = "标记状态异常"
  1111. return
  1112. }
  1113. //更新标记key
  1114. data, err := services.UpdateEnReportEditMark(req.ReportId, sysUser.AdminId, req.Status, sysUser.RealName)
  1115. if err != nil {
  1116. br.Msg = err.Error()
  1117. return
  1118. }
  1119. msg := "标记成功"
  1120. br.Ret = 200
  1121. br.Success = true
  1122. br.Msg = msg
  1123. br.Data = data
  1124. }
  1125. // @Title 编辑来自策略报告的报告
  1126. // @Description 编辑来自策略报告的报告
  1127. // @Param request body models.EditEnglishReportFromPolicyReq true "type json string"
  1128. // @Success 200 {object} models.EditEnglishReportResp
  1129. // @router /edit_policy [post]
  1130. func (this *EnglishReportController) EditPolicy() {
  1131. br := new(models.BaseResponse).Init()
  1132. defer func() {
  1133. this.Data["json"] = br
  1134. this.ServeJSON()
  1135. }()
  1136. sysUser := this.SysUser
  1137. if sysUser == nil {
  1138. br.Msg = "请登录"
  1139. br.ErrMsg = "请登录,SysUser Is Empty"
  1140. br.Ret = 408
  1141. return
  1142. }
  1143. var req models.EditEnglishReportFromPolicyReq
  1144. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1145. if err != nil {
  1146. br.Msg = "参数解析异常!"
  1147. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1148. return
  1149. }
  1150. if req.ReportId <= 0 {
  1151. br.Msg = "请选择报告ID"
  1152. return
  1153. }
  1154. if req.Title == "" {
  1155. br.Msg = "请输入标题"
  1156. return
  1157. }
  1158. if req.Abstract == "" {
  1159. br.Msg = "请输入摘要"
  1160. return
  1161. }
  1162. if req.Frequency == "" {
  1163. br.Msg = "请输入频度"
  1164. return
  1165. }
  1166. if req.Author == "" {
  1167. br.Msg = "请输入作者"
  1168. return
  1169. }
  1170. if req.CreateTime == "" {
  1171. br.Msg = "请输入创建时间"
  1172. return
  1173. }
  1174. //查询报告是否存在
  1175. reportInfo, err := models.GetEnglishReportItemById(int(req.ReportId))
  1176. if err != nil {
  1177. if err.Error() == utils.ErrNoRow() {
  1178. br.Msg = "报告不存在"
  1179. return
  1180. }
  1181. br.Msg = "查询报告信息失败"
  1182. br.ErrMsg = "查询报告信息失败, Err" + err.Error()
  1183. return
  1184. }
  1185. if reportInfo.State == 2 {
  1186. br.Msg = "报告已发布不允许编辑"
  1187. return
  1188. }
  1189. markStatus, err := services.UpdateEnReportEditMark(int(req.ReportId), sysUser.AdminId, 1, sysUser.RealName)
  1190. if err != nil {
  1191. br.Msg = err.Error()
  1192. return
  1193. }
  1194. if markStatus.Status == 1 {
  1195. br.Msg = markStatus.Msg
  1196. return
  1197. }
  1198. item := reportInfo
  1199. item.Title = req.Title
  1200. item.Abstract = req.Abstract
  1201. item.Author = req.Author
  1202. item.Frequency = req.Frequency
  1203. item.CreateTime = req.CreateTime
  1204. item.ModifyTime = time.Now()
  1205. err = item.Update([]string{"Title", "Abstract", "Author", "Frequency", "CreateTime", "ModifyTime"})
  1206. if err != nil {
  1207. br.Msg = "保存失败"
  1208. br.ErrMsg = "保存失败,Err:" + err.Error()
  1209. return
  1210. }
  1211. reportCode := reportInfo.ReportCode
  1212. resp := new(models.EditEnglishReportResp)
  1213. resp.ReportId = req.ReportId
  1214. resp.ReportCode = reportCode
  1215. br.Ret = 200
  1216. br.Success = true
  1217. br.Msg = "保存成功"
  1218. br.Data = resp
  1219. }
  1220. // SubmitApprove
  1221. // @Title 提交审批
  1222. // @Description 提交审批接口
  1223. // @Param request body models.ReportSubmitApproveReq true "type json string"
  1224. // @Success 200 Ret=200 操作成功
  1225. // @router /approve/submit [post]
  1226. func (this *EnglishReportController) SubmitApprove() {
  1227. br := new(models.BaseResponse).Init()
  1228. defer func() {
  1229. if br.ErrMsg == "" {
  1230. br.IsSendEmail = false
  1231. }
  1232. this.Data["json"] = br
  1233. this.ServeJSON()
  1234. }()
  1235. sysUser := this.SysUser
  1236. if sysUser == nil {
  1237. br.Msg = "请登录"
  1238. br.ErrMsg = "请登录,SysUser Is Empty"
  1239. return
  1240. }
  1241. var req models.ReportSubmitApproveReq
  1242. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1243. br.Msg = "参数有误"
  1244. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  1245. return
  1246. }
  1247. reportId := req.ReportId
  1248. if reportId <= 0 {
  1249. br.Msg = "参数有误"
  1250. br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
  1251. return
  1252. }
  1253. reportOb := new(models.EnglishReport)
  1254. reportItem, e := reportOb.GetItemById(reportId)
  1255. if e != nil {
  1256. if e.Error() == utils.ErrNoRow() {
  1257. br.Msg = "报告已被删除, 请刷新页面"
  1258. return
  1259. }
  1260. br.Msg = "操作失败"
  1261. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  1262. return
  1263. }
  1264. // 校验当前审批配置, 返回下一个状态
  1265. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeEnglish, reportItem.ClassifyIdFirst, reportItem.ClassifyIdSecond, models.ReportOperateSubmitApprove)
  1266. if e != nil {
  1267. br.Msg = "操作失败"
  1268. br.ErrMsg = "校验报告状态失败, Err: " + e.Error()
  1269. return
  1270. }
  1271. // 下一个状态不为待审批时, 仅更新状态
  1272. if state != models.ReportStateWaitApprove {
  1273. reportItem.State = state
  1274. e = reportItem.UpdateReport([]string{"State"})
  1275. if e != nil {
  1276. br.Msg = "操作失败"
  1277. br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1278. return
  1279. }
  1280. br.Ret = 200
  1281. br.Success = true
  1282. br.Msg = "操作成功"
  1283. return
  1284. }
  1285. // 提交审批
  1286. approveId, e := services.SubmitReportApprove(report_approve.FlowReportTypeEnglish, reportItem.Id, reportItem.Title, reportItem.ClassifyIdFirst, reportItem.ClassifyIdSecond, sysUser.AdminId, sysUser.RealName)
  1287. if e != nil {
  1288. br.Msg = "操作失败"
  1289. br.ErrMsg = "提交审批失败, Err: " + e.Error()
  1290. return
  1291. }
  1292. reportItem.ApproveId = approveId
  1293. reportItem.State = models.ReportStateWaitApprove
  1294. reportItem.ModifyTime = time.Now().Local()
  1295. e = reportItem.UpdateReport([]string{"ApproveId", "State", "ModifyTime"})
  1296. if e != nil {
  1297. br.Msg = "操作失败"
  1298. br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1299. return
  1300. }
  1301. br.Ret = 200
  1302. br.Success = true
  1303. br.Msg = "操作成功"
  1304. }
  1305. // CancelApprove
  1306. // @Title 撤销审批
  1307. // @Description 撤销审批
  1308. // @Param request body models.ReportCancelApproveReq true "type json string"
  1309. // @Success 200 Ret=200 操作成功
  1310. // @router /approve/cancel [post]
  1311. func (this *EnglishReportController) CancelApprove() {
  1312. br := new(models.BaseResponse).Init()
  1313. defer func() {
  1314. if br.ErrMsg == "" {
  1315. br.IsSendEmail = false
  1316. }
  1317. this.Data["json"] = br
  1318. this.ServeJSON()
  1319. }()
  1320. sysUser := this.SysUser
  1321. if sysUser == nil {
  1322. br.Msg = "请登录"
  1323. br.ErrMsg = "请登录,SysUser Is Empty"
  1324. return
  1325. }
  1326. var req models.ReportCancelApproveReq
  1327. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1328. br.Msg = "参数有误"
  1329. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  1330. return
  1331. }
  1332. reportId := req.ReportId
  1333. if reportId <= 0 {
  1334. br.Msg = "参数有误"
  1335. br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
  1336. return
  1337. }
  1338. reportOb := new(models.EnglishReport)
  1339. reportItem, e := reportOb.GetItemById(reportId)
  1340. if e != nil {
  1341. if e.Error() == utils.ErrNoRow() {
  1342. br.Msg = "报告已被删除, 请刷新页面"
  1343. return
  1344. }
  1345. br.Msg = "操作失败"
  1346. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  1347. return
  1348. }
  1349. // 校验当前审批配置, 返回下一个状态
  1350. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeEnglish, reportItem.ClassifyIdFirst, reportItem.ClassifyIdSecond, models.ReportOperateCancelApprove)
  1351. if e != nil {
  1352. br.Msg = "操作失败"
  1353. br.ErrMsg = "校验报告状态失败, Err: " + e.Error()
  1354. return
  1355. }
  1356. // 下一个状态不为待提交时, 仅更新状态
  1357. if state != models.ReportStateWaitSubmit {
  1358. reportItem.State = state
  1359. e = reportItem.UpdateReport([]string{"State"})
  1360. if e != nil {
  1361. br.Msg = "操作失败"
  1362. br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1363. return
  1364. }
  1365. br.Ret = 200
  1366. br.Success = true
  1367. br.Msg = "操作成功"
  1368. return
  1369. }
  1370. //if reportItem.ApproveId <= 0 {
  1371. // br.Msg = "报告审批不存在"
  1372. // br.ErrMsg = fmt.Sprintf("报告审批不存在, ApproveId: %d", reportItem.ApproveId)
  1373. // return
  1374. //}
  1375. // 撤销审批
  1376. e = services.CancelReportApprove(report_approve.FlowReportTypeEnglish, reportItem.Id, reportItem.ApproveId, sysUser.AdminId, sysUser.RealName)
  1377. if e != nil {
  1378. br.Msg = "操作失败"
  1379. br.ErrMsg = "撤销审批失败, Err: " + e.Error()
  1380. return
  1381. }
  1382. //reportItem.ApproveId = 0
  1383. //reportItem.State = models.ReportStateWaitSubmit
  1384. //reportItem.ModifyTime = time.Now().Local()
  1385. //e = reportItem.UpdateReport([]string{"ApproveId", "State", "ModifyTime"})
  1386. //if e != nil {
  1387. // br.Msg = "操作失败"
  1388. // br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1389. // return
  1390. //}
  1391. br.Ret = 200
  1392. br.Success = true
  1393. br.Msg = "操作成功"
  1394. }