report_v2.go 46 KB

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