report_v2.go 53 KB

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