report_v2.go 51 KB

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