123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614 |
- package services
- import (
- "archive/zip"
- "errors"
- "eta/eta_api/models"
- "eta/eta_api/models/company"
- "eta/eta_api/models/report"
- "eta/eta_api/models/report_approve"
- "eta/eta_api/models/system"
- "eta/eta_api/models/yb"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/file"
- "github.com/rdlucklib/rdluck_tools/http"
- "html"
- "os"
- "path"
- "strconv"
- "time"
- )
- // AddReportAndChapter
- // @Description: 新增报告(包含章节)
- // @author: Roc
- // @datetime 2024-06-04 11:23:20
- // @param reportInfo *models.Report
- // @param inheritReportId int
- // @return err error
- // @return errMsg string
- func AddReportAndChapter(reportInfo *models.Report, inheritReportId int, grantAdminIdList []int, reportDate time.Time) (err error, errMsg string) {
- // 根据审批开关及审批流判断当前报告状态
- state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperateAdd)
- if e != nil {
- //errMsg = "操作失败"
- err = errors.New("校验报告当前状态失败, Err: " + e.Error())
- return
- }
- // 报告状态
- reportInfo.State = state
- // 获取最小分类id
- minClassifyId := reportInfo.ClassifyIdThird
- if minClassifyId <= 0 {
- minClassifyId = reportInfo.ClassifyIdSecond
- }
- if minClassifyId <= 0 {
- minClassifyId = reportInfo.ClassifyIdFirst
- }
- if minClassifyId <= 0 {
- errMsg = "分类异常"
- err = errors.New(errMsg)
- return
- }
- errMsg = "生成报告失败"
- // 报告继承
- if inheritReportId > 0 {
- inheritReport, tmpErr := models.GetReportByReportId(inheritReportId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "获取待继承的报告失败"
- err = tmpErr
- return
- }
- if inheritReport != nil {
- // 判断当前的报告分类与继承的报告分类是否一致
- if inheritReport.ClassifyIdFirst != reportInfo.ClassifyIdFirst || inheritReport.ClassifyIdSecond != reportInfo.ClassifyIdSecond || inheritReport.ClassifyIdThird != reportInfo.ClassifyIdThird {
- errMsg = "分类异常,与继承的报告分类不一致"
- err = errors.New(errMsg)
- return
- }
- reportInfo.ChapterType = inheritReport.ChapterType
- reportInfo.Content = inheritReport.Content
- reportInfo.ContentSub = inheritReport.ContentSub
- reportInfo.ContentStruct = inheritReport.ContentStruct
- reportInfo.CanvasColor = inheritReport.CanvasColor
- //reportInfo.NeedSplice = inheritReport.NeedSplice
- reportInfo.NeedSplice = 1
- reportInfo.HeadResourceId = inheritReport.HeadResourceId
- //reportInfo.HeadImg = inheritReport.HeadImg
- //reportInfo.EndImg = inheritReport.EndImg
- reportInfo.HeadImg = ``
- reportInfo.EndImg = ``
- reportInfo.EndResourceId = inheritReport.EndResourceId
- if inheritReport.HeadResourceId > 0 {
- reportInfo.HeadImg = inheritReport.HeadImg
- }
- if inheritReport.EndResourceId > 0 {
- reportInfo.EndImg = inheritReport.EndImg
- }
- reportInfo.InheritReportId = inheritReport.Id
- }
- }
- // 获取待生成的报告章节
- addChapterList, allGrantUserList, err, errMsg := getAddChapter(reportInfo, minClassifyId, inheritReportId, grantAdminIdList, reportDate)
- // 新增报告及章节
- var reportId int64
- reportId, err = models.AddReportAndChapter(reportInfo, allGrantUserList, addChapterList)
- if err != nil {
- err = errors.New("新增报告及章节失败, Err: " + err.Error())
- return
- }
- reportCode := utils.MD5(strconv.Itoa(reportInfo.Id))
- reportInfo.ReportCode = reportCode
- // 修改唯一编码
- {
- go models.ModifyReportCode(reportId, reportCode)
- }
- // 报告权限处理
- go handleReportPermission(reportId, minClassifyId)
- return
- }
- // EditReport
- // @Description: 修改报告的基础信息、授权用户权限
- // @author: Roc
- // @datetime 2024-06-06 17:16:58
- // @param reportInfo *models.Report
- // @param req models.EditReq
- // @param sysUser *system.Admin
- // @return err error
- // @return errMsg string
- func EditReport(reportInfo *models.Report, req models.EditReq, sysUser *system.Admin) (err error, errMsg string) {
- errMsg = `保存失败`
- //var stage int
- //if reportInfo.ClassifyNameFirst != req.ClassifyNameFirst || reportInfo.ClassifyNameSecond != req.ClassifyNameSecond {
- // // 报告期数
- // maxStage, _ := models.GetReportStageEdit(req.ClassifyIdFirst, req.ClassifyIdSecond, req.ClassifyIdThird, int(req.ReportId))
- // maxStage = maxStage + 1
- // stage = maxStage
- //} else {
- // stage = reportInfo.Stage
- //}
- //if req.State != reportInfo.State {
- // recordItem := &models.ReportStateRecord{
- // ReportId: int(req.ReportId),
- // ReportType: 1,
- // State: req.State,
- // AdminId: this.SysUser.AdminId,
- // AdminName: this.SysUser.AdminName,
- // CreateTime: time.Now(),
- // }
- // go func() {
- // _, _ = models.AddReportStateRecord(recordItem)
- // }()
- //}
- //item := new(models.Report)
- //reportInfo.ClassifyIdFirst = req.ClassifyIdFirst
- //reportInfo.ClassifyNameFirst = req.ClassifyNameFirst
- //reportInfo.ClassifyIdSecond = req.ClassifyIdSecond
- //reportInfo.ClassifyNameSecond = req.ClassifyNameSecond
- //reportInfo.ClassifyIdThird = req.ClassifyIdThird
- //reportInfo.ClassifyNameThird = req.ClassifyNameThird
- reportInfo.Title = req.Title
- reportInfo.Abstract = req.Abstract
- reportInfo.Author = req.Author
- reportInfo.Frequency = req.Frequency
- //reportInfo.State = reportInfo.State // 编辑不变更状态
- //reportInfo.Stage = stage // 编辑不变更期数
- //reportInfo.Content = html.EscapeString(req.Content) // 编辑不变更具体内容
- //reportInfo.ContentSub = html.EscapeString(contentSub) // 编辑不变更具体内容
- reportInfo.CreateTime = req.CreateTime
- //reportInfo.CollaborateType = req.CollaborateType
- //reportInfo.ReportLayout = req.ReportLayout
- if req.IsPublicPublish <= 0 {
- req.IsPublicPublish = 1
- }
- reportInfo.IsPublicPublish = req.IsPublicPublish
- reportInfo.LastModifyAdminId = sysUser.AdminId
- reportInfo.LastModifyAdminName = sysUser.RealName
- reportInfo.ModifyTime = time.Now()
- state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperateEdit)
- if e != nil {
- //errMsg = "操作失败"
- err = errors.New("校验报告当前状态失败, Err: " + e.Error())
- return
- }
- // 报告状态
- reportInfo.State = state
- //updateCols := []string{"ClassifyIdFirst", "ClassifyNameFirst", "ClassifyIdSecond", "ClassifyNameSecond", "ClassifyIdThird", "ClassifyNameThird", "Title", "Abstract", "Author", "Frequency", "Stage", "CreateTime", "IsPublicPublish", "LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}
- updateCols := []string{"Title", "Abstract", "Author", "Frequency", "CreateTime", "IsPublicPublish", "LastModifyAdminId", "LastModifyAdminName", "ModifyTime", "State"}
- if req.HeadResourceId > 0 {
- reportInfo.HeadResourceId = req.HeadResourceId
- updateCols = append(updateCols, "HeadResourceId")
- }
- if req.EndResourceId > 0 {
- reportInfo.EndResourceId = req.EndResourceId
- updateCols = append(updateCols, "EndResourceId")
- }
- // 需要添加的报告授权数据
- addReportAdminList := make([]*report.ReportGrant, 0)
- // 待移除的报告授权数据id
- delReportGrantIdList := make([]int, 0)
- // 处理当前报告需要新增/移除的授权信息
- {
- reportGrantObj := report.ReportGrant{}
- // 获取当前报告已经授权的用户信息
- reportGrantList, tmpErr := reportGrantObj.GetGrantListById(reportInfo.Id)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 当前报告已经授权的用户信息
- currReportAdminMap := make(map[int]*report.ReportGrant)
- // 需要删除的报告授权数据
- delReportAdminMap := make(map[int]*report.ReportGrant)
- for _, v := range reportGrantList {
- currReportAdminMap[v.AdminId] = v
- delReportAdminMap[v.AdminId] = v
- }
- // 先看需要新增哪些用户
- for _, tmpAdminId := range req.GrantAdminIdList {
- _, ok := currReportAdminMap[tmpAdminId]
- // 如果章节中需要新增的用户 已经在 报告授权用户里面,那么就忽略,可以不用新增了
- if ok {
- delete(delReportAdminMap, tmpAdminId)
- continue
- }
- // 如果不存在,那么就新增授权
- addReportAdminList = append(addReportAdminList, &report.ReportGrant{
- //GrantId: 0,
- ReportId: reportInfo.Id,
- AdminId: tmpAdminId,
- CreateTime: time.Now(),
- })
- }
- // 查出需要移除的授权id
- for _, v := range delReportAdminMap {
- delReportGrantIdList = append(delReportGrantIdList, v.GrantId)
- }
- }
- // 修改报告的基本信息,以及报告的授权用户
- err = models.EditReportAndPermission(reportInfo, updateCols, addReportAdminList, delReportGrantIdList)
- if err != nil {
- return
- }
- // 报告权限处理
- {
- minClassifyId, _, tmpErr := getMinClassify(reportInfo)
- if tmpErr != nil {
- return
- }
- go handleReportPermission(int64(reportInfo.Id), minClassifyId)
- }
- return
- }
- // getAddChapter
- // @Description: 获取待新增的报告章节列表
- // @author: Roc
- // @datetime 2024-06-04 13:10:58
- // @param reportInfo *models.Report
- // @param minClassifyId int
- // @param inheritReportId int
- // @param grantAdminIdList []int
- // @return chapterList []*models.ReportChapter
- // @return err error
- // @return errMsg string
- func getAddChapter(reportInfo *models.Report, minClassifyId, inheritReportId int, grantAdminIdList []int, reportDate time.Time) (chapterList []models.AddReportChapter, allGrantUserList []*report.ReportGrant, err error, errMsg string) {
- // 待生成的报告章节内容
- chapterList = make([]models.AddReportChapter, 0)
- // 所有的授权用户
- allGrantUserList = make([]*report.ReportGrant, 0)
- // 报告授权的用户ID
- needAdminIdMap := make(map[int]bool)
- for _, adminId := range grantAdminIdList {
- needAdminIdMap[adminId] = true
- allGrantUserList = append(allGrantUserList, &report.ReportGrant{
- //GrantId: 0,
- //ReportId: 0,
- AdminId: adminId,
- CreateTime: time.Now(),
- })
- }
- if reportInfo.HasChapter != 1 {
- return
- }
- // 最小单元的分类
- var minClassifyName string
- if reportInfo.ClassifyIdThird == minClassifyId {
- minClassifyName = reportInfo.ClassifyNameThird
- } else if reportInfo.ClassifyIdSecond == minClassifyId {
- minClassifyName = reportInfo.ClassifyNameSecond
- } else {
- minClassifyName = reportInfo.ClassifyNameFirst
- }
- errMsg = "生成报告章节失败"
- // 章节类型列表
- allTypeList, err := models.GetReportChapterTypeListByClassifyId(minClassifyId)
- if err != nil {
- err = errors.New(errMsg)
- return
- }
- // 待添加的章节
- chapterTypeList := make([]*models.ReportChapterType, 0)
- // 待添加的章节类型id列表
- chapterTypeIdList := make([]int, 0)
- //nowTime := time.Now().Local()
- for _, chapterType := range allTypeList {
- // 如果被永久暂停更新了
- if chapterType.Enabled == 0 { //该章节已被永久禁用,那么就不允许继承或者新增该章节
- continue
- }
- if chapterType.PauseStartTime != "" && chapterType.PauseEndTime != "" && chapterType.PauseStartTime != utils.EmptyDateStr && chapterType.PauseEndTime != utils.EmptyDateStr {
- startTime, timeErr := time.ParseInLocation(utils.FormatDate, chapterType.PauseStartTime, time.Local)
- if timeErr != nil {
- utils.FileLog.Error("更新规则时间转换失败4001, Err: " + timeErr.Error())
- continue
- }
- endTime, timeErr := time.ParseInLocation(utils.FormatDate, chapterType.PauseEndTime, time.Local)
- if timeErr != nil {
- utils.FileLog.Error("更新规则时间转换失败4002, Err: " + timeErr.Error())
- continue
- }
- // 暂停更新(此处用报告日期去判断是否需要停更,而不是报告创建日期)
- if !reportDate.Before(startTime) && !reportDate.After(endTime) {
- continue
- }
- }
- // 正常的章节类型状态,那么应该自动创建该章节
- chapterTypeList = append(chapterTypeList, chapterType)
- chapterTypeIdList = append(chapterTypeIdList, chapterType.ReportChapterTypeId)
- }
- // 待继承的章节类id的授权用户
- oldChapterIdGrantListMap := make(map[int][]*report.ReportChapterGrant)
- // 待继承的章节类id关联的品种列表
- oldChapterPermissionListMap := make(map[int][]*report.ReportChapterPermissionMapping)
- // 自定义章节列表
- customAddChapterList := make([]models.AddReportChapter, 0)
- // 报告继承
- inheritChapterMap := make(map[int]*models.ReportChapter)
- // 当前分类下配置的章节类型id所关联的品种列表(默认配置,不是从继承报告里面获取的,如果有继承章节,那么需要从继承报告里面获取)
- currChapterTypePermissionListMap := make(map[int][]*report.ReportChapterPermissionMapping)
- if len(chapterTypeIdList) > 0 {
- mappingList, e := models.GetChapterTypePermissionByChapterTypeIdList(chapterTypeIdList)
- if e != nil {
- err = fmt.Errorf("获取章节类型权限列表失败, Err: " + e.Error())
- return
- }
- hasPermissionMap := make(map[string]bool)
- for _, v := range mappingList {
- tmpChapterTypePermissionList, ok := currChapterTypePermissionListMap[v.ReportChapterTypeId]
- if !ok {
- tmpChapterTypePermissionList = make([]*report.ReportChapterPermissionMapping, 0)
- }
- key := fmt.Sprint(v.ReportChapterTypeId, "-", v.ChartPermissionId)
- if _, has := hasPermissionMap[key]; !has {
- hasPermissionMap[key] = true
- currChapterTypePermissionListMap[v.ReportChapterTypeId] = append(tmpChapterTypePermissionList, &report.ReportChapterPermissionMapping{
- ReportChapterPermissionMappingId: 0,
- ReportChapterId: 0,
- ChartPermissionId: v.ChartPermissionId,
- CreateTime: time.Now(),
- })
- }
- }
- }
- if inheritReportId > 0 {
- // 继承待继承的报告章节内容
- inheritReportChapters, tmpErr := models.GetChapterListByReportId(inheritReportId)
- if tmpErr != nil {
- errMsg = "获取待继承的报告章节失败"
- err = tmpErr
- return
- }
- reportChaptersIdList := make([]int, 0)
- for _, v := range inheritReportChapters {
- reportChaptersIdList = append(reportChaptersIdList, v.ReportChapterId)
- }
- // 继承的报告章节用户map
- grantListMap := make(map[int][]*report.ReportChapterGrant)
- // 继承的报告章节的关联品种map
- chapterPermissionListMap := make(map[int][]*report.ReportChapterPermissionMapping)
- // 授权数据列表
- if len(reportChaptersIdList) > 0 {
- // 授权用户数据
- obj := report.ReportChapterGrant{}
- grantList, tmpErr := obj.GetGrantListByIdList(reportChaptersIdList)
- if tmpErr != nil {
- errMsg = "获取待继承的报告章节的授权用户列表失败"
- err = tmpErr
- return
- }
- for _, v := range grantList {
- // 如果不在报告授权的用户ID里面,那么该章节就不继承该授权用户
- if _, ok := needAdminIdMap[v.AdminId]; !ok {
- continue
- }
- currReportChapterId := v.ReportChapterId
- tmpGrantList, ok := grantListMap[currReportChapterId]
- if !ok {
- tmpGrantList = make([]*report.ReportChapterGrant, 0)
- }
- v.ReportChapterId = 0
- v.GrantId = 0
- tmpGrantList = append(tmpGrantList, v)
- grantListMap[currReportChapterId] = tmpGrantList
- }
- // 授权关联品种数据
- permissionObj := report.ReportChapterPermissionMapping{}
- permissionList, tmpErr := permissionObj.GetPermissionListByIdList(reportChaptersIdList)
- if tmpErr != nil {
- errMsg = "获取待继承的报告章节的授权用户列表失败"
- err = tmpErr
- return
- }
- for _, v := range permissionList {
- currReportChapterId := v.ReportChapterId
- tmpPermissionList, ok := chapterPermissionListMap[currReportChapterId]
- if !ok {
- tmpPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
- }
- v.ReportChapterId = 0
- v.ReportChapterPermissionMappingId = 0
- tmpPermissionList = append(tmpPermissionList, v)
- chapterPermissionListMap[currReportChapterId] = tmpPermissionList
- }
- }
- // 继承的报告章节内容
- for i := 0; i < len(inheritReportChapters); i++ {
- customChapter := inheritReportChapters[i]
- // 授权用户列表
- tmpGrantList, ok := grantListMap[customChapter.ReportChapterId]
- if !ok {
- tmpGrantList = make([]*report.ReportChapterGrant, 0)
- }
- oldChapterIdGrantListMap[customChapter.ReportChapterId] = tmpGrantList
- // 关联品种列表
- chapterPermissionList, ok := chapterPermissionListMap[customChapter.ReportChapterId]
- if !ok {
- chapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
- }
- oldChapterPermissionListMap[customChapter.ReportChapterId] = chapterPermissionList
- // 判断该章节是否是系统章节,如果是的话,那就是需要额外创建的
- if customChapter.TypeId > 0 {
- inheritChapterMap[customChapter.TypeId] = customChapter
- continue
- }
- // 自定义的报告内容
- customChapter.ReportId = reportInfo.Id
- customChapter.ReportChapterId = 0
- customChapter.ClassifyIdFirst = minClassifyId
- customChapter.ClassifyNameFirst = minClassifyName
- customChapter.Stage = reportInfo.Stage
- customChapter.PublishState = 1
- customChapter.CreateTime = reportInfo.CreateTime
- customChapter.ModifyTime = time.Now()
- customChapter.LastModifyAdminId = reportInfo.LastModifyAdminId
- customChapter.LastModifyAdminName = reportInfo.LastModifyAdminName
- customChapter.ContentModifyTime = time.Now()
- customAddChapter := models.AddReportChapter{
- ReportChapter: customChapter,
- GrantList: tmpGrantList,
- GrantPermissionList: chapterPermissionList,
- }
- customAddChapterList = append(customAddChapterList, customAddChapter)
- }
- }
- // 最大排序
- var maxSort int
- for _, typeItem := range chapterTypeList {
- v, ok := inheritChapterMap[typeItem.ReportChapterTypeId]
- // 章节授权用户
- var tmpGrantList []*report.ReportChapterGrant
- // 章节关联品种
- var tmpChapterPermissionList []*report.ReportChapterPermissionMapping
- chapterItem := new(models.ReportChapter)
- if ok && v != nil {
- // 如果存在继承的章节,那么就从继承的章节内容中获取
- chapterItem.AddType = 2
- chapterItem.Title = v.Title
- chapterItem.ReportType = v.ReportType
- chapterItem.ClassifyIdFirst = minClassifyId
- chapterItem.ClassifyNameFirst = minClassifyName
- chapterItem.TypeId = typeItem.ReportChapterTypeId
- chapterItem.TypeName = typeItem.ReportChapterTypeName
- chapterItem.Content = v.Content
- chapterItem.ContentSub = v.ContentSub
- chapterItem.Stage = reportInfo.Stage
- chapterItem.PublishState = 1
- chapterItem.Sort = typeItem.Sort
- chapterItem.CreateTime = reportInfo.CreateTime
- chapterItem.ModifyTime = time.Now()
- chapterItem.LastModifyAdminId = reportInfo.LastModifyAdminId
- chapterItem.LastModifyAdminName = reportInfo.LastModifyAdminName
- chapterItem.ContentModifyTime = time.Now()
- chapterItem.ContentStruct = v.ContentStruct
- chapterItem.ReportLayout = v.ReportLayout
- chapterItem.ReportCreateTime = time.Now()
- // 继承历史章节中的授权用户列表
- tmpGrantList, ok = oldChapterIdGrantListMap[v.ReportChapterId]
- if !ok {
- tmpGrantList = make([]*report.ReportChapterGrant, 0)
- }
- // 继承历史章节中的关联品种列表
- tmpChapterPermissionList, ok = oldChapterPermissionListMap[v.ReportChapterId]
- if !ok {
- tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
- }
- } else {
- // 如果系统章节未从继承的报告中找到,那么获取往期中最新版本的此章节内容
- var findChapter bool
- if inheritReportId > 0 {
- chapterNewest, e := models.GetNewestPreReportChapterByClassifyIdAndTypeId(minClassifyId, typeItem.ReportChapterTypeId)
- if e != nil && e.Error() == utils.ErrNoRow() {
- errMsg = "继承最新内容的章节失败"
- err = fmt.Errorf("获取最新内容的章节失败, %v", e)
- return
- }
- if chapterNewest != nil {
- chapterItem.AddType = 2
- chapterItem.Title = chapterNewest.Title
- chapterItem.ReportType = chapterNewest.ReportType
- chapterItem.ClassifyIdFirst = minClassifyId
- chapterItem.ClassifyNameFirst = minClassifyName
- chapterItem.TypeId = typeItem.ReportChapterTypeId
- chapterItem.TypeName = typeItem.ReportChapterTypeName
- chapterItem.Content = chapterNewest.Content
- chapterItem.ContentSub = chapterNewest.ContentSub
- chapterItem.Stage = reportInfo.Stage
- chapterItem.PublishState = 1
- chapterItem.Sort = typeItem.Sort
- chapterItem.CreateTime = reportInfo.CreateTime
- chapterItem.ModifyTime = time.Now()
- chapterItem.LastModifyAdminId = reportInfo.LastModifyAdminId
- chapterItem.LastModifyAdminName = reportInfo.LastModifyAdminName
- chapterItem.ContentModifyTime = time.Now()
- chapterItem.ContentStruct = chapterNewest.ContentStruct
- chapterItem.ReportLayout = chapterNewest.ReportLayout
- chapterItem.ReportCreateTime = time.Now()
- // 这里的授权和品种权限需要额外查询
- tmpGrantList = make([]*report.ReportChapterGrant, 0)
- tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
- {
- grantOb := report.ReportChapterGrant{}
- chapterGrant, e := grantOb.GetGrantListById(chapterNewest.ReportChapterId)
- if e != nil {
- errMsg = "获取待继承的报告章节的授权用户列表失败"
- err = fmt.Errorf("获取待继承的报告章节的授权用户列表失败, ChapterId: %d, Err: %v", chapterNewest.ReportChapterId, e)
- return
- }
- for _, cg := range chapterGrant {
- // 如果不在报告授权的用户ID里面,那么该章节就不继承该授权用户
- if _, ok := needAdminIdMap[cg.AdminId]; !ok {
- continue
- }
- cg.ReportChapterId = 0
- cg.GrantId = 0
- tmpGrantList = append(tmpGrantList, cg)
- }
- permissionOb := report.ReportChapterPermissionMapping{}
- permissions, e := permissionOb.GetPermissionListById(chapterNewest.ReportChapterId)
- if e != nil {
- errMsg = "获取待继承的报告章节的品种权限失败"
- err = fmt.Errorf("获取待继承的报告章节的品种权限失败, ChapterId: %d, Err: %v", chapterNewest.ReportChapterId, e)
- return
- }
- for _, ps := range permissions {
- ps.ReportChapterId = 0
- ps.ReportChapterPermissionMappingId = 0
- tmpChapterPermissionList = append(tmpChapterPermissionList, ps)
- }
- }
- findChapter = true
- }
- }
- // 没找到那就新增一篇空白的
- if !findChapter {
- chapterItem.AddType = 1
- chapterItem.Title = typeItem.ReportChapterTypeName
- chapterItem.ReportType = typeItem.ResearchType
- chapterItem.ClassifyIdFirst = minClassifyId
- chapterItem.ClassifyNameFirst = minClassifyName
- chapterItem.TypeId = typeItem.ReportChapterTypeId
- chapterItem.TypeName = typeItem.ReportChapterTypeName
- chapterItem.Stage = reportInfo.Stage
- chapterItem.PublishState = 1
- chapterItem.Sort = typeItem.Sort
- chapterItem.CreateTime = reportInfo.CreateTime
- chapterItem.ModifyTime = time.Now()
- chapterItem.LastModifyAdminId = reportInfo.LastModifyAdminId
- chapterItem.LastModifyAdminName = reportInfo.LastModifyAdminName
- chapterItem.ContentModifyTime = time.Now()
- //chapterItem.ContentStruct = v.ContentStruct
- chapterItem.ReportLayout = reportInfo.ReportLayout
- chapterItem.ReportCreateTime = time.Now()
- // 默认配置:从当前分类下配置的章节类型id所关联的品种列表
- tmpChapterPermissionList, ok = currChapterTypePermissionListMap[typeItem.ReportChapterTypeId]
- if !ok {
- tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
- }
- }
- }
- if typeItem.Sort > maxSort {
- maxSort = typeItem.Sort
- }
- addChapter := models.AddReportChapter{
- ReportChapter: chapterItem,
- GrantList: tmpGrantList,
- GrantPermissionList: tmpChapterPermissionList,
- }
- chapterList = append(chapterList, addChapter)
- }
- // 将自定义的章节内容添加到待生成的章节内容中
- for _, addChapterItem := range customAddChapterList {
- maxSort++
- addChapterItem.ReportChapter.Sort = maxSort
- chapterList = append(chapterList, addChapterItem)
- }
- //hasGrantUserMap := make(map[int]bool)
- //for _, grantList := range typeGrantListMap {
- // for _, grant := range grantList {
- // if _, ok := hasGrantUserMap[grant.AdminId]; !ok {
- // allGrantUserList = append(allGrantUserList, &report.ReportGrant{
- // //GrantId: 0,
- // //ReportId: 0,
- // AdminId: grant.AdminId,
- // CreateTime: time.Now(),
- // })
- // }
- // }
- //}
- return
- }
- // AddChapterBaseInfoAndPermission
- // @Description: 添加章节基本信息及权限
- // @author: Roc
- // @datetime 2024-06-11 15:35:23
- // @param reportInfo *models.Report
- // @param reportChapterInfo *models.ReportChapter
- // @param permissionIdList []int
- // @param adminIdList []int
- // @return err error
- // @return errMsg string
- func AddChapterBaseInfoAndPermission(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, permissionIdList []int, adminIdList []int) (err error, errMsg string) {
- errMsg = "新增失败"
- if reportInfo.State == 2 {
- errMsg = "该报告已发布,不允许编辑"
- err = errors.New(errMsg)
- return
- }
- // 需要添加的报告章节授权数据
- addChapterAdminList := make([]*report.ReportChapterGrant, 0)
- for _, adminId := range adminIdList {
- //新增授权
- addChapterAdminList = append(addChapterAdminList, &report.ReportChapterGrant{
- //GrantId: 0,
- //ReportChapterId: reportChapterInfo.ReportChapterId,
- AdminId: adminId,
- CreateTime: time.Now(),
- })
- }
- // 需要添加的报告章节品种权限数据
- addChapterPermissionList := make([]*report.ReportChapterPermissionMapping, 0)
- // 品种权限
- for _, permissionId := range permissionIdList {
- // 如果不存在,那么就新增品种权限配置
- addChapterPermissionList = append(addChapterPermissionList, &report.ReportChapterPermissionMapping{
- //ReportChapterPermissionMappingId: 0,
- //ReportChapterId: reportChapterInfo.ReportChapterId,
- ChartPermissionId: permissionId,
- CreateTime: time.Now(),
- })
- }
- err = models.AddChapterBaseInfoAndPermission(reportChapterInfo, addChapterAdminList, addChapterPermissionList)
- return
- }
- // EditChapterBaseInfoAndPermission
- // @Description: 修改报告章节的基础信息、授权用户权限、品种权限
- // @author: Roc
- // @datetime 2024-06-05 11:49:11
- // @param reportInfo *models.Report
- // @param reportChapterInfo *models.ReportChapter
- // @param title string
- // @param permissionIdList []int
- // @param adminIdList []int
- // @return err error
- // @return errMsg string
- func EditChapterBaseInfoAndPermission(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, title string, permissionIdList []int, adminIdList []int) (err error, errMsg string) {
- errMsg = "修改失败"
- if reportInfo.State == 2 {
- errMsg = "该报告已发布,不允许编辑"
- err = errors.New(errMsg)
- return
- }
- updateCols := make([]string, 0)
- // 如果标题内容,那么就修改
- if title != `` {
- reportChapterInfo.Title = title
- reportChapterInfo.ModifyTime = time.Now()
- updateCols = append(updateCols, "Title", "ModifyTime")
- reportChapterInfo.UpdateChapter(updateCols)
- }
- reportGrantObj := report.ReportGrant{}
- chapterGrantObj := report.ReportChapterGrant{}
- chapterPermissionObj := report.ReportChapterPermissionMapping{}
- // 报告授权的用户map
- reportGrantAdminIdMap := make(map[int]bool)
- // 获取报告授权的用户信息
- {
- // 获取当前报告已经授权的用户信息
- reportGrantList, tmpErr := reportGrantObj.GetGrantListById(reportInfo.Id)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for _, v := range reportGrantList {
- reportGrantAdminIdMap[v.AdminId] = true
- }
- }
- // 需要添加的报告章节授权数据
- addChapterAdminList := make([]*report.ReportChapterGrant, 0)
- // 待移除的报告章节授权数据id
- delReportChapterGrantIdList := make([]int, 0)
- // 处理当前报告章节需要新增/移除的授权信息
- {
- // 获取当前章节已经授权的用户信息
- chapterGrantList, tmpErr := chapterGrantObj.GetGrantListById(reportChapterInfo.ReportChapterId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 当前章节已经授权的用户信息
- currChapterAdminMap := make(map[int]*report.ReportChapterGrant)
- // 需要删除的报告章节授权数据
- delChapterAdminMap := make(map[int]*report.ReportChapterGrant)
- for _, v := range chapterGrantList {
- currChapterAdminMap[v.AdminId] = v
- delChapterAdminMap[v.AdminId] = v
- }
- for _, adminId := range adminIdList {
- // 如果该用户 不在 报告授权的用户map 里面,说明这个用户是要移除的
- if _, ok := reportGrantAdminIdMap[adminId]; !ok {
- continue
- }
- _, ok := currChapterAdminMap[adminId]
- // 如果存在,那么从 “需要删除的报告章节授权数据” 的map中移除
- if ok {
- delete(delChapterAdminMap, adminId)
- continue
- }
- // 如果不存在,那么就新增授权
- addChapterAdminList = append(addChapterAdminList, &report.ReportChapterGrant{
- //GrantId: 0,
- ReportChapterId: reportChapterInfo.ReportChapterId,
- AdminId: adminId,
- CreateTime: time.Now(),
- })
- }
- // 查出需要移除的授权id
- for _, v := range delChapterAdminMap {
- delReportChapterGrantIdList = append(delReportChapterGrantIdList, v.GrantId)
- }
- }
- // 其他章节授权的用户
- otherChapterAdminMap := make(map[int]bool)
- {
- // 获取报告所有的章节id
- reportChapterIdList, tmpErr := models.GetReportChapterIdList(reportInfo.Id)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if len(reportChapterIdList) > 0 {
- list, tmpErr := chapterGrantObj.GetGrantListByIdList(reportChapterIdList)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for _, v := range list {
- // 如果是当前章节,因为涉及到重新授权,所以得过滤
- if v.ReportChapterId == reportChapterInfo.ReportChapterId {
- continue
- }
- otherChapterAdminMap[v.AdminId] = true
- }
- }
- }
- // 需要添加的报告章节品种权限数据
- addChapterPermissionList := make([]*report.ReportChapterPermissionMapping, 0)
- // 待移除的报告章节品种权限数据id
- delChapterPermissionMappingIdList := make([]int, 0)
- // 处理当前报告章节需要新增/移除的品种权限信息
- {
- // 获取当前章节已经配置的品种权限信息
- chapterPermissionList, tmpErr := chapterPermissionObj.GetPermissionListById(reportChapterInfo.ReportChapterId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 当前章节已经配置的品种权限信息
- currChapterPermissionMap := make(map[int]*report.ReportChapterPermissionMapping)
- // 需要删除的报告章节品种权限配置
- delChapterPermissionMap := make(map[int]*report.ReportChapterPermissionMapping)
- for _, v := range chapterPermissionList {
- currChapterPermissionMap[v.ChartPermissionId] = v
- delChapterPermissionMap[v.ChartPermissionId] = v
- }
- for _, permissionId := range permissionIdList {
- _, ok := currChapterPermissionMap[permissionId]
- // 如果存在,那么从 “需要删除的报告章节品种权限配置” 的map中移除
- if ok {
- delete(delChapterPermissionMap, permissionId)
- continue
- }
- // 如果不存在,那么就新增品种权限配置
- addChapterPermissionList = append(addChapterPermissionList, &report.ReportChapterPermissionMapping{
- //ReportChapterPermissionMappingId: 0,
- ReportChapterId: reportChapterInfo.ReportChapterId,
- ChartPermissionId: permissionId,
- CreateTime: time.Now(),
- })
- }
- // 查出需要移除的品种权限配置
- for _, v := range delChapterPermissionMap {
- delChapterPermissionMappingIdList = append(delChapterPermissionMappingIdList, v.ReportChapterPermissionMappingId)
- }
- }
- err = models.EditChapterBaseInfoAndPermission(reportInfo, reportChapterInfo, updateCols, addChapterAdminList, addChapterPermissionList, delReportChapterGrantIdList, delChapterPermissionMappingIdList)
- return
- }
- // DelChapter
- // @Description: 删除报告章节、授权用户权限、品种权限
- // @author: Roc
- // @datetime 2024-06-06 17:28:37
- // @param reportInfo *models.Report
- // @param reportChapterInfo *models.ReportChapter
- // @param sysUser *system.Admin
- // @return err error
- // @return errMsg string
- func DelChapter(reportInfo *models.Report, reportChapterInfo *models.ReportChapter, sysUser *system.Admin) (err error, errMsg string) {
- errMsg = "删除失败"
- if reportInfo.State == 2 {
- errMsg = "该报告已发布,不允许删除"
- err = errors.New(errMsg)
- return
- }
- reportInfo.LastModifyAdminId = sysUser.AdminId
- reportInfo.LastModifyAdminName = sysUser.RealName
- reportInfo.ModifyTime = time.Now()
- updateReportCols := []string{"LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}
- err = models.DelChapterAndPermission(reportInfo, updateReportCols, reportChapterInfo)
- return
- }
- // DownloadVoice
- // @Description: 下载报告音频文件
- // @author: Roc
- // @datetime 2024-06-13 15:36:46
- // @param reportInfo *models.ReportDetail
- // @return savePath string
- // @return fileName string
- // @return err error
- // @return errMsg string
- func DownloadVoice(reportInfo *models.ReportDetail) (savePath, fileName string, err error, errMsg string) {
- errMsg = `下载失败`
- // 如果报告有音频,那么下载音频
- if reportInfo.VideoUrl != `` {
- savePath = time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".mp3"
- fileName = reportInfo.VideoName + ".mp3"
- fileBody, tmpErr := http.Get(reportInfo.VideoUrl)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "音频下载失败"
- return
- }
- err = file.SaveFile(fileBody, savePath)
- if err != nil {
- errMsg = "音频保存失败"
- return
- }
- }
- // 如果是章节报告,那么就下载压缩包
- if reportInfo.HasChapter == 1 {
- videoList, tmpErr := models.GetReportChapterVideoList(reportInfo.Id)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "获取音频列表失败"
- return
- }
- if len(videoList) > 0 {
- // 创建zip
- savePath = time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
- fileName = reportInfo.VideoName + ".zip"
- zipFile, tmpErr := os.Create(savePath)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- zipWriter := zip.NewWriter(zipFile)
- // 生成zip过程中报错关闭
- defer func() {
- if err != nil {
- zipWriter.Close()
- zipFile.Close()
- }
- }()
- // 获取音频,写入zip
- for i := 0; i < len(videoList); i++ {
- item := videoList[i]
- if item.VideoName == "" || item.VideoUrl == "" {
- continue
- }
- // 音频文件后缀
- ext := path.Ext(item.VideoUrl)
- ioWriter, tmpErr := zipWriter.Create(fmt.Sprintf("%s%s", item.VideoName, ext))
- if tmpErr != nil {
- err = tmpErr
- if os.IsPermission(err) {
- fmt.Println("权限不足: ", err)
- return
- }
- return
- }
- var content []byte
- content, err = http.Get(item.VideoUrl)
- if err != nil {
- content = []byte("")
- }
- ioWriter.Write(content)
- }
- // 生成zip后关闭,否则下载文件会损坏
- zipWriter.Close()
- zipFile.Close()
- }
- }
- return
- }
- // CheckReportAuthByAdminIdList
- // @Description: 根据管理员id列表,判断当前用户是否有报告权限
- // @author: Roc
- // @datetime 2024-06-13 11:03:10
- // @param adminId int
- // @param createAdminId int
- // @param grantAdminIdList []int
- // @return isAuth bool
- func CheckReportAuthByAdminIdList(adminId, createAdminId int, grantAdminIdList []int) (isAuth bool) {
- // 如果是自己创建的报告,那么就有权限
- if adminId == createAdminId {
- isAuth = true
- return
- }
- // 如果是授权用户,那么就有权限
- if utils.IsCheckInList(grantAdminIdList, adminId) {
- isAuth = true
- return
- }
- return
- }
- // CheckReportAuthByReportId
- // @Description: 根据报告ID,判断当前用户是否有报告权限
- // @author: Roc
- // @datetime 2024-06-13 16:21:28
- // @param adminId int
- // @param reportInfoId int
- // @return isAuth bool
- // @return err error
- func CheckReportAuthByReportId(sysUser *system.Admin, createAdminId int, reportInfoId int) (isAuth bool, err error) {
- // 如果是自己创建的报告,那么就有权限
- if sysUser.AdminId == createAdminId {
- isAuth = true
- return
- }
- // 如果本人是超管,那么就有权限
- if utils.IsAdminRole(sysUser.RoleTypeCode) {
- isAuth = true
- return
- }
- obj := report.ReportGrant{}
- item, err := obj.GetGrantByIdAndAdmin(reportInfoId, sysUser.AdminId)
- if err != nil {
- // 如果是没找到数据,那么就是无权限
- if err.Error() == utils.ErrNoRow() {
- err = nil
- return
- }
- // sql报错了
- return
- }
- // 用户id一致就有权限
- if item.AdminId == sysUser.AdminId {
- isAuth = true
- }
- return
- }
- // EditReportLayoutImg
- // @Description: 修改报告的版图信息
- // @author: Roc
- // @datetime 2024-06-18 11:35:00
- // @param reportInfo *models.Report
- // @param req models.EditLayoutImgReq
- // @param sysUser *system.Admin
- // @return err error
- // @return errMsg string
- func EditReportLayoutImg(reportInfo *models.Report, req models.EditLayoutImgReq, sysUser *system.Admin) (err error, errMsg string) {
- errMsg = `保存失败`
- reportInfo.HeadResourceId = req.HeadResourceId
- reportInfo.HeadImg = req.HeadImg
- reportInfo.EndResourceId = req.EndResourceId
- reportInfo.EndImg = req.EndImg
- reportInfo.CanvasColor = req.CanvasColor
- reportInfo.LastModifyAdminId = sysUser.AdminId
- reportInfo.LastModifyAdminName = sysUser.RealName
- reportInfo.ModifyTime = time.Now()
- updateCols := []string{"HeadResourceId", "HeadImg", "EndResourceId", "EndImg", "CanvasColor", "LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}
- err = reportInfo.UpdateReport(updateCols)
- if err != nil {
- return
- }
- return
- }
- // PublishReport
- // @Description: 报告发布
- // @author: Roc
- // @datetime 2024-06-20 09:44:13
- // @param reportId int
- // @param reportUrl string
- // @param sysUser *system.Admin
- // @return tips string
- // @return err error
- // @return errMsg string
- func PublishReport(reportId int, reportUrl string, sysUser *system.Admin) (tips string, err error, errMsg string) {
- errMsg = `报告发布失败`
- reportInfo, err := models.GetReportByReportId(reportId)
- if err != nil {
- errMsg = "获取报告信息失败"
- return
- }
- if reportInfo == nil {
- errMsg = "获取报告信息失败"
- err = errors.New(errMsg)
- return
- }
- var publishTime time.Time
- if reportInfo.MsgIsSend == 1 && reportInfo.PublishTime.IsZero() { //如果报告曾经发布过,并且已经发送过模版消息,则章节的发布时间为报告的发布时间
- publishTime = reportInfo.PublishTime
- } else {
- publishTime = time.Now()
- }
- var tmpErr error
- // 章节类型的报告(原来的晨周报)
- if reportInfo.HasChapter == 1 {
- tips, tmpErr, errMsg = PublishChapterReport(reportInfo, reportUrl, sysUser)
- if tmpErr != nil {
- err = errors.New("晨周报发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(reportId))
- }
- return
- }
- // 普通报告
- if reportInfo.Content == "" {
- errMsg = `报告内容为空,不可发布`
- err = errors.New("报告内容为空,不需要生成,report_id:" + strconv.Itoa(reportId))
- return
- }
- // 根据审批开关及审批流判断当前报告状态
- state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperatePublish)
- if e != nil {
- errMsg = "操作失败"
- err = errors.New("校验报告当前状态失败, Err: " + e.Error())
- return
- }
- if state == models.ReportStatePublished {
- // 发布报告
- if tmpErr = models.PublishReportById(reportId, publishTime, sysUser.AdminId, sysUser.RealName); tmpErr != nil {
- err = errors.New("报告发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(reportId))
- return
- }
- go func() {
- // 生成音频
- if reportInfo.VideoUrl == "" {
- go UpdateReportVideo(reportInfo)
- }
- // 更新报告Es
- _ = UpdateReportEs(reportId, 2)
- }()
- } else {
- // 从无审批切换为有审批, 状态重置
- if e = models.ResetReportById(reportId, state, sysUser.AdminId, sysUser.RealName); e != nil {
- errMsg = "操作失败"
- err = fmt.Errorf("重置报告状态失败, Err: %s, ReportId: %d", e.Error(), reportId)
- return
- }
- }
- recordItem := &models.ReportStateRecord{
- ReportId: reportId,
- ReportType: 1,
- State: state,
- AdminId: sysUser.AdminId,
- AdminName: sysUser.AdminName,
- CreateTime: time.Now(),
- }
- go func() {
- _, _ = models.AddReportStateRecord(recordItem)
- }()
- // 生成报告pdf和长图
- {
- reportPdfUrl := GetGeneralPdfUrl(reportInfo.ReportCode, reportInfo.ClassifyNameFirst, reportInfo.ReportLayout)
- go Report2pdfAndJpeg(reportPdfUrl, reportId, 1)
- }
- // 报告权限处理
- {
- minClassifyId, _, tmpErr := getMinClassify(reportInfo)
- if tmpErr != nil {
- return
- }
- go handleReportPermission(int64(reportInfo.Id), minClassifyId)
- }
- return
- }
- // PublishChapterReport
- // @Description: 发布章节类型的报告
- // @author: Roc
- // @datetime 2024-06-28 10:38:50
- // @param reportInfo *models.Report
- // @param reportUrl string
- // @return tips string
- // @return err error
- // @return errMsg string
- func PublishChapterReport(reportInfo *models.Report, reportUrl string, sysUser *system.Admin) (tips string, err error, errMsg string) {
- reportId := reportInfo.Id
- if reportInfo.State == 2 {
- return
- }
- // 获取所有章节列表
- chapters, err := models.GetChapterListByReportId(reportId)
- if err != nil {
- return
- }
- chapterLen := len(chapters)
- if chapterLen <= 0 {
- tips = "报告章节为空,不可发布"
- errMsg = tips
- err = errors.New(tips)
- return
- }
- // 获取报告中的所有章节列表
- chapterList, err := models.GetChapterListByReportId(reportId)
- if err != nil {
- return
- }
- for _, chapter := range chapterList {
- if chapter.PublishState == 1 {
- tips = "还存在未发布的章节"
- errMsg = tips
- err = errors.New(tips)
- return
- }
- }
- // 根据审批开关及审批流判断当前报告状态
- state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperatePublish)
- if e != nil {
- //errMsg = "操作失败"
- err = errors.New("校验报告当前状态失败, Err: " + e.Error())
- return
- }
- // 如果状态不是已发布,那么就重置状态
- if state != models.ReportStatePublished {
- // 从无审批切换为有审批, 状态重置
- if e = models.ResetReportById(reportId, state, sysUser.AdminId, sysUser.RealName); e != nil {
- //errMsg = "操作失败"
- err = fmt.Errorf("重置报告状态失败, Err: %s, ReportId: %d", e.Error(), reportId)
- return
- }
- return
- }
- // 需发布整期
- updateCols := make([]string, 0)
- reportInfo.LastModifyAdminId = sysUser.AdminId
- reportInfo.LastModifyAdminName = sysUser.RealName
- updateCols = append(updateCols, "LastModifyAdminId", "LastModifyAdminName", "Title", "State", "ModifyTime")
- // 发布后标题调整
- //title := reportInfo.Title
- //title = strings.ReplaceAll(title, "【弘则FICC晨报】", "")
- //title = strings.ReplaceAll(title, "【弘则FICC周报】", "")
- //if title == "" {
- // // 取第一个需发布章节的标题
- // firstId := publishIdArr[0]
- // firstTitle := ""
- // for i := 0; i < chapterLen; i++ {
- // if chapters[i].ReportChapterId == firstId {
- // firstTitle = chapters[i].Title
- // break
- // }
- // }
- // title = firstTitle
- //}
- //report.Title = title
- reportInfo.State = state
- // 研报后台4.4 只在没有发布过时更新发布时间,其余均按模版消息发送时间当作发布时间
- if reportInfo.MsgIsSend == 0 || reportInfo.PublishTime.IsZero() {
- reportInfo.PublishTime = time.Now().Local()
- updateCols = append(updateCols, "PublishTime")
- }
- reportInfo.ModifyTime = time.Now().Local()
- if e := models.PublishReportAndChapter(reportInfo, true, updateCols); e != nil {
- err = errors.New("发布报告及章节失败")
- return
- }
- // 生成章节音频
- go UpdateReportVideo(reportInfo)
- // 更新报告ES
- go func() {
- _ = UpdateReportEs(reportInfo.Id, 2)
- }()
- // 发布时备份内容
- go SaveReportLogs(reportInfo, chapters, reportInfo.AdminId, reportInfo.AdminRealName)
- // 生成报告pdf和长图
- {
- reportPdfUrl := GetGeneralPdfUrl(reportInfo.ReportCode, reportInfo.ClassifyNameFirst, reportInfo.ReportLayout)
- go Report2pdfAndJpeg(reportPdfUrl, reportId, 1)
- }
- return
- }
- // DeleteReportAndChapter 删除报告及章节
- func DeleteReportAndChapter(reportId int) (err error) {
- reportInfo, err := models.GetReportByReportId(reportId)
- if err != nil {
- err = errors.New("报告信息有误, Err: " + err.Error())
- return
- }
- if reportInfo.State == 2 {
- err = errors.New("报告已发布,不可删除")
- return
- }
- // 更新ES
- _ = UpdateReportEs(reportId, 1)
- // 删除
- if reportInfo.HasChapter == 1 {
- err = models.DeleteDayWeekReportAndChapter(reportId)
- } else {
- err = models.DeleteReport(reportId)
- }
- if err != nil {
- err = errors.New("删除失败, Err: " + err.Error())
- return
- }
- // 重置PPT关联报告
- go func() {
- _ = ResetPPTReport(reportId, false)
- }()
- return
- }
- // getMinClassify
- // @Description: 获取最小分类ID
- // @author: Roc
- // @datetime 2024-06-20 09:23:19
- // @param reportInfo *models.Report
- // @return minClassifyId int
- // @return minClassifyName string
- // @return err error
- func getMinClassify(reportInfo *models.Report) (minClassifyId int, minClassifyName string, err error) {
- defer func() {
- if err != nil {
- utils.FileLog.Error("获取最小分类ID失败,报告ID:%d,Err:%s", reportInfo.Id, err.Error())
- }
- }()
- minClassifyId = reportInfo.ClassifyIdThird
- minClassifyName = reportInfo.ClassifyNameThird
- if minClassifyId <= 0 {
- minClassifyId = reportInfo.ClassifyIdSecond
- minClassifyName = reportInfo.ClassifyNameSecond
- }
- if minClassifyId <= 0 {
- minClassifyId = reportInfo.ClassifyIdFirst
- minClassifyName = reportInfo.ClassifyNameFirst
- }
- if minClassifyId <= 0 {
- err = errors.New("分类异常")
- }
- return
- }
- // handleReportPermission
- // @Description: 报告权限处理
- // @author: Roc
- // @datetime 2024-06-19 18:00:51
- // @param reportId int64
- // @param minClassifyId int
- func handleReportPermission(reportId int64, minClassifyId int) {
- // TODO 报告权限处理
- var err error
- defer func() {
- if err != nil {
- utils.FileLog.Error("报告权限处理失败,报告ID:%d,分类ID:%d,Err:%s", reportId, minClassifyId, err.Error())
- //alarm_msg.SendAlarmMsg("修改删除报告权限失败,Err:"+err.Error(), 3)
- }
- }()
- err = models.RemoveChartPermissionChapterMapping(reportId)
- if err != nil {
- err = errors.New("修改删除报告权限失败,Err:" + err.Error())
- return
- }
- permissionItems, err := models.GetPermission(minClassifyId)
- if err != nil {
- err = errors.New("获取权限失败,Err:" + err.Error())
- return
- }
- for _, v := range permissionItems {
- err = models.AddChartPermissionChapterMapping(v.ChartPermissionId, reportId)
- if err != nil {
- err = errors.New("新增权限失败,Err:" + err.Error())
- return
- }
- }
- // 同步crm权限
- _ = EditReportPermissionSync(reportId, minClassifyId)
- return
- }
- // HandleReportPermission
- // @Description: 报告权限处理
- // @author: Roc
- // @datetime 2024-07-12 11:17:49
- // @param reportInfo *models.Report
- func HandleReportPermission(reportInfo *models.Report) {
- minClassifyId, _, tmpErr := getMinClassify(reportInfo)
- if tmpErr != nil {
- return
- }
- handleReportPermission(int64(reportInfo.Id), minClassifyId)
- }
- // UpdateReportVideo
- // @Description: 更新报告音频
- // @author: Roc
- // @datetime 2024-07-17 18:11:10
- // @param reportInfo *models.Report
- func UpdateReportVideo(reportInfo *models.Report) {
- if reportInfo.HasChapter == 1 {
- reportChapterIdList := make([]int, 0)
- // 获取报告中的所有章节列表
- chapterList, err := models.GetChapterListByReportId(reportInfo.Id)
- if err != nil {
- return
- }
- reportContent := ""
- for _, chapter := range chapterList {
- if chapter.PublishState == 2 {
- reportChapterIdList = append(reportChapterIdList, chapter.ReportChapterId)
- reportContent += chapter.Title + `。`
- reportContent += chapter.Content + `。`
- }
- }
- if len(reportChapterIdList) > 0 {
- err = UpdateChaptersVideo(reportChapterIdList)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("%d报告音频生产失败:%s", reportInfo.Id, err.Error()))
- }
- }
- // 生成汇总音频
- {
- if reportInfo.VideoUrl != "" && reportInfo.VideoName != "" && reportInfo.VideoSize != "" && reportInfo.VideoPlaySeconds != "" {
- return
- }
- weekClassifyId, err := company.GetReportClassifyIdByConfigKey("report_week_classify_id")
- if err != nil && err.Error() != utils.ErrNoRow() {
- msg := "获取周报ID配置失败:" + err.Error()
- utils.FileLog.Info(msg)
- return
- }
- if reportInfo.ClassifyIdFirst == weekClassifyId {
- return
- }
- videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(reportInfo.Title, html.UnescapeString(reportContent), time.Now().Format(utils.FormatDateTime))
- if e != nil {
- err = e
- return
- }
- // 修改报告的音频信息
- err = models.ModifyReportVideoByNoVideo(reportInfo.Id, videoUrl, videoName, videoSize, videoPlaySeconds)
- }
- } else {
- err := CreateVideo(reportInfo)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("%d报告音频生产失败:%s", reportInfo.Id, err.Error()))
- }
- }
- }
- // GetGeneralPdfUrl
- // @Description: 获取生成pdf的地址
- // @author: Roc
- // @datetime 2024-07-19 14:09:28
- // @param reportCode string
- // @param reportLayout int8
- // @return pdfUrl string
- func GetGeneralPdfUrl(reportCode, classifyFirstName string, reportLayout int8) (pdfUrl string) {
- // 如果是弘则,且是晨、周报,那么就不返回
- if utils.InArrayByStr([]string{utils.BusinessCodeRelease, utils.BusinessCodeSandbox, utils.BusinessCodeDebug}, utils.BusinessCode) && utils.InArrayByStr([]string{"晨报", "周报"}, classifyFirstName) {
- return
- }
- // 优先取Report2ImgUrl(用于兼容内外网环境的), 没有的话取报告详情地址
- var reportUrl string
- conf, _ := models.GetBusinessConfByKey(models.BusinessConfReport2ImgUrl)
- if conf != nil && conf.ConfVal != "" {
- reportUrl = conf.ConfVal
- }
- if reportUrl == "" {
- conf, e := models.GetBusinessConfByKey(models.BusinessConfReportViewUrl)
- if e != nil {
- return
- }
- reportUrl = conf.ConfVal
- }
- switch reportLayout {
- case 1:
- // 普通布局
- pdfUrl = fmt.Sprintf("%s/reportshare_pdf?code=%s", reportUrl, reportCode)
- case 2:
- // 智能布局
- pdfUrl = fmt.Sprintf("%s/reportshare_smart_pdf?code=%s", reportUrl, reportCode)
- }
- return
- }
- func GetReportWaterMarkPdf(reportInfo *models.Report, sysUser *system.Admin) {
- dateDir := time.Now().Format("20060102")
- uploadDir := utils.STATIC_DIR + "pdf/" + dateDir
- filePath, err := utils.DownloadFile(reportInfo.DetailPdfUrl, uploadDir)
- if err != nil {
- return
- }
- // 移除临时Pdf文件
- defer func() {
- _ = os.Remove(filePath)
- }()
- waterMarkStr := fmt.Sprintf("%s - %s", sysUser.RealName, sysUser.Mobile)
- GeneralWaterMarkPdf(filePath, waterMarkStr)
- }
- // ResetMiniProgramReportDetailCover 重置小程序报告封面
- func ResetMiniProgramReportDetailCover(reportId int) (err error) {
- if utils.MYSQL_WEEKLY_URL == `` {
- return
- }
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("ResetMiniProgramReportDetailCover-重置小程序报告封面失败, %v", err)
- utils.FileLog.Info(tips)
- alarm_msg.SendAlarmMsg(tips, 2)
- }
- }()
- ob := new(yb.YbPosterResource)
- cond := fmt.Sprintf(` AND path LIKE 'reportDetailCover?ReportId=%d&%%'`, reportId)
- pars := make([]interface{}, 0)
- list, e := ob.GetItemsByCondition(cond, pars, []string{"id"}, "")
- if e != nil {
- err = fmt.Errorf("获取报告海报失败, %v", e)
- return
- }
- if len(list) == 0 {
- return
- }
- var removeIds []int
- for _, v := range list {
- removeIds = append(removeIds, v.Id)
- }
- if e = ob.RemovePosters(removeIds); e != nil {
- err = fmt.Errorf("清除报告海报失败, %v", e)
- }
- return
- }
|