report_v2.go 54 KB

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