report_v2.go 46 KB

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