report_v2.go 52 KB

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