report_v2.go 48 KB

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