report_v2.go 45 KB

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