report_v2.go 46 KB

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