Browse Source

Merge branch 'feature/public_chart_move_0422' into debug

hsun 13 hours ago
parent
commit
8ae47ce669
2 changed files with 131 additions and 5 deletions
  1. 44 0
      models/data_manage/chart_info.go
  2. 87 5
      services/data/public_chart_classify.go

+ 44 - 0
models/data_manage/chart_info.go

@@ -3003,3 +3003,47 @@ func GetFirstChartInfoByPublicClassifyId(classifyId int) (item *ChartInfo, err e
 
 	return
 }
+
+// GetPublicChartsByClassifyId 获取公开目录下所有图表
+func GetPublicChartsByClassifyId(classifyId, source, exceptId int) (items []*ChartInfo, err error) {
+	var pars []interface{}
+	pars = append(pars, classifyId, source)
+	cond := `chart_public_classify_id = ? AND public_status = 3 AND source = ?`
+	if exceptId > 0 {
+		cond += ` AND chart_info_id <> ?`
+		pars = append(pars, exceptId)
+	}
+	sql := fmt.Sprintf(`SELECT * FROM chart_info WHERE %s ORDER BY public_sort ASC,chart_info_id ASC`, cond)
+	err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
+	return
+}
+
+// UpdatePublicChartsSort 批量更新图表公共sort
+func UpdatePublicChartsSort(updates []*ChartInfo, chartId int, updateCols []string) (err error) {
+	if len(updates) == 0 {
+		return
+	}
+	tx := global.DmSQL["data"].Begin()
+	defer func() {
+		if err != nil {
+			tx.Rollback()
+			return
+		}
+		tx.Commit()
+	}()
+
+	sql := `UPDATE chart_info SET public_sort = ? WHERE chart_info_id = ?`
+	for _, v := range updates {
+		if v.ChartInfoId == chartId {
+			if e := tx.Select(updateCols).Updates(v).Error; e != nil {
+				err = fmt.Errorf("update chart err: %v", e)
+				return
+			}
+		}
+		if e := tx.Exec(sql, v.PublicSort, v.ChartInfoId).Error; e != nil {
+			err = fmt.Errorf("update err: %v", e)
+			return
+		}
+	}
+	return
+}

+ 87 - 5
services/data/public_chart_classify.go

@@ -466,12 +466,10 @@ func movePublicChartOrClassify(parentChartClassifyInfo, chartClassifyInfo, prevC
 	if chartClassifyInfo != nil {
 		// 移动分类
 		return movePublicChartClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify, prevChartInfo, nextChartInfo, parentClassifyId, prevSort, nextSort, source)
-	} else {
-		// 移动指标
-		return moveChartPublic(prevClassify, nextClassify, chartInfo, prevChartInfo, nextChartInfo, parentClassifyId, prevSort, nextSort, source)
 	}
-
-	return
+	// 移动指标
+	return moveChartPublicV2(chartInfo, prevChartInfo, nextChartInfo, parentClassifyId, source)
+	//return moveChartPublic(prevClassify, nextClassify, chartInfo, prevChartInfo, nextChartInfo, parentClassifyId, prevSort, nextSort, source)
 }
 
 // moveChartPublic
@@ -905,3 +903,87 @@ func GetChartChildClassifyByPublicClassifyId(targetClassifyId int) (targetList [
 
 	return
 }
+
+// moveChartPublicV2
+func moveChartPublicV2(chartInfo, prevChartInfo, nextChartInfo *data_manage.ChartInfo, parentClassifyId int, source int) (err error, errMsg string) {
+	if chartInfo == nil {
+		err = fmt.Errorf("当前图表有误")
+		return
+	}
+
+	// 获取父级分类下所有图表(除当前图表的有序排列)
+	charts, e := data_manage.GetPublicChartsByClassifyId(parentClassifyId, source, chartInfo.ChartInfoId)
+	if e != nil {
+		err = fmt.Errorf("获取分类下所有图表失败, %v", e)
+		return
+	}
+
+	// 先判断是否改变了分类
+	var updateCols []string
+	updateCols = append(updateCols, "PublicSort")
+	if chartInfo.ChartClassifyId != parentClassifyId {
+		chartInfo.ChartPublicClassifyId = parentClassifyId
+		chartInfo.ModifyTime = time.Now()
+		updateCols = append(updateCols, "ChartPublicClassifyId", "ModifyTime")
+	}
+
+	// 判断移动的位置
+	var moveTop, moveMiddle, moveBottom bool
+	var prevChartId int
+	if prevChartInfo != nil && nextChartInfo != nil {
+		moveMiddle = true
+		prevChartId = prevChartInfo.ChartInfoId
+	}
+	if prevChartInfo == nil && nextChartInfo != nil {
+		moveTop = true
+	}
+	if prevChartInfo != nil && nextChartInfo == nil {
+		moveBottom = true
+	}
+
+	// 根据移动位置重新生成完整的排序
+	updates := make([]*data_manage.ChartInfo, 0)
+	var sort int
+	for k, v := range charts {
+		sort += 1
+		// 插入顶部
+		if moveTop && k == 0 {
+			chartInfo.PublicSort = sort
+			updates = append(updates, chartInfo)
+			sort += 1
+			if v.PublicSort != sort {
+				v.PublicSort = sort
+				updates = append(updates, v)
+			}
+			continue
+		}
+		// 底部
+		if moveBottom && k == (len(charts)-1) {
+			if v.PublicSort != sort {
+				v.PublicSort = sort
+				updates = append(updates, v)
+			}
+			sort += 1
+			chartInfo.PublicSort = sort
+			updates = append(updates, chartInfo)
+			continue
+		}
+		// 中间
+		if moveMiddle && v.ChartInfoId == prevChartId {
+			chartInfo.PublicSort = sort
+			updates = append(updates, chartInfo)
+			sort += 1
+		}
+		if v.PublicSort == sort {
+			continue
+		}
+		v.PublicSort = sort
+		updates = append(updates, v)
+	}
+
+	// 更新
+	if len(updates) > 0 {
+		err = data_manage.UpdatePublicChartsSort(updates, chartInfo.ChartInfoId, updateCols)
+	}
+	return
+}