report_v2.go 51 KB

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