report_v2.go 48 KB

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