report_open.go 21 KB

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