Bläddra i källkod

fix: 章节协作人继承

hsun 5 månader sedan
förälder
incheckning
0dfeb3b6df
4 ändrade filer med 241 tillägg och 34 borttagningar
  1. 39 6
      models/report.go
  2. 115 0
      models/report_chapter_grant.go
  3. 16 9
      models/report_chapter_type.go
  4. 71 19
      services/report_open.go

+ 39 - 6
models/report.go

@@ -118,9 +118,9 @@ type Report struct {
 	EndResourceId       int       `gorm:"column:end_resource_id" description:"版尾资源ID"`
 	ClassifyIdThird     int       `gorm:"column:classify_id_third" description:"三级分类id"`
 	ClassifyNameThird   string    `gorm:"column:classify_name_third" description:"三级分类名称"`
-	CollaborateType     int8      `gorm:"column:collaborate_type" description:"协作方式,1:个人,2:多人协作。默认:1"`
-	ReportLayout        int8      `gorm:"column:report_layout" description:"报告布局,1:常规布局,2:智能布局。默认:1"`
-	IsPublicPublish     int8      `gorm:"column:is_public_publish" description:"是否公开发布,1:是,2:否"`
+	CollaborateType     int       `gorm:"column:collaborate_type" description:"协作方式,1:个人,2:多人协作。默认:1"`
+	ReportLayout        int       `gorm:"column:report_layout" description:"报告布局,1:常规布局,2:智能布局。默认:1"`
+	IsPublicPublish     int       `gorm:"column:is_public_publish" description:"是否公开发布,1:是,2:否"`
 	ReportCreateTime    time.Time `gorm:"column:report_create_time" description:"报告时间创建时间"`
 	InheritReportId     int       `gorm:"column:inherit_report_id" description:"待继承的报告ID"`
 	VoiceGenerateType   int       `gorm:"column:voice_generate_type" description:"音频生成方式,0:系统生成,1:人工上传"`
@@ -221,7 +221,7 @@ func (m *Report) GetItemsByCondition(condition string, pars []interface{}, field
 }
 
 // CreateReportAndChapters 新增报告和章节
-func (m *Report) CreateReportAndChapters(newReport *Report, newChapters []*ReportChapter, newGrants []*ReportGrant) (reportId int, err error) {
+func (m *Report) CreateReportAndChapters(newReport *Report, newChapters []*ReportChapter, newGrants []*ReportGrant, chapterGrantsMapping map[int][]int) (reportId int, err error) {
 	if newReport == nil {
 		err = fmt.Errorf("report is nil")
 		return
@@ -254,12 +254,45 @@ func (m *Report) CreateReportAndChapters(newReport *Report, newChapters []*Repor
 	}
 
 	// 新增报告章节
+	originNewChapterId := make(map[int]int) // 继承章节ID-新章节ID
 	if len(newChapters) > 0 {
 		for _, v := range newChapters {
+			var originId int
+			if v.ReportChapterId > 0 {
+				originId = v.ReportChapterId
+				v.ReportChapterId = 0
+			}
 			v.ReportId = reportId
+			if e := tx.Create(v).Error; e != nil {
+				err = fmt.Errorf("insert report chapter err: %v", e)
+				return
+			}
+			if originId > 0 {
+				originNewChapterId[originId] = v.ReportChapterId
+			}
 		}
-		if e := tx.CreateInBatches(newChapters, utils.MultiAddNum).Error; e != nil {
-			err = fmt.Errorf("insert report chapters err: %v", e)
+	}
+	//fmt.Printf("originNewChapterId: %v\n", originNewChapterId)
+
+	// 新增章节授权
+	if len(chapterGrantsMapping) == 0 {
+		return
+	}
+	for k, v := range chapterGrantsMapping {
+		if k <= 0 || len(v) == 0 {
+			continue
+		}
+		chapterGrants := make([]*ReportChapterGrant, 0)
+		for _, vv := range v {
+			cg := &ReportChapterGrant{
+				AdminId:         vv,
+				ReportChapterId: originNewChapterId[k],
+				CreateTime:      time.Now(),
+			}
+			chapterGrants = append(chapterGrants, cg)
+		}
+		if e := tx.Create(chapterGrants).Error; e != nil {
+			err = fmt.Errorf("insert report chapter grants err: %v", e)
 			return
 		}
 	}

+ 115 - 0
models/report_chapter_grant.go

@@ -0,0 +1,115 @@
+package models
+
+import (
+	"eta_gn/eta_report/global"
+	"eta_gn/eta_report/utils"
+	"fmt"
+	"strings"
+	"time"
+)
+
+// ReportChapterGrant 报告授权表
+type ReportChapterGrant struct {
+	GrantId         int       `gorm:"primaryKey;column:grant_id;type:int(9) unsigned;not null"`         // 授权id
+	ReportChapterId int       `gorm:"column:report_chapter_id;type:int(9) unsigned;not null;default:0"` // 报告章节id
+	AdminId         int       `gorm:"column:admin_id;type:int(9) unsigned;default:0"`                   // 授权的用户id
+	CreateTime      time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP"`      // 授权时间
+}
+
+func (m *ReportChapterGrant) TableName() string {
+	return "report_chapter_grant"
+}
+
+type ReportChapterGrantCols struct {
+	PrimaryId       string
+	ReportChapterId string
+	AdminId         string
+	CreateTime      string
+}
+
+func (m *ReportChapterGrant) Cols() ReportChapterGrantCols {
+	return ReportChapterGrantCols{
+		PrimaryId:       "grant_id",
+		ReportChapterId: "report_chapter_id",
+		AdminId:         "admin_id",
+		CreateTime:      "create_time",
+	}
+}
+
+func (m *ReportChapterGrant) Create() (err error) {
+	err = global.DEFAULT_DmSQL.Create(m).Error
+	return
+}
+
+func (m *ReportChapterGrant) CreateMulti(items []*ReportChapterGrant) (err error) {
+	if len(items) == 0 {
+		return
+	}
+	err = global.DEFAULT_DmSQL.CreateInBatches(items, utils.MultiAddNum).Error
+	return
+}
+
+func (m *ReportChapterGrant) Update(cols []string) (err error) {
+	err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error
+	return
+}
+
+func (m *ReportChapterGrant) Remove() (err error) {
+	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
+	err = global.DEFAULT_DmSQL.Exec(sql, m.GrantId).Error
+	return
+}
+
+func (m *ReportChapterGrant) MultiRemove(ids []int) (err error) {
+	if len(ids) == 0 {
+		return
+	}
+	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
+	err = global.DEFAULT_DmSQL.Exec(sql, ids).Error
+	return
+}
+
+func (m *ReportChapterGrant) RemoveByCondition(condition string, pars []interface{}) (err error) {
+	if condition == "" {
+		return
+	}
+	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
+	err = global.DEFAULT_DmSQL.Exec(sql, pars...).Error
+	return
+}
+
+func (m *ReportChapterGrant) GetItemById(id int) (item *ReportChapterGrant, err error) {
+	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
+	err = global.DEFAULT_DmSQL.Raw(sql, id).First(&item).Error
+	return
+}
+
+func (m *ReportChapterGrant) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *ReportChapterGrant, err error) {
+	order := ``
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
+	err = global.DEFAULT_DmSQL.Raw(sql, pars...).First(&item).Error
+	return
+}
+
+func (m *ReportChapterGrant) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
+	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
+	err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
+	return
+}
+
+func (m *ReportChapterGrant) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ReportChapterGrant, err error) {
+	fields := strings.Join(fieldArr, ",")
+	if len(fieldArr) == 0 {
+		fields = `*`
+	}
+	order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
+	err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
+	return
+}

+ 16 - 9
models/report_chapter_type.go

@@ -2,7 +2,8 @@ package models
 
 import (
 	"eta_gn/eta_report/global"
-	"eta_gn/eta_report/utils"
+	"fmt"
+	"strings"
 	"time"
 )
 
@@ -31,14 +32,20 @@ type ReportChapterType struct {
 	YbBottomIcon           string    `gorm:"column:yb_bottom_icon" description:"研报小程序详情底部icon"`
 }
 
-// GetAllReportChapterTypeListByResearchType 通过报告类型获取章节类型列表
-func GetAllReportChapterTypeListByResearchType(researchType string) (list []*ReportChapterType, err error) {
-	if utils.BusinessCode != utils.BusinessCodeRelease {
-		return
-	}
-
-	sql := ` SELECT * FROM report_chapter_type WHERE research_type = ?`
-	err = global.DEFAULT_DmSQL.Raw(sql, researchType).Scan(&list).Error
+func (m *ReportChapterType) TableName() string {
+	return "report_chapter_type"
+}
 
+func (m *ReportChapterType) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ReportChapterType, err error) {
+	fields := strings.Join(fieldArr, ",")
+	if len(fieldArr) == 0 {
+		fields = `*`
+	}
+	order := `ORDER BY created_time DESC`
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
+	err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
 	return
 }

+ 71 - 19
services/report_open.go

@@ -114,7 +114,7 @@ func CreatePptReport(outReportId, classifyId int, title string, topicEndTime tim
 		cond := ` AND report_source = ? AND classify_id = ? AND state = ?`
 		pars := make([]interface{}, 0)
 		pars = append(pars, utils.ReportSourceOuter, classifyId, models.ReportStatePass)
-		inheritItem, _ = ob.GetItemByCondition(cond, pars, "")
+		inheritItem, _ = ob.GetItemByCondition(cond, pars, "create_time DESC")
 	}
 	if inheritItem != nil && inheritItem.PptId > 0 {
 		newItem.TemplateType = inheritItem.TemplateType
@@ -172,7 +172,7 @@ func UpdateClassifyReportNum(classifyId int) (err error) {
 			cond += ` AND classify_id_third = ?`
 		}
 		pars := make([]interface{}, 0)
-		pars = append(pars, classifyId, classifyId)
+		pars = append(pars, classifyId)
 		count, e := reportOb.GetCountByCondition(cond, pars)
 		if e != nil {
 			err = fmt.Errorf("获取报告计数失败, %v", e)
@@ -305,7 +305,7 @@ func CreateReport(outReportId, classifyId int, title string, topicEndTime time.T
 
 	// 各级分类信息
 	var classifyIdFirst, classifyIdSecond, classifyIdThird int
-	var classifyNameFirst, classifyNameSecond, classifyNameThird string
+	var classifyNameFirst, classifyNameSecond, classifyNameThird, classifyName string
 	{
 		classifyOb := new(models.Classify)
 		classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
@@ -322,6 +322,7 @@ func CreateReport(outReportId, classifyId int, title string, topicEndTime time.T
 			err = fmt.Errorf("分类不存在, ID: %d", classifyId)
 			return
 		}
+		classifyName = thisClassify.ClassifyName
 
 		// 根据levelPath确认各级分类信息
 		levelArr := strings.Split(thisClassify.LevelPath, ",")
@@ -409,13 +410,14 @@ func CreateReport(outReportId, classifyId int, title string, topicEndTime time.T
 
 	// 自动继承该分类上一篇外部报告(审批通过的)
 	newChapters := make([]*models.ReportChapter, 0)
+	chapterGrantsMapping := make(map[int][]int)
 	inheritItem := new(models.Report)
 	{
 		ob := new(models.Report)
 		cond := ` AND report_source = ? AND state = ? AND classify_id_first = ? AND classify_id_second = ? AND classify_id_third = ?`
 		pars := make([]interface{}, 0)
 		pars = append(pars, utils.ReportSourceOuter, models.ReportStatePass, classifyIdFirst, classifyIdSecond, classifyIdThird)
-		inheritItem, _ = ob.GetItemByCondition(cond, pars, "")
+		inheritItem, _ = ob.GetItemByCondition(cond, pars, "create_time DESC")
 	}
 	if inheritItem != nil && inheritItem.Id > 0 {
 		newItem.InheritReportId = inheritItem.Id
@@ -439,25 +441,75 @@ func CreateReport(outReportId, classifyId int, title string, topicEndTime time.T
 			err = fmt.Errorf("获取继承报告章节失败, %v", e)
 			return
 		}
-		for _, v := range inheritChapters {
-			v.ReportChapterId = 0
-			v.ReportId = 0
-			v.ClassifyIdFirst = classifyIdFirst
-			v.ClassifyNameFirst = classifyNameFirst
-			v.PublishState = 1 // 未发布
-			v.CreateTime = time.Now()
-			v.ModifyTime = time.Now()
-			v.ContentModifyTime = time.Now()
-			v.LastModifyAdminId = creatorAdmin.AdminId
-			v.LastModifyAdminName = creatorAdmin.RealName
-			newChapters = append(newChapters, v)
-		}
+		if len(inheritChapters) > 0 {
+			var originChapterIds []int
+			for _, v := range inheritChapters {
+				originChapterIds = append(originChapterIds, v.ReportChapterId)
+				//v.ReportChapterId = 0	// 此处不置空,插入时需要做新ID和旧ID的匹配
+				v.ReportId = 0
+				v.ClassifyIdFirst = classifyIdFirst
+				v.ClassifyNameFirst = classifyNameFirst
+				v.PublishState = 1 // 未发布
+				v.CreateTime = time.Now()
+				v.ModifyTime = time.Now()
+				v.ContentModifyTime = time.Now()
+				v.LastModifyAdminId = creatorAdmin.AdminId
+				v.LastModifyAdminName = creatorAdmin.RealName
+				newChapters = append(newChapters, v)
+			}
 
-		// TODO:继承的协作人
+			// 这里需要继承原章节的授权,保留在报告协作人中依旧存在的,移除不在报告协作人中的
+			chapterGrantsOb := new(models.ReportChapterGrant)
+			chapterGrantsCond := ` AND report_chapter_id IN (?)`
+			chapterGrantsPars := make([]interface{}, 0)
+			chapterGrantsPars = append(chapterGrantsPars, originChapterIds)
+			originChapterGrants, e := chapterGrantsOb.GetItemsByCondition(chapterGrantsCond, chapterGrantsPars, []string{}, "")
+			if e != nil {
+				err = fmt.Errorf("获取继承章节授权失败, %v", e)
+				return
+			}
+			for _, v := range originChapterGrants {
+				if utils.InArrayByInt(partnerIds, v.AdminId) {
+					chapterGrantsMapping[v.ReportChapterId] = append(chapterGrantsMapping[v.ReportChapterId], v.AdminId)
+				}
+			}
+		}
+	} else {
+		// 找不到继承报告,则找下分类是否有对应的章节设置
+		typesOb := new(models.ReportChapterType)
+		typesCond := ` AND report_classify_id = ?`
+		typesPars := make([]interface{}, 0)
+		typesPars = append(typesPars, classifyId)
+		chapterTypes, e := typesOb.GetItemsByCondition(typesCond, typesPars, []string{}, "")
+		if e != nil {
+			err = fmt.Errorf("获取报告分类章节设置失败, %v", e)
+			return
+		}
+		// 新增默认章节
+		for _, v := range chapterTypes {
+			cp := new(models.ReportChapter)
+			cp.AddType = 1
+			cp.Title = v.ReportChapterTypeName
+			cp.ReportType = v.ResearchType
+			cp.ClassifyIdFirst = classifyId
+			cp.ClassifyNameFirst = classifyName
+			cp.TypeId = v.ReportChapterTypeId
+			cp.TypeName = v.ReportChapterTypeName
+			cp.PublishState = 1
+			cp.Sort = v.Sort
+			cp.CreateTime = newItem.CreateTime
+			cp.ModifyTime = time.Now()
+			cp.LastModifyAdminId = newItem.LastModifyAdminId
+			cp.LastModifyAdminName = newItem.LastModifyAdminName
+			cp.ContentModifyTime = time.Now()
+			cp.ReportLayout = newItem.ReportLayout
+			cp.ReportCreateTime = time.Now()
+			newChapters = append(newChapters, cp)
+		}
 	}
 
 	// 新增报告、章节以及协作人权限
-	newId, e := newItem.CreateReportAndChapters(newItem, newChapters, newGrants)
+	newId, e := newItem.CreateReportAndChapters(newItem, newChapters, newGrants, chapterGrantsMapping)
 	if e != nil {
 		err = fmt.Errorf("新增报告、章节及授权失败, %v", e)
 		return