ppt_report.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/system"
  6. "eta_gn/eta_api/services"
  7. "eta_gn/eta_api/utils"
  8. "fmt"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // ReportClassify
  15. // @Title 获取ppt报告分类
  16. // @Description 获取ppt报告分类
  17. // @Param Source query int false "来源:1-我的;2-协作;3-公共"
  18. // @Success 200 {object} models.PptReportClassifyItem
  19. // @router /report/classify [get]
  20. func (this *PptV2Controller) ReportClassify() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. if br.ErrMsg == "" {
  24. br.IsSendEmail = false
  25. }
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. sysUser := this.SysUser
  30. if sysUser == nil {
  31. br.Msg = "请登录"
  32. br.ErrMsg = "请登录,SysUser Is Empty"
  33. return
  34. }
  35. source, _ := this.GetInt("Source", 1)
  36. if source < 1 || source > 3 {
  37. source = 1
  38. }
  39. // 获取PPT, source:1-我的;2-协作;3-公共
  40. pptList := make([]*models.PptV2, 0)
  41. {
  42. cond := ``
  43. pars := make([]interface{}, 0)
  44. switch source {
  45. case 1:
  46. cond += ` AND admin_id = ?`
  47. pars = append(pars, sysUser.AdminId)
  48. case 2:
  49. cond += ` AND collaborate_type = ? AND (admin_id = ? OR FIND_IN_SET(?, collaborate_users)) `
  50. pars = append(pars, utils.ReportWriteTypeGroup, sysUser.AdminId)
  51. case 3:
  52. cond += ` AND report_source = ?`
  53. pars = append(pars, utils.ReportSourceOuter)
  54. }
  55. pptOb := new(models.PptV2)
  56. list, e := pptOb.GetItemsByCondition(cond, pars, models.PptReportQueryFields, "modify_time DESC")
  57. if e != nil {
  58. br.Msg = "获取失败"
  59. br.ErrMsg = fmt.Sprintf("获取PPT失败, %v", e)
  60. return
  61. }
  62. pptList = list
  63. }
  64. classifyPpt := make(map[int][]*models.PptReportItem)
  65. for _, v := range pptList {
  66. // 当前编辑人
  67. t := v.Format2ReportItem(v)
  68. editor, e := services.UpdatePptEditing(v.PptId, 0, sysUser.AdminId, sysUser.RealName, false)
  69. if e != nil {
  70. br.Msg = "获取失败"
  71. br.ErrMsg = fmt.Sprintf("获取PPT编辑状态失败, err: %s", e.Error())
  72. return
  73. }
  74. t.Editor = editor
  75. // 权限
  76. if source == 1 || source == 2 {
  77. t.HasAuth = true
  78. } else {
  79. if v.AdminId == sysUser.AdminId {
  80. t.HasAuth = true
  81. }
  82. if t.HasAuth == false && v.CollaborateUsers != "" {
  83. authorArr := strings.Split(v.CollaborateUsers, ",")
  84. strId := strconv.Itoa(sysUser.AdminId)
  85. if utils.InArrayByStr(authorArr, strId) {
  86. t.HasAuth = true
  87. }
  88. }
  89. }
  90. if classifyPpt[v.ClassifyId] == nil {
  91. classifyPpt[v.ClassifyId] = make([]*models.PptReportItem, 0)
  92. }
  93. classifyPpt[v.ClassifyId] = append(classifyPpt[v.ClassifyId], t)
  94. }
  95. resp := make([]*models.PptReportClassifyItem, 0)
  96. // 获取分类
  97. classifies := make([]*models.Classify, 0)
  98. {
  99. // 获取所有分类
  100. ob := new(models.Classify)
  101. cond := ` AND enabled = ?`
  102. pars := make([]interface{}, 0)
  103. pars = append(pars, 1, utils.ReportTypePPT)
  104. list, e := ob.GetItemsByCondition(cond, pars, []string{}, "sort ASC, create_time ASC")
  105. if e != nil {
  106. br.Msg = "获取失败"
  107. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  108. return
  109. }
  110. // 上级中的分类类型可能与最下层的不一致,但是要把上级也一起取出来, 所以这里要过滤一遍
  111. classifyIds := make([]int, 0)
  112. for _, v := range list {
  113. if v.ClassifyType != utils.ReportTypePPT {
  114. continue
  115. }
  116. // 没有PPT的分类也都过滤掉
  117. //if len(classifyPpt[v.Id]) == 0 {
  118. // continue
  119. //}
  120. // 根据LevelPath去处理
  121. pathArr := strings.Split(v.LevelPath, ",")
  122. for _, p := range pathArr {
  123. id, _ := strconv.Atoi(p)
  124. if id <= 0 {
  125. continue
  126. }
  127. if utils.InArrayByInt(classifyIds, id) {
  128. continue
  129. }
  130. classifyIds = append(classifyIds, id)
  131. }
  132. }
  133. // 过滤掉不在ID中的
  134. for _, v := range list {
  135. if !utils.InArrayByInt(classifyIds, v.Id) {
  136. continue
  137. }
  138. classifies = append(classifies, v)
  139. }
  140. }
  141. resp = services.GetPptReportClassifyTreeRecursive(classifies, 0, classifyPpt)
  142. br.Data = resp
  143. br.Ret = 200
  144. br.Success = true
  145. br.Msg = "获取成功"
  146. }
  147. // ReportList
  148. // @Title 获取ppt报告列表-分页
  149. // @Description 获取ppt报告列表-分页
  150. // @Param Source query int false "来源:1-我的;2-协作;3-公共"
  151. // @Param ClassifyId query int false "分类ID"
  152. // @Param Keyword query string false "搜索关键词"
  153. // @Success 200 {object} models.PptPageReportResp
  154. // @router /report/list [get]
  155. func (this *PptV2Controller) ReportList() {
  156. br := new(models.BaseResponse).Init()
  157. defer func() {
  158. if br.ErrMsg == "" {
  159. br.IsSendEmail = false
  160. }
  161. this.Data["json"] = br
  162. this.ServeJSON()
  163. }()
  164. sysUser := this.SysUser
  165. if sysUser == nil {
  166. br.Msg = "请登录"
  167. br.ErrMsg = "请登录,SysUser Is Empty"
  168. return
  169. }
  170. pageSize, _ := this.GetInt("PageSize")
  171. currentIndex, _ := this.GetInt("CurrentIndex")
  172. source, _ := this.GetInt("Source", 1)
  173. if source < 1 || source > 3 {
  174. source = 1
  175. }
  176. classifyId, _ := this.GetInt("ClassifyId", 0)
  177. keyword := this.GetString("Keyword")
  178. var startSize int
  179. if pageSize <= 0 {
  180. pageSize = utils.PageSize20
  181. }
  182. if currentIndex <= 0 {
  183. currentIndex = 1
  184. }
  185. startSize = utils.StartIndex(currentIndex, pageSize)
  186. resp := new(models.PptPageReportResp)
  187. resp.List = make([]*models.PptReportItem, 0)
  188. // 获取PPT, source:1-我的;2-协作;3-公共
  189. pptList := make([]*models.PptV2, 0)
  190. cond := ``
  191. pars := make([]interface{}, 0)
  192. {
  193. switch source {
  194. case 1:
  195. cond += ` AND admin_id = ?`
  196. pars = append(pars, sysUser.AdminId)
  197. case 2:
  198. cond += ` AND collaborate_type = ? AND (admin_id = ? OR FIND_IN_SET(?, collaborate_users)) `
  199. pars = append(pars, utils.ReportWriteTypeGroup, sysUser.AdminId, sysUser.AdminId)
  200. case 3:
  201. cond += ` AND report_source = ?`
  202. pars = append(pars, utils.ReportSourceOuter)
  203. }
  204. if classifyId > 0 {
  205. // 查询分类及子集
  206. classifyOb := new(models.Classify)
  207. childCond := ` AND FIND_IN_SET(?, level_path)`
  208. childPars := make([]interface{}, 0)
  209. childPars = append(childPars, classifyId)
  210. children, e := classifyOb.GetItemsByCondition(childCond, childPars, []string{"id"}, "")
  211. if e != nil {
  212. br.Msg = "获取失败"
  213. br.ErrMsg = fmt.Sprintf("获取分类及子分类失败, %v", e)
  214. return
  215. }
  216. var childIds []int
  217. for _, v := range children {
  218. childIds = append(childIds, v.Id)
  219. }
  220. if len(childIds) == 0 {
  221. page := paging.GetPaging(currentIndex, pageSize, 0)
  222. resp.Paging = page
  223. br.Data = resp
  224. br.Ret = 200
  225. br.Success = true
  226. br.Msg = "获取成功"
  227. return
  228. }
  229. cond += ` AND classify_id IN (?)`
  230. pars = append(pars, childIds)
  231. }
  232. keyword = strings.TrimSpace(keyword)
  233. if keyword != "" {
  234. cond += ` AND title LIKE ?`
  235. pars = append(pars, fmt.Sprint("%", keyword, "%"))
  236. }
  237. }
  238. pptOb := new(models.PptV2)
  239. total, e := pptOb.GetCountByCondition(cond, pars)
  240. if e != nil {
  241. br.Msg = "获取失败"
  242. br.ErrMsg = fmt.Sprintf("获取PPT总数失败, %v", e)
  243. return
  244. }
  245. list, e := pptOb.GetPageItemsByCondition(cond, pars, models.PptReportQueryFields, "modify_time DESC", startSize, pageSize)
  246. if e != nil {
  247. br.Msg = "获取失败"
  248. br.ErrMsg = fmt.Sprintf("获取PPT失败, %v", e)
  249. return
  250. }
  251. pptList = list
  252. for _, v := range pptList {
  253. // 当前编辑人
  254. t := v.Format2ReportItem(v)
  255. editor, e := services.UpdatePptEditing(v.PptId, 0, sysUser.AdminId, sysUser.RealName, false)
  256. if e != nil {
  257. br.Msg = "获取失败"
  258. br.ErrMsg = fmt.Sprintf("获取PPT编辑状态失败, err: %s", e.Error())
  259. return
  260. }
  261. t.Editor = editor
  262. // 权限
  263. if source == 1 || source == 2 {
  264. t.HasAuth = true
  265. } else {
  266. if v.AdminId == sysUser.AdminId {
  267. t.HasAuth = true
  268. }
  269. if t.HasAuth == false && v.CollaborateUsers != "" {
  270. authorArr := strings.Split(v.CollaborateUsers, ",")
  271. strId := strconv.Itoa(sysUser.AdminId)
  272. if utils.InArrayByStr(authorArr, strId) {
  273. t.HasAuth = true
  274. }
  275. }
  276. }
  277. resp.List = append(resp.List, t)
  278. }
  279. page := paging.GetPaging(currentIndex, pageSize, total)
  280. resp.Paging = page
  281. br.Data = resp
  282. br.Ret = 200
  283. br.Success = true
  284. br.Msg = "获取成功"
  285. }
  286. // CreateReport
  287. // @Title 新增ppt报告
  288. // @Description 新增ppt报告
  289. // @Param request body models.PptReportCreateReq true "type json string"
  290. // @Success 200 Ret=200 新增成功
  291. // @router /report/add [post]
  292. func (this *PptV2Controller) CreateReport() {
  293. br := new(models.BaseResponse).Init()
  294. defer func() {
  295. if br.ErrMsg == "" {
  296. br.IsSendEmail = false
  297. }
  298. this.Data["json"] = br
  299. this.ServeJSON()
  300. }()
  301. sysUser := this.SysUser
  302. if sysUser == nil {
  303. br.Msg = "请登录"
  304. br.ErrMsg = "请登录,SysUser Is Empty"
  305. return
  306. }
  307. var req models.PptReportCreateReq
  308. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  309. br.Msg = "参数解析异常"
  310. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  311. return
  312. }
  313. req.Title = strings.TrimSpace(req.Title)
  314. if req.Title == "" {
  315. br.Msg = "请输入标题"
  316. return
  317. }
  318. if req.AddType != 1 && req.AddType != 2 {
  319. req.AddType = 1
  320. }
  321. if req.ClassifyId <= 0 {
  322. br.Msg = "请选择分类"
  323. return
  324. }
  325. if req.CollaborateType != 1 && req.CollaborateType != 2 {
  326. br.Msg = "协作方式异常"
  327. br.ErrMsg = fmt.Sprintf("协作方式异常, %d", req.CollaborateType)
  328. return
  329. }
  330. if req.CollaborateType == 2 && len(req.CollaborateUserIds) == 0 {
  331. br.Msg = "请选择协作人"
  332. return
  333. }
  334. // 新报告
  335. newItem := new(models.PptV2)
  336. newItem.Title = req.Title
  337. newItem.AddType = req.AddType
  338. newItem.ClassifyId = req.ClassifyId
  339. newItem.CollaborateType = req.CollaborateType
  340. if len(req.CollaborateUserIds) > 0 {
  341. var partnerArr []string
  342. for _, v := range req.CollaborateUserIds {
  343. partnerArr = append(partnerArr, strconv.Itoa(v))
  344. }
  345. newItem.CollaborateUsers = strings.Trim(strings.Join(partnerArr, ","), `"`)
  346. }
  347. newItem.PptVersion = 2
  348. newItem.AdminId = sysUser.AdminId
  349. newItem.AdminRealName = sysUser.RealName
  350. newItem.ReportSource = utils.ReportSourceLocal // 固定本地PPT
  351. newItem.State = models.ReportStateUnpublished // 默认未发布
  352. newItem.CreateTime = time.Now()
  353. newItem.ModifyTime = time.Now()
  354. // 继承PPT内容
  355. if req.AddType == utils.ReportAddTypeInherit && req.InheritPptId > 0 {
  356. inheritPpt, e := models.GetPptV2ById(req.InheritPptId)
  357. if e != nil {
  358. if utils.IsErrNoRow(e) {
  359. br.Msg = "被继承报告不存在"
  360. return
  361. }
  362. br.Msg = "操作失败"
  363. br.ErrMsg = fmt.Sprintf("操作失败, %v", e)
  364. return
  365. }
  366. newItem.TemplateType = inheritPpt.TemplateType
  367. newItem.BackgroundImg = inheritPpt.BackgroundImg
  368. newItem.ReportType = inheritPpt.ReportType
  369. newItem.PptDate = inheritPpt.PptDate
  370. newItem.Content = inheritPpt.Content
  371. newItem.CoverContent = inheritPpt.CoverContent
  372. newItem.TitleSetting = inheritPpt.TitleSetting
  373. }
  374. // 非继承PPT,有内容时写入内容(如合并PPT时)
  375. if req.InheritPptId <= 0 {
  376. newItem.TemplateType = req.FirstPage.TemplateType
  377. newItem.BackgroundImg = req.FirstPage.ImgUrl
  378. newItem.ReportType = req.FirstPage.ReportType
  379. newItem.PptDate = req.FirstPage.PptDate
  380. newItem.Content = req.Content
  381. newItem.CoverContent = req.CoverContent
  382. newItem.TitleSetting = req.TitleSetting
  383. }
  384. newId, e := models.AddPptV2(newItem)
  385. if e != nil {
  386. br.Msg = "操作失败"
  387. br.ErrMsg = fmt.Sprintf("新增PPT报告失败, %v", e)
  388. return
  389. }
  390. // 更新报告分类计数
  391. go func() {
  392. _ = services.UpdateClassifyReportNum(req.ClassifyId)
  393. }()
  394. resp := models.AddPptResp{
  395. PptId: newId,
  396. }
  397. br.Data = resp
  398. br.Ret = 200
  399. br.Success = true
  400. br.Msg = "操作成功"
  401. }
  402. // AuthList
  403. // @Title 获取有权限的列表
  404. // @Description 获取有权限的列表
  405. // @Param PageSize query int true "每页数据条数"
  406. // @Param CurrentIndex query int true "当前页页码,从1开始"
  407. // @Param Keyword query string false "搜索关键词"
  408. // @Param ClassifyId query int false "分类ID"
  409. // @Success 200 {object} models.PptPageReportResp
  410. // @router /report/auth_list [get]
  411. func (this *PptV2Controller) AuthList() {
  412. br := new(models.BaseResponse).Init()
  413. defer func() {
  414. if br.ErrMsg == "" {
  415. br.IsSendEmail = false
  416. }
  417. this.Data["json"] = br
  418. this.ServeJSON()
  419. }()
  420. sysUser := this.SysUser
  421. if sysUser == nil {
  422. br.Msg = "请登录"
  423. br.ErrMsg = "请登录,SysUser Is Empty"
  424. br.Ret = 408
  425. return
  426. }
  427. pageSize, _ := this.GetInt("PageSize")
  428. currentIndex, _ := this.GetInt("CurrentIndex")
  429. keyword := this.GetString("Keyword")
  430. classifyId, _ := this.GetInt("ClassifyId", 0)
  431. var startSize int
  432. if pageSize <= 0 {
  433. pageSize = utils.PageSize20
  434. }
  435. if currentIndex <= 0 {
  436. currentIndex = 1
  437. }
  438. startSize = utils.StartIndex(currentIndex, pageSize)
  439. var pptList []*models.PptReportItem
  440. // 无相关搜索,返回空集
  441. //if keyword == `` && classifyId <= 0 {
  442. // page := paging.GetPaging(currentIndex, pageSize, 0)
  443. // resp := new(models.PptPageReportResp)
  444. // resp.Paging = page
  445. // resp.List = pptList
  446. // br.Ret = 200
  447. // br.Success = true
  448. // br.Msg = "获取成功"
  449. // br.Data = resp
  450. // return
  451. //}
  452. // 查询自己创建的以及协作人包含自己的报告
  453. var cond string
  454. var pars []interface{}
  455. cond += ` AND (admin_id = ? OR (admin_id <> ? AND FIND_IN_SET(?, collaborate_users)))`
  456. pars = append(pars, sysUser.AdminId, sysUser.AdminId, sysUser.AdminId)
  457. if classifyId > 0 {
  458. cond += ` AND classify_id = ? `
  459. pars = append(pars, classifyId)
  460. }
  461. keyword = strings.TrimSpace(keyword)
  462. if keyword != `` {
  463. cond += ` AND title LIKE ? `
  464. pars = utils.GetLikeKeywordPars(pars, keyword, 1)
  465. }
  466. pptOb := new(models.PptV2)
  467. total, e := pptOb.GetCountByCondition(cond, pars)
  468. if e != nil {
  469. br.Msg = "获取失败"
  470. br.ErrMsg = fmt.Sprintf("获取PPT总数失败, %v", e)
  471. return
  472. }
  473. list, e := pptOb.GetPageItemsByCondition(cond, pars, models.PptReportQueryFields, "", startSize, pageSize)
  474. if e != nil {
  475. br.Msg = "获取失败"
  476. br.ErrMsg = fmt.Sprintf("获取PPT失败, %v", e)
  477. return
  478. }
  479. // 分类完整路径、协作人姓名
  480. classifyIdFull := make(map[int]string)
  481. {
  482. ob := new(models.Classify)
  483. classifies, e := ob.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  484. if e != nil {
  485. br.Msg = "获取失败"
  486. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  487. return
  488. }
  489. classifyIdName := make(map[string]string)
  490. for _, v := range classifies {
  491. classifyIdName[strconv.Itoa(v.Id)] = v.ClassifyName
  492. }
  493. for _, v := range classifies {
  494. arr := strings.Split(v.LevelPath, ",")
  495. if len(arr) == 0 {
  496. continue
  497. }
  498. var nameArr []string
  499. for _, a := range arr {
  500. n := classifyIdName[a]
  501. if n == "" {
  502. continue
  503. }
  504. nameArr = append(nameArr, n)
  505. }
  506. classifyIdFull[v.Id] = strings.Join(nameArr, "/")
  507. }
  508. }
  509. adminIdName := make(map[int]string)
  510. {
  511. cond := ` AND enabled = 1`
  512. pars := make([]interface{}, 0)
  513. sysAdmin, e := system.GetSysAdminList(cond, pars, []string{}, "")
  514. if e != nil {
  515. br.Msg = "获取失败"
  516. br.ErrMsg = "获取用户失败,Err:" + e.Error()
  517. return
  518. }
  519. for _, v := range sysAdmin {
  520. adminIdName[v.AdminId] = v.RealName
  521. }
  522. }
  523. // 格式化数据
  524. for _, v := range list {
  525. t := v.Format2ReportItem(v)
  526. t.HasAuth = true // 该列表固定有权限
  527. t.FullClassify = classifyIdFull[v.ClassifyId] // 分类的完整路径
  528. // 协作人
  529. if v.CollaborateUsers != "" {
  530. var authors []models.PptReportCollaborateUser
  531. authorArr := strings.Split(v.CollaborateUsers, ",")
  532. for _, au := range authorArr {
  533. uid, _ := strconv.Atoi(au)
  534. if uid <= 0 {
  535. continue
  536. }
  537. name := adminIdName[uid]
  538. if name == "" {
  539. continue
  540. }
  541. authors = append(authors, models.PptReportCollaborateUser{
  542. AdminId: uid,
  543. RealName: name,
  544. })
  545. }
  546. t.CollaborateUsers = authors
  547. }
  548. pptList = append(pptList, t)
  549. }
  550. page := paging.GetPaging(currentIndex, pageSize, total)
  551. resp := new(models.PptPageReportResp)
  552. resp.Paging = page
  553. resp.List = pptList
  554. br.Ret = 200
  555. br.Success = true
  556. br.Msg = "获取成功"
  557. br.Data = resp
  558. }