report.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. package english_report
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hongze_ETA_mobile_api/controllers"
  6. "hongze/hongze_ETA_mobile_api/models"
  7. "hongze/hongze_ETA_mobile_api/models/company"
  8. "hongze/hongze_ETA_mobile_api/services"
  9. "hongze/hongze_ETA_mobile_api/utils"
  10. "html"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. )
  15. // EnglishReportController 研报活动模块
  16. type EnglishReportController struct {
  17. controllers.BaseAuthController
  18. }
  19. // @Title 获取报告详情接口
  20. // @Description 获取报告详情
  21. // @Param request body models.ReportDetailReq true "type json string"
  22. // @Success 200 {object} models.EnglishReportDetailView
  23. // @router /detail [get]
  24. func (this *EnglishReportController) Detail() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. sysUser := this.SysUser
  31. if sysUser == nil {
  32. br.Msg = "请登录"
  33. br.ErrMsg = "请登录,SysUser Is Empty"
  34. br.Ret = 408
  35. return
  36. }
  37. reportId, err := this.GetInt("ReportId")
  38. if err != nil {
  39. br.Msg = "获取参数失败!"
  40. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  41. return
  42. }
  43. if reportId <= 0 {
  44. br.Msg = "参数错误"
  45. return
  46. }
  47. item, err := models.GetEnglishReportById(reportId)
  48. if err != nil {
  49. br.Msg = "获取失败"
  50. br.ErrMsg = "获取失败,Err:" + err.Error()
  51. return
  52. }
  53. item.Content = html.UnescapeString(item.Content)
  54. item.ContentSub = html.UnescapeString(item.ContentSub)
  55. // 获取邮件配置-是否有权限群发
  56. conf := new(models.EnglishReportEmailConf)
  57. authKey := "english_report_email_conf"
  58. confAuth, e := company.GetConfigDetailByCode(authKey)
  59. if e != nil {
  60. br.Msg = "获取失败"
  61. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  62. return
  63. }
  64. if confAuth.ConfigValue == "" {
  65. br.Msg = "获取失败"
  66. br.ErrMsg = "群发邮件配置为空"
  67. return
  68. }
  69. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  70. br.Msg = "获取失败"
  71. br.ErrMsg = "群发邮件配置有误"
  72. return
  73. }
  74. authOk := false
  75. authArr := strings.Split(conf.SendAuthGroup, ",")
  76. if utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  77. authOk = true
  78. }
  79. item.EmailAuth = authOk
  80. // 是否有群发邮件失败的记录,标记红点
  81. failList, e := models.GetEnglishReportEmailLogFailList(0)
  82. if e != nil {
  83. br.Msg = "获取失败"
  84. br.ErrMsg = "获取群发邮件记录失败, Err: " + e.Error()
  85. return
  86. }
  87. failMap := make(map[int]bool, 0)
  88. for i := range failList {
  89. failMap[failList[i].ReportId] = true
  90. }
  91. item.EmailHasFail = failMap[item.Id]
  92. br.Ret = 200
  93. br.Success = true
  94. br.Msg = "获取成功"
  95. br.Data = item
  96. }
  97. // @Title 获取报告列表接口
  98. // @Description 获取报告列表
  99. // @Param PageSize query int true "每页数据条数"
  100. // @Param CurrentIndex query int true "当前页页码,从1开始"
  101. // @Param StartDate query string true "开始时间"
  102. // @Param EndDate query string true "结束时间"
  103. // @Param Frequency query string true "频度"
  104. // @Param ClassifyNameFirst query string true "一级分类名称"
  105. // @Param ClassifyNameSecond query string true "二级分类名称"
  106. // @Param State query int true "状态"
  107. // @Param KeyWord query string true "搜索关键词"
  108. // @Param PublishSort query string true "desc:降序,asc 升序(预留)"
  109. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  110. // @Success 200 {object} models.ReportListResp
  111. // @router /list [get]
  112. func (this *EnglishReportController) ListReport() {
  113. br := new(models.BaseResponse).Init()
  114. defer func() {
  115. this.Data["json"] = br
  116. this.ServeJSON()
  117. }()
  118. sysUser := this.SysUser
  119. if sysUser == nil {
  120. br.Msg = "请登录"
  121. br.ErrMsg = "请登录,SysUser Is Empty"
  122. br.Ret = 408
  123. return
  124. }
  125. pageSize, _ := this.GetInt("PageSize")
  126. currentIndex, _ := this.GetInt("CurrentIndex")
  127. startDate := this.GetString("StartDate")
  128. endDate := this.GetString("EndDate")
  129. frequency := this.GetString("Frequency")
  130. classifyNameFirst := this.GetString("ClassifyNameFirst")
  131. classifyNameSecond := this.GetString("ClassifyNameSecond")
  132. state, _ := this.GetInt("State")
  133. keyWord := this.GetString("KeyWord")
  134. companyType := this.GetString("CompanyType")
  135. // 群发邮件状态筛选
  136. emailState, _ := this.GetInt("EmailState")
  137. var startSize int
  138. if pageSize <= 0 {
  139. pageSize = utils.PageSize20
  140. }
  141. if currentIndex <= 0 {
  142. currentIndex = 1
  143. }
  144. startSize = utils.StartIndex(currentIndex, pageSize)
  145. var condition string
  146. var pars []interface{}
  147. if keyWord != "" {
  148. condition += ` AND (title LIKE '%` + keyWord + `%' OR author LIKE '%` + keyWord + `%' ) `
  149. }
  150. if startDate != "" {
  151. condition += ` AND create_time >= ? `
  152. pars = append(pars, startDate)
  153. }
  154. if endDate != "" {
  155. condition += ` AND create_time <= ? `
  156. pars = append(pars, endDate)
  157. }
  158. if frequency != "" {
  159. condition += ` AND frequency = ? `
  160. pars = append(pars, frequency)
  161. }
  162. if classifyNameFirst != "" {
  163. condition += ` AND classify_name_first = ? `
  164. pars = append(pars, classifyNameFirst)
  165. }
  166. if classifyNameSecond != "" {
  167. condition += ` AND classify_name_second = ? `
  168. pars = append(pars, classifyNameSecond)
  169. }
  170. if state > 0 {
  171. condition += ` AND state = ? `
  172. pars = append(pars, state)
  173. }
  174. // 未群发邮件(包含推送邮件失败的)
  175. if emailState == 1 {
  176. failIds, e := models.GetHasFailEmailLogReportIds()
  177. if e != nil {
  178. br.Msg = "获取失败"
  179. br.ErrMsg = "获取存在邮件推送失败记录的英文报告IDs失败, Err:" + e.Error()
  180. return
  181. }
  182. condition += ` AND email_state = 0`
  183. if len(failIds) > 0 {
  184. condition += ` OR id IN (` + utils.GetOrmInReplace(len(failIds)) + `)`
  185. pars = append(pars, failIds)
  186. }
  187. }
  188. // 已群发邮件
  189. if emailState == 2 {
  190. successIds, e := models.GetSuccessEmailLogReportIds()
  191. if e != nil {
  192. br.Msg = "获取失败"
  193. br.ErrMsg = "获取邮件推送记录均为成功的英文报告IDs失败, Err:" + e.Error()
  194. return
  195. }
  196. condition += ` AND email_state = 1`
  197. if len(successIds) > 0 {
  198. condition += ` AND id IN (` + utils.GetOrmInReplace(len(successIds)) + `)`
  199. pars = append(pars, successIds)
  200. }
  201. }
  202. total, err := models.GetEnglishReportListCount(condition, pars, companyType)
  203. if err != nil {
  204. br.Msg = "获取失败"
  205. br.ErrMsg = "获取失败,Err:" + err.Error()
  206. return
  207. }
  208. list, err := models.GetEnglishReportList(condition, pars, companyType, startSize, pageSize)
  209. if err != nil {
  210. br.Msg = "获取失败"
  211. br.ErrMsg = "获取失败,Err:" + err.Error()
  212. return
  213. }
  214. // 获取邮件配置-是否有权限群发
  215. conf := new(models.EnglishReportEmailConf)
  216. authKey := "english_report_email_conf"
  217. confAuth, e := company.GetConfigDetailByCode(authKey)
  218. if e != nil {
  219. br.Msg = "获取失败"
  220. br.ErrMsg = "获取群发邮件权限失败, Err: " + e.Error()
  221. return
  222. }
  223. if confAuth.ConfigValue == "" {
  224. br.Msg = "获取失败"
  225. br.ErrMsg = "群发邮件配置为空"
  226. return
  227. }
  228. if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
  229. br.Msg = "获取失败"
  230. br.ErrMsg = "群发邮件配置有误"
  231. return
  232. }
  233. authOk := false
  234. authArr := strings.Split(conf.SendAuthGroup, ",")
  235. if utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
  236. authOk = true
  237. }
  238. // 是否有群发邮件失败的记录,标记红点
  239. failList, e := models.GetEnglishReportEmailLogFailList(0)
  240. if e != nil {
  241. br.Msg = "获取失败"
  242. br.ErrMsg = "获取群发邮件记录失败, Err: " + e.Error()
  243. return
  244. }
  245. failMap := make(map[int]bool, 0)
  246. for i := range failList {
  247. failMap[failList[i].ReportId] = true
  248. }
  249. for _, item := range list {
  250. if item.State == 2 {
  251. item.ShareUrl = "https://share.hzinsights.com/reportEn?code=" + item.ReportCode
  252. }
  253. item.EmailAuth = authOk
  254. item.EmailHasFail = failMap[item.Id]
  255. // 邮箱PV大于0的时候, 不展示最初版本的PV
  256. if item.PvEmail > 0 {
  257. item.Pv = 0
  258. }
  259. /*key := fmt.Sprint(`crm:enReport:edit:`, item.Id)
  260. opUserId, _ := utils.Rc.RedisInt(key)
  261. //如果当前没有人操作,获取当前操作人是本人,那么编辑按钮可用
  262. if opUserId <= 0 || (opUserId == this.SysUser.AdminId) {
  263. item.CanEdit = true
  264. } else {
  265. adminInfo, errAdmin := system.GetSysUserById(opUserId)
  266. if errAdmin != nil {
  267. br.Msg = "获取失败"
  268. br.ErrMsg = "获取失败,Err:" + errAdmin.Error()
  269. return
  270. }
  271. item.Editor = adminInfo.RealName
  272. }*/
  273. markStatus, err := services.UpdateEnReportEditMark(item.Id, this.SysUser.AdminId, 2, this.SysUser.RealName)
  274. if err != nil {
  275. br.Msg = "查询标记状态失败"
  276. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  277. return
  278. }
  279. if markStatus.Status == 0 {
  280. item.CanEdit = true
  281. } else {
  282. item.Editor = markStatus.Editor
  283. }
  284. }
  285. page := paging.GetPaging(currentIndex, pageSize, total)
  286. resp := new(models.EnglishReportListResp)
  287. resp.Paging = page
  288. resp.List = list
  289. br.Ret = 200
  290. br.Success = true
  291. br.Msg = "获取成功"
  292. br.Data = resp
  293. }
  294. // @Title 获取分类列表
  295. // @Description 获取分类列表
  296. // @Param PageSize query int true "每页数据条数"
  297. // @Param CurrentIndex query int true "当前页页码,从1开始"
  298. // @Param KeyWord query string true "检索关键词"
  299. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  300. // @Success 200 {object} models.EnglishClassifyListResp
  301. // @router /classify/list [get]
  302. func (this *EnglishReportController) ListClassify() {
  303. br := new(models.BaseResponse).Init()
  304. defer func() {
  305. this.Data["json"] = br
  306. this.ServeJSON()
  307. }()
  308. pageSize, _ := this.GetInt("PageSize")
  309. currentIndex, _ := this.GetInt("CurrentIndex")
  310. keyWord := this.GetString("KeyWord")
  311. classifyType, _ := this.GetInt("ClassifyType", 0)
  312. var startSize int
  313. if pageSize <= 0 {
  314. pageSize = utils.PageSize20
  315. }
  316. if currentIndex <= 0 {
  317. currentIndex = 1
  318. }
  319. startSize = utils.StartIndex(currentIndex, pageSize)
  320. page := paging.GetPaging(currentIndex, pageSize, 0)
  321. resp := new(models.EnglishClassifyListResp)
  322. list, err := models.GetEnglishClassifyList(startSize, pageSize, keyWord, classifyType)
  323. if err != nil {
  324. br.Msg = "获取失败"
  325. br.ErrMsg = "获取失败,Err:" + err.Error()
  326. return
  327. }
  328. total, err := models.GetEnglishClassifyListCount(keyWord, classifyType)
  329. if err != nil {
  330. br.Msg = "获取失败"
  331. br.ErrMsg = "获取失败,Err:" + err.Error()
  332. return
  333. }
  334. if total == 0 {
  335. resp.List = make([]*models.EnglishClassifyList, 0)
  336. resp.Paging = page
  337. br.Data = resp
  338. br.Ret = 200
  339. br.Success = true
  340. br.Msg = "获取成功"
  341. return
  342. }
  343. var parentIds []int
  344. for _, v := range list {
  345. parentIds = append(parentIds, v.Id)
  346. }
  347. childMap := make(map[int][]*models.EnglishClassify)
  348. tmpList, err := models.GetEnglishClassifyChildByParentIds(parentIds, keyWord, classifyType)
  349. if err != nil {
  350. br.Msg = "获取二级分类失败"
  351. br.ErrMsg = "获取二级分类失败,Err:" + err.Error()
  352. return
  353. }
  354. for _, v := range tmpList {
  355. childMap[v.ParentId] = append(childMap[v.ParentId], v)
  356. }
  357. for _, v := range list {
  358. if child, ok := childMap[v.Id]; ok {
  359. v.Child = child
  360. }
  361. }
  362. var sortList models.RSClassifyList
  363. sortList = list
  364. sort.Sort(sortList)
  365. for _, item := range sortList {
  366. var sortChildList models.RSChildClassifyList
  367. sortChildList = item.Child
  368. sort.Sort(sortChildList)
  369. item.Child = sortChildList
  370. }
  371. resp.List = sortList
  372. resp.Paging = page
  373. br.Data = resp
  374. br.Ret = 200
  375. br.Success = true
  376. br.Msg = "获取成功"
  377. }
  378. // @Title 删除分类列表
  379. // @Description 删除分类列表
  380. // @Param ClassifyId int true "分类名称"
  381. // @Param ParentId query int true "父级Id"
  382. // @Success 200 保存成功
  383. // @router /classify/delete [get]
  384. func (this *EnglishReportController) DelClassify() {
  385. br := new(models.BaseResponse).Init()
  386. defer func() {
  387. this.Data["json"] = br
  388. this.ServeJSON()
  389. }()
  390. classifyId, _ := this.GetInt("ClassifyId")
  391. //parentId, _ := this.GetInt("ParentId")
  392. classifyInfo, err := models.GetEnglishReportClassifyById(classifyId)
  393. if err != nil {
  394. if err.Error() == utils.ErrNoRow() {
  395. br.Msg = "当前分类不存在"
  396. br.ErrMsg = "当前分类不存在"
  397. return
  398. }
  399. br.Msg = "获取分类信息失败"
  400. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  401. return
  402. }
  403. count, err := models.GetEnglishClassifyChildCounts(classifyId)
  404. if err != nil && err.Error() != utils.ErrNoRow() {
  405. br.Msg = "获取信息失败"
  406. br.ErrMsg = "获取失败,Err:" + err.Error()
  407. return
  408. }
  409. if count > 0 {
  410. br.Msg = "请先删除该分类下关联分类"
  411. br.Ret = 403
  412. return
  413. }
  414. if classifyInfo.ClassifyType == 0 {
  415. reportCount, e := models.GetEnglishReportCounts(classifyId, classifyInfo.ParentId)
  416. if e != nil && e.Error() != utils.ErrNoRow() {
  417. br.Msg = "获取信息失败"
  418. br.ErrMsg = "获取失败,Err:" + e.Error()
  419. return
  420. }
  421. if reportCount > 0 {
  422. br.Msg = "该分类有关联报告,不允许删除"
  423. br.Ret = 403
  424. return
  425. }
  426. } else {
  427. videoCount, e := models.GetEnglishVideoCounts(classifyId, classifyInfo.ParentId)
  428. if e != nil && e.Error() != utils.ErrNoRow() {
  429. br.Msg = "获取信息失败"
  430. br.ErrMsg = "获取失败,Err:" + e.Error()
  431. return
  432. }
  433. if videoCount > 0 {
  434. br.Msg = "该分类有关联的路演视频,不允许删除"
  435. br.Ret = 403
  436. return
  437. }
  438. }
  439. if err = models.DeleteEnglishClassify(classifyId); err != nil {
  440. br.Msg = "删除失败"
  441. br.ErrMsg = "删除报告失败, Err: " + err.Error()
  442. return
  443. }
  444. br.Ret = 200
  445. br.Success = true
  446. br.Msg = "删除成功"
  447. }
  448. // @Title 发布报告接口
  449. // @Description 发布报告
  450. // @Param request body models.PublishReq true "type json string"
  451. // @Success 200 Ret=200 发布成功
  452. // @router /publish [post]
  453. func (this *EnglishReportController) PublishReport() {
  454. br := new(models.BaseResponse).Init()
  455. defer func() {
  456. this.Data["json"] = br
  457. this.ServeJSON()
  458. }()
  459. var req models.PublishReq
  460. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  461. if err != nil {
  462. br.Msg = "参数解析异常!"
  463. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  464. return
  465. }
  466. reportIds := req.ReportIds
  467. if reportIds == "" {
  468. br.Msg = "参数错误"
  469. br.ErrMsg = "参数错误,报告id不可为空"
  470. return
  471. }
  472. reportArr := strings.Split(reportIds, ",")
  473. for _, v := range reportArr {
  474. vint, err := strconv.Atoi(v)
  475. if err != nil {
  476. br.Msg = "参数错误"
  477. br.ErrMsg = "参数错误,Err:" + err.Error()
  478. return
  479. }
  480. report, err := models.GetEnglishReportById(vint)
  481. if err != nil {
  482. br.Msg = "获取报告信息失败"
  483. br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
  484. return
  485. }
  486. if report == nil {
  487. br.Msg = "报告不存在"
  488. return
  489. }
  490. var tmpErr error
  491. if report.Content == "" {
  492. br.Msg = "报告内容为空,不可发布"
  493. br.ErrMsg = "报告内容为空,不需要生成,report_id:" + strconv.Itoa(report.Id)
  494. return
  495. }
  496. if tmpErr = models.PublishEnglishReportById(report.Id); tmpErr != nil {
  497. br.Msg = "报告发布失败"
  498. br.ErrMsg = "报告发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(report.Id)
  499. return
  500. }
  501. go func() {
  502. _ = services.UpdateEnglishReportEs(report.Id, 2)
  503. }()
  504. }
  505. br.Ret = 200
  506. br.Success = true
  507. br.Msg = "发布成功"
  508. }
  509. // @Title 取消发布报告接口
  510. // @Description 取消发布报告
  511. // @Param request body models.PublishCancelReq true "type json string"
  512. // @Success 200 Ret=200 取消发布成功
  513. // @router /publish/cancel [post]
  514. func (this *EnglishReportController) PublishCancleReport() {
  515. br := new(models.BaseResponse).Init()
  516. defer func() {
  517. this.Data["json"] = br
  518. this.ServeJSON()
  519. }()
  520. var req models.PublishCancelReq
  521. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  522. if err != nil {
  523. br.Msg = "参数解析异常!"
  524. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  525. return
  526. }
  527. if req.ReportIds <= 0 {
  528. br.Msg = "参数错误"
  529. br.ErrMsg = "参数错误,报告id不可为空"
  530. return
  531. }
  532. err = models.PublishCancelEnglishReport(req.ReportIds)
  533. if err != nil {
  534. br.Msg = "取消发布失败"
  535. br.ErrMsg = "取消发布失败,Err:" + err.Error()
  536. return
  537. }
  538. // 更新es里的报告状态
  539. go func() {
  540. _ = services.UpdateEnglishReportEs(req.ReportIds, 1)
  541. }()
  542. br.Ret = 200
  543. br.Success = true
  544. br.Msg = "取消发布成功"
  545. }