report_v2.go 51 KB

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