report_v2.go 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. package services
  2. import (
  3. "archive/zip"
  4. "errors"
  5. "eta/eta_mobile/models"
  6. "eta/eta_mobile/models/report"
  7. "eta/eta_mobile/models/report_approve"
  8. "eta/eta_mobile/models/system"
  9. "eta/eta_mobile/utils"
  10. "fmt"
  11. "os"
  12. "path"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/beego/beego/v2/server/web"
  17. "github.com/beego/beego/v2/server/web/context"
  18. "github.com/go-redis/redis/v8"
  19. "github.com/rdlucklib/rdluck_tools/file"
  20. "github.com/rdlucklib/rdluck_tools/http"
  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, reportDate time.Time) (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, reportDate)
  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, reportDate time.Time) (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 !reportDate.Before(startTime) && !reportDate.After(endTime) {
  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. // 如果系统章节未从继承的报告中找到,那么获取往期中最新版本的此章节内容
  517. var findChapter bool
  518. if inheritReportId > 0 {
  519. chapterNewest, e := models.GetNewestPreReportChapterByClassifyIdAndTypeId(minClassifyId, typeItem.ReportChapterTypeId)
  520. if e != nil && e.Error() == utils.ErrNoRow() {
  521. errMsg = "继承最新内容的章节失败"
  522. err = fmt.Errorf("获取最新内容的章节失败, %v", e)
  523. return
  524. }
  525. if chapterNewest != nil {
  526. chapterItem.AddType = 2
  527. chapterItem.Title = chapterNewest.Title
  528. chapterItem.ReportType = chapterNewest.ReportType
  529. chapterItem.ClassifyIdFirst = minClassifyId
  530. chapterItem.ClassifyNameFirst = minClassifyName
  531. chapterItem.TypeId = typeItem.ReportChapterTypeId
  532. chapterItem.TypeName = typeItem.ReportChapterTypeName
  533. chapterItem.Content = chapterNewest.Content
  534. chapterItem.ContentSub = chapterNewest.ContentSub
  535. chapterItem.Stage = reportInfo.Stage
  536. chapterItem.PublishState = 1
  537. chapterItem.Sort = typeItem.Sort
  538. chapterItem.CreateTime = reportInfo.CreateTime
  539. chapterItem.ModifyTime = time.Now()
  540. chapterItem.LastModifyAdminId = reportInfo.LastModifyAdminId
  541. chapterItem.LastModifyAdminName = reportInfo.LastModifyAdminName
  542. chapterItem.ContentModifyTime = time.Now()
  543. chapterItem.ContentStruct = chapterNewest.ContentStruct
  544. chapterItem.ReportLayout = chapterNewest.ReportLayout
  545. chapterItem.ReportCreateTime = time.Now()
  546. // 这里的授权和品种权限需要额外查询
  547. tmpGrantList = make([]*report.ReportChapterGrant, 0)
  548. tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  549. {
  550. grantOb := report.ReportChapterGrant{}
  551. chapterGrant, e := grantOb.GetGrantListById(chapterNewest.ReportChapterId)
  552. if e != nil {
  553. errMsg = "获取待继承的报告章节的授权用户列表失败"
  554. err = fmt.Errorf("获取待继承的报告章节的授权用户列表失败, ChapterId: %d, Err: %v", chapterNewest.ReportChapterId, e)
  555. return
  556. }
  557. for _, cg := range chapterGrant {
  558. // 如果不在报告授权的用户ID里面,那么该章节就不继承该授权用户
  559. if _, ok := needAdminIdMap[cg.AdminId]; !ok {
  560. continue
  561. }
  562. cg.ReportChapterId = 0
  563. cg.GrantId = 0
  564. tmpGrantList = append(tmpGrantList, cg)
  565. }
  566. permissionOb := report.ReportChapterPermissionMapping{}
  567. permissions, e := permissionOb.GetPermissionListById(chapterNewest.ReportChapterId)
  568. if e != nil {
  569. errMsg = "获取待继承的报告章节的品种权限失败"
  570. err = fmt.Errorf("获取待继承的报告章节的品种权限失败, ChapterId: %d, Err: %v", chapterNewest.ReportChapterId, e)
  571. return
  572. }
  573. for _, ps := range permissions {
  574. ps.ReportChapterId = 0
  575. ps.ReportChapterPermissionMappingId = 0
  576. tmpChapterPermissionList = append(tmpChapterPermissionList, ps)
  577. }
  578. }
  579. findChapter = true
  580. }
  581. }
  582. // 没找到那就新增一篇空白的
  583. if !findChapter {
  584. chapterItem.AddType = 1
  585. chapterItem.Title = typeItem.ReportChapterTypeName
  586. chapterItem.ReportType = typeItem.ResearchType
  587. chapterItem.ClassifyIdFirst = minClassifyId
  588. chapterItem.ClassifyNameFirst = minClassifyName
  589. chapterItem.TypeId = typeItem.ReportChapterTypeId
  590. chapterItem.TypeName = typeItem.ReportChapterTypeName
  591. chapterItem.Stage = reportInfo.Stage
  592. chapterItem.PublishState = 1
  593. chapterItem.Sort = typeItem.Sort
  594. chapterItem.CreateTime = reportInfo.CreateTime
  595. chapterItem.ModifyTime = time.Now()
  596. chapterItem.LastModifyAdminId = reportInfo.LastModifyAdminId
  597. chapterItem.LastModifyAdminName = reportInfo.LastModifyAdminName
  598. chapterItem.ContentModifyTime = time.Now()
  599. //chapterItem.ContentStruct = v.ContentStruct
  600. chapterItem.ReportLayout = reportInfo.ReportLayout
  601. chapterItem.ReportCreateTime = time.Now()
  602. // 默认配置:从当前分类下配置的章节类型id所关联的品种列表
  603. tmpChapterPermissionList, ok = currChapterTypePermissionListMap[typeItem.ReportChapterTypeId]
  604. if !ok {
  605. tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
  606. }
  607. }
  608. }
  609. if typeItem.Sort > maxSort {
  610. maxSort = typeItem.Sort
  611. }
  612. addChapter := models.AddReportChapter{
  613. ReportChapter: chapterItem,
  614. GrantList: tmpGrantList,
  615. GrantPermissionList: tmpChapterPermissionList,
  616. }
  617. chapterList = append(chapterList, addChapter)
  618. }
  619. // 将自定义的章节内容添加到待生成的章节内容中
  620. for _, addChapterItem := range customAddChapterList {
  621. maxSort++
  622. addChapterItem.ReportChapter.Sort = maxSort
  623. chapterList = append(chapterList, addChapterItem)
  624. }
  625. //hasGrantUserMap := make(map[int]bool)
  626. //for _, grantList := range typeGrantListMap {
  627. // for _, grant := range grantList {
  628. // if _, ok := hasGrantUserMap[grant.AdminId]; !ok {
  629. // allGrantUserList = append(allGrantUserList, &report.ReportGrant{
  630. // //GrantId: 0,
  631. // //ReportId: 0,
  632. // AdminId: grant.AdminId,
  633. // CreateTime: time.Now(),
  634. // })
  635. // }
  636. // }
  637. //}
  638. return
  639. }
  640. // AddChapterBaseInfoAndPermission
  641. // @Description: 添加章节基本信息及权限
  642. // @author: Roc
  643. // @datetime 2024-06-11 15:35:23
  644. // @param reportInfo *models.Report
  645. // @param reportChapterInfo *models.ReportChapter
  646. // @param permissionIdList []int
  647. // @param adminIdList []int
  648. // @return err error
  649. // @return errMsg string
  650. func AddChapterBaseInfoAndPermission(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, permissionIdList []int, adminIdList []int) (err error, errMsg string) {
  651. errMsg = "新增失败"
  652. if reportInfo.State == 2 {
  653. errMsg = "该报告已发布,不允许编辑"
  654. err = errors.New(errMsg)
  655. return
  656. }
  657. // 需要添加的报告章节授权数据
  658. addChapterAdminList := make([]*report.ReportChapterGrant, 0)
  659. for _, adminId := range adminIdList {
  660. //新增授权
  661. addChapterAdminList = append(addChapterAdminList, &report.ReportChapterGrant{
  662. //GrantId: 0,
  663. //ReportChapterId: reportChapterInfo.ReportChapterId,
  664. AdminId: adminId,
  665. CreateTime: time.Now(),
  666. })
  667. }
  668. // 需要添加的报告章节品种权限数据
  669. addChapterPermissionList := make([]*report.ReportChapterPermissionMapping, 0)
  670. // 品种权限
  671. for _, permissionId := range permissionIdList {
  672. // 如果不存在,那么就新增品种权限配置
  673. addChapterPermissionList = append(addChapterPermissionList, &report.ReportChapterPermissionMapping{
  674. //ReportChapterPermissionMappingId: 0,
  675. //ReportChapterId: reportChapterInfo.ReportChapterId,
  676. ChartPermissionId: permissionId,
  677. CreateTime: time.Now(),
  678. })
  679. }
  680. err = models.AddChapterBaseInfoAndPermission(reportChapterInfo, addChapterAdminList, addChapterPermissionList)
  681. return
  682. }
  683. // EditChapterBaseInfoAndPermission
  684. // @Description: 修改报告章节的基础信息、授权用户权限、品种权限
  685. // @author: Roc
  686. // @datetime 2024-06-05 11:49:11
  687. // @param reportInfo *models.Report
  688. // @param reportChapterInfo *models.ReportChapter
  689. // @param title string
  690. // @param permissionIdList []int
  691. // @param adminIdList []int
  692. // @return err error
  693. // @return errMsg string
  694. func EditChapterBaseInfoAndPermission(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, title string, permissionIdList []int, adminIdList []int) (err error, errMsg string) {
  695. errMsg = "修改失败"
  696. if reportInfo.State == 2 {
  697. errMsg = "该报告已发布,不允许编辑"
  698. err = errors.New(errMsg)
  699. return
  700. }
  701. updateCols := make([]string, 0)
  702. // 如果标题内容,那么就修改
  703. if title != `` {
  704. reportChapterInfo.Title = title
  705. reportChapterInfo.ModifyTime = time.Now()
  706. updateCols = append(updateCols, "Title", "ModifyTime")
  707. reportChapterInfo.UpdateChapter(updateCols)
  708. }
  709. reportGrantObj := report.ReportGrant{}
  710. chapterGrantObj := report.ReportChapterGrant{}
  711. chapterPermissionObj := report.ReportChapterPermissionMapping{}
  712. // 报告授权的用户map
  713. reportGrantAdminIdMap := make(map[int]bool)
  714. // 获取报告授权的用户信息
  715. {
  716. // 获取当前报告已经授权的用户信息
  717. reportGrantList, tmpErr := reportGrantObj.GetGrantListById(reportInfo.Id)
  718. if tmpErr != nil {
  719. err = tmpErr
  720. return
  721. }
  722. for _, v := range reportGrantList {
  723. reportGrantAdminIdMap[v.AdminId] = true
  724. }
  725. }
  726. // 需要添加的报告章节授权数据
  727. addChapterAdminList := make([]*report.ReportChapterGrant, 0)
  728. // 待移除的报告章节授权数据id
  729. delReportChapterGrantIdList := make([]int, 0)
  730. // 处理当前报告章节需要新增/移除的授权信息
  731. {
  732. // 获取当前章节已经授权的用户信息
  733. chapterGrantList, tmpErr := chapterGrantObj.GetGrantListById(reportChapterInfo.ReportChapterId)
  734. if tmpErr != nil {
  735. err = tmpErr
  736. return
  737. }
  738. // 当前章节已经授权的用户信息
  739. currChapterAdminMap := make(map[int]*report.ReportChapterGrant)
  740. // 需要删除的报告章节授权数据
  741. delChapterAdminMap := make(map[int]*report.ReportChapterGrant)
  742. for _, v := range chapterGrantList {
  743. currChapterAdminMap[v.AdminId] = v
  744. delChapterAdminMap[v.AdminId] = v
  745. }
  746. for _, adminId := range adminIdList {
  747. // 如果该用户 不在 报告授权的用户map 里面,说明这个用户是要移除的
  748. if _, ok := reportGrantAdminIdMap[adminId]; !ok {
  749. continue
  750. }
  751. _, ok := currChapterAdminMap[adminId]
  752. // 如果存在,那么从 “需要删除的报告章节授权数据” 的map中移除
  753. if ok {
  754. delete(delChapterAdminMap, adminId)
  755. continue
  756. }
  757. // 如果不存在,那么就新增授权
  758. addChapterAdminList = append(addChapterAdminList, &report.ReportChapterGrant{
  759. //GrantId: 0,
  760. ReportChapterId: reportChapterInfo.ReportChapterId,
  761. AdminId: adminId,
  762. CreateTime: time.Now(),
  763. })
  764. }
  765. // 查出需要移除的授权id
  766. for _, v := range delChapterAdminMap {
  767. delReportChapterGrantIdList = append(delReportChapterGrantIdList, v.GrantId)
  768. }
  769. }
  770. // 其他章节授权的用户
  771. otherChapterAdminMap := make(map[int]bool)
  772. {
  773. // 获取报告所有的章节id
  774. reportChapterIdList, tmpErr := models.GetReportChapterIdList(reportInfo.Id)
  775. if tmpErr != nil {
  776. err = tmpErr
  777. return
  778. }
  779. if len(reportChapterIdList) > 0 {
  780. list, tmpErr := chapterGrantObj.GetGrantListByIdList(reportChapterIdList)
  781. if tmpErr != nil {
  782. err = tmpErr
  783. return
  784. }
  785. for _, v := range list {
  786. // 如果是当前章节,因为涉及到重新授权,所以得过滤
  787. if v.ReportChapterId == reportChapterInfo.ReportChapterId {
  788. continue
  789. }
  790. otherChapterAdminMap[v.AdminId] = true
  791. }
  792. }
  793. }
  794. // 需要添加的报告章节品种权限数据
  795. addChapterPermissionList := make([]*report.ReportChapterPermissionMapping, 0)
  796. // 待移除的报告章节品种权限数据id
  797. delChapterPermissionMappingIdList := make([]int, 0)
  798. // 处理当前报告章节需要新增/移除的品种权限信息
  799. {
  800. // 获取当前章节已经配置的品种权限信息
  801. chapterPermissionList, tmpErr := chapterPermissionObj.GetPermissionListById(reportChapterInfo.ReportChapterId)
  802. if tmpErr != nil {
  803. err = tmpErr
  804. return
  805. }
  806. // 当前章节已经配置的品种权限信息
  807. currChapterPermissionMap := make(map[int]*report.ReportChapterPermissionMapping)
  808. // 需要删除的报告章节品种权限配置
  809. delChapterPermissionMap := make(map[int]*report.ReportChapterPermissionMapping)
  810. for _, v := range chapterPermissionList {
  811. currChapterPermissionMap[v.ChartPermissionId] = v
  812. delChapterPermissionMap[v.ChartPermissionId] = v
  813. }
  814. for _, permissionId := range permissionIdList {
  815. _, ok := currChapterPermissionMap[permissionId]
  816. // 如果存在,那么从 “需要删除的报告章节品种权限配置” 的map中移除
  817. if ok {
  818. delete(delChapterPermissionMap, permissionId)
  819. continue
  820. }
  821. // 如果不存在,那么就新增品种权限配置
  822. addChapterPermissionList = append(addChapterPermissionList, &report.ReportChapterPermissionMapping{
  823. //ReportChapterPermissionMappingId: 0,
  824. ReportChapterId: reportChapterInfo.ReportChapterId,
  825. ChartPermissionId: permissionId,
  826. CreateTime: time.Now(),
  827. })
  828. }
  829. // 查出需要移除的品种权限配置
  830. for _, v := range delChapterPermissionMap {
  831. delChapterPermissionMappingIdList = append(delChapterPermissionMappingIdList, v.ReportChapterPermissionMappingId)
  832. }
  833. }
  834. err = models.EditChapterBaseInfoAndPermission(reportInfo, reportChapterInfo, updateCols, addChapterAdminList, addChapterPermissionList, delReportChapterGrantIdList, delChapterPermissionMappingIdList)
  835. return
  836. }
  837. // DelChapter
  838. // @Description: 删除报告章节、授权用户权限、品种权限
  839. // @author: Roc
  840. // @datetime 2024-06-06 17:28:37
  841. // @param reportInfo *models.Report
  842. // @param reportChapterInfo *models.ReportChapter
  843. // @param sysUser *system.Admin
  844. // @return err error
  845. // @return errMsg string
  846. func DelChapter(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, sysUser *system.Admin) (err error, errMsg string) {
  847. errMsg = "删除失败"
  848. if reportInfo.State == 2 {
  849. errMsg = "该报告已发布,不允许删除"
  850. err = errors.New(errMsg)
  851. return
  852. }
  853. reportInfo.LastModifyAdminId = sysUser.AdminId
  854. reportInfo.LastModifyAdminName = sysUser.RealName
  855. reportInfo.ModifyTime = time.Now()
  856. updateReportCols := []string{"LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}
  857. err = models.DelChapterAndPermission(reportInfo, updateReportCols, reportChapterInfo)
  858. return
  859. }
  860. // DownloadVoice
  861. // @Description: 下载报告音频文件
  862. // @author: Roc
  863. // @datetime 2024-06-13 15:36:46
  864. // @param reportInfo *models.ReportDetail
  865. // @return savePath string
  866. // @return fileName string
  867. // @return err error
  868. // @return errMsg string
  869. func DownloadVoice(reportInfo *models.ReportDetail) (savePath, fileName string, err error, errMsg string) {
  870. errMsg = `下载失败`
  871. // 如果报告有音频,那么下载音频
  872. if reportInfo.VideoUrl != `` {
  873. savePath = time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
  874. fileName = reportInfo.VideoName + ".mp3"
  875. fileBody, tmpErr := http.Get(reportInfo.VideoUrl)
  876. if tmpErr != nil {
  877. err = tmpErr
  878. errMsg = "音频下载失败"
  879. return
  880. }
  881. err = file.SaveFile(fileBody, savePath)
  882. if err != nil {
  883. errMsg = "音频保存失败"
  884. return
  885. }
  886. }
  887. // 如果是章节报告,那么就下载压缩包
  888. if reportInfo.HasChapter == 1 {
  889. videoList, tmpErr := models.GetReportChapterVideoList(reportInfo.Id)
  890. if tmpErr != nil {
  891. err = tmpErr
  892. errMsg = "获取音频列表失败"
  893. return
  894. }
  895. if len(videoList) > 0 {
  896. // 创建zip
  897. savePath = time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  898. fileName = reportInfo.VideoName + ".zip"
  899. zipFile, tmpErr := os.Create(savePath)
  900. if tmpErr != nil {
  901. err = tmpErr
  902. return
  903. }
  904. zipWriter := zip.NewWriter(zipFile)
  905. // 生成zip过程中报错关闭
  906. defer func() {
  907. if err != nil {
  908. zipWriter.Close()
  909. zipFile.Close()
  910. }
  911. }()
  912. // 获取音频,写入zip
  913. for i := 0; i < len(videoList); i++ {
  914. item := videoList[i]
  915. if item.VideoName == "" || item.VideoUrl == "" {
  916. continue
  917. }
  918. // 音频文件后缀
  919. ext := path.Ext(item.VideoUrl)
  920. ioWriter, tmpErr := zipWriter.Create(fmt.Sprintf("%s%s", item.VideoName, ext))
  921. if tmpErr != nil {
  922. err = tmpErr
  923. if os.IsPermission(err) {
  924. fmt.Println("权限不足: ", err)
  925. return
  926. }
  927. return
  928. }
  929. var content []byte
  930. content, err = http.Get(item.VideoUrl)
  931. if err != nil {
  932. content = []byte("")
  933. }
  934. ioWriter.Write(content)
  935. }
  936. // 生成zip后关闭,否则下载文件会损坏
  937. zipWriter.Close()
  938. zipFile.Close()
  939. }
  940. }
  941. return
  942. }
  943. // CheckReportAuthByAdminIdList
  944. // @Description: 根据管理员id列表,判断当前用户是否有报告权限
  945. // @author: Roc
  946. // @datetime 2024-06-13 11:03:10
  947. // @param adminId int
  948. // @param createAdminId int
  949. // @param grantAdminIdList []int
  950. // @return isAuth bool
  951. func CheckReportAuthByAdminIdList(adminId, createAdminId int, grantAdminIdList []int) (isAuth bool) {
  952. // 如果是自己创建的报告,那么就有权限
  953. if adminId == createAdminId {
  954. isAuth = true
  955. return
  956. }
  957. // 如果是授权用户,那么就有权限
  958. if utils.IsCheckInList(grantAdminIdList, adminId) {
  959. isAuth = true
  960. return
  961. }
  962. return
  963. }
  964. // CheckReportAuthByReportChapterInfo
  965. // @Description: 根据报告ID,判断当前用户是否有报告权限
  966. // @author: Roc
  967. // @datetime 2024-06-13 16:21:28
  968. // @param adminId int
  969. // @param reportInfoId int
  970. // @return isAuth bool
  971. // @return err error
  972. func CheckReportAuthByReportChapterInfo(adminId, createAdminId int, reportInfoId int) (isAuth bool, err error) {
  973. // 如果是自己创建的报告,那么就有权限
  974. if adminId == createAdminId {
  975. isAuth = true
  976. return
  977. }
  978. obj := report.ReportGrant{}
  979. chapterGrantList, err := obj.GetGrantListById(reportInfoId)
  980. if err != nil {
  981. return
  982. }
  983. for _, v := range chapterGrantList {
  984. if v.AdminId == adminId {
  985. isAuth = true
  986. return
  987. }
  988. }
  989. return
  990. }
  991. // EditReportLayoutImg
  992. // @Description: 修改报告的版图信息
  993. // @author: Roc
  994. // @datetime 2024-06-18 11:35:00
  995. // @param reportInfo *models.Report
  996. // @param req models.EditLayoutImgReq
  997. // @param sysUser *system.Admin
  998. // @return err error
  999. // @return errMsg string
  1000. func EditReportLayoutImg(reportInfo *models.Report, req models.EditLayoutImgReq, sysUser *system.Admin) (err error, errMsg string) {
  1001. errMsg = `保存失败`
  1002. reportInfo.HeadResourceId = req.HeadResourceId
  1003. reportInfo.HeadImg = req.HeadImg
  1004. reportInfo.EndResourceId = req.EndResourceId
  1005. reportInfo.EndImg = req.EndImg
  1006. reportInfo.CanvasColor = req.CanvasColor
  1007. reportInfo.LastModifyAdminId = sysUser.AdminId
  1008. reportInfo.LastModifyAdminName = sysUser.RealName
  1009. reportInfo.ModifyTime = time.Now()
  1010. updateCols := []string{"HeadResourceId", "HeadImg", "EndResourceId", "EndImg", "CanvasColor", "LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}
  1011. err = reportInfo.UpdateReport(updateCols)
  1012. if err != nil {
  1013. return
  1014. }
  1015. return
  1016. }
  1017. // PublishReport
  1018. // @Description: 报告发布
  1019. // @author: Roc
  1020. // @datetime 2024-06-20 09:44:13
  1021. // @param reportId int
  1022. // @param reportUrl string
  1023. // @param sysUser *system.Admin
  1024. // @return tips string
  1025. // @return err error
  1026. // @return errMsg string
  1027. func PublishReport(reportId int, reportUrl string, sysUser *system.Admin) (tips string, err error, errMsg string) {
  1028. errMsg = `报告发布失败`
  1029. reportInfo, err := models.GetReportByReportId(reportId)
  1030. if err != nil {
  1031. errMsg = "获取报告信息失败"
  1032. return
  1033. }
  1034. if reportInfo == nil {
  1035. errMsg = "获取报告信息失败"
  1036. err = errors.New(errMsg)
  1037. return
  1038. }
  1039. var publishTime time.Time
  1040. if reportInfo.MsgIsSend == 1 && reportInfo.PublishTime.IsZero() { //如果报告曾经发布过,并且已经发送过模版消息,则章节的发布时间为报告的发布时间
  1041. publishTime = reportInfo.PublishTime
  1042. } else {
  1043. publishTime = time.Now()
  1044. }
  1045. var tmpErr error
  1046. // 章节类型的报告(原来的晨周报)
  1047. if reportInfo.HasChapter == 1 {
  1048. tips, tmpErr, errMsg = PublishChapterReport(reportInfo, reportUrl, sysUser)
  1049. if tmpErr != nil {
  1050. err = errors.New("晨周报发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(reportId))
  1051. }
  1052. return
  1053. }
  1054. // 普通报告
  1055. if reportInfo.Content == "" {
  1056. errMsg = `报告内容为空,不可发布`
  1057. err = errors.New("报告内容为空,不需要生成,report_id:" + strconv.Itoa(reportId))
  1058. return
  1059. }
  1060. // 根据审批开关及审批流判断当前报告状态
  1061. state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperatePublish)
  1062. if e != nil {
  1063. errMsg = "操作失败"
  1064. err = errors.New("校验报告当前状态失败, Err: " + e.Error())
  1065. return
  1066. }
  1067. if state == models.ReportStatePublished {
  1068. // 发布报告
  1069. if tmpErr = models.PublishReportById(reportId, publishTime, sysUser.AdminId, sysUser.RealName); tmpErr != nil {
  1070. err = errors.New("报告发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(reportId))
  1071. return
  1072. }
  1073. go func() {
  1074. // 生成音频
  1075. if reportInfo.VideoUrl == "" {
  1076. go UpdateReportVideo(reportInfo)
  1077. }
  1078. // 更新报告Es
  1079. _ = UpdateReportEs(reportId, 2)
  1080. }()
  1081. } else {
  1082. // 从无审批切换为有审批, 状态重置
  1083. if e = models.ResetReportById(reportId, state, sysUser.AdminId, sysUser.RealName); e != nil {
  1084. errMsg = "操作失败"
  1085. err = fmt.Errorf("重置报告状态失败, Err: %s, ReportId: %d", e.Error(), reportId)
  1086. return
  1087. }
  1088. }
  1089. recordItem := &models.ReportStateRecord{
  1090. ReportId: reportId,
  1091. ReportType: 1,
  1092. State: state,
  1093. AdminId: sysUser.AdminId,
  1094. AdminName: sysUser.AdminName,
  1095. CreateTime: time.Now(),
  1096. }
  1097. go func() {
  1098. _, _ = models.AddReportStateRecord(recordItem)
  1099. }()
  1100. // 生成报告pdf和长图
  1101. {
  1102. reportPdfUrl := GetGeneralPdfUrl(reportInfo.ReportCode, reportInfo.ReportLayout)
  1103. go Report2pdfAndJpeg(reportPdfUrl, reportId, 1)
  1104. }
  1105. // 报告权限处理
  1106. {
  1107. minClassifyId, _, tmpErr := getMinClassify(reportInfo)
  1108. if tmpErr != nil {
  1109. return
  1110. }
  1111. go handleReportPermission(int64(reportInfo.Id), minClassifyId)
  1112. }
  1113. return
  1114. }
  1115. // PublishChapterReport
  1116. // @Description: 发布章节类型的报告
  1117. // @author: Roc
  1118. // @datetime 2024-06-28 10:38:50
  1119. // @param reportInfo *models.Report
  1120. // @param reportUrl string
  1121. // @return tips string
  1122. // @return err error
  1123. // @return errMsg string
  1124. func PublishChapterReport(reportInfo *models.Report, reportUrl string, sysUser *system.Admin) (tips string, err error, errMsg string) {
  1125. reportId := reportInfo.Id
  1126. if reportInfo.State == 2 {
  1127. return
  1128. }
  1129. // 获取所有章节列表
  1130. chapters, err := models.GetChapterListByReportId(reportId)
  1131. if err != nil {
  1132. return
  1133. }
  1134. chapterLen := len(chapters)
  1135. if chapterLen <= 0 {
  1136. tips = "报告章节为空,不可发布"
  1137. errMsg = tips
  1138. err = errors.New(tips)
  1139. return
  1140. }
  1141. // 获取报告中的所有章节列表
  1142. chapterList, err := models.GetChapterListByReportId(reportId)
  1143. if err != nil {
  1144. return
  1145. }
  1146. for _, chapter := range chapterList {
  1147. if chapter.PublishState == 1 {
  1148. tips = "还存在未发布的章节"
  1149. errMsg = tips
  1150. err = errors.New(tips)
  1151. return
  1152. }
  1153. }
  1154. // 根据审批开关及审批流判断当前报告状态
  1155. state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperatePublish)
  1156. if e != nil {
  1157. //errMsg = "操作失败"
  1158. err = errors.New("校验报告当前状态失败, Err: " + e.Error())
  1159. return
  1160. }
  1161. // 如果状态不是已发布,那么就重置状态
  1162. if state != models.ReportStatePublished {
  1163. // 从无审批切换为有审批, 状态重置
  1164. if e = models.ResetReportById(reportId, state, sysUser.AdminId, sysUser.RealName); e != nil {
  1165. //errMsg = "操作失败"
  1166. err = fmt.Errorf("重置报告状态失败, Err: %s, ReportId: %d", e.Error(), reportId)
  1167. return
  1168. }
  1169. return
  1170. }
  1171. // 需发布整期
  1172. updateCols := make([]string, 0)
  1173. reportInfo.LastModifyAdminId = sysUser.AdminId
  1174. reportInfo.LastModifyAdminName = sysUser.RealName
  1175. updateCols = append(updateCols, "LastModifyAdminId", "LastModifyAdminName", "Title", "State", "ModifyTime")
  1176. // 发布后标题调整
  1177. //title := reportInfo.Title
  1178. //title = strings.ReplaceAll(title, "【弘则FICC晨报】", "")
  1179. //title = strings.ReplaceAll(title, "【弘则FICC周报】", "")
  1180. //if title == "" {
  1181. // // 取第一个需发布章节的标题
  1182. // firstId := publishIdArr[0]
  1183. // firstTitle := ""
  1184. // for i := 0; i < chapterLen; i++ {
  1185. // if chapters[i].ReportChapterId == firstId {
  1186. // firstTitle = chapters[i].Title
  1187. // break
  1188. // }
  1189. // }
  1190. // title = firstTitle
  1191. //}
  1192. //report.Title = title
  1193. reportInfo.State = state
  1194. // 研报后台4.4 只在没有发布过时更新发布时间,其余均按模版消息发送时间当作发布时间
  1195. if reportInfo.MsgIsSend == 0 || reportInfo.PublishTime.IsZero() {
  1196. reportInfo.PublishTime = time.Now().Local()
  1197. updateCols = append(updateCols, "PublishTime")
  1198. }
  1199. reportInfo.ModifyTime = time.Now().Local()
  1200. if e := models.PublishReportAndChapter(reportInfo, true, updateCols); e != nil {
  1201. err = errors.New("发布报告及章节失败")
  1202. return
  1203. }
  1204. // 生成章节音频
  1205. go UpdateReportVideo(reportInfo)
  1206. // 更新报告ES
  1207. go func() {
  1208. _ = UpdateReportEs(reportInfo.Id, 2)
  1209. }()
  1210. // 发布时备份内容
  1211. go SaveReportLogs(reportInfo, chapters, reportInfo.AdminId, reportInfo.AdminRealName)
  1212. // 生成报告pdf和长图
  1213. {
  1214. reportPdfUrl := GetGeneralPdfUrl(reportInfo.ReportCode, reportInfo.ReportLayout)
  1215. go Report2pdfAndJpeg(reportPdfUrl, reportId, 1)
  1216. }
  1217. return
  1218. }
  1219. // DeleteReportAndChapter 删除报告及章节
  1220. func DeleteReportAndChapter(reportId int) (err error) {
  1221. reportInfo, err := models.GetReportByReportId(reportId)
  1222. if err != nil {
  1223. err = errors.New("报告信息有误, Err: " + err.Error())
  1224. return
  1225. }
  1226. if reportInfo.State == 2 {
  1227. err = errors.New("报告已发布,不可删除")
  1228. return
  1229. }
  1230. // 更新ES
  1231. _ = UpdateReportEs(reportId, 1)
  1232. // 删除
  1233. if reportInfo.HasChapter == 1 {
  1234. err = models.DeleteDayWeekReportAndChapter(reportId)
  1235. } else {
  1236. err = models.DeleteReport(reportId)
  1237. }
  1238. if err != nil {
  1239. err = errors.New("删除失败, Err: " + err.Error())
  1240. return
  1241. }
  1242. // 重置PPT关联报告
  1243. go func() {
  1244. _ = ResetPPTReport(reportId, false)
  1245. }()
  1246. return
  1247. }
  1248. // getMinClassify
  1249. // @Description: 获取最小分类ID
  1250. // @author: Roc
  1251. // @datetime 2024-06-20 09:23:19
  1252. // @param reportInfo *models.Report
  1253. // @return minClassifyId int
  1254. // @return minClassifyName string
  1255. // @return err error
  1256. func getMinClassify(reportInfo *models.Report) (minClassifyId int, minClassifyName string, err error) {
  1257. defer func() {
  1258. if err != nil {
  1259. utils.FileLog.Error("获取最小分类ID失败,报告ID:%d,Err:%s", reportInfo.Id, err.Error())
  1260. }
  1261. }()
  1262. minClassifyId = reportInfo.ClassifyIdThird
  1263. minClassifyName = reportInfo.ClassifyNameThird
  1264. if minClassifyId <= 0 {
  1265. minClassifyId = reportInfo.ClassifyIdSecond
  1266. minClassifyName = reportInfo.ClassifyNameSecond
  1267. }
  1268. if minClassifyId <= 0 {
  1269. minClassifyId = reportInfo.ClassifyIdFirst
  1270. minClassifyName = reportInfo.ClassifyNameFirst
  1271. }
  1272. if minClassifyId <= 0 {
  1273. err = errors.New("分类异常")
  1274. }
  1275. return
  1276. }
  1277. // handleReportPermission
  1278. // @Description: 报告权限处理
  1279. // @author: Roc
  1280. // @datetime 2024-06-19 18:00:51
  1281. // @param reportId int64
  1282. // @param minClassifyId int
  1283. func handleReportPermission(reportId int64, minClassifyId int) {
  1284. // TODO 报告权限处理
  1285. var err error
  1286. defer func() {
  1287. if err != nil {
  1288. utils.FileLog.Error("报告权限处理失败,报告ID:%d,分类ID:%d,Err:%s", reportId, minClassifyId, err.Error())
  1289. //alarm_msg.SendAlarmMsg("修改删除报告权限失败,Err:"+err.Error(), 3)
  1290. }
  1291. }()
  1292. err = models.RemoveChartPermissionChapterMapping(reportId)
  1293. if err != nil {
  1294. err = errors.New("修改删除报告权限失败,Err:" + err.Error())
  1295. return
  1296. }
  1297. permissionItems, err := models.GetPermission(minClassifyId)
  1298. if err != nil {
  1299. err = errors.New("获取权限失败,Err:" + err.Error())
  1300. return
  1301. }
  1302. for _, v := range permissionItems {
  1303. err = models.AddChartPermissionChapterMapping(v.ChartPermissionId, reportId)
  1304. if err != nil {
  1305. err = errors.New("新增权限失败,Err:" + err.Error())
  1306. return
  1307. }
  1308. }
  1309. // 同步crm权限
  1310. _ = EditReportPermissionSync(reportId, minClassifyId)
  1311. return
  1312. }
  1313. // HandleReportPermission
  1314. // @Description: 报告权限处理
  1315. // @author: Roc
  1316. // @datetime 2024-07-12 11:17:49
  1317. // @param reportInfo *models.Report
  1318. func HandleReportPermission(reportInfo *models.Report) {
  1319. minClassifyId, _, tmpErr := getMinClassify(reportInfo)
  1320. if tmpErr != nil {
  1321. return
  1322. }
  1323. handleReportPermission(int64(reportInfo.Id), minClassifyId)
  1324. }
  1325. // UpdateReportVideo
  1326. // @Description: 更新报告音频
  1327. // @author: Roc
  1328. // @datetime 2024-07-17 18:11:10
  1329. // @param reportInfo *models.Report
  1330. func UpdateReportVideo(reportInfo *models.Report) {
  1331. if reportInfo.HasChapter == 1 {
  1332. reportChapterIdList := make([]int, 0)
  1333. // 获取报告中的所有章节列表
  1334. chapterList, err := models.GetChapterListByReportId(reportInfo.Id)
  1335. if err != nil {
  1336. return
  1337. }
  1338. for _, chapter := range chapterList {
  1339. if chapter.PublishState == 2 {
  1340. reportChapterIdList = append(reportChapterIdList, chapter.ReportChapterId)
  1341. }
  1342. }
  1343. if len(reportChapterIdList) > 0 {
  1344. err = UpdateChaptersVideo(reportChapterIdList)
  1345. if err != nil {
  1346. utils.FileLog.Info(fmt.Sprintf("%d报告音频生产失败:%s", reportInfo.Id, err.Error()))
  1347. }
  1348. }
  1349. } else {
  1350. err := CreateVideo(reportInfo)
  1351. if err != nil {
  1352. utils.FileLog.Info(fmt.Sprintf("%d报告音频生产失败:%s", reportInfo.Id, err.Error()))
  1353. }
  1354. }
  1355. }
  1356. // GetGeneralPdfUrl
  1357. // @Description: 获取生成pdf的地址
  1358. // @author: Roc
  1359. // @datetime 2024-07-19 14:09:28
  1360. // @param reportCode string
  1361. // @param reportLayout int8
  1362. // @return pdfUrl string
  1363. func GetGeneralPdfUrl(reportCode string, reportLayout int8) (pdfUrl string) {
  1364. conf, e := models.GetBusinessConfByKey("ReportViewUrl")
  1365. if e != nil {
  1366. return
  1367. }
  1368. switch reportLayout {
  1369. case 1:
  1370. // 普通布局
  1371. pdfUrl = fmt.Sprintf("%s/reportshare_pdf?code=%s", conf.ConfVal, reportCode)
  1372. case 2:
  1373. // 智能布局
  1374. pdfUrl = fmt.Sprintf("%s/reportshare_smart_pdf?code=%s", conf.ConfVal, reportCode)
  1375. }
  1376. return
  1377. }
  1378. // GetReportShareUrlToken 获取报告分享链接token
  1379. func GetReportShareUrlToken(req models.ReportShartUrlReq, adminId int) (linkToken string, err error) {
  1380. cacheLinkKey := utils.CACHE_REPORT_SHARE_SHORT_Url + strconv.Itoa(req.ReportId) + "userId:" + strconv.Itoa(adminId)
  1381. linkToken, _ = utils.Rc.RedisString(cacheLinkKey)
  1382. if linkToken != "" && utils.Rc.IsExist(utils.CACHE_REPORT_SHARE_ORIGIN_Url+linkToken) {
  1383. return
  1384. }
  1385. var ok bool
  1386. // 冲突检测
  1387. for i := 0; i < 3; i++ {
  1388. linkToken = req.Url
  1389. if i > 0 {
  1390. linkToken += "_" + utils.GetRandDigit(3)
  1391. }
  1392. hashUrl := utils.MurmurHash64([]byte(linkToken))
  1393. linkToken = utils.ConvertNumToBase62(hashUrl)
  1394. ok = utils.Rc.IsExist(utils.CACHE_REPORT_SHARE_ORIGIN_Url + linkToken)
  1395. if !ok {
  1396. break
  1397. }
  1398. }
  1399. if !ok {
  1400. after := time.Now().AddDate(0, 0, 7)
  1401. err = utils.Rc.Put(cacheLinkKey, linkToken, time.Until(after))
  1402. if err != nil {
  1403. return
  1404. }
  1405. err = utils.Rc.Put(utils.CACHE_REPORT_SHARE_ORIGIN_Url+linkToken, req.Url, time.Until(after))
  1406. if err != nil {
  1407. return
  1408. }
  1409. } else {
  1410. linkToken = ""
  1411. err = errors.New("生成链接失败")
  1412. }
  1413. return
  1414. }
  1415. func TransfromToOriginUrl(linkToken string) (originLink string, msg string, err error) {
  1416. cacheLinkKey := utils.CACHE_REPORT_SHARE_ORIGIN_Url + linkToken
  1417. originLink, err = utils.Rc.RedisString(cacheLinkKey)
  1418. if err != nil {
  1419. if err == redis.Nil {
  1420. msg = "链接已失效, 请重新获取"
  1421. return
  1422. }
  1423. msg = "获取链接失败"
  1424. return
  1425. }
  1426. if originLink == "" {
  1427. msg = "链接已失效, 请重新获取"
  1428. return
  1429. }
  1430. return
  1431. }
  1432. func FilterShareUrl() web.FilterFunc {
  1433. return func(c *context.Context) {
  1434. path := c.Input.Context.Request.URL.Path
  1435. tokenArr := strings.Split(path, "/")
  1436. token := tokenArr[len(tokenArr)-1]
  1437. newPath := "/v1/report/share/link"
  1438. q := c.Input.Context.Request.URL.Query()
  1439. q.Add("Token", token)
  1440. c.Input.Context.Request.URL.Path = newPath
  1441. c.Input.Context.Request.URL.RawQuery = q.Encode()
  1442. utils.ApiLog.Info(fmt.Sprintf("原始请求为:%s, 已修改请求路径为:%s?%s", path, newPath, q.Encode()))
  1443. }
  1444. }