Browse Source

Merge branch 'eta/2.3' into debug

Roc 4 months ago
parent
commit
d8bf3331b1

+ 44 - 0
controllers/data_manage/edb_classify.go

@@ -1380,3 +1380,47 @@ func (this *EdbClassifyController) ClassifyTree() {
 //	br.Success = true
 //	br.Msg = "移动成功"
 //}
+
+// initFixClassify
+// @Description: 修复分类完整路径
+// @author: Roc
+// @datetime 2024-11-26 15:40:57
+func initFixClassify() {
+	allList := make([]*data_manage.EdbClassifyItems, 0)
+	allList, e := data_manage.GetEdbClassifyByClassifyTypes([]int{utils.EdbClassifyTypeBase, utils.EdbClassifyTypePredict, utils.EdbClassifyTypeCalculate})
+	if e != nil {
+		fmt.Println("获取分类失败", e)
+		return
+	}
+
+	var sortList data_manage.EdbClassifyItemList
+
+	allList = data.GetClassifyTreeRecursive(allList, 0)
+	//根据sort值排序
+	sortList = allList
+	sort.Sort(sortList)
+
+	for _, v := range sortList {
+		updatePath(v, "", "")
+	}
+	fmt.Println("修复分类路径完成")
+}
+
+func updatePath(item *data_manage.EdbClassifyItems, classifyNamePath, classifyIdPath string) {
+	if classifyNamePath == `` {
+		classifyNamePath = item.ClassifyName
+		classifyIdPath = fmt.Sprint(item.ClassifyId)
+	} else {
+		classifyNamePath = fmt.Sprint(classifyNamePath, "|", item.ClassifyName)
+		classifyIdPath = fmt.Sprint(classifyIdPath, ",", item.ClassifyId)
+	}
+	err := data_manage.UpdateEdbClassify(item.ClassifyId, classifyNamePath, classifyIdPath)
+	if err != nil {
+		fmt.Println(item.ClassifyId, ";更新失败", err)
+	}
+	if item.Children != nil {
+		for _, v := range item.Children {
+			updatePath(v, classifyNamePath, classifyIdPath)
+		}
+	}
+}

+ 49 - 5
models/data_manage/edb_classify.go

@@ -25,14 +25,12 @@ type EdbClassify struct {
 	LastModifyUserRealName string    `description:"最后修改人姓名"`
 	IsJoinPermission       int       `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
 	ClassifyNameEn         string    `description:"英文分类名称"`
+	ClassifyNamePath       string    `description:"分类名称的完整路径,格式为:父级分类名称|当前分类名称"`
+	ClassifyIdPath         string    `description:"分类的完整路径,格式为:父级ID,当前ID"`
 }
 
-func AddEdbClassify(item *EdbClassify) (lastId int64, err error) {
+func AddEdbClassify(item *EdbClassify) (err error) {
 	err = global.DmSQL["data"].Create(item).Error
-	if err != nil {
-		return
-	}
-	lastId = int64(item.ClassifyId)
 
 	return
 }
@@ -554,3 +552,49 @@ func GetEdbClassifyByClassifyTypes(classifyTypes []int) (items []*EdbClassifyIte
 	err = o.Raw(sql, classifyTypes).Scan(&items).Error
 	return
 }
+
+// UpdateEdbClassifyNameAndNamePath
+// @Description: 更新分类名称和子分类的全路径
+// @author: Roc
+// @receiver edbClassify
+// @datetime 2024-11-26 14:39:09
+// @param cols []string
+// @param oldClassifyNamePath string
+// @param newClassifyNamePath string
+// @return err error
+func (edbClassify *EdbClassify) UpdateEdbClassifyNameAndNamePath(cols []string, oldClassifyNamePath, newClassifyNamePath string) (err error) {
+	tx := global.DmSQL["data"].Begin()
+	defer func() {
+		if err != nil {
+			_ = tx.Rollback()
+			return
+		}
+		_ = tx.Commit()
+	}()
+
+	// 变更分类信息
+	err = tx.Select(cols).Updates(edbClassify).Error
+	if err != nil {
+		return
+	}
+
+	// 更改子分类的完整的路径
+	if oldClassifyNamePath != `` && newClassifyNamePath != `` {
+		sql := `UPDATE edb_classify SET classify_name_path = REPLACE(classify_name_path,?,?) WHERE classify_id_path LIKE ?`
+		err = tx.Exec(sql, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(edbClassify.ClassifyIdPath+`,%`)).Error
+		if err != nil {
+			return
+		}
+	}
+
+	return
+}
+
+// UpdateEdbClassify 更新分类基础信息
+func UpdateEdbClassify(classifyId int, classifyNamePath, classifyIdPath string) (err error) {
+	sql := `UPDATE edb_classify SET classify_name_path = ?,classify_id_path = ? WHERE classify_id = ?`
+
+	err = global.DmSQL["data"].Exec(sql, classifyNamePath, classifyIdPath, classifyId).Error
+
+	return
+}

+ 59 - 24
services/data/edb_classify.go

@@ -288,6 +288,7 @@ func AddEdbClassify(classifyName string, parentId, level int, classifyType uint8
 	}
 	//查询顶级rootId
 	rootId := 0
+	var classifyNamePath, classifyIdPath string
 	if parentId > 0 {
 		parentClassify, tErr := data_manage.GetEdbClassifyById(parentId)
 		if tErr != nil {
@@ -301,30 +302,39 @@ func AddEdbClassify(classifyName string, parentId, level int, classifyType uint8
 			return
 		}
 		rootId = parentClassify.RootId
+
+		classifyNamePath = fmt.Sprint(parentClassify.ClassifyNamePath, "|", classifyName)
+		classifyIdPath = fmt.Sprint(parentClassify.ClassifyIdPath, ",")
+	} else {
+		classifyNamePath = classifyName
 	}
 
 	timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
 	classifyInfo = &data_manage.EdbClassify{
 		//ClassifyId:      0,
-		ClassifyType:    classifyType,
-		ClassifyName:    classifyName,
-		ClassifyNameEn:  classifyName,
-		ParentId:        parentId,
-		RootId:          rootId,
-		HasData:         0,
-		CreateTime:      time.Now(),
-		ModifyTime:      time.Now(),
-		SysUserId:       sysUserId,
-		SysUserRealName: sysUserName,
-		Level:           level + 1,
-		UniqueCode:      utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
-		Sort:            maxSort + 1,
+		ClassifyType:     classifyType,
+		ClassifyName:     classifyName,
+		ClassifyNameEn:   classifyName,
+		ParentId:         parentId,
+		RootId:           rootId,
+		HasData:          0,
+		CreateTime:       time.Now(),
+		ModifyTime:       time.Now(),
+		SysUserId:        sysUserId,
+		SysUserRealName:  sysUserName,
+		Level:            level + 1,
+		UniqueCode:       utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
+		Sort:             maxSort + 1,
+		ClassifyNamePath: classifyNamePath,
 	}
-	newId, err := data_manage.AddEdbClassify(classifyInfo)
+	err = data_manage.AddEdbClassify(classifyInfo)
 	if err != nil {
 		errMsg = "保存分类失败"
 		return
 	}
+	// 更改分类id完整路径path
+	classifyInfo.ClassifyIdPath = fmt.Sprint(classifyIdPath, classifyInfo.ClassifyId)
+	_ = classifyInfo.Update([]string{"ClassifyIdPath"})
 
 	// 继承分类权限
 	{
@@ -335,17 +345,22 @@ func AddEdbClassify(classifyName string, parentId, level int, classifyType uint8
 		if classifyType == utils.EdbClassifyTypeCalculate {
 			source = utils.EdbPermissionSourceCalculate
 		}
-		go data_manage_permission.InheritParentClassify(source, int(classifyType), int(newId), classifyInfo.ParentId, classifyInfo.ClassifyName)
+		go data_manage_permission.InheritParentClassify(source, int(classifyType), classifyInfo.ClassifyId, classifyInfo.ParentId, classifyInfo.ClassifyName)
 	}
 
+	// 更改分类id完整路径path
+	updateCols := []string{"ClassifyIdPath"}
+	classifyInfo.ClassifyIdPath = fmt.Sprint(classifyIdPath, classifyInfo.ClassifyId)
+
 	if parentId == 0 { //一级目录的rootId等于自己本身
-		classifyInfo.ClassifyId = int(newId)
-		classifyInfo.RootId = int(newId)
-		err = classifyInfo.Update([]string{"RootId"})
-		if err != nil {
-			errMsg = "更新分类失败"
-			return
-		}
+		classifyInfo.RootId = classifyInfo.ClassifyId
+		updateCols = append(updateCols, "RootId")
+
+	}
+	err = classifyInfo.Update(updateCols)
+	if err != nil {
+		errMsg = "更新分类失败"
+		return
 	}
 
 	return
@@ -392,6 +407,9 @@ func EditEdbClassify(classifyId int, classifyName, lang string, sysUser *system.
 	// 需要变更的字段
 	updateCols := make([]string, 0)
 
+	// 旧完整路径  , 新的完整路径
+	var oldClassifyNamePath, newClassifyNamePath string
+
 	switch lang {
 	case utils.EnLangVersion:
 		// 名字相同,那么就直接返回
@@ -434,15 +452,32 @@ func EditEdbClassify(classifyId int, classifyName, lang string, sysUser *system.
 			err = errors.New(errMsg)
 			return
 		}
+
+		// 旧完整路径
+		oldClassifyNamePath = item.ClassifyNamePath
+
+		if item.ParentId > 0 {
+			parentItem, tmpErr := data_manage.GetEdbClassifyById(item.ParentId)
+			if tmpErr != nil {
+				err = tmpErr
+				return
+			}
+			newClassifyNamePath = fmt.Sprint(parentItem.ClassifyNamePath, "|", classifyName)
+		} else {
+			newClassifyNamePath = classifyName
+		}
+
+		// 更新自己的信息
 		item.ClassifyName = classifyName
+		item.ClassifyNamePath = newClassifyNamePath
 		item.LastModifyUserId = sysUser.AdminId
 		item.LastModifyUserRealName = sysUser.RealName
-		updateCols = append(updateCols, "ClassifyName", "LastModifyUserId", "LastModifyUserRealName")
+		updateCols = append(updateCols, "ClassifyName", "ClassifyNamePath", "LastModifyUserId", "LastModifyUserRealName")
 	}
 
 	// 修改数据
 	if len(updateCols) > 0 {
-		err = item.Update(updateCols)
+		err = item.UpdateEdbClassifyNameAndNamePath(updateCols, oldClassifyNamePath, newClassifyNamePath)
 		if err != nil {
 			errMsg = "保存失败"
 		}