report.go 33 KB

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