report_v2.go 48 KB

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