report_v2.go 51 KB

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