report_v2.go 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. package services
  2. import (
  3. "archive/zip"
  4. "errors"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/report"
  7. "eta/eta_api/models/report_approve"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/file"
  12. "github.com/rdlucklib/rdluck_tools/http"
  13. "html"
  14. "os"
  15. "path"
  16. "strconv"
  17. "time"
  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.ClassifyNameFirst, 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.ClassifyNameFirst, 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. reportContent := ""
  1247. for _, chapter := range chapterList {
  1248. if chapter.PublishState == 2 {
  1249. reportChapterIdList = append(reportChapterIdList, chapter.ReportChapterId)
  1250. reportContent += chapter.Title + `。`
  1251. reportContent += chapter.Content + `。`
  1252. }
  1253. }
  1254. if len(reportChapterIdList) > 0 {
  1255. err = UpdateChaptersVideo(reportChapterIdList)
  1256. if err != nil {
  1257. utils.FileLog.Info(fmt.Sprintf("%d报告音频生产失败:%s", reportInfo.Id, err.Error()))
  1258. }
  1259. }
  1260. // 生成汇总音频
  1261. {
  1262. if reportInfo.VideoUrl != "" && reportInfo.VideoName != "" && reportInfo.VideoSize != "" && reportInfo.VideoPlaySeconds != "" {
  1263. return
  1264. }
  1265. videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(reportInfo.Title, html.UnescapeString(reportContent), time.Now().Format(utils.FormatDateTime))
  1266. if e != nil {
  1267. err = e
  1268. return
  1269. }
  1270. // 修改报告的音频信息
  1271. err = models.ModifyReportVideoByNoVideo(reportInfo.Id, videoUrl, videoName, videoSize, videoPlaySeconds)
  1272. }
  1273. } else {
  1274. err := CreateVideo(reportInfo)
  1275. if err != nil {
  1276. utils.FileLog.Info(fmt.Sprintf("%d报告音频生产失败:%s", reportInfo.Id, err.Error()))
  1277. }
  1278. }
  1279. }
  1280. // GetGeneralPdfUrl
  1281. // @Description: 获取生成pdf的地址
  1282. // @author: Roc
  1283. // @datetime 2024-07-19 14:09:28
  1284. // @param reportCode string
  1285. // @param reportLayout int8
  1286. // @return pdfUrl string
  1287. func GetGeneralPdfUrl(reportCode, classifyFirstName string, reportLayout int8) (pdfUrl string) {
  1288. // 如果是弘则,且是晨、周报,那么就不返回
  1289. if utils.InArrayByStr([]string{utils.BusinessCodeRelease, utils.BusinessCodeSandbox, utils.BusinessCodeDebug}, utils.BusinessCode) && utils.InArrayByStr([]string{"晨报", "周报"}, classifyFirstName) {
  1290. return
  1291. }
  1292. conf, e := models.GetBusinessConfByKey("ReportViewUrl")
  1293. if e != nil {
  1294. return
  1295. }
  1296. switch reportLayout {
  1297. case 1:
  1298. // 普通布局
  1299. pdfUrl = fmt.Sprintf("%s/reportshare_pdf?code=%s", conf.ConfVal, reportCode)
  1300. case 2:
  1301. // 智能布局
  1302. pdfUrl = fmt.Sprintf("%s/reportshare_smart_pdf?code=%s", conf.ConfVal, reportCode)
  1303. }
  1304. return
  1305. }
  1306. func GetReportWaterMarkPdf(reportInfo *models.Report, sysUser *system.Admin) {
  1307. dateDir := time.Now().Format("20060102")
  1308. uploadDir := utils.STATIC_DIR + "pdf/" + dateDir
  1309. filePath, err := utils.DownloadFile(reportInfo.DetailPdfUrl, uploadDir)
  1310. if err != nil {
  1311. return
  1312. }
  1313. // 移除临时Pdf文件
  1314. defer func() {
  1315. _ = os.Remove(filePath)
  1316. }()
  1317. waterMarkStr := fmt.Sprintf("%s - %s", sysUser.RealName, sysUser.Mobile)
  1318. GeneralWaterMarkPdf(filePath, waterMarkStr)
  1319. }