ppt_report.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 = ? AND state = ?`
  53. pars = append(pars, utils.ReportSourceOuter, models.ReportStatePass)
  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 = ? AND state = ?`
  202. pars = append(pars, utils.ReportSourceOuter, models.ReportStatePass)
  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. // 初始的PPT页数
  385. if newItem.Content != "" {
  386. var pageContents []services.PPTContent
  387. _ = json.Unmarshal([]byte(newItem.Content), &pageContents) // 这里转不过去问题也不大,编辑时自动保存会更新页数字段
  388. newItem.PptPage = len(pageContents)
  389. }
  390. newId, e := models.AddPptV2(newItem)
  391. if e != nil {
  392. br.Msg = "操作失败"
  393. br.ErrMsg = fmt.Sprintf("新增PPT报告失败, %v", e)
  394. return
  395. }
  396. // 更新报告分类计数
  397. go func() {
  398. _ = services.UpdateClassifyReportNum(req.ClassifyId)
  399. }()
  400. resp := models.AddPptResp{
  401. PptId: newId,
  402. }
  403. br.Data = resp
  404. br.Ret = 200
  405. br.Success = true
  406. br.Msg = "操作成功"
  407. }
  408. // AuthList
  409. // @Title 获取有权限的列表
  410. // @Description 获取有权限的列表
  411. // @Param PageSize query int true "每页数据条数"
  412. // @Param CurrentIndex query int true "当前页页码,从1开始"
  413. // @Param Keyword query string false "搜索关键词"
  414. // @Param ClassifyId query int false "分类ID"
  415. // @Success 200 {object} models.PptPageReportResp
  416. // @router /report/auth_list [get]
  417. func (this *PptV2Controller) AuthList() {
  418. br := new(models.BaseResponse).Init()
  419. defer func() {
  420. if br.ErrMsg == "" {
  421. br.IsSendEmail = false
  422. }
  423. this.Data["json"] = br
  424. this.ServeJSON()
  425. }()
  426. sysUser := this.SysUser
  427. if sysUser == nil {
  428. br.Msg = "请登录"
  429. br.ErrMsg = "请登录,SysUser Is Empty"
  430. br.Ret = 408
  431. return
  432. }
  433. pageSize, _ := this.GetInt("PageSize")
  434. currentIndex, _ := this.GetInt("CurrentIndex")
  435. keyword := this.GetString("Keyword")
  436. classifyId, _ := this.GetInt("ClassifyId", 0)
  437. var startSize int
  438. if pageSize <= 0 {
  439. pageSize = utils.PageSize20
  440. }
  441. if currentIndex <= 0 {
  442. currentIndex = 1
  443. }
  444. startSize = utils.StartIndex(currentIndex, pageSize)
  445. var pptList []*models.PptReportItem
  446. // 无相关搜索时,返回空集(其实我感觉没必要,没关键词查全部也属于正常=_=!)
  447. if keyword == `` && classifyId <= 0 {
  448. page := paging.GetPaging(currentIndex, pageSize, 0)
  449. resp := new(models.PptPageReportResp)
  450. resp.Paging = page
  451. resp.List = pptList
  452. br.Ret = 200
  453. br.Success = true
  454. br.Msg = "获取成功"
  455. br.Data = resp
  456. return
  457. }
  458. // 查询自己创建的以及协作人包含自己的报告
  459. var cond string
  460. var pars []interface{}
  461. cond += ` AND (admin_id = ? OR (admin_id <> ? AND FIND_IN_SET(?, collaborate_users)))`
  462. pars = append(pars, sysUser.AdminId, sysUser.AdminId, sysUser.AdminId)
  463. if classifyId > 0 {
  464. cond += ` AND classify_id = ? `
  465. pars = append(pars, classifyId)
  466. }
  467. keyword = strings.TrimSpace(keyword)
  468. if keyword != `` {
  469. cond += ` AND title LIKE ? `
  470. pars = utils.GetLikeKeywordPars(pars, keyword, 1)
  471. }
  472. pptOb := new(models.PptV2)
  473. total, e := pptOb.GetCountByCondition(cond, pars)
  474. if e != nil {
  475. br.Msg = "获取失败"
  476. br.ErrMsg = fmt.Sprintf("获取PPT总数失败, %v", e)
  477. return
  478. }
  479. list, e := pptOb.GetPageItemsByCondition(cond, pars, models.PptReportQueryFields, "", startSize, pageSize)
  480. if e != nil {
  481. br.Msg = "获取失败"
  482. br.ErrMsg = fmt.Sprintf("获取PPT失败, %v", e)
  483. return
  484. }
  485. // 分类完整路径、协作人姓名
  486. classifyIdFull := make(map[int]string)
  487. {
  488. ob := new(models.Classify)
  489. classifies, e := ob.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  490. if e != nil {
  491. br.Msg = "获取失败"
  492. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  493. return
  494. }
  495. classifyIdName := make(map[string]string)
  496. for _, v := range classifies {
  497. classifyIdName[strconv.Itoa(v.Id)] = v.ClassifyName
  498. }
  499. for _, v := range classifies {
  500. arr := strings.Split(v.LevelPath, ",")
  501. if len(arr) == 0 {
  502. continue
  503. }
  504. var nameArr []string
  505. for _, a := range arr {
  506. n := classifyIdName[a]
  507. if n == "" {
  508. continue
  509. }
  510. nameArr = append(nameArr, n)
  511. }
  512. classifyIdFull[v.Id] = strings.Join(nameArr, "/")
  513. }
  514. }
  515. adminIdName := make(map[int]string)
  516. {
  517. cond := ` AND enabled = 1`
  518. pars := make([]interface{}, 0)
  519. sysAdmin, e := system.GetSysAdminList(cond, pars, []string{}, "")
  520. if e != nil {
  521. br.Msg = "获取失败"
  522. br.ErrMsg = "获取用户失败,Err:" + e.Error()
  523. return
  524. }
  525. for _, v := range sysAdmin {
  526. adminIdName[v.AdminId] = v.RealName
  527. }
  528. }
  529. // 格式化数据
  530. for _, v := range list {
  531. t := v.Format2ReportItem(v)
  532. t.HasAuth = true // 该列表固定有权限
  533. t.FullClassify = classifyIdFull[v.ClassifyId] // 分类的完整路径
  534. // 协作人
  535. if v.CollaborateUsers != "" {
  536. var authors []models.PptReportCollaborateUser
  537. authorArr := strings.Split(v.CollaborateUsers, ",")
  538. for _, au := range authorArr {
  539. uid, _ := strconv.Atoi(au)
  540. if uid <= 0 {
  541. continue
  542. }
  543. name := adminIdName[uid]
  544. if name == "" {
  545. continue
  546. }
  547. authors = append(authors, models.PptReportCollaborateUser{
  548. AdminId: uid,
  549. RealName: name,
  550. })
  551. }
  552. t.CollaborateUsers = authors
  553. }
  554. pptList = append(pptList, t)
  555. }
  556. page := paging.GetPaging(currentIndex, pageSize, total)
  557. resp := new(models.PptPageReportResp)
  558. resp.Paging = page
  559. resp.List = pptList
  560. br.Ret = 200
  561. br.Success = true
  562. br.Msg = "获取成功"
  563. br.Data = resp
  564. }