report_v2.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. package services
  2. import (
  3. "archive/zip"
  4. "errors"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/company"
  7. "eta/eta_api/models/report"
  8. "eta/eta_api/models/report_approve"
  9. "eta/eta_api/models/system"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "github.com/rdlucklib/rdluck_tools/file"
  13. "github.com/rdlucklib/rdluck_tools/http"
  14. "html"
  15. "os"
  16. "path"
  17. "strconv"
  18. "time"
  19. )
  20. // AddReportAndChapter
  21. // @Description: 新增报告(包含章节)
  22. // @author: Roc
  23. // @datetime 2024-06-04 11:23:20
  24. // @param reportInfo *models.Report
  25. // @param inheritReportId int
  26. // @return err error
  27. // @return errMsg string
  28. func AddReportAndChapter(reportInfo *models.Report, inheritReportId int, grantAdminIdList []int, reportDate time.Time) (err error, errMsg string) {
  29. // 根据审批开关及审批流判断当前报告状态
  30. state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperateAdd)
  31. if e != nil {
  32. //errMsg = "操作失败"
  33. err = errors.New("校验报告当前状态失败, Err: " + e.Error())
  34. return
  35. }
  36. // 报告状态
  37. reportInfo.State = state
  38. // 获取最小分类id
  39. minClassifyId := reportInfo.ClassifyIdThird
  40. if minClassifyId <= 0 {
  41. minClassifyId = reportInfo.ClassifyIdSecond
  42. }
  43. if minClassifyId <= 0 {
  44. minClassifyId = reportInfo.ClassifyIdFirst
  45. }
  46. if minClassifyId <= 0 {
  47. errMsg = "分类异常"
  48. err = errors.New(errMsg)
  49. return
  50. }
  51. errMsg = "生成报告失败"
  52. // 报告继承
  53. if inheritReportId > 0 {
  54. inheritReport, tmpErr := models.GetReportByReportId(inheritReportId)
  55. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  56. errMsg = "获取待继承的报告失败"
  57. err = tmpErr
  58. return
  59. }
  60. if inheritReport != nil {
  61. // 判断当前的报告分类与继承的报告分类是否一致
  62. if inheritReport.ClassifyIdFirst != reportInfo.ClassifyIdFirst || inheritReport.ClassifyIdSecond != reportInfo.ClassifyIdSecond || inheritReport.ClassifyIdThird != reportInfo.ClassifyIdThird {
  63. errMsg = "分类异常,与继承的报告分类不一致"
  64. err = errors.New(errMsg)
  65. return
  66. }
  67. reportInfo.ChapterType = inheritReport.ChapterType
  68. reportInfo.Content = inheritReport.Content
  69. reportInfo.ContentSub = inheritReport.ContentSub
  70. reportInfo.ContentStruct = inheritReport.ContentStruct
  71. reportInfo.CanvasColor = inheritReport.CanvasColor
  72. //reportInfo.NeedSplice = inheritReport.NeedSplice
  73. reportInfo.NeedSplice = 1
  74. reportInfo.HeadResourceId = inheritReport.HeadResourceId
  75. //reportInfo.HeadImg = inheritReport.HeadImg
  76. //reportInfo.EndImg = inheritReport.EndImg
  77. reportInfo.HeadImg = ``
  78. reportInfo.EndImg = ``
  79. reportInfo.EndResourceId = inheritReport.EndResourceId
  80. if inheritReport.HeadResourceId > 0 {
  81. reportInfo.HeadImg = inheritReport.HeadImg
  82. }
  83. if inheritReport.EndResourceId > 0 {
  84. reportInfo.EndImg = inheritReport.EndImg
  85. }
  86. reportInfo.InheritReportId = inheritReport.Id
  87. }
  88. }
  89. // 获取待生成的报告章节
  90. addChapterList, allGrantUserList, err, errMsg := getAddChapter(reportInfo, minClassifyId, inheritReportId, grantAdminIdList, reportDate)
  91. // 新增报告及章节
  92. var reportId int64
  93. reportId, err = models.AddReportAndChapter(reportInfo, allGrantUserList, addChapterList)
  94. if err != nil {
  95. err = errors.New("新增报告及章节失败, Err: " + err.Error())
  96. return
  97. }
  98. reportCode := utils.MD5(strconv.Itoa(reportInfo.Id))
  99. reportInfo.ReportCode = reportCode
  100. // 修改唯一编码
  101. {
  102. go models.ModifyReportCode(reportId, reportCode)
  103. }
  104. // 报告权限处理
  105. go handleReportPermission(reportId, minClassifyId)
  106. return
  107. }
  108. // EditReport
  109. // @Description: 修改报告的基础信息、授权用户权限
  110. // @author: Roc
  111. // @datetime 2024-06-06 17:16:58
  112. // @param reportInfo *models.Report
  113. // @param req models.EditReq
  114. // @param sysUser *system.Admin
  115. // @return err error
  116. // @return errMsg string
  117. func EditReport(reportInfo *models.Report, req models.EditReq, sysUser *system.Admin) (err error, errMsg string) {
  118. errMsg = `保存失败`
  119. //var stage int
  120. //if reportInfo.ClassifyNameFirst != req.ClassifyNameFirst || reportInfo.ClassifyNameSecond != req.ClassifyNameSecond {
  121. // // 报告期数
  122. // maxStage, _ := models.GetReportStageEdit(req.ClassifyIdFirst, req.ClassifyIdSecond, req.ClassifyIdThird, int(req.ReportId))
  123. // maxStage = maxStage + 1
  124. // stage = maxStage
  125. //} else {
  126. // stage = reportInfo.Stage
  127. //}
  128. //if req.State != reportInfo.State {
  129. // recordItem := &models.ReportStateRecord{
  130. // ReportId: int(req.ReportId),
  131. // ReportType: 1,
  132. // State: req.State,
  133. // AdminId: this.SysUser.AdminId,
  134. // AdminName: this.SysUser.AdminName,
  135. // CreateTime: time.Now(),
  136. // }
  137. // go func() {
  138. // _, _ = models.AddReportStateRecord(recordItem)
  139. // }()
  140. //}
  141. //item := new(models.Report)
  142. //reportInfo.ClassifyIdFirst = req.ClassifyIdFirst
  143. //reportInfo.ClassifyNameFirst = req.ClassifyNameFirst
  144. //reportInfo.ClassifyIdSecond = req.ClassifyIdSecond
  145. //reportInfo.ClassifyNameSecond = req.ClassifyNameSecond
  146. //reportInfo.ClassifyIdThird = req.ClassifyIdThird
  147. //reportInfo.ClassifyNameThird = req.ClassifyNameThird
  148. reportInfo.Title = req.Title
  149. reportInfo.Abstract = req.Abstract
  150. reportInfo.Author = req.Author
  151. reportInfo.Frequency = req.Frequency
  152. //reportInfo.State = reportInfo.State // 编辑不变更状态
  153. //reportInfo.Stage = stage // 编辑不变更期数
  154. //reportInfo.Content = html.EscapeString(req.Content) // 编辑不变更具体内容
  155. //reportInfo.ContentSub = html.EscapeString(contentSub) // 编辑不变更具体内容
  156. reportInfo.CreateTime = req.CreateTime
  157. //reportInfo.CollaborateType = req.CollaborateType
  158. //reportInfo.ReportLayout = req.ReportLayout
  159. if req.IsPublicPublish <= 0 {
  160. req.IsPublicPublish = 1
  161. }
  162. reportInfo.IsPublicPublish = req.IsPublicPublish
  163. reportInfo.LastModifyAdminId = sysUser.AdminId
  164. reportInfo.LastModifyAdminName = sysUser.RealName
  165. reportInfo.ModifyTime = time.Now()
  166. state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperateEdit)
  167. if e != nil {
  168. //errMsg = "操作失败"
  169. err = errors.New("校验报告当前状态失败, Err: " + e.Error())
  170. return
  171. }
  172. // 报告状态
  173. reportInfo.State = state
  174. //updateCols := []string{"ClassifyIdFirst", "ClassifyNameFirst", "ClassifyIdSecond", "ClassifyNameSecond", "ClassifyIdThird", "ClassifyNameThird", "Title", "Abstract", "Author", "Frequency", "Stage", "CreateTime", "IsPublicPublish", "LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}
  175. updateCols := []string{"Title", "Abstract", "Author", "Frequency", "CreateTime", "IsPublicPublish", "LastModifyAdminId", "LastModifyAdminName", "ModifyTime", "State"}
  176. if req.HeadResourceId > 0 {
  177. reportInfo.HeadResourceId = req.HeadResourceId
  178. updateCols = append(updateCols, "HeadResourceId")
  179. }
  180. if req.EndResourceId > 0 {
  181. reportInfo.EndResourceId = req.EndResourceId
  182. updateCols = append(updateCols, "EndResourceId")
  183. }
  184. // 需要添加的报告授权数据
  185. addReportAdminList := make([]*report.ReportGrant, 0)
  186. // 待移除的报告授权数据id
  187. delReportGrantIdList := make([]int, 0)
  188. // 处理当前报告需要新增/移除的授权信息
  189. {
  190. reportGrantObj := report.ReportGrant{}
  191. // 获取当前报告已经授权的用户信息
  192. reportGrantList, tmpErr := reportGrantObj.GetGrantListById(reportInfo.Id)
  193. if tmpErr != nil {
  194. err = tmpErr
  195. return
  196. }
  197. // 当前报告已经授权的用户信息
  198. currReportAdminMap := make(map[int]*report.ReportGrant)
  199. // 需要删除的报告授权数据
  200. delReportAdminMap := make(map[int]*report.ReportGrant)
  201. for _, v := range reportGrantList {
  202. currReportAdminMap[v.AdminId] = v
  203. delReportAdminMap[v.AdminId] = v
  204. }
  205. // 先看需要新增哪些用户
  206. for _, tmpAdminId := range req.GrantAdminIdList {
  207. _, ok := currReportAdminMap[tmpAdminId]
  208. // 如果章节中需要新增的用户 已经在 报告授权用户里面,那么就忽略,可以不用新增了
  209. if ok {
  210. delete(delReportAdminMap, tmpAdminId)
  211. continue
  212. }
  213. // 如果不存在,那么就新增授权
  214. addReportAdminList = append(addReportAdminList, &report.ReportGrant{
  215. //GrantId: 0,
  216. ReportId: reportInfo.Id,
  217. AdminId: tmpAdminId,
  218. CreateTime: time.Now(),
  219. })
  220. }
  221. // 查出需要移除的授权id
  222. for _, v := range delReportAdminMap {
  223. delReportGrantIdList = append(delReportGrantIdList, v.GrantId)
  224. }
  225. }
  226. // 修改报告的基本信息,以及报告的授权用户
  227. err = models.EditReportAndPermission(reportInfo, updateCols, addReportAdminList, delReportGrantIdList)
  228. if err != nil {
  229. return
  230. }
  231. // 报告权限处理
  232. {
  233. minClassifyId, _, tmpErr := getMinClassify(reportInfo)
  234. if tmpErr != nil {
  235. return
  236. }
  237. go handleReportPermission(int64(reportInfo.Id), minClassifyId)
  238. }
  239. return
  240. }
  241. // getAddChapter
  242. // @Description: 获取待新增的报告章节列表
  243. // @author: Roc
  244. // @datetime 2024-06-04 13:10:58
  245. // @param reportInfo *models.Report
  246. // @param minClassifyId int
  247. // @param inheritReportId int
  248. // @param grantAdminIdList []int
  249. // @return chapterList []*models.ReportChapter
  250. // @return err error
  251. // @return errMsg string
  252. func getAddChapter(reportInfo *models.Report, minClassifyId, inheritReportId int, grantAdminIdList []int, reportDate time.Time) (chapterList []models.AddReportChapter, allGrantUserList []*report.ReportGrant, err error, errMsg string) {
  253. // 待生成的报告章节内容
  254. chapterList = make([]models.AddReportChapter, 0)
  255. // 所有的授权用户
  256. allGrantUserList = make([]*report.ReportGrant, 0)
  257. // 报告授权的用户ID
  258. needAdminIdMap := make(map[int]bool)
  259. for _, adminId := range grantAdminIdList {
  260. needAdminIdMap[adminId] = true
  261. allGrantUserList = append(allGrantUserList, &report.ReportGrant{
  262. //GrantId: 0,
  263. //ReportId: 0,
  264. AdminId: adminId,
  265. CreateTime: time.Now(),
  266. })
  267. }
  268. if reportInfo.HasChapter != 1 {
  269. return
  270. }
  271. // 最小单元的分类
  272. var minClassifyName string
  273. if reportInfo.ClassifyIdThird == minClassifyId {
  274. minClassifyName = reportInfo.ClassifyNameThird
  275. } else if reportInfo.ClassifyIdSecond == minClassifyId {
  276. minClassifyName = reportInfo.ClassifyNameSecond
  277. } else {
  278. minClassifyName = reportInfo.ClassifyNameFirst
  279. }
  280. errMsg = "生成报告章节失败"
  281. // 章节类型列表
  282. allTypeList, err := models.GetReportChapterTypeListByClassifyId(minClassifyId)
  283. if err != nil {
  284. err = errors.New(errMsg)
  285. return
  286. }
  287. // 待添加的章节
  288. chapterTypeList := make([]*models.ReportChapterType, 0)
  289. // 待添加的章节类型id列表
  290. chapterTypeIdList := make([]int, 0)
  291. //nowTime := time.Now().Local()
  292. for _, chapterType := range allTypeList {
  293. // 如果被永久暂停更新了
  294. if chapterType.Enabled == 0 { //该章节已被永久禁用,那么就不允许继承或者新增该章节
  295. continue
  296. }
  297. if chapterType.PauseStartTime != "" && chapterType.PauseEndTime != "" && chapterType.PauseStartTime != utils.EmptyDateStr && chapterType.PauseEndTime != utils.EmptyDateStr {
  298. startTime, timeErr := time.ParseInLocation(utils.FormatDate, chapterType.PauseStartTime, time.Local)
  299. if timeErr != nil {
  300. utils.FileLog.Error("更新规则时间转换失败4001, Err: " + timeErr.Error())
  301. continue
  302. }
  303. endTime, timeErr := time.ParseInLocation(utils.FormatDate, chapterType.PauseEndTime, time.Local)
  304. if timeErr != nil {
  305. utils.FileLog.Error("更新规则时间转换失败4002, Err: " + timeErr.Error())
  306. continue
  307. }
  308. // 暂停更新(此处用报告日期去判断是否需要停更,而不是报告创建日期)
  309. if reportDate.After(startTime) && reportDate.Before(endTime.AddDate(0, 0, 1)) {
  310. continue
  311. }
  312. }
  313. // 正常的章节类型状态,那么应该自动创建该章节
  314. chapterTypeList = append(chapterTypeList, chapterType)
  315. chapterTypeIdList = append(chapterTypeIdList, chapterType.ReportChapterTypeId)
  316. }
  317. // 待继承的章节类id的授权用户
  318. oldChapterIdGrantListMap := make(map[int][]*report.ReportChapterGrant)
  319. // 待继承的章节类id关联的品种列表
  320. oldChapterPermissionListMap := make(map[int][]*report.ReportChapterPermissionMapping)
  321. // 自定义章节列表
  322. customAddChapterList := make([]models.AddReportChapter, 0)
  323. // 报告继承
  324. inheritChapterMap := make(map[int]*models.ReportChapter)
  325. // 当前分类下配置的章节类型id所关联的品种列表(默认配置,不是从继承报告里面获取的,如果有继承章节,那么需要从继承报告里面获取)
  326. currChapterTypePermissionListMap := make(map[int][]*report.ReportChapterPermissionMapping)
  327. if len(chapterTypeIdList) > 0 {
  328. mappingList, e := models.GetChapterTypePermissionByChapterTypeIdList(chapterTypeIdList)
  329. if e != nil {
  330. err = fmt.Errorf("获取章节类型权限列表失败, Err: " + e.Error())
  331. return
  332. }
  333. hasPermissionMap := make(map[string]bool)
  334. for _, v := range mappingList {
  335. tmpChapterTypePermissionList, ok := currChapterTypePermissionListMap[v.ReportChapterTypeId]
  336. if !ok {
  337. tmpChapterTypePermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  338. }
  339. key := fmt.Sprint(v.ReportChapterTypeId, "-", v.ChartPermissionId)
  340. if _, has := hasPermissionMap[key]; !has {
  341. hasPermissionMap[key] = true
  342. currChapterTypePermissionListMap[v.ReportChapterTypeId] = append(tmpChapterTypePermissionList, &report.ReportChapterPermissionMapping{
  343. ReportChapterPermissionMappingId: 0,
  344. ReportChapterId: 0,
  345. ChartPermissionId: v.ChartPermissionId,
  346. CreateTime: time.Now(),
  347. })
  348. }
  349. }
  350. }
  351. if inheritReportId > 0 {
  352. // 继承待继承的报告章节内容
  353. inheritReportChapters, tmpErr := models.GetChapterListByReportId(inheritReportId)
  354. if tmpErr != nil {
  355. errMsg = "获取待继承的报告章节失败"
  356. err = tmpErr
  357. return
  358. }
  359. reportChaptersIdList := make([]int, 0)
  360. for _, v := range inheritReportChapters {
  361. reportChaptersIdList = append(reportChaptersIdList, v.ReportChapterId)
  362. }
  363. // 继承的报告章节用户map
  364. grantListMap := make(map[int][]*report.ReportChapterGrant)
  365. // 继承的报告章节的关联品种map
  366. chapterPermissionListMap := make(map[int][]*report.ReportChapterPermissionMapping)
  367. // 授权数据列表
  368. if len(reportChaptersIdList) > 0 {
  369. // 授权用户数据
  370. obj := report.ReportChapterGrant{}
  371. grantList, tmpErr := obj.GetGrantListByIdList(reportChaptersIdList)
  372. if tmpErr != nil {
  373. errMsg = "获取待继承的报告章节的授权用户列表失败"
  374. err = tmpErr
  375. return
  376. }
  377. for _, v := range grantList {
  378. // 如果不在报告授权的用户ID里面,那么该章节就不继承该授权用户
  379. if _, ok := needAdminIdMap[v.AdminId]; !ok {
  380. continue
  381. }
  382. currReportChapterId := v.ReportChapterId
  383. tmpGrantList, ok := grantListMap[currReportChapterId]
  384. if !ok {
  385. tmpGrantList = make([]*report.ReportChapterGrant, 0)
  386. }
  387. v.ReportChapterId = 0
  388. v.GrantId = 0
  389. tmpGrantList = append(tmpGrantList, v)
  390. grantListMap[currReportChapterId] = tmpGrantList
  391. }
  392. // 授权关联品种数据
  393. permissionObj := report.ReportChapterPermissionMapping{}
  394. permissionList, tmpErr := permissionObj.GetPermissionListByIdList(reportChaptersIdList)
  395. if tmpErr != nil {
  396. errMsg = "获取待继承的报告章节的授权用户列表失败"
  397. err = tmpErr
  398. return
  399. }
  400. for _, v := range permissionList {
  401. currReportChapterId := v.ReportChapterId
  402. tmpPermissionList, ok := chapterPermissionListMap[currReportChapterId]
  403. if !ok {
  404. tmpPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  405. }
  406. v.ReportChapterId = 0
  407. v.ReportChapterPermissionMappingId = 0
  408. tmpPermissionList = append(tmpPermissionList, v)
  409. chapterPermissionListMap[currReportChapterId] = tmpPermissionList
  410. }
  411. }
  412. // 继承的报告章节内容
  413. for i := 0; i < len(inheritReportChapters); i++ {
  414. customChapter := inheritReportChapters[i]
  415. // 授权用户列表
  416. tmpGrantList, ok := grantListMap[customChapter.ReportChapterId]
  417. if !ok {
  418. tmpGrantList = make([]*report.ReportChapterGrant, 0)
  419. }
  420. oldChapterIdGrantListMap[customChapter.ReportChapterId] = tmpGrantList
  421. // 关联品种列表
  422. chapterPermissionList, ok := chapterPermissionListMap[customChapter.ReportChapterId]
  423. if !ok {
  424. chapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  425. }
  426. oldChapterPermissionListMap[customChapter.ReportChapterId] = chapterPermissionList
  427. // 判断该章节是否是系统章节,如果是的话,那就是需要额外创建的
  428. if customChapter.TypeId > 0 {
  429. inheritChapterMap[customChapter.TypeId] = customChapter
  430. continue
  431. }
  432. // 自定义的报告内容
  433. customChapter.ReportId = reportInfo.Id
  434. customChapter.ReportChapterId = 0
  435. customChapter.ClassifyIdFirst = minClassifyId
  436. customChapter.ClassifyNameFirst = minClassifyName
  437. customChapter.Stage = reportInfo.Stage
  438. customChapter.PublishState = 1
  439. customChapter.CreateTime = reportInfo.CreateTime
  440. customChapter.ModifyTime = time.Now()
  441. customChapter.LastModifyAdminId = reportInfo.LastModifyAdminId
  442. customChapter.LastModifyAdminName = reportInfo.LastModifyAdminName
  443. customChapter.ContentModifyTime = time.Now()
  444. customAddChapter := models.AddReportChapter{
  445. ReportChapter: customChapter,
  446. GrantList: tmpGrantList,
  447. GrantPermissionList: chapterPermissionList,
  448. }
  449. customAddChapterList = append(customAddChapterList, customAddChapter)
  450. }
  451. }
  452. // 最大排序
  453. var maxSort int
  454. for _, typeItem := range chapterTypeList {
  455. v, ok := inheritChapterMap[typeItem.ReportChapterTypeId]
  456. // 章节授权用户
  457. var tmpGrantList []*report.ReportChapterGrant
  458. // 章节关联品种
  459. var tmpChapterPermissionList []*report.ReportChapterPermissionMapping
  460. chapterItem := new(models.ReportChapter)
  461. if ok && v != nil {
  462. // 如果存在继承的章节,那么就从继承的章节内容中获取
  463. chapterItem.AddType = 2
  464. chapterItem.Title = v.Title
  465. chapterItem.ReportType = v.ReportType
  466. chapterItem.ClassifyIdFirst = minClassifyId
  467. chapterItem.ClassifyNameFirst = minClassifyName
  468. chapterItem.TypeId = typeItem.ReportChapterTypeId
  469. chapterItem.TypeName = typeItem.ReportChapterTypeName
  470. chapterItem.Content = v.Content
  471. chapterItem.ContentSub = v.ContentSub
  472. chapterItem.Stage = reportInfo.Stage
  473. chapterItem.PublishState = 1
  474. chapterItem.Sort = typeItem.Sort
  475. chapterItem.CreateTime = reportInfo.CreateTime
  476. chapterItem.ModifyTime = time.Now()
  477. chapterItem.LastModifyAdminId = reportInfo.LastModifyAdminId
  478. chapterItem.LastModifyAdminName = reportInfo.LastModifyAdminName
  479. chapterItem.ContentModifyTime = time.Now()
  480. chapterItem.ContentStruct = v.ContentStruct
  481. chapterItem.ReportLayout = v.ReportLayout
  482. chapterItem.ReportCreateTime = time.Now()
  483. // 继承历史章节中的授权用户列表
  484. tmpGrantList, ok = oldChapterIdGrantListMap[v.ReportChapterId]
  485. if !ok {
  486. tmpGrantList = make([]*report.ReportChapterGrant, 0)
  487. }
  488. // 继承历史章节中的关联品种列表
  489. tmpChapterPermissionList, ok = oldChapterPermissionListMap[v.ReportChapterId]
  490. if !ok {
  491. tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  492. }
  493. } else {
  494. // 如果系统章节未从继承的报告中找到,那么获取往期中最新版本的此章节内容
  495. var findChapter bool
  496. if inheritReportId > 0 {
  497. chapterNewest, e := models.GetNewestPreReportChapterByClassifyIdAndTypeId(minClassifyId, typeItem.ReportChapterTypeId)
  498. if e != nil && e.Error() == utils.ErrNoRow() {
  499. errMsg = "继承最新内容的章节失败"
  500. err = fmt.Errorf("获取最新内容的章节失败, %v", e)
  501. return
  502. }
  503. if chapterNewest != nil {
  504. chapterItem.AddType = 2
  505. chapterItem.Title = chapterNewest.Title
  506. chapterItem.ReportType = chapterNewest.ReportType
  507. chapterItem.ClassifyIdFirst = minClassifyId
  508. chapterItem.ClassifyNameFirst = minClassifyName
  509. chapterItem.TypeId = typeItem.ReportChapterTypeId
  510. chapterItem.TypeName = typeItem.ReportChapterTypeName
  511. chapterItem.Content = chapterNewest.Content
  512. chapterItem.ContentSub = chapterNewest.ContentSub
  513. chapterItem.Stage = reportInfo.Stage
  514. chapterItem.PublishState = 1
  515. chapterItem.Sort = typeItem.Sort
  516. chapterItem.CreateTime = reportInfo.CreateTime
  517. chapterItem.ModifyTime = time.Now()
  518. chapterItem.LastModifyAdminId = reportInfo.LastModifyAdminId
  519. chapterItem.LastModifyAdminName = reportInfo.LastModifyAdminName
  520. chapterItem.ContentModifyTime = time.Now()
  521. chapterItem.ContentStruct = chapterNewest.ContentStruct
  522. chapterItem.ReportLayout = chapterNewest.ReportLayout
  523. chapterItem.ReportCreateTime = time.Now()
  524. // 这里的授权和品种权限需要额外查询
  525. tmpGrantList = make([]*report.ReportChapterGrant, 0)
  526. tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  527. {
  528. grantOb := report.ReportChapterGrant{}
  529. chapterGrant, e := grantOb.GetGrantListById(chapterNewest.ReportChapterId)
  530. if e != nil {
  531. errMsg = "获取待继承的报告章节的授权用户列表失败"
  532. err = fmt.Errorf("获取待继承的报告章节的授权用户列表失败, ChapterId: %d, Err: %v", chapterNewest.ReportChapterId, e)
  533. return
  534. }
  535. for _, cg := range chapterGrant {
  536. // 如果不在报告授权的用户ID里面,那么该章节就不继承该授权用户
  537. if _, ok := needAdminIdMap[cg.AdminId]; !ok {
  538. continue
  539. }
  540. cg.ReportChapterId = 0
  541. cg.GrantId = 0
  542. tmpGrantList = append(tmpGrantList, cg)
  543. }
  544. permissionOb := report.ReportChapterPermissionMapping{}
  545. permissions, e := permissionOb.GetPermissionListById(chapterNewest.ReportChapterId)
  546. if e != nil {
  547. errMsg = "获取待继承的报告章节的品种权限失败"
  548. err = fmt.Errorf("获取待继承的报告章节的品种权限失败, ChapterId: %d, Err: %v", chapterNewest.ReportChapterId, e)
  549. return
  550. }
  551. for _, ps := range permissions {
  552. ps.ReportChapterId = 0
  553. ps.ReportChapterPermissionMappingId = 0
  554. tmpChapterPermissionList = append(tmpChapterPermissionList, ps)
  555. }
  556. }
  557. findChapter = true
  558. }
  559. }
  560. // 没找到那就新增一篇空白的
  561. if !findChapter {
  562. chapterItem.AddType = 1
  563. chapterItem.Title = typeItem.ReportChapterTypeName
  564. chapterItem.ReportType = typeItem.ResearchType
  565. chapterItem.ClassifyIdFirst = minClassifyId
  566. chapterItem.ClassifyNameFirst = minClassifyName
  567. chapterItem.TypeId = typeItem.ReportChapterTypeId
  568. chapterItem.TypeName = typeItem.ReportChapterTypeName
  569. chapterItem.Stage = reportInfo.Stage
  570. chapterItem.PublishState = 1
  571. chapterItem.Sort = typeItem.Sort
  572. chapterItem.CreateTime = reportInfo.CreateTime
  573. chapterItem.ModifyTime = time.Now()
  574. chapterItem.LastModifyAdminId = reportInfo.LastModifyAdminId
  575. chapterItem.LastModifyAdminName = reportInfo.LastModifyAdminName
  576. chapterItem.ContentModifyTime = time.Now()
  577. //chapterItem.ContentStruct = v.ContentStruct
  578. chapterItem.ReportLayout = reportInfo.ReportLayout
  579. chapterItem.ReportCreateTime = time.Now()
  580. // 默认配置:从当前分类下配置的章节类型id所关联的品种列表
  581. tmpChapterPermissionList, ok = currChapterTypePermissionListMap[typeItem.ReportChapterTypeId]
  582. if !ok {
  583. tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  584. }
  585. }
  586. }
  587. if typeItem.Sort > maxSort {
  588. maxSort = typeItem.Sort
  589. }
  590. addChapter := models.AddReportChapter{
  591. ReportChapter: chapterItem,
  592. GrantList: tmpGrantList,
  593. GrantPermissionList: tmpChapterPermissionList,
  594. }
  595. chapterList = append(chapterList, addChapter)
  596. }
  597. // 将自定义的章节内容添加到待生成的章节内容中
  598. for _, addChapterItem := range customAddChapterList {
  599. maxSort++
  600. addChapterItem.ReportChapter.Sort = maxSort
  601. chapterList = append(chapterList, addChapterItem)
  602. }
  603. //hasGrantUserMap := make(map[int]bool)
  604. //for _, grantList := range typeGrantListMap {
  605. // for _, grant := range grantList {
  606. // if _, ok := hasGrantUserMap[grant.AdminId]; !ok {
  607. // allGrantUserList = append(allGrantUserList, &report.ReportGrant{
  608. // //GrantId: 0,
  609. // //ReportId: 0,
  610. // AdminId: grant.AdminId,
  611. // CreateTime: time.Now(),
  612. // })
  613. // }
  614. // }
  615. //}
  616. return
  617. }
  618. // AddChapterBaseInfoAndPermission
  619. // @Description: 添加章节基本信息及权限
  620. // @author: Roc
  621. // @datetime 2024-06-11 15:35:23
  622. // @param reportInfo *models.Report
  623. // @param reportChapterInfo *models.ReportChapter
  624. // @param permissionIdList []int
  625. // @param adminIdList []int
  626. // @return err error
  627. // @return errMsg string
  628. func AddChapterBaseInfoAndPermission(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, permissionIdList []int, adminIdList []int) (err error, errMsg string) {
  629. errMsg = "新增失败"
  630. if reportInfo.State == 2 {
  631. errMsg = "该报告已发布,不允许编辑"
  632. err = errors.New(errMsg)
  633. return
  634. }
  635. // 需要添加的报告章节授权数据
  636. addChapterAdminList := make([]*report.ReportChapterGrant, 0)
  637. for _, adminId := range adminIdList {
  638. //新增授权
  639. addChapterAdminList = append(addChapterAdminList, &report.ReportChapterGrant{
  640. //GrantId: 0,
  641. //ReportChapterId: reportChapterInfo.ReportChapterId,
  642. AdminId: adminId,
  643. CreateTime: time.Now(),
  644. })
  645. }
  646. // 需要添加的报告章节品种权限数据
  647. addChapterPermissionList := make([]*report.ReportChapterPermissionMapping, 0)
  648. // 品种权限
  649. for _, permissionId := range permissionIdList {
  650. // 如果不存在,那么就新增品种权限配置
  651. addChapterPermissionList = append(addChapterPermissionList, &report.ReportChapterPermissionMapping{
  652. //ReportChapterPermissionMappingId: 0,
  653. //ReportChapterId: reportChapterInfo.ReportChapterId,
  654. ChartPermissionId: permissionId,
  655. CreateTime: time.Now(),
  656. })
  657. }
  658. err = models.AddChapterBaseInfoAndPermission(reportChapterInfo, addChapterAdminList, addChapterPermissionList)
  659. return
  660. }
  661. // EditChapterBaseInfoAndPermission
  662. // @Description: 修改报告章节的基础信息、授权用户权限、品种权限
  663. // @author: Roc
  664. // @datetime 2024-06-05 11:49:11
  665. // @param reportInfo *models.Report
  666. // @param reportChapterInfo *models.ReportChapter
  667. // @param title string
  668. // @param permissionIdList []int
  669. // @param adminIdList []int
  670. // @return err error
  671. // @return errMsg string
  672. func EditChapterBaseInfoAndPermission(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, title string, permissionIdList []int, adminIdList []int) (err error, errMsg string) {
  673. errMsg = "修改失败"
  674. if reportInfo.State == 2 {
  675. errMsg = "该报告已发布,不允许编辑"
  676. err = errors.New(errMsg)
  677. return
  678. }
  679. updateCols := make([]string, 0)
  680. // 如果标题内容,那么就修改
  681. if title != `` {
  682. reportChapterInfo.Title = title
  683. reportChapterInfo.ModifyTime = time.Now()
  684. updateCols = append(updateCols, "Title", "ModifyTime")
  685. reportChapterInfo.UpdateChapter(updateCols)
  686. }
  687. reportGrantObj := report.ReportGrant{}
  688. chapterGrantObj := report.ReportChapterGrant{}
  689. chapterPermissionObj := report.ReportChapterPermissionMapping{}
  690. // 报告授权的用户map
  691. reportGrantAdminIdMap := make(map[int]bool)
  692. // 获取报告授权的用户信息
  693. {
  694. // 获取当前报告已经授权的用户信息
  695. reportGrantList, tmpErr := reportGrantObj.GetGrantListById(reportInfo.Id)
  696. if tmpErr != nil {
  697. err = tmpErr
  698. return
  699. }
  700. for _, v := range reportGrantList {
  701. reportGrantAdminIdMap[v.AdminId] = true
  702. }
  703. }
  704. // 需要添加的报告章节授权数据
  705. addChapterAdminList := make([]*report.ReportChapterGrant, 0)
  706. // 待移除的报告章节授权数据id
  707. delReportChapterGrantIdList := make([]int, 0)
  708. // 处理当前报告章节需要新增/移除的授权信息
  709. {
  710. // 获取当前章节已经授权的用户信息
  711. chapterGrantList, tmpErr := chapterGrantObj.GetGrantListById(reportChapterInfo.ReportChapterId)
  712. if tmpErr != nil {
  713. err = tmpErr
  714. return
  715. }
  716. // 当前章节已经授权的用户信息
  717. currChapterAdminMap := make(map[int]*report.ReportChapterGrant)
  718. // 需要删除的报告章节授权数据
  719. delChapterAdminMap := make(map[int]*report.ReportChapterGrant)
  720. for _, v := range chapterGrantList {
  721. currChapterAdminMap[v.AdminId] = v
  722. delChapterAdminMap[v.AdminId] = v
  723. }
  724. for _, adminId := range adminIdList {
  725. // 如果该用户 不在 报告授权的用户map 里面,说明这个用户是要移除的
  726. if _, ok := reportGrantAdminIdMap[adminId]; !ok {
  727. continue
  728. }
  729. _, ok := currChapterAdminMap[adminId]
  730. // 如果存在,那么从 “需要删除的报告章节授权数据” 的map中移除
  731. if ok {
  732. delete(delChapterAdminMap, adminId)
  733. continue
  734. }
  735. // 如果不存在,那么就新增授权
  736. addChapterAdminList = append(addChapterAdminList, &report.ReportChapterGrant{
  737. //GrantId: 0,
  738. ReportChapterId: reportChapterInfo.ReportChapterId,
  739. AdminId: adminId,
  740. CreateTime: time.Now(),
  741. })
  742. }
  743. // 查出需要移除的授权id
  744. for _, v := range delChapterAdminMap {
  745. delReportChapterGrantIdList = append(delReportChapterGrantIdList, v.GrantId)
  746. }
  747. }
  748. // 其他章节授权的用户
  749. otherChapterAdminMap := make(map[int]bool)
  750. {
  751. // 获取报告所有的章节id
  752. reportChapterIdList, tmpErr := models.GetReportChapterIdList(reportInfo.Id)
  753. if tmpErr != nil {
  754. err = tmpErr
  755. return
  756. }
  757. if len(reportChapterIdList) > 0 {
  758. list, tmpErr := chapterGrantObj.GetGrantListByIdList(reportChapterIdList)
  759. if tmpErr != nil {
  760. err = tmpErr
  761. return
  762. }
  763. for _, v := range list {
  764. // 如果是当前章节,因为涉及到重新授权,所以得过滤
  765. if v.ReportChapterId == reportChapterInfo.ReportChapterId {
  766. continue
  767. }
  768. otherChapterAdminMap[v.AdminId] = true
  769. }
  770. }
  771. }
  772. // 需要添加的报告章节品种权限数据
  773. addChapterPermissionList := make([]*report.ReportChapterPermissionMapping, 0)
  774. // 待移除的报告章节品种权限数据id
  775. delChapterPermissionMappingIdList := make([]int, 0)
  776. // 处理当前报告章节需要新增/移除的品种权限信息
  777. {
  778. // 获取当前章节已经配置的品种权限信息
  779. chapterPermissionList, tmpErr := chapterPermissionObj.GetPermissionListById(reportChapterInfo.ReportChapterId)
  780. if tmpErr != nil {
  781. err = tmpErr
  782. return
  783. }
  784. // 当前章节已经配置的品种权限信息
  785. currChapterPermissionMap := make(map[int]*report.ReportChapterPermissionMapping)
  786. // 需要删除的报告章节品种权限配置
  787. delChapterPermissionMap := make(map[int]*report.ReportChapterPermissionMapping)
  788. for _, v := range chapterPermissionList {
  789. currChapterPermissionMap[v.ChartPermissionId] = v
  790. delChapterPermissionMap[v.ChartPermissionId] = v
  791. }
  792. for _, permissionId := range permissionIdList {
  793. _, ok := currChapterPermissionMap[permissionId]
  794. // 如果存在,那么从 “需要删除的报告章节品种权限配置” 的map中移除
  795. if ok {
  796. delete(delChapterPermissionMap, permissionId)
  797. continue
  798. }
  799. // 如果不存在,那么就新增品种权限配置
  800. addChapterPermissionList = append(addChapterPermissionList, &report.ReportChapterPermissionMapping{
  801. //ReportChapterPermissionMappingId: 0,
  802. ReportChapterId: reportChapterInfo.ReportChapterId,
  803. ChartPermissionId: permissionId,
  804. CreateTime: time.Now(),
  805. })
  806. }
  807. // 查出需要移除的品种权限配置
  808. for _, v := range delChapterPermissionMap {
  809. delChapterPermissionMappingIdList = append(delChapterPermissionMappingIdList, v.ReportChapterPermissionMappingId)
  810. }
  811. }
  812. err = models.EditChapterBaseInfoAndPermission(reportInfo, reportChapterInfo, updateCols, addChapterAdminList, addChapterPermissionList, delReportChapterGrantIdList, delChapterPermissionMappingIdList)
  813. return
  814. }
  815. // DelChapter
  816. // @Description: 删除报告章节、授权用户权限、品种权限
  817. // @author: Roc
  818. // @datetime 2024-06-06 17:28:37
  819. // @param reportInfo *models.Report
  820. // @param reportChapterInfo *models.ReportChapter
  821. // @param sysUser *system.Admin
  822. // @return err error
  823. // @return errMsg string
  824. func DelChapter(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, sysUser *system.Admin) (err error, errMsg string) {
  825. errMsg = "删除失败"
  826. if reportInfo.State == 2 {
  827. errMsg = "该报告已发布,不允许删除"
  828. err = errors.New(errMsg)
  829. return
  830. }
  831. reportInfo.LastModifyAdminId = sysUser.AdminId
  832. reportInfo.LastModifyAdminName = sysUser.RealName
  833. reportInfo.ModifyTime = time.Now()
  834. updateReportCols := []string{"LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}
  835. err = models.DelChapterAndPermission(reportInfo, updateReportCols, reportChapterInfo)
  836. return
  837. }
  838. // DownloadVoice
  839. // @Description: 下载报告音频文件
  840. // @author: Roc
  841. // @datetime 2024-06-13 15:36:46
  842. // @param reportInfo *models.ReportDetail
  843. // @return savePath string
  844. // @return fileName string
  845. // @return err error
  846. // @return errMsg string
  847. func DownloadVoice(reportInfo *models.ReportDetail) (savePath, fileName string, err error, errMsg string) {
  848. errMsg = `下载失败`
  849. // 如果报告有音频,那么下载音频
  850. if reportInfo.VideoUrl != `` {
  851. savePath = time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
  852. fileName = reportInfo.VideoName + ".mp3"
  853. fileBody, tmpErr := http.Get(reportInfo.VideoUrl)
  854. if tmpErr != nil {
  855. err = tmpErr
  856. errMsg = "音频下载失败"
  857. return
  858. }
  859. err = file.SaveFile(fileBody, savePath)
  860. if err != nil {
  861. errMsg = "音频保存失败"
  862. return
  863. }
  864. }
  865. // 如果是章节报告,那么就下载压缩包
  866. if reportInfo.HasChapter == 1 {
  867. videoList, tmpErr := models.GetReportChapterVideoList(reportInfo.Id)
  868. if tmpErr != nil {
  869. err = tmpErr
  870. errMsg = "获取音频列表失败"
  871. return
  872. }
  873. if len(videoList) > 0 {
  874. // 创建zip
  875. savePath = time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  876. fileName = reportInfo.VideoName + ".zip"
  877. zipFile, tmpErr := os.Create(savePath)
  878. if tmpErr != nil {
  879. err = tmpErr
  880. return
  881. }
  882. zipWriter := zip.NewWriter(zipFile)
  883. // 生成zip过程中报错关闭
  884. defer func() {
  885. if err != nil {
  886. zipWriter.Close()
  887. zipFile.Close()
  888. }
  889. }()
  890. // 获取音频,写入zip
  891. for i := 0; i < len(videoList); i++ {
  892. item := videoList[i]
  893. if item.VideoName == "" || item.VideoUrl == "" {
  894. continue
  895. }
  896. // 音频文件后缀
  897. ext := path.Ext(item.VideoUrl)
  898. ioWriter, tmpErr := zipWriter.Create(fmt.Sprintf("%s%s", item.VideoName, ext))
  899. if tmpErr != nil {
  900. err = tmpErr
  901. if os.IsPermission(err) {
  902. fmt.Println("权限不足: ", err)
  903. return
  904. }
  905. return
  906. }
  907. var content []byte
  908. content, err = http.Get(item.VideoUrl)
  909. if err != nil {
  910. content = []byte("")
  911. }
  912. ioWriter.Write(content)
  913. }
  914. // 生成zip后关闭,否则下载文件会损坏
  915. zipWriter.Close()
  916. zipFile.Close()
  917. }
  918. }
  919. return
  920. }
  921. // CheckReportAuthByAdminIdList
  922. // @Description: 根据管理员id列表,判断当前用户是否有报告权限
  923. // @author: Roc
  924. // @datetime 2024-06-13 11:03:10
  925. // @param adminId int
  926. // @param createAdminId int
  927. // @param grantAdminIdList []int
  928. // @return isAuth bool
  929. func CheckReportAuthByAdminIdList(adminId, createAdminId int, grantAdminIdList []int) (isAuth bool) {
  930. // 如果是自己创建的报告,那么就有权限
  931. if adminId == createAdminId {
  932. isAuth = true
  933. return
  934. }
  935. // 如果是授权用户,那么就有权限
  936. if utils.IsCheckInList(grantAdminIdList, adminId) {
  937. isAuth = true
  938. return
  939. }
  940. return
  941. }
  942. // CheckReportAuthByReportChapterInfo
  943. // @Description: 根据报告ID,判断当前用户是否有报告权限
  944. // @author: Roc
  945. // @datetime 2024-06-13 16:21:28
  946. // @param adminId int
  947. // @param reportInfoId int
  948. // @return isAuth bool
  949. // @return err error
  950. func CheckReportAuthByReportChapterInfo(adminId, createAdminId int, reportInfoId int) (isAuth bool, err error) {
  951. // 如果是自己创建的报告,那么就有权限
  952. if adminId == createAdminId {
  953. isAuth = true
  954. return
  955. }
  956. obj := report.ReportGrant{}
  957. chapterGrantList, err := obj.GetGrantListById(reportInfoId)
  958. if err != nil {
  959. return
  960. }
  961. for _, v := range chapterGrantList {
  962. if v.AdminId == adminId {
  963. isAuth = true
  964. return
  965. }
  966. }
  967. return
  968. }
  969. // EditReportLayoutImg
  970. // @Description: 修改报告的版图信息
  971. // @author: Roc
  972. // @datetime 2024-06-18 11:35:00
  973. // @param reportInfo *models.Report
  974. // @param req models.EditLayoutImgReq
  975. // @param sysUser *system.Admin
  976. // @return err error
  977. // @return errMsg string
  978. func EditReportLayoutImg(reportInfo *models.Report, req models.EditLayoutImgReq, sysUser *system.Admin) (err error, errMsg string) {
  979. errMsg = `保存失败`
  980. reportInfo.HeadResourceId = req.HeadResourceId
  981. reportInfo.HeadImg = req.HeadImg
  982. reportInfo.EndResourceId = req.EndResourceId
  983. reportInfo.EndImg = req.EndImg
  984. reportInfo.CanvasColor = req.CanvasColor
  985. reportInfo.LastModifyAdminId = sysUser.AdminId
  986. reportInfo.LastModifyAdminName = sysUser.RealName
  987. reportInfo.ModifyTime = time.Now()
  988. updateCols := []string{"HeadResourceId", "HeadImg", "EndResourceId", "EndImg", "CanvasColor", "LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}
  989. err = reportInfo.UpdateReport(updateCols)
  990. if err != nil {
  991. return
  992. }
  993. return
  994. }
  995. // PublishReport
  996. // @Description: 报告发布
  997. // @author: Roc
  998. // @datetime 2024-06-20 09:44:13
  999. // @param reportId int
  1000. // @param reportUrl string
  1001. // @param sysUser *system.Admin
  1002. // @return tips string
  1003. // @return err error
  1004. // @return errMsg string
  1005. func PublishReport(reportId int, reportUrl string, sysUser *system.Admin) (tips string, err error, errMsg string) {
  1006. errMsg = `报告发布失败`
  1007. reportInfo, err := models.GetReportByReportId(reportId)
  1008. if err != nil {
  1009. errMsg = "获取报告信息失败"
  1010. return
  1011. }
  1012. if reportInfo == nil {
  1013. errMsg = "获取报告信息失败"
  1014. err = errors.New(errMsg)
  1015. return
  1016. }
  1017. var publishTime time.Time
  1018. if reportInfo.MsgIsSend == 1 && reportInfo.PublishTime.IsZero() { //如果报告曾经发布过,并且已经发送过模版消息,则章节的发布时间为报告的发布时间
  1019. publishTime = reportInfo.PublishTime
  1020. } else {
  1021. publishTime = time.Now()
  1022. }
  1023. var tmpErr error
  1024. // 章节类型的报告(原来的晨周报)
  1025. if reportInfo.HasChapter == 1 {
  1026. tips, tmpErr, errMsg = PublishChapterReport(reportInfo, reportUrl, sysUser)
  1027. if tmpErr != nil {
  1028. err = errors.New("晨周报发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(reportId))
  1029. }
  1030. return
  1031. }
  1032. // 普通报告
  1033. if reportInfo.Content == "" {
  1034. errMsg = `报告内容为空,不可发布`
  1035. err = errors.New("报告内容为空,不需要生成,report_id:" + strconv.Itoa(reportId))
  1036. return
  1037. }
  1038. // 根据审批开关及审批流判断当前报告状态
  1039. state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperatePublish)
  1040. if e != nil {
  1041. errMsg = "操作失败"
  1042. err = errors.New("校验报告当前状态失败, Err: " + e.Error())
  1043. return
  1044. }
  1045. if state == models.ReportStatePublished {
  1046. // 发布报告
  1047. if tmpErr = models.PublishReportById(reportId, publishTime, sysUser.AdminId, sysUser.RealName); tmpErr != nil {
  1048. err = errors.New("报告发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(reportId))
  1049. return
  1050. }
  1051. go func() {
  1052. // 生成音频
  1053. if reportInfo.VideoUrl == "" {
  1054. go UpdateReportVideo(reportInfo)
  1055. }
  1056. // 更新报告Es
  1057. _ = UpdateReportEs(reportId, 2)
  1058. }()
  1059. } else {
  1060. // 从无审批切换为有审批, 状态重置
  1061. if e = models.ResetReportById(reportId, state, sysUser.AdminId, sysUser.RealName); e != nil {
  1062. errMsg = "操作失败"
  1063. err = fmt.Errorf("重置报告状态失败, Err: %s, ReportId: %d", e.Error(), reportId)
  1064. return
  1065. }
  1066. }
  1067. recordItem := &models.ReportStateRecord{
  1068. ReportId: reportId,
  1069. ReportType: 1,
  1070. State: state,
  1071. AdminId: sysUser.AdminId,
  1072. AdminName: sysUser.AdminName,
  1073. CreateTime: time.Now(),
  1074. }
  1075. go func() {
  1076. _, _ = models.AddReportStateRecord(recordItem)
  1077. }()
  1078. // 生成报告pdf和长图
  1079. {
  1080. reportPdfUrl := GetGeneralPdfUrl(reportInfo.ReportCode, reportInfo.ClassifyNameFirst, reportInfo.ReportLayout)
  1081. go Report2pdfAndJpeg(reportPdfUrl, reportId, 1)
  1082. }
  1083. // 报告权限处理
  1084. {
  1085. minClassifyId, _, tmpErr := getMinClassify(reportInfo)
  1086. if tmpErr != nil {
  1087. return
  1088. }
  1089. go handleReportPermission(int64(reportInfo.Id), minClassifyId)
  1090. }
  1091. return
  1092. }
  1093. // PublishChapterReport
  1094. // @Description: 发布章节类型的报告
  1095. // @author: Roc
  1096. // @datetime 2024-06-28 10:38:50
  1097. // @param reportInfo *models.Report
  1098. // @param reportUrl string
  1099. // @return tips string
  1100. // @return err error
  1101. // @return errMsg string
  1102. func PublishChapterReport(reportInfo *models.Report, reportUrl string, sysUser *system.Admin) (tips string, err error, errMsg string) {
  1103. reportId := reportInfo.Id
  1104. if reportInfo.State == 2 {
  1105. return
  1106. }
  1107. // 获取所有章节列表
  1108. chapters, err := models.GetChapterListByReportId(reportId)
  1109. if err != nil {
  1110. return
  1111. }
  1112. chapterLen := len(chapters)
  1113. if chapterLen <= 0 {
  1114. tips = "报告章节为空,不可发布"
  1115. errMsg = tips
  1116. err = errors.New(tips)
  1117. return
  1118. }
  1119. // 获取报告中的所有章节列表
  1120. chapterList, err := models.GetChapterListByReportId(reportId)
  1121. if err != nil {
  1122. return
  1123. }
  1124. for _, chapter := range chapterList {
  1125. if chapter.PublishState == 1 {
  1126. tips = "还存在未发布的章节"
  1127. errMsg = tips
  1128. err = errors.New(tips)
  1129. return
  1130. }
  1131. }
  1132. // 根据审批开关及审批流判断当前报告状态
  1133. state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperatePublish)
  1134. if e != nil {
  1135. //errMsg = "操作失败"
  1136. err = errors.New("校验报告当前状态失败, Err: " + e.Error())
  1137. return
  1138. }
  1139. // 如果状态不是已发布,那么就重置状态
  1140. if state != models.ReportStatePublished {
  1141. // 从无审批切换为有审批, 状态重置
  1142. if e = models.ResetReportById(reportId, state, sysUser.AdminId, sysUser.RealName); e != nil {
  1143. //errMsg = "操作失败"
  1144. err = fmt.Errorf("重置报告状态失败, Err: %s, ReportId: %d", e.Error(), reportId)
  1145. return
  1146. }
  1147. return
  1148. }
  1149. // 需发布整期
  1150. updateCols := make([]string, 0)
  1151. reportInfo.LastModifyAdminId = sysUser.AdminId
  1152. reportInfo.LastModifyAdminName = sysUser.RealName
  1153. updateCols = append(updateCols, "LastModifyAdminId", "LastModifyAdminName", "Title", "State", "ModifyTime")
  1154. // 发布后标题调整
  1155. //title := reportInfo.Title
  1156. //title = strings.ReplaceAll(title, "【弘则FICC晨报】", "")
  1157. //title = strings.ReplaceAll(title, "【弘则FICC周报】", "")
  1158. //if title == "" {
  1159. // // 取第一个需发布章节的标题
  1160. // firstId := publishIdArr[0]
  1161. // firstTitle := ""
  1162. // for i := 0; i < chapterLen; i++ {
  1163. // if chapters[i].ReportChapterId == firstId {
  1164. // firstTitle = chapters[i].Title
  1165. // break
  1166. // }
  1167. // }
  1168. // title = firstTitle
  1169. //}
  1170. //report.Title = title
  1171. reportInfo.State = state
  1172. // 研报后台4.4 只在没有发布过时更新发布时间,其余均按模版消息发送时间当作发布时间
  1173. if reportInfo.MsgIsSend == 0 || reportInfo.PublishTime.IsZero() {
  1174. reportInfo.PublishTime = time.Now().Local()
  1175. updateCols = append(updateCols, "PublishTime")
  1176. }
  1177. reportInfo.ModifyTime = time.Now().Local()
  1178. if e := models.PublishReportAndChapter(reportInfo, true, updateCols); e != nil {
  1179. err = errors.New("发布报告及章节失败")
  1180. return
  1181. }
  1182. // 生成章节音频
  1183. go UpdateReportVideo(reportInfo)
  1184. // 更新报告ES
  1185. go func() {
  1186. _ = UpdateReportEs(reportInfo.Id, 2)
  1187. }()
  1188. // 发布时备份内容
  1189. go SaveReportLogs(reportInfo, chapters, reportInfo.AdminId, reportInfo.AdminRealName)
  1190. // 生成报告pdf和长图
  1191. {
  1192. reportPdfUrl := GetGeneralPdfUrl(reportInfo.ReportCode, reportInfo.ClassifyNameFirst, reportInfo.ReportLayout)
  1193. go Report2pdfAndJpeg(reportPdfUrl, reportId, 1)
  1194. }
  1195. return
  1196. }
  1197. // DeleteReportAndChapter 删除报告及章节
  1198. func DeleteReportAndChapter(reportId int) (err error) {
  1199. reportInfo, err := models.GetReportByReportId(reportId)
  1200. if err != nil {
  1201. err = errors.New("报告信息有误, Err: " + err.Error())
  1202. return
  1203. }
  1204. if reportInfo.State == 2 {
  1205. err = errors.New("报告已发布,不可删除")
  1206. return
  1207. }
  1208. // 更新ES
  1209. _ = UpdateReportEs(reportId, 1)
  1210. // 删除
  1211. if reportInfo.HasChapter == 1 {
  1212. err = models.DeleteDayWeekReportAndChapter(reportId)
  1213. } else {
  1214. err = models.DeleteReport(reportId)
  1215. }
  1216. if err != nil {
  1217. err = errors.New("删除失败, Err: " + err.Error())
  1218. return
  1219. }
  1220. // 重置PPT关联报告
  1221. go func() {
  1222. _ = ResetPPTReport(reportId, false)
  1223. }()
  1224. return
  1225. }
  1226. // getMinClassify
  1227. // @Description: 获取最小分类ID
  1228. // @author: Roc
  1229. // @datetime 2024-06-20 09:23:19
  1230. // @param reportInfo *models.Report
  1231. // @return minClassifyId int
  1232. // @return minClassifyName string
  1233. // @return err error
  1234. func getMinClassify(reportInfo *models.Report) (minClassifyId int, minClassifyName string, err error) {
  1235. defer func() {
  1236. if err != nil {
  1237. utils.FileLog.Error("获取最小分类ID失败,报告ID:%d,Err:%s", reportInfo.Id, err.Error())
  1238. }
  1239. }()
  1240. minClassifyId = reportInfo.ClassifyIdThird
  1241. minClassifyName = reportInfo.ClassifyNameThird
  1242. if minClassifyId <= 0 {
  1243. minClassifyId = reportInfo.ClassifyIdSecond
  1244. minClassifyName = reportInfo.ClassifyNameSecond
  1245. }
  1246. if minClassifyId <= 0 {
  1247. minClassifyId = reportInfo.ClassifyIdFirst
  1248. minClassifyName = reportInfo.ClassifyNameFirst
  1249. }
  1250. if minClassifyId <= 0 {
  1251. err = errors.New("分类异常")
  1252. }
  1253. return
  1254. }
  1255. // handleReportPermission
  1256. // @Description: 报告权限处理
  1257. // @author: Roc
  1258. // @datetime 2024-06-19 18:00:51
  1259. // @param reportId int64
  1260. // @param minClassifyId int
  1261. func handleReportPermission(reportId int64, minClassifyId int) {
  1262. // TODO 报告权限处理
  1263. var err error
  1264. defer func() {
  1265. if err != nil {
  1266. utils.FileLog.Error("报告权限处理失败,报告ID:%d,分类ID:%d,Err:%s", reportId, minClassifyId, err.Error())
  1267. //alarm_msg.SendAlarmMsg("修改删除报告权限失败,Err:"+err.Error(), 3)
  1268. }
  1269. }()
  1270. err = models.RemoveChartPermissionChapterMapping(reportId)
  1271. if err != nil {
  1272. err = errors.New("修改删除报告权限失败,Err:" + err.Error())
  1273. return
  1274. }
  1275. permissionItems, err := models.GetPermission(minClassifyId)
  1276. if err != nil {
  1277. err = errors.New("获取权限失败,Err:" + err.Error())
  1278. return
  1279. }
  1280. for _, v := range permissionItems {
  1281. err = models.AddChartPermissionChapterMapping(v.ChartPermissionId, reportId)
  1282. if err != nil {
  1283. err = errors.New("新增权限失败,Err:" + err.Error())
  1284. return
  1285. }
  1286. }
  1287. // 同步crm权限
  1288. _ = EditReportPermissionSync(reportId, minClassifyId)
  1289. return
  1290. }
  1291. // HandleReportPermission
  1292. // @Description: 报告权限处理
  1293. // @author: Roc
  1294. // @datetime 2024-07-12 11:17:49
  1295. // @param reportInfo *models.Report
  1296. func HandleReportPermission(reportInfo *models.Report) {
  1297. minClassifyId, _, tmpErr := getMinClassify(reportInfo)
  1298. if tmpErr != nil {
  1299. return
  1300. }
  1301. handleReportPermission(int64(reportInfo.Id), minClassifyId)
  1302. }
  1303. // UpdateReportVideo
  1304. // @Description: 更新报告音频
  1305. // @author: Roc
  1306. // @datetime 2024-07-17 18:11:10
  1307. // @param reportInfo *models.Report
  1308. func UpdateReportVideo(reportInfo *models.Report) {
  1309. if reportInfo.HasChapter == 1 {
  1310. reportChapterIdList := make([]int, 0)
  1311. // 获取报告中的所有章节列表
  1312. chapterList, err := models.GetChapterListByReportId(reportInfo.Id)
  1313. if err != nil {
  1314. return
  1315. }
  1316. reportContent := ""
  1317. for _, chapter := range chapterList {
  1318. if chapter.PublishState == 2 {
  1319. reportChapterIdList = append(reportChapterIdList, chapter.ReportChapterId)
  1320. reportContent += chapter.Title + `。`
  1321. reportContent += chapter.Content + `。`
  1322. }
  1323. }
  1324. if len(reportChapterIdList) > 0 {
  1325. err = UpdateChaptersVideo(reportChapterIdList)
  1326. if err != nil {
  1327. utils.FileLog.Info(fmt.Sprintf("%d报告音频生产失败:%s", reportInfo.Id, err.Error()))
  1328. }
  1329. }
  1330. // 生成汇总音频
  1331. {
  1332. if reportInfo.VideoUrl != "" && reportInfo.VideoName != "" && reportInfo.VideoSize != "" && reportInfo.VideoPlaySeconds != "" {
  1333. return
  1334. }
  1335. weekClassifyId, err := company.GetReportClassifyIdByConfigKey("report_week_classify_id")
  1336. if err != nil && err.Error() != utils.ErrNoRow() {
  1337. msg := "获取周报ID配置失败:" + err.Error()
  1338. utils.FileLog.Info(msg)
  1339. return
  1340. }
  1341. if reportInfo.ClassifyIdFirst == weekClassifyId {
  1342. return
  1343. }
  1344. videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(reportInfo.Title, html.UnescapeString(reportContent), time.Now().Format(utils.FormatDateTime))
  1345. if e != nil {
  1346. err = e
  1347. return
  1348. }
  1349. // 修改报告的音频信息
  1350. err = models.ModifyReportVideoByNoVideo(reportInfo.Id, videoUrl, videoName, videoSize, videoPlaySeconds)
  1351. }
  1352. } else {
  1353. err := CreateVideo(reportInfo)
  1354. if err != nil {
  1355. utils.FileLog.Info(fmt.Sprintf("%d报告音频生产失败:%s", reportInfo.Id, err.Error()))
  1356. }
  1357. }
  1358. }
  1359. // GetGeneralPdfUrl
  1360. // @Description: 获取生成pdf的地址
  1361. // @author: Roc
  1362. // @datetime 2024-07-19 14:09:28
  1363. // @param reportCode string
  1364. // @param reportLayout int8
  1365. // @return pdfUrl string
  1366. func GetGeneralPdfUrl(reportCode, classifyFirstName string, reportLayout int8) (pdfUrl string) {
  1367. // 如果是弘则,且是晨、周报,那么就不返回
  1368. if utils.InArrayByStr([]string{utils.BusinessCodeRelease, utils.BusinessCodeSandbox, utils.BusinessCodeDebug}, utils.BusinessCode) && utils.InArrayByStr([]string{"晨报", "周报"}, classifyFirstName) {
  1369. return
  1370. }
  1371. // 优先取Report2ImgUrl(用于兼容内外网环境的), 没有的话取报告详情地址
  1372. var reportUrl string
  1373. conf, _ := models.GetBusinessConfByKey(models.BusinessConfReport2ImgUrl)
  1374. if conf != nil && conf.ConfVal != "" {
  1375. reportUrl = conf.ConfVal
  1376. }
  1377. if reportUrl == "" {
  1378. conf, e := models.GetBusinessConfByKey(models.BusinessConfReportViewUrl)
  1379. if e != nil {
  1380. return
  1381. }
  1382. reportUrl = conf.ConfVal
  1383. }
  1384. switch reportLayout {
  1385. case 1:
  1386. // 普通布局
  1387. pdfUrl = fmt.Sprintf("%s/reportshare_pdf?code=%s", reportUrl, reportCode)
  1388. case 2:
  1389. // 智能布局
  1390. pdfUrl = fmt.Sprintf("%s/reportshare_smart_pdf?code=%s", reportUrl, reportCode)
  1391. }
  1392. return
  1393. }
  1394. func GetReportWaterMarkPdf(reportInfo *models.Report, sysUser *system.Admin) {
  1395. dateDir := time.Now().Format("20060102")
  1396. uploadDir := utils.STATIC_DIR + "pdf/" + dateDir
  1397. filePath, err := utils.DownloadFile(reportInfo.DetailPdfUrl, uploadDir)
  1398. if err != nil {
  1399. return
  1400. }
  1401. // 移除临时Pdf文件
  1402. defer func() {
  1403. _ = os.Remove(filePath)
  1404. }()
  1405. waterMarkStr := fmt.Sprintf("%s - %s", sysUser.RealName, sysUser.Mobile)
  1406. GeneralWaterMarkPdf(filePath, waterMarkStr)
  1407. }