report_v2.go 45 KB

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