Browse Source

fix:同一个报告内,章节名称不能重复

Roc 8 months ago
parent
commit
2779db9b1f
2 changed files with 100 additions and 2 deletions
  1. 81 2
      controllers/report_chapter.go
  2. 19 0
      models/report_chapter.go

+ 81 - 2
controllers/report_chapter.go

@@ -13,6 +13,7 @@ import (
 	"os"
 	"path"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -50,6 +51,12 @@ func (this *ReportController) AddChapter() {
 		return
 	}
 
+	req.Title = strings.TrimSpace(req.Title)
+	if req.Title == `` {
+		br.Msg = "章节名称不能为空"
+		return
+	}
+
 	// 获取报告详情
 	reportInfo, err := models.GetReportByReportId(req.ReportId)
 	if err != nil {
@@ -106,6 +113,27 @@ func (this *ReportController) AddChapter() {
 		minClassifyName = reportInfo.ClassifyNameFirst
 	}
 
+	// 判断名称是否重复
+	{
+		var condition string
+		var pars []interface{}
+
+		condition += " AND title=? "
+		pars = append(pars, req.Title)
+		count, err := models.GetCountReportChapterByCondition(condition, pars)
+		if err != nil {
+			br.Msg = "新增失败"
+			br.ErrMsg = "判断章节名称是否存在失败,Err:" + err.Error()
+			return
+		}
+		if count > 0 {
+			br.Msg = "章节名称不允许重复"
+			br.ErrMsg = "章节名称不允许重复"
+			br.IsSendEmail = false
+			return
+		}
+	}
+
 	reportChapterInfo := new(models.ReportChapter)
 	reportChapterInfo.ReportId = reportInfo.Id
 	reportChapterInfo.ClassifyIdFirst = minClassifyId
@@ -181,6 +209,34 @@ func (this *ReportController) EditChapterBaseInfoAndPermission() {
 		br.Msg = "报告章节ID有误"
 		return
 	}
+
+	req.Title = strings.TrimSpace(req.Title)
+	if req.Title == `` {
+		br.Msg = "章节名称不能为空"
+		return
+	}
+
+	// 判断名称是否重复
+	{
+		var condition string
+		var pars []interface{}
+
+		condition += " AND title=? AND report_chapter_id != ? "
+		pars = append(pars, req.Title, reportChapterId)
+		count, err := models.GetCountReportChapterByCondition(condition, pars)
+		if err != nil {
+			br.Msg = "新增失败"
+			br.ErrMsg = "判断章节名称是否存在失败,Err:" + err.Error()
+			return
+		}
+		if count > 0 {
+			br.Msg = "章节名称不允许重复"
+			br.ErrMsg = "章节名称不允许重复"
+			br.IsSendEmail = false
+			return
+		}
+	}
+
 	// 获取章节详情
 	reportChapterInfo, err := models.GetReportChapterInfoById(reportChapterId)
 	if err != nil {
@@ -1435,11 +1491,34 @@ func (this *ReportController) EditChapterTitle() {
 		br.Msg = "报告章节ID有误"
 		return
 	}
-	if req.Title == "" {
-		br.Msg = "请输入标题"
+
+	req.Title = strings.TrimSpace(req.Title)
+	if req.Title == `` {
+		br.Msg = "章节名称不能为空"
 		return
 	}
 
+	// 判断名称是否重复
+	{
+		var condition string
+		var pars []interface{}
+
+		condition += " AND title=? AND report_chapter_id != ? "
+		pars = append(pars, req.Title, reportChapterId)
+		count, err := models.GetCountReportChapterByCondition(condition, pars)
+		if err != nil {
+			br.Msg = "新增失败"
+			br.ErrMsg = "判断章节名称是否存在失败,Err:" + err.Error()
+			return
+		}
+		if count > 0 {
+			br.Msg = "章节名称不允许重复"
+			br.ErrMsg = "章节名称不允许重复"
+			br.IsSendEmail = false
+			return
+		}
+	}
+
 	// 获取章节详情
 	reportChapterInfo, err := models.GetReportChapterInfoById(reportChapterId)
 	if err != nil {

+ 19 - 0
models/report_chapter.go

@@ -585,3 +585,22 @@ func GetAllReportChapter() (items []*ReportChapter, err error) {
 
 	return
 }
+
+// GetCountReportChapterByCondition
+// @Description: 根据条件获取章节数量
+// @author: Roc
+// @datetime 2024-07-15 15:37:50
+// @param condition string
+// @param pars []interface{}
+// @return count int
+// @return err error
+func GetCountReportChapterByCondition(condition string, pars []interface{}) (count int, err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := ` SELECT * FROM report_chapter WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	err = o.Raw(sql, pars).QueryRow(&count)
+
+	return
+}