report_open.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. package services
  2. import (
  3. "eta_gn/eta_report/models"
  4. "eta_gn/eta_report/utils"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "time"
  10. )
  11. // GetReportClassifyTreeRecursive 递归获取目录树
  12. func GetReportClassifyTreeRecursive(list []*models.Classify, parentId int) []*models.ClassifyTreeItem {
  13. res := make([]*models.ClassifyTreeItem, 0)
  14. for _, v := range list {
  15. if v.ParentId == parentId {
  16. t := new(models.ClassifyTreeItem)
  17. t.ClassifyId = v.Id
  18. t.ClassifyName = v.ClassifyName
  19. t.ParentId = v.ParentId
  20. t.Sort = v.Sort
  21. t.Level = v.Level
  22. t.ClassifyType = v.ClassifyType
  23. t.HasChild = v.HasChild
  24. t.Children = GetReportClassifyTreeRecursive(list, v.Id)
  25. res = append(res, t)
  26. }
  27. }
  28. return res
  29. }
  30. func RecursiveFilterNoChildTreeClassify(list []*models.ClassifyTreeItem) []*models.ClassifyTreeItem {
  31. res := make([]*models.ClassifyTreeItem, 0)
  32. for _, v := range list {
  33. v.Children = RecursiveFilterNoChildTreeClassify(v.Children)
  34. if len(v.Children) == 0 && v.HasChild == 1 {
  35. continue
  36. }
  37. res = append(res, v)
  38. }
  39. return res
  40. }
  41. // GetReportByOutReportId 根据外部ID获取报告
  42. func GetReportByOutReportId(outReportId int) (reportItem *models.Report, pptItem *models.PptV2) {
  43. reportOb := new(models.Report)
  44. cond := ` AND out_report_id = ?`
  45. pars := make([]interface{}, 0)
  46. pars = append(pars, outReportId)
  47. reportExist, _ := reportOb.GetItemByCondition(cond, pars, "")
  48. if reportExist != nil && reportExist.Id > 0 {
  49. reportItem = reportExist
  50. }
  51. pptOb := new(models.PptV2)
  52. pptExist, _ := pptOb.GetItemByCondition(cond, pars, "")
  53. if pptExist != nil && pptExist.PptId > 0 {
  54. pptItem = pptExist
  55. }
  56. return
  57. }
  58. // CreatePptReport 创建PPT报告
  59. func CreatePptReport(outReportId, classifyId int, title string, topicEndTime time.Time, creator string, authors []string) (reportId int, err error) {
  60. defer func() {
  61. if err != nil {
  62. utils.FileLog.Info(fmt.Sprintf("创建外部PPT报告失败, OutReportId: %d, %v", outReportId, err))
  63. }
  64. }()
  65. // 获取用户
  66. outIdAdmin := make(map[string]*models.Admin)
  67. {
  68. ob := new(models.Admin)
  69. list, e := ob.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  70. if e != nil {
  71. err = fmt.Errorf("获取用户列表失败, %v", e)
  72. return
  73. }
  74. for _, v := range list {
  75. if v.OutId == "" {
  76. continue
  77. }
  78. outIdAdmin[v.OutId] = v
  79. }
  80. }
  81. creatorAdmin := outIdAdmin[creator]
  82. if creatorAdmin == nil {
  83. err = fmt.Errorf("创建人不存在, OutId: %s", creator)
  84. return
  85. }
  86. // 新建PPT基础信息
  87. newItem := new(models.PptV2)
  88. newItem.Title = title
  89. newItem.AddType = utils.ReportAddTypeInherit
  90. newItem.ClassifyId = classifyId
  91. newItem.CollaborateType = utils.ReportWriteTypeGroup
  92. if len(authors) > 0 {
  93. var partnerArr []string
  94. for _, v := range authors {
  95. ad := outIdAdmin[v]
  96. // 忽略无法识别的工号
  97. if ad == nil {
  98. continue
  99. }
  100. partnerArr = append(partnerArr, strconv.Itoa(ad.AdminId))
  101. }
  102. newItem.CollaborateUsers = strings.Trim(strings.Join(partnerArr, ","), `"`)
  103. }
  104. newItem.PptVersion = 2
  105. newItem.AdminId = creatorAdmin.AdminId
  106. newItem.AdminRealName = creatorAdmin.RealName
  107. newItem.ReportSource = utils.ReportSourceOuter // 固定外部PPT
  108. newItem.State = models.ReportStateWaitSubmit // 默认待提交
  109. newItem.CreateTime = time.Now()
  110. newItem.ModifyTime = time.Now()
  111. newItem.OutReportId = strconv.Itoa(outReportId)
  112. newItem.TopicEndTime = topicEndTime
  113. newItem.TemplateType = 1 // 模板类型默认1,有继承那么就继承
  114. // 自动继承该分类上一篇外部报告(审批通过的)
  115. inheritItem := new(models.PptV2)
  116. {
  117. ob := new(models.PptV2)
  118. cond := ` AND report_source = ? AND classify_id = ? AND state = ?`
  119. pars := make([]interface{}, 0)
  120. pars = append(pars, utils.ReportSourceOuter, classifyId, models.ReportStatePass)
  121. inheritItem, _ = ob.GetItemByCondition(cond, pars, "create_time DESC")
  122. }
  123. if inheritItem != nil && inheritItem.PptId > 0 {
  124. newItem.TemplateType = inheritItem.TemplateType
  125. newItem.BackgroundImg = inheritItem.BackgroundImg
  126. newItem.ReportType = inheritItem.ReportType
  127. newItem.PptDate = inheritItem.PptDate
  128. newItem.Content = inheritItem.Content
  129. newItem.CoverContent = inheritItem.CoverContent
  130. newItem.TitleSetting = inheritItem.TitleSetting
  131. }
  132. if e := newItem.Create(); e != nil {
  133. err = fmt.Errorf("新增PPT报告失败, %v", e)
  134. return
  135. }
  136. reportId = newItem.PptId
  137. //reportCode = utils.MD5(fmt.Sprintf("PPT%d", newItem.PptId))
  138. // 更新报告分类计数
  139. go func() {
  140. _ = UpdateClassifyReportNum(classifyId)
  141. }()
  142. return
  143. }
  144. // 更新分类报告计数加个锁
  145. var classifyReportNumLock sync.Mutex
  146. // UpdateClassifyReportNum 更新分类报告计数
  147. func UpdateClassifyReportNum(classifyId int) (err error) {
  148. classifyReportNumLock.Lock()
  149. defer func() {
  150. if err != nil {
  151. utils.FileLog.Info(fmt.Sprintf("更新分类报告计数失败, %v", err))
  152. }
  153. classifyReportNumLock.Unlock()
  154. }()
  155. classifyItem, e := models.GetClassifyById(classifyId)
  156. if e != nil {
  157. err = fmt.Errorf("获取分类失败, %v", e)
  158. return
  159. }
  160. // 更新分类报告数
  161. var total int
  162. {
  163. reportOb := new(models.Report)
  164. var cond string
  165. switch classifyItem.Level {
  166. case 1:
  167. cond += ` AND classify_id_first = ?`
  168. case 2:
  169. cond += ` AND classify_id_second = ?`
  170. case 3:
  171. cond += ` AND classify_id_third = ?`
  172. }
  173. pars := make([]interface{}, 0)
  174. pars = append(pars, classifyId)
  175. count, e := reportOb.GetCountByCondition(cond, pars)
  176. if e != nil {
  177. err = fmt.Errorf("获取报告计数失败, %v", e)
  178. return
  179. }
  180. total += count
  181. }
  182. {
  183. pptOb := new(models.PptV2)
  184. cond := ` AND classify_id = ?`
  185. pars := make([]interface{}, 0)
  186. pars = append(pars, classifyId)
  187. count, e := pptOb.GetCountByCondition(cond, pars)
  188. if e != nil {
  189. err = fmt.Errorf("获取PPT报告计数失败, %v", e)
  190. return
  191. }
  192. total += count
  193. }
  194. classifyItem.ReportNum = total
  195. if e = classifyItem.UpdateClassify([]string{"ReportNum"}); e != nil {
  196. err = fmt.Errorf("更新分类报告计数失败, %v", e)
  197. return
  198. }
  199. // 获取所有分类, 更新父级, 无父级忽略
  200. if classifyItem.ParentId <= 0 {
  201. return
  202. }
  203. classifyOb := new(models.Classify)
  204. classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  205. if e != nil {
  206. err = fmt.Errorf("获取报告列表失败, %v", e)
  207. return
  208. }
  209. classifyIdMap := make(map[int]*models.Classify)
  210. classifyParentChildMap := make(map[int][]*models.Classify)
  211. for _, v := range classifies {
  212. classifyIdMap[v.Id] = v
  213. if v.ParentId <= 0 {
  214. continue
  215. }
  216. if classifyParentChildMap[v.ParentId] == nil {
  217. classifyParentChildMap[v.ParentId] = make([]*models.Classify, 0)
  218. }
  219. classifyParentChildMap[v.ParentId] = append(classifyParentChildMap[v.ParentId], v)
  220. }
  221. // 递归获取需要更新的父级报告数
  222. updateClassifies := CountParentClassifyReportNumRecursive(classifies, classifyItem.ParentId, classifyIdMap, classifyParentChildMap)
  223. if len(updateClassifies) == 0 {
  224. return
  225. }
  226. for _, v := range updateClassifies {
  227. if e = v.UpdateClassify([]string{"ReportNum"}); e != nil {
  228. err = fmt.Errorf("更新父级分类报告计数失败, %v", e)
  229. return
  230. }
  231. }
  232. return
  233. }
  234. // CountParentClassifyReportNumRecursive 递归统计父级分类报告数
  235. func CountParentClassifyReportNumRecursive(list []*models.Classify, parentId int, classifyIdMap map[int]*models.Classify, classifyParentChildMap map[int][]*models.Classify) []*models.Classify {
  236. res := make([]*models.Classify, 0)
  237. for _, v := range list {
  238. // 找父级
  239. if v.Id != parentId {
  240. continue
  241. }
  242. parentItem := classifyIdMap[v.Id]
  243. if parentItem == nil {
  244. break
  245. }
  246. // 合计所有直系子分类报告数
  247. children := classifyParentChildMap[v.Id]
  248. if len(children) == 0 {
  249. break
  250. }
  251. var t int
  252. for _, child := range children {
  253. t += child.ReportNum
  254. }
  255. parentItem.ReportNum = t
  256. res = append(res, parentItem)
  257. // 继续向上统计父级
  258. if v.ParentId <= 0 {
  259. break
  260. }
  261. parents := CountParentClassifyReportNumRecursive(list, v.ParentId, classifyIdMap, classifyParentChildMap)
  262. if len(parents) == 0 {
  263. break
  264. }
  265. res = append(res, parents...)
  266. }
  267. return res
  268. }
  269. // CreateReport 创建报告
  270. func CreateReport(outReportId, classifyId int, title string, topicEndTime time.Time, creator string, authors []string) (reportId int, err error) {
  271. defer func() {
  272. if err != nil {
  273. utils.FileLog.Info(fmt.Sprintf("创建外部报告失败, OutReportId: %d, %v", outReportId, err))
  274. }
  275. }()
  276. // 获取用户
  277. outIdAdmin := make(map[string]*models.Admin)
  278. {
  279. ob := new(models.Admin)
  280. list, e := ob.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  281. if e != nil {
  282. err = fmt.Errorf("获取用户列表失败, %v", e)
  283. return
  284. }
  285. for _, v := range list {
  286. if v.OutId == "" {
  287. continue
  288. }
  289. outIdAdmin[v.OutId] = v
  290. }
  291. }
  292. creatorAdmin := outIdAdmin[creator]
  293. if creatorAdmin == nil {
  294. err = fmt.Errorf("创建人不存在, OutId: %s", creator)
  295. return
  296. }
  297. // 各级分类信息
  298. var classifyIdFirst, classifyIdSecond, classifyIdThird int
  299. var classifyNameFirst, classifyNameSecond, classifyNameThird, classifyName string
  300. {
  301. classifyOb := new(models.Classify)
  302. classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  303. if e != nil {
  304. err = fmt.Errorf("获取报告列表失败, %v", e)
  305. return
  306. }
  307. classifyIdMap := make(map[int]*models.Classify)
  308. for _, v := range classifies {
  309. classifyIdMap[v.Id] = v
  310. }
  311. thisClassify := classifyIdMap[classifyId]
  312. if thisClassify == nil {
  313. err = fmt.Errorf("分类不存在, ID: %d", classifyId)
  314. return
  315. }
  316. classifyName = thisClassify.ClassifyName
  317. // 根据levelPath确认各级分类信息
  318. levelArr := strings.Split(thisClassify.LevelPath, ",")
  319. levelLen := len(levelArr)
  320. if levelLen <= 0 {
  321. err = fmt.Errorf("分类层级信息有误, ID: %d, LevelPath: %s", classifyId, thisClassify.LevelPath)
  322. return
  323. }
  324. if levelLen > 2 {
  325. strThird := levelArr[2]
  326. classifyIdThird, _ = strconv.Atoi(strThird)
  327. classifyThird := classifyIdMap[classifyIdThird]
  328. if classifyThird != nil {
  329. classifyNameThird = classifyThird.ClassifyName
  330. }
  331. }
  332. if levelLen > 1 {
  333. strSecond := levelArr[1]
  334. classifyIdSecond, _ = strconv.Atoi(strSecond)
  335. classifySecond := classifyIdMap[classifyIdSecond]
  336. if classifySecond != nil {
  337. classifyNameSecond = classifySecond.ClassifyName
  338. }
  339. }
  340. if levelLen > 0 {
  341. strFirst := levelArr[0]
  342. classifyIdFirst, _ = strconv.Atoi(strFirst)
  343. classifyFirst := classifyIdMap[classifyIdFirst]
  344. if classifyFirst != nil {
  345. classifyNameFirst = classifyFirst.ClassifyName
  346. }
  347. }
  348. }
  349. // 新报告信息
  350. newItem := new(models.Report)
  351. newItem.AddType = utils.ReportAddTypeInherit // 固定继承
  352. newItem.ReportVersion = 2 // 固定新版报告
  353. newItem.ClassifyIdFirst = classifyIdFirst
  354. newItem.ClassifyNameFirst = classifyNameFirst
  355. newItem.ClassifyIdSecond = classifyIdSecond
  356. newItem.ClassifyNameSecond = classifyNameSecond
  357. newItem.ClassifyIdThird = classifyIdThird
  358. newItem.ClassifyNameThird = classifyNameThird
  359. newItem.Title = title
  360. newItem.Frequency = "日度" // 默认日度吧,这个也没啥用
  361. newItem.State = models.ReportStateWaitSubmit // 固定待提交
  362. newItem.Stage = 0 // 期数去掉了
  363. newItem.NeedSplice = 1 // TODO:是否默认拼接版头
  364. newItem.CollaborateType = 2 // 固定协作报告
  365. newItem.HasChapter = 1 // 协作报告固定有章节
  366. newItem.ReportLayout = 2 // 固定智能布局
  367. newItem.IsPublicPublish = 1 // 固定公开发布
  368. newItem.ReportCreateTime = time.Now()
  369. newItem.ReportSource = utils.ReportSourceOuter // 固定外部报告
  370. newItem.OutReportId = strconv.Itoa(outReportId)
  371. newItem.TopicEndTime = topicEndTime // 课题结束时间
  372. newItem.AdminId = creatorAdmin.AdminId
  373. newItem.AdminRealName = creatorAdmin.RealName
  374. newItem.ContentModifyTime = time.Now()
  375. newItem.CreateTime = time.Now()
  376. newItem.ModifyTime = time.Now()
  377. newItem.LastModifyAdminId = creatorAdmin.AdminId
  378. newItem.LastModifyAdminName = creatorAdmin.RealName
  379. // 协作人信息
  380. newGrants := make([]*models.ReportGrant, 0)
  381. var partnerIds []int
  382. if len(authors) > 0 {
  383. for _, v := range authors {
  384. ad := outIdAdmin[v]
  385. // 忽略无法识别的工号
  386. if ad == nil {
  387. continue
  388. }
  389. partnerIds = append(partnerIds, ad.AdminId)
  390. }
  391. }
  392. for _, v := range partnerIds {
  393. newGrants = append(newGrants, &models.ReportGrant{
  394. AdminId: v,
  395. CreateTime: time.Now(),
  396. })
  397. }
  398. // 自动继承该分类上一篇外部报告(审批通过的)
  399. newChapters := make([]*models.ReportChapter, 0)
  400. chapterGrantsMapping := make(map[int][]int)
  401. inheritItem := new(models.Report)
  402. {
  403. ob := new(models.Report)
  404. cond := ` AND report_source = ? AND state = ? AND classify_id_first = ? AND classify_id_second = ? AND classify_id_third = ?`
  405. pars := make([]interface{}, 0)
  406. pars = append(pars, utils.ReportSourceOuter, models.ReportStatePass, classifyIdFirst, classifyIdSecond, classifyIdThird)
  407. inheritItem, _ = ob.GetItemByCondition(cond, pars, "create_time DESC")
  408. }
  409. if inheritItem != nil && inheritItem.Id > 0 {
  410. newItem.InheritReportId = inheritItem.Id
  411. newItem.Abstract = inheritItem.Abstract
  412. newItem.Content = inheritItem.Content
  413. newItem.ContentSub = inheritItem.ContentSub
  414. newItem.ContentStruct = inheritItem.ContentStruct
  415. newItem.HeadImg = inheritItem.HeadImg
  416. newItem.EndImg = inheritItem.EndImg
  417. newItem.CanvasColor = inheritItem.CanvasColor
  418. newItem.HeadResourceId = inheritItem.HeadResourceId
  419. newItem.EndResourceId = inheritItem.EndResourceId
  420. // 继承报告章节内容
  421. chapterOb := new(models.ReportChapter)
  422. chapterCond := ` AND report_id = ?`
  423. chapterPars := make([]interface{}, 0)
  424. chapterPars = append(chapterPars, inheritItem.Id)
  425. inheritChapters, e := chapterOb.GetItemsByCondition(chapterCond, chapterPars, []string{}, "")
  426. if e != nil {
  427. err = fmt.Errorf("获取继承报告章节失败, %v", e)
  428. return
  429. }
  430. if len(inheritChapters) > 0 {
  431. var originChapterIds []int
  432. for _, v := range inheritChapters {
  433. originChapterIds = append(originChapterIds, v.ReportChapterId)
  434. //v.ReportChapterId = 0 // 此处不置空,插入时需要做新ID和旧ID的匹配
  435. v.ReportId = 0
  436. v.ClassifyIdFirst = classifyIdFirst
  437. v.ClassifyNameFirst = classifyNameFirst
  438. v.PublishState = 1 // 未发布
  439. v.CreateTime = time.Now()
  440. v.ModifyTime = time.Now()
  441. v.ContentModifyTime = time.Now()
  442. v.LastModifyAdminId = creatorAdmin.AdminId
  443. v.LastModifyAdminName = creatorAdmin.RealName
  444. newChapters = append(newChapters, v)
  445. }
  446. // 这里需要继承原章节的授权,保留在报告协作人中依旧存在的,移除不在报告协作人中的
  447. chapterGrantsOb := new(models.ReportChapterGrant)
  448. chapterGrantsCond := ` AND report_chapter_id IN (?)`
  449. chapterGrantsPars := make([]interface{}, 0)
  450. chapterGrantsPars = append(chapterGrantsPars, originChapterIds)
  451. originChapterGrants, e := chapterGrantsOb.GetItemsByCondition(chapterGrantsCond, chapterGrantsPars, []string{}, "")
  452. if e != nil {
  453. err = fmt.Errorf("获取继承章节授权失败, %v", e)
  454. return
  455. }
  456. for _, v := range originChapterGrants {
  457. if utils.InArrayByInt(partnerIds, v.AdminId) {
  458. chapterGrantsMapping[v.ReportChapterId] = append(chapterGrantsMapping[v.ReportChapterId], v.AdminId)
  459. }
  460. }
  461. }
  462. } else {
  463. // 找不到继承报告,则找下分类是否有对应的章节设置
  464. typesOb := new(models.ReportChapterType)
  465. typesCond := ` AND report_classify_id = ?`
  466. typesPars := make([]interface{}, 0)
  467. typesPars = append(typesPars, classifyId)
  468. chapterTypes, e := typesOb.GetItemsByCondition(typesCond, typesPars, []string{}, "")
  469. if e != nil {
  470. err = fmt.Errorf("获取报告分类章节设置失败, %v", e)
  471. return
  472. }
  473. // 新增默认章节
  474. for _, v := range chapterTypes {
  475. cp := new(models.ReportChapter)
  476. cp.AddType = 1
  477. cp.Title = v.ReportChapterTypeName
  478. cp.ReportType = v.ResearchType
  479. cp.ClassifyIdFirst = classifyId
  480. cp.ClassifyNameFirst = classifyName
  481. cp.TypeId = v.ReportChapterTypeId
  482. cp.TypeName = v.ReportChapterTypeName
  483. cp.PublishState = 1
  484. cp.Sort = v.Sort
  485. cp.CreateTime = newItem.CreateTime
  486. cp.ModifyTime = time.Now()
  487. cp.LastModifyAdminId = newItem.LastModifyAdminId
  488. cp.LastModifyAdminName = newItem.LastModifyAdminName
  489. cp.ContentModifyTime = time.Now()
  490. cp.ReportLayout = newItem.ReportLayout
  491. cp.ReportCreateTime = time.Now()
  492. newChapters = append(newChapters, cp)
  493. }
  494. }
  495. // 新增报告、章节以及协作人权限
  496. newId, e := newItem.CreateReportAndChapters(newItem, newChapters, newGrants, chapterGrantsMapping)
  497. if e != nil {
  498. err = fmt.Errorf("新增报告、章节及授权失败, %v", e)
  499. return
  500. }
  501. if newId <= 0 {
  502. err = fmt.Errorf("新增报告ID为0")
  503. return
  504. }
  505. reportId = newId
  506. // 更新唯一编码、更新报告分类计数
  507. go func() {
  508. newItem.ReportCode = utils.MD5(strconv.Itoa(newItem.Id))
  509. if e = newItem.Update([]string{"ReportCode"}); e != nil {
  510. utils.FileLog.Info(fmt.Sprintf("报告唯一编码更想失败, ID: %d", newItem.Id))
  511. }
  512. _ = UpdateClassifyReportNum(classifyId)
  513. }()
  514. return
  515. }
  516. // EditReport 编辑报告
  517. func EditReport(reportItem *models.Report, title string, topicEndTime time.Time, authors []string) (err error) {
  518. if reportItem == nil {
  519. err = fmt.Errorf("报告信息有误")
  520. return
  521. }
  522. defer func() {
  523. if err != nil {
  524. utils.FileLog.Info(fmt.Sprintf("编辑外部报告失败, OutReportId: %s, %v", reportItem.OutReportId, err))
  525. }
  526. }()
  527. // 获取用户
  528. outIdAdmin := make(map[string]*models.Admin)
  529. {
  530. ob := new(models.Admin)
  531. list, e := ob.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  532. if e != nil {
  533. err = fmt.Errorf("获取用户列表失败, %v", e)
  534. return
  535. }
  536. for _, v := range list {
  537. if v.OutId == "" {
  538. continue
  539. }
  540. outIdAdmin[v.OutId] = v
  541. }
  542. }
  543. // 比对协作人,移除被删除协作人的权限
  544. var removePartner []int
  545. {
  546. var originPartner []int
  547. var partnerIds []int
  548. if len(authors) > 0 {
  549. for _, v := range authors {
  550. ad := outIdAdmin[v]
  551. // 忽略无法识别的工号
  552. if ad == nil {
  553. continue
  554. }
  555. partnerIds = append(partnerIds, ad.AdminId)
  556. }
  557. }
  558. grantOb := new(models.ReportGrant)
  559. grantCond := ` AND report_id = ?`
  560. grantPars := make([]interface{}, 0)
  561. grantPars = append(grantPars, reportItem.Id)
  562. grants, e := grantOb.GetItemsByCondition(grantCond, grantPars, []string{}, "")
  563. if e != nil {
  564. err = fmt.Errorf("获取报告授权失败, %v", e)
  565. return
  566. }
  567. for _, v := range grants {
  568. originPartner = append(originPartner, v.AdminId)
  569. }
  570. if len(partnerIds) == 0 {
  571. removePartner = originPartner
  572. }
  573. // 原协作人不在新协作人中则移除
  574. for _, v := range originPartner {
  575. if !utils.InArrayByInt(partnerIds, v) {
  576. removePartner = append(removePartner, v)
  577. }
  578. }
  579. }
  580. // 获取章节IDs,已有章节清除授权
  581. var chapterIds []int
  582. if len(removePartner) > 0 {
  583. chapterOb := new(models.ReportChapter)
  584. chapterCond := ` AND report_id = ?`
  585. chapterPars := make([]interface{}, 0)
  586. chapterPars = append(chapterPars, reportItem.Id)
  587. chapters, e := chapterOb.GetItemsByCondition(chapterCond, chapterPars, []string{"report_chapter_id"}, "")
  588. if e != nil {
  589. err = fmt.Errorf("获取报告章节失败, %v", e)
  590. return
  591. }
  592. for _, v := range chapters {
  593. chapterIds = append(chapterIds, v.ReportChapterId)
  594. }
  595. }
  596. // 更新报告和移除授权
  597. reportItem.Title = title
  598. reportItem.TopicEndTime = topicEndTime
  599. reportItem.ModifyTime = time.Now()
  600. updateCols := []string{"Title", "TopicEndTime", "ModifyTime"}
  601. if e := reportItem.EditReportAndClearGrant(reportItem, updateCols, chapterIds, removePartner); e != nil {
  602. err = fmt.Errorf("更新报告失败, %v", e)
  603. return
  604. }
  605. return
  606. }
  607. // EditPptReport 编辑PPT报告
  608. func EditPptReport(pptItem *models.PptV2, title string, topicEndTime time.Time, authors []string) (err error) {
  609. if pptItem == nil {
  610. err = fmt.Errorf("PPT报告信息有误")
  611. return
  612. }
  613. defer func() {
  614. if err != nil {
  615. utils.FileLog.Info(fmt.Sprintf("编辑外部PPT报告失败, OutReportId: %s, %v", pptItem.OutReportId, err))
  616. }
  617. }()
  618. // 获取用户
  619. outIdAdmin := make(map[string]*models.Admin)
  620. {
  621. ob := new(models.Admin)
  622. list, e := ob.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  623. if e != nil {
  624. err = fmt.Errorf("获取用户列表失败, %v", e)
  625. return
  626. }
  627. for _, v := range list {
  628. if v.OutId == "" {
  629. continue
  630. }
  631. outIdAdmin[v.OutId] = v
  632. }
  633. }
  634. // 新建PPT基础信息
  635. pptItem.Title = title
  636. pptItem.TopicEndTime = topicEndTime
  637. if len(authors) > 0 {
  638. var partnerArr []string
  639. for _, v := range authors {
  640. ad := outIdAdmin[v]
  641. // 忽略无法识别的工号
  642. if ad == nil {
  643. continue
  644. }
  645. partnerArr = append(partnerArr, strconv.Itoa(ad.AdminId))
  646. }
  647. pptItem.CollaborateUsers = strings.Trim(strings.Join(partnerArr, ","), `"`)
  648. }
  649. updateCols := []string{"Title", "TopicEndTime", "CollaborateUsers"}
  650. if e := pptItem.Update(updateCols); e != nil {
  651. err = fmt.Errorf("更新PPT报告失败, %v", e)
  652. return
  653. }
  654. return
  655. }