report_v2.go 59 KB

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