report_v2.go 45 KB

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