report.go 33 KB

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