report_v2.go 49 KB

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