report_v2.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. package services
  2. import (
  3. "archive/zip"
  4. "errors"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/report"
  7. "eta_gn/eta_api/models/report_approve"
  8. "eta_gn/eta_api/models/system"
  9. "eta_gn/eta_api/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/file"
  12. "github.com/rdlucklib/rdluck_tools/http"
  13. "html"
  14. "os"
  15. "path"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. func AddReportAndChapter(reportInfo *models.Report, inheritReportId int, grantAdminIdList []int) (err error, errMsg string) {
  21. state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperateAdd)
  22. if e != nil {
  23. err = errors.New("校验报告当前状态失败, Err: " + e.Error())
  24. return
  25. }
  26. reportInfo.State = state
  27. minClassifyId := reportInfo.ClassifyIdThird
  28. if minClassifyId <= 0 {
  29. minClassifyId = reportInfo.ClassifyIdSecond
  30. }
  31. if minClassifyId <= 0 {
  32. minClassifyId = reportInfo.ClassifyIdFirst
  33. }
  34. if minClassifyId <= 0 {
  35. errMsg = "分类异常"
  36. err = errors.New(errMsg)
  37. return
  38. }
  39. errMsg = "生成报告失败"
  40. if inheritReportId > 0 {
  41. inheritReport, tmpErr := models.GetReportByReportId(inheritReportId)
  42. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  43. errMsg = "获取待继承的报告失败"
  44. err = tmpErr
  45. return
  46. }
  47. if inheritReport != nil {
  48. if inheritReport.ClassifyIdFirst != reportInfo.ClassifyIdFirst || inheritReport.ClassifyIdSecond != reportInfo.ClassifyIdSecond || inheritReport.ClassifyIdThird != reportInfo.ClassifyIdThird {
  49. errMsg = "分类异常,与继承的报告分类不一致"
  50. err = errors.New(errMsg)
  51. return
  52. }
  53. reportInfo.ChapterType = inheritReport.ChapterType
  54. reportInfo.Content = inheritReport.Content
  55. reportInfo.ContentSub = inheritReport.ContentSub
  56. reportInfo.ContentStruct = inheritReport.ContentStruct
  57. reportInfo.HeadImg = inheritReport.HeadImg
  58. reportInfo.EndImg = inheritReport.EndImg
  59. reportInfo.CanvasColor = inheritReport.CanvasColor
  60. reportInfo.NeedSplice = inheritReport.NeedSplice
  61. reportInfo.HeadResourceId = inheritReport.HeadResourceId
  62. reportInfo.EndResourceId = inheritReport.EndResourceId
  63. reportInfo.InheritReportId = inheritReport.Id
  64. }
  65. }
  66. addChapterList, allGrantUserList, err, errMsg := getAddChapter(reportInfo, minClassifyId, inheritReportId, grantAdminIdList)
  67. var reportId int64
  68. reportId, err = models.AddReportAndChapter(reportInfo, allGrantUserList, addChapterList)
  69. if err != nil {
  70. err = errors.New("新增报告及章节失败, Err: " + err.Error())
  71. return
  72. }
  73. reportCode := utils.MD5(strconv.Itoa(reportInfo.Id))
  74. reportInfo.ReportCode = reportCode
  75. {
  76. go models.ModifyReportCode(reportId, reportCode)
  77. }
  78. go handleReportPermission(reportId, minClassifyId)
  79. return
  80. }
  81. func EditReport(reportInfo *models.Report, req models.EditReq, sysUser *system.Admin) (err error, errMsg string) {
  82. errMsg = `保存失败`
  83. reportInfo.Title = req.Title
  84. reportInfo.Abstract = req.Abstract
  85. reportInfo.Author = req.Author
  86. reportInfo.Frequency = req.Frequency
  87. if req.CreateTime != `` {
  88. formatDate := utils.FormatDateTime
  89. if !strings.Contains(req.CreateTime, ":") {
  90. formatDate = utils.FormatDate
  91. }
  92. createTime, tmpErr := time.ParseInLocation(formatDate, req.CreateTime, time.Local)
  93. if tmpErr != nil {
  94. err = tmpErr
  95. return
  96. }
  97. reportInfo.CreateTime = createTime
  98. }
  99. if req.IsPublicPublish <= 0 {
  100. req.IsPublicPublish = 1
  101. }
  102. reportInfo.IsPublicPublish = req.IsPublicPublish
  103. reportInfo.LastModifyAdminId = sysUser.AdminId
  104. reportInfo.LastModifyAdminName = sysUser.RealName
  105. reportInfo.ModifyTime = time.Now()
  106. state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperateEdit)
  107. if e != nil {
  108. err = errors.New("校验报告当前状态失败, Err: " + e.Error())
  109. return
  110. }
  111. reportInfo.State = state
  112. updateCols := []string{"Title", "Abstract", "Author", "Frequency", "CreateTime", "IsPublicPublish", "LastModifyAdminId", "LastModifyAdminName", "ModifyTime", "State"}
  113. if req.HeadResourceId > 0 {
  114. reportInfo.HeadResourceId = req.HeadResourceId
  115. updateCols = append(updateCols, "HeadResourceId")
  116. }
  117. if req.EndResourceId > 0 {
  118. reportInfo.EndResourceId = req.EndResourceId
  119. updateCols = append(updateCols, "EndResourceId")
  120. }
  121. addReportAdminList := make([]*report.ReportGrant, 0)
  122. delReportGrantIdList := make([]int, 0)
  123. {
  124. reportGrantObj := report.ReportGrant{}
  125. reportGrantList, tmpErr := reportGrantObj.GetGrantListById(reportInfo.Id)
  126. if tmpErr != nil {
  127. err = tmpErr
  128. return
  129. }
  130. currReportAdminMap := make(map[int]*report.ReportGrant)
  131. delReportAdminMap := make(map[int]*report.ReportGrant)
  132. for _, v := range reportGrantList {
  133. currReportAdminMap[v.AdminId] = v
  134. delReportAdminMap[v.AdminId] = v
  135. }
  136. for _, tmpAdminId := range req.GrantAdminIdList {
  137. _, ok := currReportAdminMap[tmpAdminId]
  138. if ok {
  139. delete(delReportAdminMap, tmpAdminId)
  140. continue
  141. }
  142. addReportAdminList = append(addReportAdminList, &report.ReportGrant{
  143. ReportId: reportInfo.Id,
  144. AdminId: tmpAdminId,
  145. CreateTime: time.Now(),
  146. })
  147. }
  148. for _, v := range delReportAdminMap {
  149. delReportGrantIdList = append(delReportGrantIdList, v.GrantId)
  150. }
  151. }
  152. err = models.EditReportAndPermission(reportInfo, updateCols, addReportAdminList, delReportGrantIdList)
  153. if err != nil {
  154. return
  155. }
  156. {
  157. minClassifyId, _, tmpErr := getMinClassify(reportInfo)
  158. if tmpErr != nil {
  159. return
  160. }
  161. go handleReportPermission(int64(reportInfo.Id), minClassifyId)
  162. }
  163. return
  164. }
  165. func getAddChapter(reportInfo *models.Report, minClassifyId, inheritReportId int, grantAdminIdList []int) (chapterList []models.AddReportChapter, allGrantUserList []*report.ReportGrant, err error, errMsg string) {
  166. chapterList = make([]models.AddReportChapter, 0)
  167. allGrantUserList = make([]*report.ReportGrant, 0)
  168. needAdminIdMap := make(map[int]bool)
  169. for _, adminId := range grantAdminIdList {
  170. needAdminIdMap[adminId] = true
  171. allGrantUserList = append(allGrantUserList, &report.ReportGrant{
  172. AdminId: adminId,
  173. CreateTime: time.Now(),
  174. })
  175. }
  176. if reportInfo.HasChapter != 1 {
  177. return
  178. }
  179. var minClassifyName string
  180. if reportInfo.ClassifyIdThird == minClassifyId {
  181. minClassifyName = reportInfo.ClassifyNameThird
  182. } else if reportInfo.ClassifyIdSecond == minClassifyId {
  183. minClassifyName = reportInfo.ClassifyNameSecond
  184. } else {
  185. minClassifyName = reportInfo.ClassifyNameFirst
  186. }
  187. errMsg = "生成报告章节失败"
  188. allTypeList, err := models.GetReportChapterTypeListByClassifyId(minClassifyId)
  189. if err != nil {
  190. err = errors.New(errMsg)
  191. return
  192. }
  193. chapterTypeList := make([]*models.ReportChapterType, 0)
  194. chapterTypeIdList := make([]int, 0)
  195. nowTime := time.Now().Local()
  196. for _, chapterType := range allTypeList {
  197. if chapterType.Enabled == 0 { //该章节已被永久禁用,那么就不允许继承或者新增该章节
  198. continue
  199. }
  200. if chapterType.PauseStartTime != "" && chapterType.PauseEndTime != "" && chapterType.PauseStartTime != utils.EmptyDateStr && chapterType.PauseEndTime != utils.EmptyDateStr {
  201. startTime, timeErr := time.ParseInLocation(utils.FormatDate, chapterType.PauseStartTime, time.Local)
  202. if timeErr != nil {
  203. utils.FileLog.Error("更新规则时间转换失败4001, Err: " + timeErr.Error())
  204. continue
  205. }
  206. endTime, timeErr := time.ParseInLocation(utils.FormatDate, chapterType.PauseEndTime, time.Local)
  207. if timeErr != nil {
  208. utils.FileLog.Error("更新规则时间转换失败4002, Err: " + timeErr.Error())
  209. continue
  210. }
  211. if nowTime.After(startTime) && nowTime.Before(endTime.AddDate(0, 0, 1)) {
  212. continue
  213. }
  214. }
  215. chapterTypeList = append(chapterTypeList, chapterType)
  216. chapterTypeIdList = append(chapterTypeIdList, chapterType.ReportChapterTypeId)
  217. }
  218. oldChapterIdGrantListMap := make(map[int][]*report.ReportChapterGrant)
  219. oldChapterPermissionListMap := make(map[int][]*report.ReportChapterPermissionMapping)
  220. customAddChapterList := make([]models.AddReportChapter, 0)
  221. inheritChapterMap := make(map[int]*models.ReportChapter)
  222. currChapterTypePermissionListMap := make(map[int][]*report.ReportChapterPermissionMapping)
  223. if len(chapterTypeIdList) > 0 {
  224. mappingList, e := models.GetChapterTypePermissionByChapterTypeIdList(chapterTypeIdList)
  225. if e != nil {
  226. err = fmt.Errorf("获取章节类型权限列表失败, Err: " + e.Error())
  227. return
  228. }
  229. hasPermissionMap := make(map[string]bool)
  230. for _, v := range mappingList {
  231. tmpChapterTypePermissionList, ok := currChapterTypePermissionListMap[v.ReportChapterTypeId]
  232. if !ok {
  233. tmpChapterTypePermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  234. }
  235. key := fmt.Sprint(v.ReportChapterTypeId, "-", v.ChartPermissionId)
  236. if _, has := hasPermissionMap[key]; !has {
  237. hasPermissionMap[key] = true
  238. currChapterTypePermissionListMap[v.ReportChapterTypeId] = append(tmpChapterTypePermissionList, &report.ReportChapterPermissionMapping{
  239. ReportChapterPermissionMappingId: 0,
  240. ReportChapterId: 0,
  241. ChartPermissionId: v.ChartPermissionId,
  242. CreateTime: time.Now(),
  243. })
  244. }
  245. }
  246. }
  247. if inheritReportId > 0 {
  248. inheritReportChapters, tmpErr := models.GetChapterListByReportId(inheritReportId)
  249. if tmpErr != nil {
  250. errMsg = "获取待继承的报告章节失败"
  251. err = tmpErr
  252. return
  253. }
  254. reportChaptersIdList := make([]int, 0)
  255. for _, v := range inheritReportChapters {
  256. reportChaptersIdList = append(reportChaptersIdList, v.ReportChapterId)
  257. }
  258. grantListMap := make(map[int][]*report.ReportChapterGrant)
  259. chapterPermissionListMap := make(map[int][]*report.ReportChapterPermissionMapping)
  260. if len(reportChaptersIdList) > 0 {
  261. obj := report.ReportChapterGrant{}
  262. grantList, tmpErr := obj.GetGrantListByIdList(reportChaptersIdList)
  263. if tmpErr != nil {
  264. errMsg = "获取待继承的报告章节的授权用户列表失败"
  265. err = tmpErr
  266. return
  267. }
  268. for _, v := range grantList {
  269. if _, ok := needAdminIdMap[v.AdminId]; !ok {
  270. continue
  271. }
  272. currReportChapterId := v.ReportChapterId
  273. tmpGrantList, ok := grantListMap[currReportChapterId]
  274. if !ok {
  275. tmpGrantList = make([]*report.ReportChapterGrant, 0)
  276. }
  277. v.ReportChapterId = 0
  278. v.GrantId = 0
  279. tmpGrantList = append(tmpGrantList, v)
  280. grantListMap[currReportChapterId] = tmpGrantList
  281. }
  282. permissionObj := report.ReportChapterPermissionMapping{}
  283. permissionList, tmpErr := permissionObj.GetPermissionListByIdList(reportChaptersIdList)
  284. if tmpErr != nil {
  285. errMsg = "获取待继承的报告章节的授权用户列表失败"
  286. err = tmpErr
  287. return
  288. }
  289. for _, v := range permissionList {
  290. currReportChapterId := v.ReportChapterId
  291. tmpPermissionList, ok := chapterPermissionListMap[currReportChapterId]
  292. if !ok {
  293. tmpPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  294. }
  295. v.ReportChapterId = 0
  296. v.ReportChapterPermissionMappingId = 0
  297. tmpPermissionList = append(tmpPermissionList, v)
  298. chapterPermissionListMap[currReportChapterId] = tmpPermissionList
  299. }
  300. }
  301. for i := 0; i < len(inheritReportChapters); i++ {
  302. customChapter := inheritReportChapters[i]
  303. tmpGrantList, ok := grantListMap[customChapter.ReportChapterId]
  304. if !ok {
  305. tmpGrantList = make([]*report.ReportChapterGrant, 0)
  306. }
  307. oldChapterIdGrantListMap[customChapter.ReportChapterId] = tmpGrantList
  308. chapterPermissionList, ok := chapterPermissionListMap[customChapter.ReportChapterId]
  309. if !ok {
  310. chapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  311. }
  312. oldChapterPermissionListMap[customChapter.ReportChapterId] = chapterPermissionList
  313. if customChapter.TypeId > 0 {
  314. inheritChapterMap[customChapter.TypeId] = customChapter
  315. continue
  316. }
  317. customChapter.ReportId = reportInfo.Id
  318. customChapter.ReportChapterId = 0
  319. customChapter.ClassifyIdFirst = minClassifyId
  320. customChapter.ClassifyNameFirst = minClassifyName
  321. customChapter.Stage = reportInfo.Stage
  322. customChapter.PublishState = 1
  323. customChapter.CreateTime = reportInfo.CreateTime
  324. customChapter.ModifyTime = time.Now()
  325. customChapter.LastModifyAdminId = reportInfo.LastModifyAdminId
  326. customChapter.LastModifyAdminName = reportInfo.LastModifyAdminName
  327. customChapter.ContentModifyTime = time.Now()
  328. customAddChapter := models.AddReportChapter{
  329. ReportChapter: customChapter,
  330. GrantList: tmpGrantList,
  331. GrantPermissionList: chapterPermissionList,
  332. }
  333. customAddChapterList = append(customAddChapterList, customAddChapter)
  334. }
  335. }
  336. var maxSort int
  337. for _, typeItem := range chapterTypeList {
  338. v, ok := inheritChapterMap[typeItem.ReportChapterTypeId]
  339. var tmpGrantList []*report.ReportChapterGrant
  340. var tmpChapterPermissionList []*report.ReportChapterPermissionMapping
  341. chapterItem := new(models.ReportChapter)
  342. if ok && v != nil {
  343. chapterItem.AddType = 2
  344. chapterItem.Title = v.Title
  345. chapterItem.ReportType = v.ReportType
  346. chapterItem.ClassifyIdFirst = minClassifyId
  347. chapterItem.ClassifyNameFirst = minClassifyName
  348. chapterItem.TypeId = typeItem.ReportChapterTypeId
  349. chapterItem.TypeName = typeItem.ReportChapterTypeName
  350. chapterItem.Content = v.Content
  351. chapterItem.ContentSub = v.ContentSub
  352. chapterItem.Stage = reportInfo.Stage
  353. chapterItem.PublishState = 1
  354. chapterItem.Sort = typeItem.Sort
  355. chapterItem.CreateTime = reportInfo.CreateTime
  356. chapterItem.ModifyTime = time.Now()
  357. chapterItem.LastModifyAdminId = reportInfo.LastModifyAdminId
  358. chapterItem.LastModifyAdminName = reportInfo.LastModifyAdminName
  359. chapterItem.ContentModifyTime = time.Now()
  360. chapterItem.ContentStruct = v.ContentStruct
  361. chapterItem.ReportLayout = v.ReportLayout
  362. chapterItem.ReportCreateTime = time.Now()
  363. tmpGrantList, ok = oldChapterIdGrantListMap[v.ReportChapterId]
  364. if !ok {
  365. tmpGrantList = make([]*report.ReportChapterGrant, 0)
  366. }
  367. tmpChapterPermissionList, ok = oldChapterPermissionListMap[v.ReportChapterId]
  368. if !ok {
  369. tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  370. }
  371. } else {
  372. chapterItem.AddType = 1
  373. chapterItem.Title = typeItem.ReportChapterTypeName
  374. chapterItem.ReportType = typeItem.ResearchType
  375. chapterItem.ClassifyIdFirst = minClassifyId
  376. chapterItem.ClassifyNameFirst = minClassifyName
  377. chapterItem.TypeId = typeItem.ReportChapterTypeId
  378. chapterItem.TypeName = typeItem.ReportChapterTypeName
  379. chapterItem.Stage = reportInfo.Stage
  380. chapterItem.PublishState = 1
  381. chapterItem.Sort = typeItem.Sort
  382. chapterItem.CreateTime = reportInfo.CreateTime
  383. chapterItem.ModifyTime = time.Now()
  384. chapterItem.LastModifyAdminId = reportInfo.LastModifyAdminId
  385. chapterItem.LastModifyAdminName = reportInfo.LastModifyAdminName
  386. chapterItem.ContentModifyTime = time.Now()
  387. chapterItem.ReportLayout = reportInfo.ReportLayout
  388. chapterItem.ReportCreateTime = time.Now()
  389. tmpChapterPermissionList, ok = currChapterTypePermissionListMap[typeItem.ReportChapterTypeId]
  390. if !ok {
  391. tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  392. }
  393. }
  394. if typeItem.Sort > maxSort {
  395. maxSort = typeItem.Sort
  396. }
  397. addChapter := models.AddReportChapter{
  398. ReportChapter: chapterItem,
  399. GrantList: tmpGrantList,
  400. GrantPermissionList: tmpChapterPermissionList,
  401. }
  402. chapterList = append(chapterList, addChapter)
  403. }
  404. for _, addChapterItem := range customAddChapterList {
  405. maxSort++
  406. addChapterItem.ReportChapter.Sort = maxSort
  407. chapterList = append(chapterList, addChapterItem)
  408. }
  409. return
  410. }
  411. func AddChapterBaseInfoAndPermission(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, permissionIdList []int, adminIdList []int) (err error, errMsg string) {
  412. errMsg = "新增失败"
  413. if reportInfo.State == 2 {
  414. errMsg = "该报告已发布,不允许编辑"
  415. err = errors.New(errMsg)
  416. return
  417. }
  418. addChapterAdminList := make([]*report.ReportChapterGrant, 0)
  419. for _, adminId := range adminIdList {
  420. addChapterAdminList = append(addChapterAdminList, &report.ReportChapterGrant{
  421. AdminId: adminId,
  422. CreateTime: time.Now(),
  423. })
  424. }
  425. addChapterPermissionList := make([]*report.ReportChapterPermissionMapping, 0)
  426. for _, permissionId := range permissionIdList {
  427. addChapterPermissionList = append(addChapterPermissionList, &report.ReportChapterPermissionMapping{
  428. ChartPermissionId: permissionId,
  429. CreateTime: time.Now(),
  430. })
  431. }
  432. err = models.AddChapterBaseInfoAndPermission(reportChapterInfo, addChapterAdminList, addChapterPermissionList)
  433. return
  434. }
  435. func EditChapterBaseInfoAndPermission(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, title string, permissionIdList []int, adminIdList []int) (err error, errMsg string) {
  436. errMsg = "修改失败"
  437. if reportInfo.State == 2 {
  438. errMsg = "该报告已发布,不允许编辑"
  439. err = errors.New(errMsg)
  440. return
  441. }
  442. updateCols := make([]string, 0)
  443. if title != `` {
  444. reportChapterInfo.Title = title
  445. reportChapterInfo.ModifyTime = time.Now()
  446. updateCols = append(updateCols, "Title", "ModifyTime")
  447. reportChapterInfo.UpdateChapter(updateCols)
  448. }
  449. reportGrantObj := report.ReportGrant{}
  450. chapterGrantObj := report.ReportChapterGrant{}
  451. chapterPermissionObj := report.ReportChapterPermissionMapping{}
  452. reportGrantAdminIdMap := make(map[int]bool)
  453. {
  454. reportGrantList, tmpErr := reportGrantObj.GetGrantListById(reportInfo.Id)
  455. if tmpErr != nil {
  456. err = tmpErr
  457. return
  458. }
  459. for _, v := range reportGrantList {
  460. reportGrantAdminIdMap[v.AdminId] = true
  461. }
  462. }
  463. addChapterAdminList := make([]*report.ReportChapterGrant, 0)
  464. delReportChapterGrantIdList := make([]int, 0)
  465. {
  466. chapterGrantList, tmpErr := chapterGrantObj.GetGrantListById(reportChapterInfo.ReportChapterId)
  467. if tmpErr != nil {
  468. err = tmpErr
  469. return
  470. }
  471. currChapterAdminMap := make(map[int]*report.ReportChapterGrant)
  472. delChapterAdminMap := make(map[int]*report.ReportChapterGrant)
  473. for _, v := range chapterGrantList {
  474. currChapterAdminMap[v.AdminId] = v
  475. delChapterAdminMap[v.AdminId] = v
  476. }
  477. for _, adminId := range adminIdList {
  478. if _, ok := reportGrantAdminIdMap[adminId]; !ok {
  479. continue
  480. }
  481. _, ok := currChapterAdminMap[adminId]
  482. if ok {
  483. delete(delChapterAdminMap, adminId)
  484. continue
  485. }
  486. addChapterAdminList = append(addChapterAdminList, &report.ReportChapterGrant{
  487. ReportChapterId: reportChapterInfo.ReportChapterId,
  488. AdminId: adminId,
  489. CreateTime: time.Now(),
  490. })
  491. }
  492. for _, v := range delChapterAdminMap {
  493. delReportChapterGrantIdList = append(delReportChapterGrantIdList, v.GrantId)
  494. }
  495. }
  496. otherChapterAdminMap := make(map[int]bool)
  497. {
  498. reportChapterIdList, tmpErr := models.GetReportChapterIdList(reportInfo.Id)
  499. if tmpErr != nil {
  500. err = tmpErr
  501. return
  502. }
  503. if len(reportChapterIdList) > 0 {
  504. list, tmpErr := chapterGrantObj.GetGrantListByIdList(reportChapterIdList)
  505. if tmpErr != nil {
  506. err = tmpErr
  507. return
  508. }
  509. for _, v := range list {
  510. if v.ReportChapterId == reportChapterInfo.ReportChapterId {
  511. continue
  512. }
  513. otherChapterAdminMap[v.AdminId] = true
  514. }
  515. }
  516. }
  517. addChapterPermissionList := make([]*report.ReportChapterPermissionMapping, 0)
  518. delChapterPermissionMappingIdList := make([]int, 0)
  519. {
  520. chapterPermissionList, tmpErr := chapterPermissionObj.GetPermissionListById(reportChapterInfo.ReportChapterId)
  521. if tmpErr != nil {
  522. err = tmpErr
  523. return
  524. }
  525. currChapterPermissionMap := make(map[int]*report.ReportChapterPermissionMapping)
  526. delChapterPermissionMap := make(map[int]*report.ReportChapterPermissionMapping)
  527. for _, v := range chapterPermissionList {
  528. currChapterPermissionMap[v.ChartPermissionId] = v
  529. delChapterPermissionMap[v.ChartPermissionId] = v
  530. }
  531. }
  532. err = models.EditChapterBaseInfoAndPermission(reportInfo, reportChapterInfo, updateCols, addChapterAdminList, addChapterPermissionList, delReportChapterGrantIdList, delChapterPermissionMappingIdList)
  533. return
  534. }
  535. func DelChapter(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, sysUser *system.Admin) (err error, errMsg string) {
  536. errMsg = "删除失败"
  537. if reportInfo.State == 2 {
  538. errMsg = "该报告已发布,不允许删除"
  539. err = errors.New(errMsg)
  540. return
  541. }
  542. reportInfo.LastModifyAdminId = sysUser.AdminId
  543. reportInfo.LastModifyAdminName = sysUser.RealName
  544. reportInfo.ModifyTime = time.Now()
  545. updateReportCols := []string{"LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}
  546. err = models.DelChapterAndPermission(reportInfo, updateReportCols, reportChapterInfo)
  547. return
  548. }
  549. func DownloadVoice(reportInfo *models.ReportDetail) (savePath, fileName string, err error, errMsg string) {
  550. errMsg = `下载失败`
  551. if reportInfo.VideoUrl != `` {
  552. savePath = time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
  553. fileName = reportInfo.VideoName + ".mp3"
  554. fileBody, tmpErr := http.Get(reportInfo.VideoUrl)
  555. if tmpErr != nil {
  556. err = tmpErr
  557. errMsg = "音频下载失败"
  558. return
  559. }
  560. err = file.SaveFile(fileBody, savePath)
  561. if err != nil {
  562. errMsg = "音频保存失败"
  563. return
  564. }
  565. }
  566. if reportInfo.HasChapter == 1 {
  567. videoList, tmpErr := models.GetReportChapterVideoList(reportInfo.Id)
  568. if tmpErr != nil {
  569. err = tmpErr
  570. errMsg = "获取音频列表失败"
  571. return
  572. }
  573. if len(videoList) > 0 {
  574. savePath = time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  575. fileName = reportInfo.VideoName + ".zip"
  576. zipFile, tmpErr := os.Create(savePath)
  577. if tmpErr != nil {
  578. err = tmpErr
  579. return
  580. }
  581. zipWriter := zip.NewWriter(zipFile)
  582. defer func() {
  583. if err != nil {
  584. zipWriter.Close()
  585. zipFile.Close()
  586. }
  587. }()
  588. for i := 0; i < len(videoList); i++ {
  589. item := videoList[i]
  590. if item.VideoName == "" || item.VideoUrl == "" {
  591. continue
  592. }
  593. ext := path.Ext(item.VideoUrl)
  594. ioWriter, tmpErr := zipWriter.Create(fmt.Sprintf("%s%s", item.VideoName, ext))
  595. if tmpErr != nil {
  596. err = tmpErr
  597. if os.IsPermission(err) {
  598. fmt.Println("权限不足: ", err)
  599. return
  600. }
  601. return
  602. }
  603. var content []byte
  604. content, err = http.Get(item.VideoUrl)
  605. if err != nil {
  606. content = []byte("")
  607. }
  608. ioWriter.Write(content)
  609. }
  610. zipWriter.Close()
  611. zipFile.Close()
  612. }
  613. }
  614. return
  615. }
  616. func CheckReportAuthByAdminIdList(adminId, createAdminId int, grantAdminIdList []int) (isAuth bool) {
  617. if adminId == createAdminId {
  618. isAuth = true
  619. return
  620. }
  621. if utils.IsCheckInList(grantAdminIdList, adminId) {
  622. isAuth = true
  623. return
  624. }
  625. return
  626. }
  627. func CheckReportAuthByReportChapterInfo(adminId, createAdminId int, reportInfoId int) (isAuth bool, err error) {
  628. if adminId == createAdminId {
  629. isAuth = true
  630. return
  631. }
  632. obj := report.ReportGrant{}
  633. chapterGrantList, err := obj.GetGrantListById(reportInfoId)
  634. if err != nil {
  635. return
  636. }
  637. for _, v := range chapterGrantList {
  638. if v.AdminId == adminId {
  639. isAuth = true
  640. return
  641. }
  642. }
  643. return
  644. }
  645. func EditReportLayoutImg(reportInfo *models.Report, req models.EditLayoutImgReq, sysUser *system.Admin) (err error, errMsg string) {
  646. errMsg = `保存失败`
  647. reportInfo.HeadResourceId = req.HeadResourceId
  648. reportInfo.HeadImg = req.HeadImg
  649. reportInfo.EndResourceId = req.EndResourceId
  650. reportInfo.EndImg = req.EndImg
  651. reportInfo.CanvasColor = req.CanvasColor
  652. reportInfo.LastModifyAdminId = sysUser.AdminId
  653. reportInfo.LastModifyAdminName = sysUser.RealName
  654. reportInfo.ModifyTime = time.Now()
  655. updateCols := []string{"HeadResourceId", "HeadImg", "EndResourceId", "EndImg", "CanvasColor", "LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}
  656. err = reportInfo.UpdateReport(updateCols)
  657. if err != nil {
  658. return
  659. }
  660. return
  661. }
  662. func PublishReport(reportId int, reportUrl string, sysUser *system.Admin) (tips string, err error, errMsg string) {
  663. errMsg = `报告发布失败`
  664. reportInfo, err := models.GetReportByReportId(reportId)
  665. if err != nil {
  666. errMsg = "获取报告信息失败"
  667. return
  668. }
  669. if reportInfo == nil {
  670. errMsg = "获取报告信息失败"
  671. err = errors.New(errMsg)
  672. return
  673. }
  674. var publishTime time.Time
  675. if reportInfo.MsgIsSend == 1 && reportInfo.PublishTime.IsZero() { //如果报告曾经发布过,并且已经发送过模版消息,则章节的发布时间为报告的发布时间
  676. publishTime = reportInfo.PublishTime
  677. } else {
  678. publishTime = time.Now()
  679. }
  680. var tmpErr error
  681. if reportInfo.HasChapter == 1 {
  682. tips, tmpErr, errMsg = PublishChapterReport(reportInfo, reportUrl, sysUser)
  683. if tmpErr != nil {
  684. err = errors.New("晨周报发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(reportId))
  685. }
  686. return
  687. }
  688. if reportInfo.Content == "" {
  689. errMsg = `报告内容为空,不可发布`
  690. err = errors.New("报告内容为空,不需要生成,report_id:" + strconv.Itoa(reportId))
  691. return
  692. }
  693. state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperatePublish)
  694. if e != nil {
  695. errMsg = "操作失败"
  696. err = errors.New("校验报告当前状态失败, Err: " + e.Error())
  697. return
  698. }
  699. if state == models.ReportStatePublished {
  700. if tmpErr = models.PublishReportById(reportId, publishTime, sysUser.AdminId, sysUser.RealName); tmpErr != nil {
  701. err = errors.New("报告发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(reportId))
  702. return
  703. }
  704. go func() {
  705. if reportInfo.VideoUrl == "" {
  706. go UpdateReportVideo(reportInfo)
  707. }
  708. _ = UpdateReportEs(reportId, 2)
  709. }()
  710. } else {
  711. if e = models.ResetReportById(reportId, state, sysUser.AdminId, sysUser.RealName); e != nil {
  712. errMsg = "操作失败"
  713. err = fmt.Errorf("重置报告状态失败, Err: %s, ReportId: %d", e.Error(), reportId)
  714. return
  715. }
  716. }
  717. recordItem := &models.ReportStateRecord{
  718. ReportId: reportId,
  719. ReportType: 1,
  720. State: state,
  721. AdminId: sysUser.AdminId,
  722. AdminName: sysUser.AdminName,
  723. CreateTime: time.Now(),
  724. }
  725. go func() {
  726. _, _ = models.AddReportStateRecord(recordItem)
  727. }()
  728. {
  729. reportPdfUrl := GetGeneralPdfUrl(reportInfo.ReportCode, reportInfo.ClassifyNameFirst, reportInfo.ReportLayout)
  730. go Report2pdfAndJpeg(reportPdfUrl, reportId, 1)
  731. }
  732. {
  733. minClassifyId, _, tmpErr := getMinClassify(reportInfo)
  734. if tmpErr != nil {
  735. return
  736. }
  737. go handleReportPermission(int64(reportInfo.Id), minClassifyId)
  738. }
  739. return
  740. }
  741. func PublishChapterReport(reportInfo *models.Report, reportUrl string, sysUser *system.Admin) (tips string, err error, errMsg string) {
  742. reportId := reportInfo.Id
  743. if reportInfo.State == 2 {
  744. return
  745. }
  746. chapters, err := models.GetChapterListByReportId(reportId)
  747. if err != nil {
  748. return
  749. }
  750. chapterLen := len(chapters)
  751. if chapterLen <= 0 {
  752. tips = "报告章节为空,不可发布"
  753. errMsg = tips
  754. err = errors.New(tips)
  755. return
  756. }
  757. chapterList, err := models.GetChapterListByReportId(reportId)
  758. if err != nil {
  759. return
  760. }
  761. for _, chapter := range chapterList {
  762. if chapter.PublishState == 1 {
  763. tips = "还存在未发布的章节"
  764. errMsg = tips
  765. err = errors.New(tips)
  766. return
  767. }
  768. }
  769. state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperatePublish)
  770. if e != nil {
  771. err = errors.New("校验报告当前状态失败, Err: " + e.Error())
  772. return
  773. }
  774. if state != models.ReportStatePublished {
  775. if e = models.ResetReportById(reportId, state, sysUser.AdminId, sysUser.RealName); e != nil {
  776. err = fmt.Errorf("重置报告状态失败, Err: %s, ReportId: %d", e.Error(), reportId)
  777. return
  778. }
  779. return
  780. }
  781. updateCols := make([]string, 0)
  782. reportInfo.LastModifyAdminId = sysUser.AdminId
  783. reportInfo.LastModifyAdminName = sysUser.RealName
  784. updateCols = append(updateCols, "LastModifyAdminId", "LastModifyAdminName", "Title", "State", "ModifyTime")
  785. reportInfo.State = state
  786. if reportInfo.MsgIsSend == 0 || reportInfo.PublishTime.IsZero() {
  787. reportInfo.PublishTime = time.Now().Local()
  788. updateCols = append(updateCols, "PublishTime")
  789. }
  790. reportInfo.ModifyTime = time.Now().Local()
  791. if e := models.PublishReportAndChapter(reportInfo, true, updateCols); e != nil {
  792. err = errors.New("发布报告及章节失败")
  793. return
  794. }
  795. go UpdateReportVideo(reportInfo)
  796. go func() {
  797. _ = UpdateReportEs(reportInfo.Id, 2)
  798. }()
  799. go SaveReportLogs(reportInfo, chapters, reportInfo.AdminId, reportInfo.AdminRealName)
  800. {
  801. reportPdfUrl := GetGeneralPdfUrl(reportInfo.ReportCode, reportInfo.ClassifyNameFirst, reportInfo.ReportLayout)
  802. go Report2pdfAndJpeg(reportPdfUrl, reportId, 1)
  803. }
  804. return
  805. }
  806. func DeleteReportAndChapter(reportId int) (err error) {
  807. reportInfo, err := models.GetReportByReportId(reportId)
  808. if err != nil {
  809. err = errors.New("报告信息有误, Err: " + err.Error())
  810. return
  811. }
  812. if reportInfo.State == 2 {
  813. err = errors.New("报告已发布,不可删除")
  814. return
  815. }
  816. _ = UpdateReportEs(reportId, 1)
  817. if reportInfo.HasChapter == 1 {
  818. err = models.DeleteDayWeekReportAndChapter(reportId)
  819. } else {
  820. err = models.DeleteReport(reportId)
  821. }
  822. if err != nil {
  823. err = errors.New("删除失败, Err: " + err.Error())
  824. return
  825. }
  826. go func() {
  827. _ = ResetPPTReport(reportId, false)
  828. }()
  829. return
  830. }
  831. func getMinClassify(reportInfo *models.Report) (minClassifyId int, minClassifyName string, err error) {
  832. defer func() {
  833. if err != nil {
  834. utils.FileLog.Error("获取最小分类ID失败,报告ID:%d,Err:%s", reportInfo.Id, err.Error())
  835. }
  836. }()
  837. minClassifyId = reportInfo.ClassifyIdThird
  838. minClassifyName = reportInfo.ClassifyNameThird
  839. if minClassifyId <= 0 {
  840. minClassifyId = reportInfo.ClassifyIdSecond
  841. minClassifyName = reportInfo.ClassifyNameSecond
  842. }
  843. if minClassifyId <= 0 {
  844. minClassifyId = reportInfo.ClassifyIdFirst
  845. minClassifyName = reportInfo.ClassifyNameFirst
  846. }
  847. if minClassifyId <= 0 {
  848. err = errors.New("分类异常")
  849. }
  850. return
  851. }
  852. func handleReportPermission(reportId int64, minClassifyId int) {
  853. var err error
  854. defer func() {
  855. if err != nil {
  856. utils.FileLog.Error("报告权限处理失败,报告ID:%d,分类ID:%d,Err:%s", reportId, minClassifyId, err.Error())
  857. }
  858. }()
  859. err = models.RemoveChartPermissionChapterMapping(reportId)
  860. if err != nil {
  861. err = errors.New("修改删除报告权限失败,Err:" + err.Error())
  862. return
  863. }
  864. permissionItems, err := models.GetPermission(minClassifyId)
  865. if err != nil {
  866. err = errors.New("获取权限失败,Err:" + err.Error())
  867. return
  868. }
  869. for _, v := range permissionItems {
  870. err = models.AddChartPermissionChapterMapping(v.ChartPermissionId, reportId)
  871. if err != nil {
  872. err = errors.New("新增权限失败,Err:" + err.Error())
  873. return
  874. }
  875. }
  876. _ = EditReportPermissionSync(reportId, minClassifyId)
  877. return
  878. }
  879. func HandleReportPermission(reportInfo *models.Report) {
  880. minClassifyId, _, tmpErr := getMinClassify(reportInfo)
  881. if tmpErr != nil {
  882. return
  883. }
  884. handleReportPermission(int64(reportInfo.Id), minClassifyId)
  885. }
  886. func UpdateReportVideo(reportInfo *models.Report) {
  887. if reportInfo.HasChapter == 1 {
  888. reportChapterIdList := make([]int, 0)
  889. chapterList, err := models.GetChapterListByReportId(reportInfo.Id)
  890. if err != nil {
  891. return
  892. }
  893. reportContent := ""
  894. for _, chapter := range chapterList {
  895. if chapter.PublishState == 2 {
  896. reportChapterIdList = append(reportChapterIdList, chapter.ReportChapterId)
  897. reportContent += chapter.Title + `。`
  898. reportContent += chapter.Content + `。`
  899. }
  900. }
  901. if len(reportChapterIdList) > 0 {
  902. err = UpdateChaptersVideo(reportChapterIdList)
  903. if err != nil {
  904. utils.FileLog.Info(fmt.Sprintf("%d报告音频生产失败:%s", reportInfo.Id, err.Error()))
  905. }
  906. }
  907. {
  908. if reportInfo.VideoUrl != "" && reportInfo.VideoName != "" && reportInfo.VideoSize != "" && reportInfo.VideoPlaySeconds != "" {
  909. return
  910. }
  911. videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(reportInfo.Title, html.UnescapeString(reportContent), time.Now().Format(utils.FormatDateTime))
  912. if e != nil {
  913. err = e
  914. return
  915. }
  916. err = models.ModifyReportVideoByNoVideo(reportInfo.Id, videoUrl, videoName, videoSize, videoPlaySeconds)
  917. }
  918. } else {
  919. err := CreateVideo(reportInfo)
  920. if err != nil {
  921. utils.FileLog.Info(fmt.Sprintf("%d报告音频生产失败:%s", reportInfo.Id, err.Error()))
  922. }
  923. }
  924. }
  925. func GetGeneralPdfUrl(reportCode, classifyFirstName string, reportLayout int8) (pdfUrl string) {
  926. conf, e := models.GetBusinessConfByKey("ReportViewUrl")
  927. if e != nil {
  928. return
  929. }
  930. switch reportLayout {
  931. case 1:
  932. pdfUrl = fmt.Sprintf("%s/reportshare_pdf?code=%s", conf.ConfVal, reportCode)
  933. case 2:
  934. pdfUrl = fmt.Sprintf("%s/reportshare_smart_pdf?code=%s", conf.ConfVal, reportCode)
  935. }
  936. return
  937. }
  938. func GetReportWaterMarkPdf(reportInfo *models.Report, sysUser *system.Admin) {
  939. dateDir := time.Now().Format("20060102")
  940. uploadDir := utils.STATIC_DIR + "pdf/" + dateDir
  941. filePath, err := utils.DownloadFile(reportInfo.DetailPdfUrl, uploadDir)
  942. if err != nil {
  943. return
  944. }
  945. defer func() {
  946. _ = os.Remove(filePath)
  947. }()
  948. waterMarkStr := fmt.Sprintf("%s - %s", sysUser.RealName, sysUser.Mobile)
  949. GeneralWaterMarkPdf(filePath, waterMarkStr)
  950. }