report_v2.go 59 KB

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