report_v2.go 48 KB

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