Browse Source

Merge branch 'feature/report_chapter_1129'

hsun 1 day ago
parent
commit
16afb1fac4
4 changed files with 121 additions and 31 deletions
  1. 6 1
      controllers/report_v2.go
  2. 8 0
      models/report_chapter.go
  3. 6 1
      services/report.go
  4. 101 29
      services/report_v2.go

+ 6 - 1
controllers/report_v2.go

@@ -479,7 +479,12 @@ func (this *ReportController) Add() {
 	item.IsPublicPublish = req.IsPublicPublish
 	item.ReportCreateTime = time.Now()
 
-	err, errMsg := services.AddReportAndChapter(item, req.InheritReportId, req.GrantAdminIdList)
+	reportDate := time.Now()
+	t, _ := time.ParseInLocation(utils.FormatDate, req.CreateTime, time.Local)
+	if !t.IsZero() {
+		reportDate = t
+	}
+	err, errMsg := services.AddReportAndChapter(item, req.InheritReportId, req.GrantAdminIdList, reportDate)
 	if err != nil {
 		br.Msg = "保存失败"
 		if errMsg != "" {

+ 8 - 0
models/report_chapter.go

@@ -605,3 +605,11 @@ func GetCountReportChapterByCondition(condition string, pars []interface{}) (cou
 
 	return
 }
+
+// GetNewestPreReportChapterByClassifyIdAndTypeId 获取分类下往期中最新发布的系统章节
+func GetNewestPreReportChapterByClassifyIdAndTypeId(classifyId, typeId int) (item *ReportChapter, err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `SELECT a.* FROM report_chapter AS a JOIN report AS b ON a.report_id = b.id WHERE a.classify_id_first = ? AND a.type_id = ? AND a.publish_state = 2 AND b.state IN (2,6) ORDER BY a.publish_time DESC LIMIT 1`
+	err = o.Raw(sql, classifyId, typeId).QueryRow(&item)
+	return
+}

+ 6 - 1
services/report.go

@@ -732,7 +732,12 @@ func CreateNewReport(req models.AddReq, adminInfo *system.Admin) (newReportId in
 	item.IsPublicPublish = req.IsPublicPublish
 	item.ReportCreateTime = time.Now()
 
-	err, errMsg = AddReportAndChapter(item, 0, req.GrantAdminIdList)
+	reportDate := time.Now()
+	t, _ := time.ParseInLocation(utils.FormatDate, req.CreateTime, time.Local)
+	if !t.IsZero() {
+		reportDate = t
+	}
+	err, errMsg = AddReportAndChapter(item, 0, req.GrantAdminIdList, reportDate)
 
 	return
 }

+ 101 - 29
services/report_v2.go

@@ -25,7 +25,7 @@ import (
 // @param inheritReportId int
 // @return err error
 // @return errMsg string
-func AddReportAndChapter(reportInfo *models.Report, inheritReportId int, grantAdminIdList []int) (err error, 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 {
@@ -92,7 +92,7 @@ func AddReportAndChapter(reportInfo *models.Report, inheritReportId int, grantAd
 	}
 
 	// 获取待生成的报告章节
-	addChapterList, allGrantUserList, err, errMsg := getAddChapter(reportInfo, minClassifyId, inheritReportId, grantAdminIdList)
+	addChapterList, allGrantUserList, err, errMsg := getAddChapter(reportInfo, minClassifyId, inheritReportId, grantAdminIdList, reportDate)
 
 	// 新增报告及章节
 	var reportId int64
@@ -274,7 +274,7 @@ func EditReport(reportInfo *models.Report, req models.EditReq, sysUser *system.A
 // @return chapterList []*models.ReportChapter
 // @return err error
 // @return errMsg string
-func getAddChapter(reportInfo *models.Report, minClassifyId, inheritReportId int, grantAdminIdList []int) (chapterList []models.AddReportChapter, allGrantUserList []*report.ReportGrant, err error, 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)
 
@@ -318,7 +318,7 @@ func getAddChapter(reportInfo *models.Report, minClassifyId, inheritReportId int
 	// 待添加的章节类型id列表
 	chapterTypeIdList := make([]int, 0)
 
-	nowTime := time.Now().Local()
+	//nowTime := time.Now().Local()
 	for _, chapterType := range allTypeList {
 		// 如果被永久暂停更新了
 		if chapterType.Enabled == 0 { //该章节已被永久禁用,那么就不允许继承或者新增该章节
@@ -336,8 +336,8 @@ func getAddChapter(reportInfo *models.Report, minClassifyId, inheritReportId int
 				utils.FileLog.Error("更新规则时间转换失败4002, Err: " + timeErr.Error())
 				continue
 			}
-			// 暂停更新
-			if nowTime.After(startTime) && nowTime.Before(endTime.AddDate(0, 0, 1)) {
+			// 暂停更新(此处用报告日期去判断是否需要停更,而不是报告创建日期)
+			if !reportDate.Before(startTime) && !reportDate.After(endTime) {
 				continue
 			}
 		}
@@ -546,32 +546,104 @@ func getAddChapter(reportInfo *models.Report, minClassifyId, inheritReportId int
 				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.ContentStruct = v.ContentStruct
-			chapterItem.ReportLayout = reportInfo.ReportLayout
-			chapterItem.ReportCreateTime = time.Now()
+			// 如果系统章节未从继承的报告中找到,那么获取往期中最新版本的此章节内容
+			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)
+						}
+					}
 
-			// 默认配置:从当前分类下配置的章节类型id所关联的品种列表
-			tmpChapterPermissionList, ok = currChapterTypePermissionListMap[typeItem.ReportChapterTypeId]
-			if !ok {
-				tmpChapterPermissionList = make([]*report.ReportChapterPermissionMapping, 0)
+					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 {