report_v2.go 47 KB

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