report_v2.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/report"
  6. "eta/eta_api/models/report_approve"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services"
  9. "eta/eta_api/services/alarm_msg"
  10. "eta/eta_api/utils"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. "html"
  13. "strconv"
  14. "time"
  15. )
  16. // TODO 报告的权限校验(跳过列表页,直接进入详情页,不管是普通报告还是章节报告)
  17. // ListReport
  18. // @Title 获取报告列表接口
  19. // @Description 获取报告列表
  20. // @Param PageSize query int true "每页数据条数"
  21. // @Param CurrentIndex query int true "当前页页码,从1开始"
  22. // @Param TimeType query string true "筛选的时间类别:publish_time(发布时间),modify_time(更新时间);approve_time(审批时间)"
  23. // @Param StartDate query string true "开始时间"
  24. // @Param EndDate query string true "结束时间"
  25. // @Param Frequency query string true "频度"
  26. // @Param ClassifyIdFirst query int true "一级分类id"
  27. // @Param ClassifyIdSecond query int true "二级分类id"
  28. // @Param ClassifyIdThird query int true "三级分类id"
  29. // @Param State query int true "状态"
  30. // @Param KeyWord query string true "搜索关键词"
  31. // @Param PublishSort query string true "desc:降序,asc 升序(预留)"
  32. // @Param FilterReportType query string true "筛选报告类型,1:公共研报,2:共享研报,3:我的研报"
  33. // @Success 200 {object} models.ReportListResp
  34. // @router /list [get]
  35. func (this *ReportController) ListReport() {
  36. br := new(models.BaseResponse).Init()
  37. defer func() {
  38. this.Data["json"] = br
  39. this.ServeJSON()
  40. }()
  41. pageSize, _ := this.GetInt("PageSize")
  42. currentIndex, _ := this.GetInt("CurrentIndex")
  43. timeType := this.GetString("TimeType")
  44. startDate := this.GetString("StartDate")
  45. endDate := this.GetString("EndDate")
  46. frequency := this.GetString("Frequency")
  47. classifyIdFirst, _ := this.GetInt("ClassifyIdFirst", 0)
  48. classifyIdSecond, _ := this.GetInt("ClassifyIdSecond", 0)
  49. classifyIdThird, _ := this.GetInt("ClassifyIdThird", 0)
  50. state, _ := this.GetInt("State")
  51. keyWord := this.GetString("KeyWord")
  52. msgIsSend, _ := this.GetInt("MsgIsSend")
  53. filterReportType, _ := this.GetInt("FilterReportType", 1)
  54. var startSize int
  55. if pageSize <= 0 {
  56. pageSize = utils.PageSize20
  57. }
  58. if currentIndex <= 0 {
  59. currentIndex = 1
  60. }
  61. startSize = utils.StartIndex(currentIndex, pageSize)
  62. if timeType == "" {
  63. timeType = "publish_time"
  64. }
  65. if timeType != "publish_time" && timeType != "modify_time" && timeType != "approve_time" {
  66. br.Msg = "请选择正确的时间"
  67. br.ErrMsg = "请选择正确的时间"
  68. return
  69. }
  70. var condition string
  71. var pars []interface{}
  72. if keyWord != "" {
  73. condition += ` AND (a.title LIKE ? OR a.admin_real_name LIKE ? ) `
  74. pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  75. }
  76. if startDate != "" {
  77. condition += ` AND a.` + timeType + ` >= ? `
  78. pars = append(pars, startDate)
  79. }
  80. if endDate != "" {
  81. condition += ` AND a.` + timeType + ` <= ? `
  82. pars = append(pars, endDate)
  83. }
  84. if frequency != "" {
  85. condition += ` AND a.frequency = ? `
  86. pars = append(pars, frequency)
  87. }
  88. if classifyIdFirst > 0 {
  89. condition += ` AND a.classify_id_first = ? `
  90. pars = append(pars, classifyIdFirst)
  91. }
  92. if classifyIdSecond > 0 {
  93. condition += ` AND a.classify_id_second = ? `
  94. pars = append(pars, classifyIdSecond)
  95. }
  96. if classifyIdThird > 0 {
  97. condition += ` AND a.classify_id_third = ? `
  98. pars = append(pars, classifyIdSecond)
  99. }
  100. if state > 0 {
  101. condition += ` AND a.state = ? `
  102. pars = append(pars, state)
  103. }
  104. // 消息是否已推送 1-未推送; 2-已推送
  105. if msgIsSend == 1 {
  106. condition += ` AND (a.msg_is_send = 0 OR a.ths_msg_is_send = 0) `
  107. }
  108. if msgIsSend == 2 {
  109. condition += ` AND a.msg_is_send = 1 AND a.ths_msg_is_send = 1 `
  110. }
  111. var err error
  112. var total int
  113. var list []*models.ReportList
  114. switch filterReportType {
  115. // 筛选报告类型,1:公共研报,2:共享研报,3:我的研报
  116. case 1:
  117. condition += ` AND a.is_public_publish = ? `
  118. // TODO 临时使用,提测上线前得打开注释
  119. //condition += ` AND a.state in (2,6) `
  120. pars = append(pars, 1)
  121. case 3:
  122. condition += ` AND a.admin_id = ? `
  123. pars = append(pars, this.SysUser.AdminId)
  124. case 2:
  125. condition += ` AND (a.admin_id = ? or b.admin_id = ?) `
  126. pars = append(pars, this.SysUser.AdminId, this.SysUser.AdminId)
  127. }
  128. // 共享报告需要连表查询,所以需要单独写
  129. if filterReportType == 2 {
  130. total, err = models.GetReportListCountByGrant(condition, pars)
  131. if err != nil {
  132. br.Msg = "获取失败"
  133. br.ErrMsg = "获取失败,Err:" + err.Error()
  134. return
  135. }
  136. list, err = models.GetReportListByGrant(condition, pars, startSize, pageSize)
  137. if err != nil {
  138. br.Msg = "获取失败"
  139. br.ErrMsg = "获取失败,Err:" + err.Error()
  140. return
  141. }
  142. } else {
  143. total, err = models.GetReportListCountV1(condition, pars)
  144. if err != nil {
  145. br.Msg = "获取失败"
  146. br.ErrMsg = "获取失败,Err:" + err.Error()
  147. return
  148. }
  149. list, err = models.GetReportListV1(condition, pars, startSize, pageSize)
  150. if err != nil {
  151. br.Msg = "获取失败"
  152. br.ErrMsg = "获取失败,Err:" + err.Error()
  153. return
  154. }
  155. }
  156. listLen := len(list)
  157. if listLen > 0 {
  158. pvMap := make(map[int]int)
  159. uvMap := make(map[int]int)
  160. reportIdArr := make([]string, 0)
  161. syncReportIdArr := make([]string, 0) // 同步过来的报告IDs
  162. oldAndNewReportIdMap := make(map[int]int) // 旧报告和新报告的id对应关系
  163. for i := 0; i < listLen; i++ {
  164. reportIdArr = append(reportIdArr, strconv.Itoa(list[i].Id))
  165. if list[i].OldReportId > 0 {
  166. syncReportIdArr = append(syncReportIdArr, strconv.Itoa(list[i].OldReportId))
  167. oldAndNewReportIdMap[list[i].OldReportId] = list[i].Id
  168. }
  169. pvMap[list[i].Id] = list[i].Pv
  170. uvMap[list[i].Id] = list[i].Uv
  171. }
  172. // 当下报告的pv,uv
  173. if len(reportIdArr) > 0 {
  174. pvList, e := models.GetReportPvUvByReportIdList(reportIdArr)
  175. if e != nil {
  176. br.Msg = "获取失败"
  177. br.ErrMsg = "获取同步报告对应的PV、UV失败, Err: " + e.Error()
  178. return
  179. }
  180. for _, v := range pvList {
  181. pv := pvMap[v.ReportId]
  182. uv := uvMap[v.ReportId]
  183. pvMap[v.ReportId] = v.PvTotal + pv
  184. uvMap[v.ReportId] = v.UvTotal + uv
  185. }
  186. }
  187. //reportIds := strings.Join(reportIdArr, ",")
  188. //syncReportIds := strings.Join(syncReportIdArr, ",")
  189. // 查询同步过来的报告对应的老报告PV+UV
  190. if len(syncReportIdArr) > 0 {
  191. puvList, e := models.GetPUVByResearchReportIds(syncReportIdArr)
  192. if e != nil {
  193. br.Msg = "获取失败"
  194. br.ErrMsg = "获取同步报告对应的PV、UV失败, Err: " + e.Error()
  195. return
  196. }
  197. puvLen := len(puvList)
  198. for i := 0; i < puvLen; i++ {
  199. newReportId, ok := oldAndNewReportIdMap[puvList[i].ResearchReportId]
  200. if ok {
  201. pv := pvMap[newReportId]
  202. uv := uvMap[newReportId]
  203. pvMap[newReportId] = puvList[i].Pv + pv
  204. uvMap[newReportId] = puvList[i].Uv + uv
  205. }
  206. }
  207. }
  208. // 晨周报音频列表
  209. videoList, err := models.GetReportChapterVideoListByReportIds(reportIdArr)
  210. if err != nil {
  211. br.Msg = "获取失败"
  212. br.ErrMsg = "获取报告音频文件失败,Err:" + err.Error()
  213. return
  214. }
  215. for i := 0; i < listLen; i++ {
  216. list[i].Content = html.UnescapeString(list[i].Content)
  217. list[i].ContentSub = html.UnescapeString(list[i].ContentSub)
  218. // 除周报外其余报告均可推送客群
  219. list[i].NeedThsMsg = 1
  220. //if list[i].HasChapter == 1 && list[i].ChapterType == utils.REPORT_TYPE_WEEK {
  221. // list[i].NeedThsMsg = 0
  222. //}
  223. chapterList := make([]*models.ReportChapterVideoList, 0)
  224. for ii := 0; ii < len(videoList); ii++ {
  225. if list[i].Id == videoList[ii].ReportId {
  226. chapterList = append(chapterList, videoList[ii])
  227. }
  228. }
  229. list[i].ChapterVideoList = chapterList
  230. list[i].Pv += pvMap[list[i].Id]
  231. list[i].Uv += uvMap[list[i].Id]
  232. }
  233. }
  234. for _, item := range list {
  235. /*key := fmt.Sprint(`crm:report:edit:`, item.Id)
  236. opUserId, _ := utils.Rc.RedisInt(key)
  237. //如果当前没有人操作,获取当前操作人是本人,那么编辑按钮可用
  238. if opUserId <= 0 || (opUserId == this.SysUser.AdminId) || item.ClassifyNameFirst == "周报" || item.ClassifyNameFirst == "晨报" {
  239. item.CanEdit = true
  240. } else {
  241. adminInfo, errAdmin := system.GetSysUserById(opUserId)
  242. if errAdmin != nil {
  243. br.Msg = "获取失败"
  244. br.ErrMsg = "获取失败,Err:" + errAdmin.Error()
  245. return
  246. }
  247. item.Editor = adminInfo.RealName
  248. }*/
  249. if item.HasChapter == 1 {
  250. item.CanEdit = true
  251. continue
  252. }
  253. markStatus, err := services.UpdateReportEditMark(item.Id, 0, this.SysUser.AdminId, 2, this.SysUser.RealName, this.Lang)
  254. if err != nil {
  255. br.Msg = "查询标记状态失败"
  256. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  257. return
  258. }
  259. if markStatus.Status == 0 {
  260. item.CanEdit = true
  261. } else {
  262. item.Editor = markStatus.Editor
  263. }
  264. }
  265. page := paging.GetPaging(currentIndex, pageSize, total)
  266. resp := new(models.ReportListResp)
  267. resp.Paging = page
  268. resp.List = list
  269. br.Ret = 200
  270. br.Success = true
  271. br.Msg = "获取成功"
  272. br.Data = resp
  273. }
  274. // Add
  275. // @Title 新增报告接口
  276. // @Description 新增报告(不区分报告类型)
  277. // @Param request body models.AddReq true "type json string"
  278. // @Success 200 {object} models.AddResp
  279. // @router /add [post]
  280. func (this *ReportController) Add() {
  281. br := new(models.BaseResponse).Init()
  282. defer func() {
  283. this.Data["json"] = br
  284. this.ServeJSON()
  285. }()
  286. sysUser := this.SysUser
  287. if sysUser == nil {
  288. br.Msg = "请登录"
  289. br.ErrMsg = "请登录,SysUser Is Empty"
  290. br.Ret = 408
  291. return
  292. }
  293. var req models.AddReq
  294. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  295. if err != nil {
  296. br.Msg = "参数解析异常!"
  297. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  298. return
  299. }
  300. if req.Title == `` {
  301. br.Msg = "标题不能为空"
  302. br.ErrMsg = "标题不能为空"
  303. br.IsSendEmail = false
  304. return
  305. }
  306. if req.ClassifyIdFirst <= 0 {
  307. br.Msg = "分类必填"
  308. br.ErrMsg = "分类必填"
  309. br.IsSendEmail = false
  310. return
  311. }
  312. var contentSub string
  313. if req.Content != "" {
  314. e := utils.ContentXssCheck(req.Content)
  315. if e != nil {
  316. br.Msg = "存在非法标签"
  317. br.ErrMsg = "存在非法标签, Err: " + e.Error()
  318. return
  319. }
  320. content, e := services.FilterReportContentBr(req.Content)
  321. if e != nil {
  322. br.Msg = "内容去除前后空格失败"
  323. br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
  324. return
  325. }
  326. req.Content = content
  327. contentSub, err = services.GetReportContentSub(req.Content)
  328. if err != nil {
  329. go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+err.Error(), 3)
  330. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  331. }
  332. }
  333. // 报告期数
  334. maxStage, err := models.GetReportStage(req.ClassifyIdFirst, req.ClassifyIdSecond, req.ClassifyIdThird)
  335. if err != nil {
  336. br.Msg = "期数获取失败!"
  337. br.ErrMsg = "期数获取失败,Err:" + err.Error()
  338. return
  339. }
  340. // 根据审批开关及审批流判断当前报告状态
  341. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeChinese, req.ClassifyIdFirst, req.ClassifyIdSecond, req.ClassifyIdThird, models.ReportOperateAdd)
  342. if e != nil {
  343. br.Msg = "操作失败"
  344. br.ErrMsg = "校验报告当前状态失败, Err: " + e.Error()
  345. return
  346. }
  347. // 协作方式,1:个人,2:多人协作。默认:1
  348. if req.CollaborateType == 0 {
  349. req.CollaborateType = 1
  350. }
  351. // 报告布局,1:常规布局,2:智能布局。默认:1
  352. if req.ReportLayout == 0 {
  353. req.ReportLayout = 1
  354. }
  355. // 是否公开发布,1:是,2:否
  356. if req.IsPublicPublish == 0 {
  357. req.IsPublicPublish = 1
  358. }
  359. item := new(models.Report)
  360. item.AddType = req.AddType
  361. item.ReportVersion = 2
  362. item.ClassifyIdFirst = req.ClassifyIdFirst
  363. item.ClassifyNameFirst = req.ClassifyNameFirst
  364. item.ClassifyIdSecond = req.ClassifyIdSecond
  365. item.ClassifyNameSecond = req.ClassifyNameSecond
  366. item.Title = req.Title
  367. item.Abstract = req.Abstract
  368. item.Author = req.Author
  369. item.Frequency = req.Frequency
  370. item.State = state
  371. item.Content = html.EscapeString(req.Content)
  372. item.Stage = maxStage + 1
  373. item.ContentSub = html.EscapeString(contentSub)
  374. item.CreateTime = req.CreateTime
  375. item.ModifyTime = time.Now()
  376. item.ReportVersion = req.ReportVersion
  377. item.AdminId = sysUser.AdminId
  378. item.AdminRealName = sysUser.RealName
  379. item.ClassifyIdThird = req.ClassifyIdThird
  380. item.ClassifyNameThird = req.ClassifyNameThird
  381. // 产品要求,如果是多人协作,那么就是章节类型的报告
  382. if req.CollaborateType == 2 {
  383. item.HasChapter = 1
  384. item.ChapterType = ""
  385. }
  386. item.LastModifyAdminId = sysUser.AdminId
  387. item.LastModifyAdminName = sysUser.RealName
  388. item.ContentModifyTime = time.Now()
  389. item.NeedSplice = 0
  390. item.ContentStruct = html.EscapeString(req.ContentStruct)
  391. item.HeadImg = req.HeadImg
  392. item.EndImg = req.EndImg
  393. item.CanvasColor = req.CanvasColor
  394. item.HeadResourceId = req.HeadResourceId
  395. item.EndResourceId = req.EndResourceId
  396. item.CollaborateType = req.CollaborateType
  397. item.ReportLayout = req.ReportLayout
  398. item.IsPublicPublish = req.IsPublicPublish
  399. item.ReportCreateTime = time.Now()
  400. err, errMsg := services.AddReportAndChapter(item, req.InheritReportId, req.GrantAdminIdList)
  401. if err != nil {
  402. br.Msg = "保存失败"
  403. if errMsg != "" {
  404. br.Msg = errMsg
  405. }
  406. br.ErrMsg = "保存失败,Err:" + err.Error()
  407. return
  408. }
  409. recordItem := &models.ReportStateRecord{
  410. ReportId: item.Id,
  411. ReportType: 1,
  412. State: 1,
  413. AdminId: this.SysUser.AdminId,
  414. AdminName: this.SysUser.AdminName,
  415. CreateTime: time.Now(),
  416. }
  417. go func() {
  418. _, _ = models.AddReportStateRecord(recordItem)
  419. }()
  420. resp := new(models.AddResp)
  421. resp.ReportId = int64(item.Id)
  422. resp.ReportCode = item.ReportCode
  423. br.Ret = 200
  424. br.Success = true
  425. br.Msg = "保存成功"
  426. br.Data = resp
  427. }
  428. // Edit
  429. // @Title 编辑报告基础信息接口
  430. // @Description 编辑报告基础信息(不区分报告类型)
  431. // @Param request body models.EditReq true "type json string"
  432. // @Success 200 {object} models.EditResp
  433. // @router /edit [post]
  434. func (this *ReportController) Edit() {
  435. br := new(models.BaseResponse).Init()
  436. defer func() {
  437. this.Data["json"] = br
  438. this.ServeJSON()
  439. }()
  440. sysUser := this.SysUser
  441. if sysUser == nil {
  442. br.Msg = "请登录"
  443. br.ErrMsg = "请登录,SysUser Is Empty"
  444. br.Ret = 408
  445. return
  446. }
  447. var req models.EditReq
  448. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  449. if err != nil {
  450. br.Msg = "参数解析异常!"
  451. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  452. return
  453. }
  454. //if req.Content == "" {
  455. // br.Msg = "报告内容不能为空"
  456. // return
  457. //}
  458. //更新标记key
  459. markStatus, err := services.UpdateReportEditMark(int(req.ReportId), 0, sysUser.AdminId, 1, sysUser.RealName, this.Lang)
  460. if err != nil {
  461. br.Msg = err.Error()
  462. return
  463. }
  464. if markStatus.Status == 1 {
  465. br.Msg = markStatus.Msg
  466. //br.Ret = 202 //202 服务器已接受请求,但尚未处理。
  467. return
  468. }
  469. reportInfo, e := models.GetReportByReportId(int(req.ReportId))
  470. if e != nil {
  471. if e.Error() == utils.ErrNoRow() {
  472. br.Msg = "报告已被删除, 请刷新页面"
  473. return
  474. }
  475. br.Msg = "操作失败"
  476. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  477. return
  478. }
  479. if reportInfo.State == models.ReportStatePublished || reportInfo.State == models.ReportStatePass {
  480. br.Msg = "该报告已发布,不允许编辑"
  481. br.ErrMsg = "该报告已发布,不允许编辑"
  482. return
  483. }
  484. // 编辑报告信息
  485. err, errMsg := services.EditReport(reportInfo, req, sysUser)
  486. if err != nil {
  487. br.Msg = "保存失败"
  488. if errMsg != "" {
  489. br.Msg = errMsg
  490. }
  491. br.ErrMsg = "保存失败,Err:" + err.Error()
  492. return
  493. }
  494. reportCode := utils.MD5(strconv.Itoa(int(req.ReportId)))
  495. resp := new(models.EditResp)
  496. resp.ReportId = req.ReportId
  497. resp.ReportCode = reportCode
  498. br.Ret = 200
  499. br.Success = true
  500. br.Msg = "保存成功"
  501. br.Data = resp
  502. }
  503. // Detail
  504. // @Title 获取报告详情接口
  505. // @Description 获取报告详情
  506. // @Param request body models.ReportDetailReq true "type json string"
  507. // @Success 200 {object} models.Report
  508. // @router /detail [get]
  509. func (this *ReportController) Detail() {
  510. br := new(models.BaseResponse).Init()
  511. defer func() {
  512. this.Data["json"] = br
  513. this.ServeJSON()
  514. }()
  515. /*var req models.ReportDetailReq
  516. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  517. if err != nil {
  518. br.Msg = "参数解析异常!"
  519. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  520. return
  521. }
  522. if req.ReportId <= 0 {
  523. br.Msg = "参数错误"
  524. return
  525. }*/
  526. reportId, err := this.GetInt("ReportId")
  527. if err != nil {
  528. br.Msg = "获取参数失败!"
  529. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  530. return
  531. }
  532. if reportId <= 0 {
  533. br.Msg = "参数错误"
  534. return
  535. }
  536. item, err := models.GetReportById(reportId)
  537. if err != nil {
  538. if err.Error() == utils.ErrNoRow() {
  539. br.Msg = "报告已被删除"
  540. return
  541. }
  542. br.Msg = "获取失败"
  543. br.ErrMsg = "获取失败,Err:" + err.Error()
  544. return
  545. }
  546. chapterList := make([]*models.ReportChapter, 0)
  547. if item.HasChapter == 1 {
  548. // 获取章节内容
  549. tmpChapterList, err := models.GetPublishedChapterListByReportId(item.Id)
  550. if err != nil {
  551. br.Msg = "获取失败"
  552. br.ErrMsg = "获取晨/周报章节列表失败, Err: " + err.Error()
  553. return
  554. }
  555. // TODO 更新规则
  556. if len(tmpChapterList) > 0 {
  557. // 获取更新规则
  558. researchType := tmpChapterList[0].ReportType
  559. chapterTypeList, tmpErr := models.GetAllReportChapterTypeListByResearchType(researchType)
  560. if tmpErr != nil {
  561. br.Msg = "获取更新规则失败"
  562. br.ErrMsg = "获取更新规则失败, Err: " + tmpErr.Error()
  563. return
  564. }
  565. // 调整章节更新
  566. nowTime := time.Now().Local()
  567. for _, item := range tmpChapterList {
  568. stop := false
  569. for _, rule := range chapterTypeList {
  570. if rule.ReportChapterTypeId == item.TypeId {
  571. //fmt.Println("rule.Enabled :", rule.Enabled, ";name=", rule.ReportChapterTypeName, "item.IsEdit:", item.IsEdit, "rule.IsSet:", rule.IsSet)
  572. // 如果被永久暂停更新了
  573. if rule.Enabled == 0 && item.IsEdit == 0 { //该章节已被永久禁用,同时未被操作过
  574. stop = true
  575. } else if rule.PauseStartTime != "" && rule.PauseEndTime != "" && rule.PauseStartTime != utils.EmptyDateStr && rule.PauseEndTime != utils.EmptyDateStr {
  576. startTime, timeErr := time.ParseInLocation(utils.FormatDate, rule.PauseStartTime, time.Local)
  577. if timeErr != nil {
  578. br.Msg = "获取更新规则失败"
  579. br.ErrMsg = "更新规则时间转换失败4001, Err: " + timeErr.Error()
  580. return
  581. }
  582. endTime, timeErr := time.ParseInLocation(utils.FormatDate, rule.PauseEndTime, time.Local)
  583. if timeErr != nil {
  584. br.Msg = "获取更新规则失败"
  585. br.ErrMsg = "更新规则时间转换失败4002, Err: " + timeErr.Error()
  586. return
  587. }
  588. // 暂停更新
  589. if nowTime.After(startTime) && nowTime.Before(endTime.AddDate(0, 0, 1)) {
  590. stop = true
  591. }
  592. break
  593. }
  594. }
  595. }
  596. if !stop {
  597. item.Content = html.UnescapeString(item.Content)
  598. item.ContentSub = html.UnescapeString(item.ContentSub)
  599. chapterList = append(chapterList, item)
  600. }
  601. }
  602. }
  603. //item.Abstract = item.Title
  604. }
  605. item.Content = html.UnescapeString(item.Content)
  606. item.ContentSub = html.UnescapeString(item.ContentSub)
  607. item.ContentStruct = html.UnescapeString(item.ContentStruct)
  608. resp := &models.ReportDetailView{
  609. ReportDetail: item,
  610. ChapterList: chapterList,
  611. }
  612. br.Ret = 200
  613. br.Success = true
  614. br.Msg = "获取成功"
  615. br.Data = resp
  616. }
  617. // SaveReportContent
  618. // @Title 保存草稿
  619. // @Description 保存草稿
  620. // @Param request body models.SaveReportContent true "type json string"
  621. // @Success 200 {object} models.ReportAuthorResp
  622. // @router /saveReportContent [post]
  623. func (this *ReportController) SaveReportContent() {
  624. br := new(models.BaseResponse).Init()
  625. br.IsSendEmail = false
  626. defer func() {
  627. this.Data["json"] = br
  628. this.ServeJSON()
  629. }()
  630. sysUser := this.SysUser
  631. if sysUser == nil {
  632. br.Msg = "请登录"
  633. br.ErrMsg = "请登录,SysUser Is Empty"
  634. br.Ret = 408
  635. return
  636. }
  637. var req models.SaveReportContent
  638. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  639. if err != nil {
  640. br.Msg = "参数解析异常!"
  641. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  642. return
  643. }
  644. reportId := req.ReportId
  645. noChangeFlag := req.NoChange
  646. if reportId <= 0 {
  647. resp := new(models.SaveReportContentResp)
  648. resp.ReportId = reportId
  649. br.Ret = 200
  650. br.Success = true
  651. br.Msg = "保存成功"
  652. br.Data = resp
  653. return
  654. }
  655. // 获取报告详情
  656. reportInfo, _ := models.GetReportByReportId(int(req.ReportId))
  657. if reportInfo != nil && reportInfo.State == 2 {
  658. br.Msg = "该报告已发布,不允许编辑"
  659. br.ErrMsg = "该报告已发布,不允许编辑"
  660. return
  661. }
  662. // 标记更新中
  663. {
  664. markStatus, err := services.UpdateReportEditMark(int(req.ReportId), 0, sysUser.AdminId, 1, sysUser.RealName, this.Lang)
  665. if err != nil {
  666. br.Msg = err.Error()
  667. return
  668. }
  669. if markStatus.Status == 1 {
  670. br.Msg = markStatus.Msg
  671. return
  672. }
  673. }
  674. // 内容有过修改的话,那么逻辑处理
  675. if noChangeFlag != 1 {
  676. content := req.Content
  677. if content == "" {
  678. content = this.GetString("Content")
  679. }
  680. if content != "" {
  681. e := utils.ContentXssCheck(req.Content)
  682. if e != nil {
  683. br.Msg = "存在非法标签"
  684. br.ErrMsg = "存在非法标签, Err: " + e.Error()
  685. return
  686. }
  687. contentClean, e := services.FilterReportContentBr(req.Content)
  688. if e != nil {
  689. br.Msg = "内容去除前后空格失败"
  690. br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
  691. return
  692. }
  693. content = contentClean
  694. contentSub, err := services.GetReportContentSub(content)
  695. if err != nil {
  696. go alarm_msg.SendAlarmMsg("解析 ContentSub 失败,Err:"+err.Error(), 3)
  697. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  698. }
  699. reportInfo.Content = html.EscapeString(content)
  700. reportInfo.ContentSub = html.EscapeString(contentSub)
  701. reportInfo.ContentStruct = html.EscapeString(req.ContentStruct)
  702. reportInfo.HeadImg = req.HeadImg
  703. reportInfo.EndImg = req.EndImg
  704. reportInfo.CanvasColor = req.CanvasColor
  705. reportInfo.HeadResourceId = req.HeadResourceId
  706. reportInfo.EndResourceId = req.EndResourceId
  707. reportInfo.ModifyTime = time.Now()
  708. reportInfo.ContentModifyTime = time.Now()
  709. updateCols := []string{"Content", "ContentSub", "ContentStruct", "HeadImg", "EndImg", "CanvasColor", "HeadResourceId", "EndResourceId", "ModifyTime", "ContentModifyTime"}
  710. err = reportInfo.UpdateReport(updateCols)
  711. if err != nil {
  712. br.Msg = "保存失败"
  713. br.ErrMsg = "保存失败,Err:" + err.Error()
  714. return
  715. }
  716. go models.AddReportSaveLog(reportId, this.SysUser.AdminId, reportInfo.Content, reportInfo.ContentSub, reportInfo.ContentStruct, reportInfo.CanvasColor, this.SysUser.AdminName, reportInfo.HeadResourceId, reportInfo.EndResourceId)
  717. }
  718. }
  719. resp := new(models.SaveReportContentResp)
  720. resp.ReportId = reportId
  721. br.Ret = 200
  722. br.Success = true
  723. br.Msg = "保存成功"
  724. br.Data = resp
  725. }
  726. // AuthorizedListReport
  727. // @Title 获取有权限的报告列表接口
  728. // @Description 获取有权限的报告列表接口
  729. // @Param PageSize query int true "每页数据条数"
  730. // @Param CurrentIndex query int true "当前页页码,从1开始"
  731. // @Param Keyword query string true "搜索关键词"
  732. // @Param ClassifyIdFirst query int true "一级分类id"
  733. // @Param ClassifyIdSecond query int true "二级分类id"
  734. // @Param ClassifyIdThird query int true "三级分类id"
  735. // @Success 200 {object} models.ReportListResp
  736. // @router /list/authorized [get]
  737. func (this *ReportController) AuthorizedListReport() {
  738. br := new(models.BaseResponse).Init()
  739. defer func() {
  740. this.Data["json"] = br
  741. this.ServeJSON()
  742. }()
  743. pageSize, _ := this.GetInt("PageSize")
  744. currentIndex, _ := this.GetInt("CurrentIndex")
  745. keyword := this.GetString("Keyword")
  746. classifyIdFirst, _ := this.GetInt("ClassifyIdFirst", 0)
  747. classifyIdSecond, _ := this.GetInt("ClassifyIdSecond", 0)
  748. classifyIdThird, _ := this.GetInt("ClassifyIdThird", 0)
  749. var startSize int
  750. if pageSize <= 0 {
  751. pageSize = utils.PageSize20
  752. }
  753. if currentIndex <= 0 {
  754. currentIndex = 1
  755. }
  756. startSize = utils.StartIndex(currentIndex, pageSize)
  757. var list []*models.ReportList
  758. // 没有输入信息,那就不展示
  759. if keyword == `` && classifyIdFirst <= 0 {
  760. page := paging.GetPaging(currentIndex, pageSize, 0)
  761. resp := new(models.ReportListResp)
  762. resp.Paging = page
  763. resp.List = list
  764. br.Ret = 200
  765. br.Success = true
  766. br.Msg = "获取成功"
  767. br.Data = resp
  768. return
  769. }
  770. // 当前用户有权限的报告id列表
  771. grantReportIdList := make([]int, 0)
  772. {
  773. obj := report.ReportGrant{}
  774. grantList, err := obj.GetGrantListByAdminId(this.SysUser.AdminId)
  775. if err != nil {
  776. br.Msg = "获取失败"
  777. br.ErrMsg = "获取授权报告id失败,Err:" + err.Error()
  778. return
  779. }
  780. for _, v := range grantList {
  781. grantReportIdList = append(grantReportIdList, v.ReportId)
  782. }
  783. }
  784. var condition string
  785. var pars []interface{}
  786. if classifyIdFirst > 0 {
  787. condition += ` AND a.classify_id_first = ? `
  788. pars = append(pars, classifyIdFirst)
  789. }
  790. if classifyIdSecond > 0 {
  791. condition += ` AND a.classify_id_second = ? `
  792. pars = append(pars, classifyIdSecond)
  793. }
  794. if classifyIdThird > 0 {
  795. condition += ` AND a.classify_id_third = ? `
  796. pars = append(pars, classifyIdSecond)
  797. }
  798. if keyword != `` {
  799. condition += ` AND a.title LIKE ? `
  800. pars = utils.GetLikeKeywordPars(pars, keyword, 1)
  801. }
  802. var err error
  803. var total int
  804. orCondition := `AND ( (a.is_public_publish = ? AND a.state in (2,6)) or a.admin_id = ? `
  805. pars = append(pars, 1, this.SysUser.AdminId)
  806. // 当前用户有权限的报告id列表
  807. num := len(grantReportIdList)
  808. if num > 0 {
  809. orCondition += ` OR a.id in (` + utils.GetOrmInReplace(num) + `)`
  810. pars = append(pars, grantReportIdList)
  811. }
  812. orCondition += ` ) `
  813. condition += orCondition
  814. total, err = models.GetReportListCountByAuthorized(condition, pars)
  815. if err != nil {
  816. br.Msg = "获取失败"
  817. br.ErrMsg = "获取失败,Err:" + err.Error()
  818. return
  819. }
  820. list, err = models.GetReportListByAuthorized(condition, pars, startSize, pageSize)
  821. if err != nil {
  822. br.Msg = "获取失败"
  823. br.ErrMsg = "获取失败,Err:" + err.Error()
  824. return
  825. }
  826. //for _, item := range list {
  827. // if item.HasChapter == 1 {
  828. // item.CanEdit = true
  829. // } else {
  830. // markStatus, err := services.UpdateReportEditMark(item.Id, this.SysUser.AdminId, 2, this.SysUser.RealName, this.Lang)
  831. // if err != nil {
  832. // br.Msg = "查询标记状态失败"
  833. // br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  834. // return
  835. // }
  836. // if markStatus.Status == 0 {
  837. // item.CanEdit = true
  838. // } else {
  839. // item.Editor = markStatus.Editor
  840. // }
  841. // }
  842. //}
  843. page := paging.GetPaging(currentIndex, pageSize, total)
  844. resp := new(models.ReportListResp)
  845. resp.Paging = page
  846. resp.List = list
  847. br.Ret = 200
  848. br.Success = true
  849. br.Msg = "获取成功"
  850. br.Data = resp
  851. }
  852. // BaseDetail
  853. // @Title 获取报告基础信息详情接口
  854. // @Description 获取报告基础信息详情接口
  855. // @Param request body models.ReportDetailReq true "type json string"
  856. // @Success 200 {object} models.Report
  857. // @router /detail/base [get]
  858. func (this *ReportController) BaseDetail() {
  859. br := new(models.BaseResponse).Init()
  860. defer func() {
  861. this.Data["json"] = br
  862. this.ServeJSON()
  863. }()
  864. /*var req models.ReportDetailReq
  865. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  866. if err != nil {
  867. br.Msg = "参数解析异常!"
  868. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  869. return
  870. }
  871. if req.ReportId <= 0 {
  872. br.Msg = "参数错误"
  873. return
  874. }*/
  875. reportId, err := this.GetInt("ReportId")
  876. if err != nil {
  877. br.Msg = "获取参数失败!"
  878. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  879. return
  880. }
  881. if reportId <= 0 {
  882. br.Msg = "参数错误"
  883. return
  884. }
  885. reportInfo, err := models.GetReportById(reportId)
  886. if err != nil {
  887. if err.Error() == utils.ErrNoRow() {
  888. br.Msg = "报告已被删除"
  889. return
  890. }
  891. br.Msg = "获取失败"
  892. br.ErrMsg = "获取失败,Err:" + err.Error()
  893. return
  894. }
  895. // 基础信息就不获取章节信息了
  896. chapterList := make([]*models.ReportChapter, 0)
  897. reportInfo.Content = html.UnescapeString(reportInfo.Content)
  898. reportInfo.ContentSub = html.UnescapeString(reportInfo.ContentSub)
  899. grandAdminList := make([]models.ReportDetailViewAdmin, 0)
  900. permissionList := make([]models.ReportDetailViewPermission, 0)
  901. // 处理报告授权用户列表
  902. {
  903. obj := report.ReportGrant{}
  904. grantList, tmpErr := obj.GetGrantListById(reportId)
  905. if tmpErr != nil {
  906. br.Msg = "获取章节id授权用户列表失败"
  907. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  908. return
  909. }
  910. if len(grantList) > 0 {
  911. grandAdminIdList := make([]int, 0)
  912. for _, v := range grantList {
  913. grandAdminIdList = append(grandAdminIdList, v.AdminId)
  914. }
  915. adminList, tmpErr := system.GetAdminListByIdList(grandAdminIdList)
  916. if tmpErr != nil {
  917. br.Msg = "获取章节id授权用户列表失败"
  918. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  919. return
  920. }
  921. for _, v := range adminList {
  922. grandAdminList = append(grandAdminList, models.ReportDetailViewAdmin{
  923. AdminId: v.AdminId,
  924. AdminName: v.RealName,
  925. })
  926. }
  927. }
  928. }
  929. // 处理章节id关联品种id列表
  930. {
  931. minClassifyId := reportInfo.ClassifyIdThird
  932. if minClassifyId <= 0 {
  933. minClassifyId = reportInfo.ClassifyIdSecond
  934. }
  935. if minClassifyId <= 0 {
  936. minClassifyId = reportInfo.ClassifyIdFirst
  937. }
  938. if minClassifyId <= 0 {
  939. br.Msg = "分类异常"
  940. br.ErrMsg = "分类异常"
  941. return
  942. }
  943. // 获取分类关联的品种id
  944. classifyPermissionList, tmpErr := models.GetPermission(minClassifyId)
  945. if tmpErr != nil {
  946. br.Msg = "获取分类信息失败"
  947. br.ErrMsg = "获取失败,Err:" + tmpErr.Error()
  948. return
  949. }
  950. if len(classifyPermissionList) > 0 {
  951. permissionIdList := make([]int, 0)
  952. for _, v := range classifyPermissionList {
  953. permissionIdList = append(permissionIdList, v.ChartPermissionId)
  954. }
  955. adminList, tmpErr := models.GetChartPermissionByIdList(permissionIdList)
  956. if tmpErr != nil {
  957. br.Msg = "获取章节id授权用户列表失败"
  958. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  959. return
  960. }
  961. for _, v := range adminList {
  962. permissionList = append(permissionList, models.ReportDetailViewPermission{
  963. PermissionId: v.ChartPermissionId,
  964. PermissionName: v.ChartPermissionName,
  965. })
  966. }
  967. }
  968. }
  969. resp := &models.ReportDetailView{
  970. ReportDetail: reportInfo,
  971. ChapterList: chapterList,
  972. GrandAdminList: grandAdminList,
  973. PermissionList: permissionList,
  974. }
  975. br.Ret = 200
  976. br.Success = true
  977. br.Msg = "获取成功"
  978. br.Data = resp
  979. }
  980. // TODO 报告发布后,图片和pdf的生成