report_open.go 20 KB

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