1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102 |
- package services
- import (
- "archive/zip"
- "errors"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/models/report"
- "eta_gn/eta_api/models/report_approve"
- "eta_gn/eta_api/models/system"
- "eta_gn/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/file"
- "github.com/rdlucklib/rdluck_tools/http"
- "html"
- "os"
- "path"
- "strconv"
- "strings"
- "time"
- )
- func AddReportAndChapter(reportInfo *models.Report, inheritReportId int, grantAdminIdList []int) (err error, errMsg string) {
- state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperateAdd)
- if e != nil {
- err = errors.New("校验报告当前状态失败, Err: " + e.Error())
- return
- }
- reportInfo.State = state
- 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 && !utils.IsErrNoRow(tmpErr) {
- 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.HeadImg = inheritReport.HeadImg
- reportInfo.EndImg = inheritReport.EndImg
- reportInfo.CanvasColor = inheritReport.CanvasColor
- reportInfo.NeedSplice = inheritReport.NeedSplice
- reportInfo.HeadResourceId = inheritReport.HeadResourceId
- reportInfo.EndResourceId = inheritReport.EndResourceId
- reportInfo.InheritReportId = inheritReport.Id
- }
- }
- addChapterList, allGrantUserList, err, errMsg := getAddChapter(reportInfo, minClassifyId, inheritReportId, grantAdminIdList)
- 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
- }
- func EditReport(reportInfo *models.Report, req models.EditReq, sysUser *system.Admin) (err error, errMsg string) {
- errMsg = `保存失败`
- reportInfo.Title = req.Title
- reportInfo.Abstract = req.Abstract
- reportInfo.Author = req.Author
- reportInfo.Frequency = req.Frequency
- if req.CreateTime != `` {
- formatDate := utils.FormatDateTime
- if !strings.Contains(req.CreateTime, ":") {
- formatDate = utils.FormatDate
- }
- createTime, tmpErr := time.ParseInLocation(formatDate, req.CreateTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- reportInfo.CreateTime = createTime
- }
- 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 {
- err = errors.New("校验报告当前状态失败, Err: " + e.Error())
- return
- }
- reportInfo.State = state
- 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)
- 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{
- ReportId: reportInfo.Id,
- AdminId: tmpAdminId,
- CreateTime: time.Now(),
- })
- }
- 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
- }
- func getAddChapter(reportInfo *models.Report, minClassifyId, inheritReportId int, grantAdminIdList []int) (chapterList []models.AddReportChapter, allGrantUserList []*report.ReportGrant, err error, errMsg string) {
- chapterList = make([]models.AddReportChapter, 0)
- allGrantUserList = make([]*report.ReportGrant, 0)
- needAdminIdMap := make(map[int]bool)
- for _, adminId := range grantAdminIdList {
- needAdminIdMap[adminId] = true
- allGrantUserList = append(allGrantUserList, &report.ReportGrant{
- 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)
- 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 nowTime.After(startTime) && nowTime.Before(endTime.AddDate(0, 0, 1)) {
- continue
- }
- }
- chapterTypeList = append(chapterTypeList, chapterType)
- chapterTypeIdList = append(chapterTypeIdList, chapterType.ReportChapterTypeId)
- }
- oldChapterIdGrantListMap := make(map[int][]*report.ReportChapterGrant)
- oldChapterPermissionListMap := make(map[int][]*report.ReportChapterPermissionMapping)
- customAddChapterList := make([]models.AddReportChapter, 0)
- inheritChapterMap := make(map[int]*models.ReportChapter)
- 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)
- }
- grantListMap := make(map[int][]*report.ReportChapterGrant)
- 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 {
- 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 {
- 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.ReportLayout = reportInfo.ReportLayout
- chapterItem.ReportCreateTime = time.Now()
- 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)
- }
- return
- }
- 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{
- AdminId: adminId,
- CreateTime: time.Now(),
- })
- }
- addChapterPermissionList := make([]*report.ReportChapterPermissionMapping, 0)
- for _, permissionId := range permissionIdList {
- addChapterPermissionList = append(addChapterPermissionList, &report.ReportChapterPermissionMapping{
- ChartPermissionId: permissionId,
- CreateTime: time.Now(),
- })
- }
- err = models.AddChapterBaseInfoAndPermission(reportChapterInfo, addChapterAdminList, addChapterPermissionList)
- return
- }
- 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{}
- 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)
- 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 {
- if _, ok := reportGrantAdminIdMap[adminId]; !ok {
- continue
- }
- _, ok := currChapterAdminMap[adminId]
- if ok {
- delete(delChapterAdminMap, adminId)
- continue
- }
- addChapterAdminList = append(addChapterAdminList, &report.ReportChapterGrant{
- ReportChapterId: reportChapterInfo.ReportChapterId,
- AdminId: adminId,
- CreateTime: time.Now(),
- })
- }
- for _, v := range delChapterAdminMap {
- delReportChapterGrantIdList = append(delReportChapterGrantIdList, v.GrantId)
- }
- }
- otherChapterAdminMap := make(map[int]bool)
- {
- 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)
- 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
- }
- }
- err = models.EditChapterBaseInfoAndPermission(reportInfo, reportChapterInfo, updateCols, addChapterAdminList, addChapterPermissionList, delReportChapterGrantIdList, delChapterPermissionMappingIdList)
- return
- }
- 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
- }
- 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 {
- 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)
- defer func() {
- if err != nil {
- zipWriter.Close()
- zipFile.Close()
- }
- }()
- 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)
- }
- zipWriter.Close()
- zipFile.Close()
- }
- }
- return
- }
- func CheckReportAuthByAdminIdList(adminId, createAdminId int, grantAdminIdList []int) (isAuth bool) {
- if adminId == createAdminId {
- isAuth = true
- return
- }
- if utils.IsCheckInList(grantAdminIdList, adminId) {
- isAuth = true
- return
- }
- return
- }
- func CheckReportAuthByReportChapterInfo(adminId, createAdminId int, reportInfoId int) (isAuth bool, err error) {
- if adminId == createAdminId {
- isAuth = true
- return
- }
- obj := report.ReportGrant{}
- chapterGrantList, err := obj.GetGrantListById(reportInfoId)
- if err != nil {
- return
- }
- for _, v := range chapterGrantList {
- if v.AdminId == adminId {
- isAuth = true
- return
- }
- }
- return
- }
- 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
- }
- 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)
- }
- _ = 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)
- }()
- {
- 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
- }
- 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 {
- err = errors.New("校验报告当前状态失败, Err: " + e.Error())
- return
- }
- if state != models.ReportStatePublished {
- if e = models.ResetReportById(reportId, state, sysUser.AdminId, sysUser.RealName); e != nil {
- 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")
- reportInfo.State = state
- 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)
- go func() {
- _ = UpdateReportEs(reportInfo.Id, 2)
- }()
- go SaveReportLogs(reportInfo, chapters, reportInfo.AdminId, reportInfo.AdminRealName)
- {
- reportPdfUrl := GetGeneralPdfUrl(reportInfo.ReportCode, reportInfo.ClassifyNameFirst, reportInfo.ReportLayout)
- go Report2pdfAndJpeg(reportPdfUrl, reportId, 1)
- }
- return
- }
- 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
- }
- _ = 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
- }
- go func() {
- _ = ResetPPTReport(reportId, false)
- }()
- return
- }
- 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
- }
- func handleReportPermission(reportId int64, minClassifyId int) {
- var err error
- defer func() {
- if err != nil {
- utils.FileLog.Error("报告权限处理失败,报告ID:%d,分类ID:%d,Err:%s", reportId, minClassifyId, err.Error())
- }
- }()
- 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
- }
- }
- _ = EditReportPermissionSync(reportId, minClassifyId)
- return
- }
- func HandleReportPermission(reportInfo *models.Report) {
- minClassifyId, _, tmpErr := getMinClassify(reportInfo)
- if tmpErr != nil {
- return
- }
- handleReportPermission(int64(reportInfo.Id), minClassifyId)
- }
- 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
- }
- 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()))
- }
- }
- }
- func GetGeneralPdfUrl(reportCode, classifyFirstName string, reportLayout int8) (pdfUrl string) {
- conf, e := models.GetBusinessConfByKey("ReportViewUrl")
- if e != nil {
- return
- }
- switch reportLayout {
- case 1:
- pdfUrl = fmt.Sprintf("%s/reportshare_pdf?code=%s", conf.ConfVal, reportCode)
- case 2:
- pdfUrl = fmt.Sprintf("%s/reportshare_smart_pdf?code=%s", conf.ConfVal, 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
- }
- defer func() {
- _ = os.Remove(filePath)
- }()
- waterMarkStr := fmt.Sprintf("%s - %s", sysUser.RealName, sysUser.Mobile)
- GeneralWaterMarkPdf(filePath, waterMarkStr)
- }
|