f
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
@@ -1258,15 +1259,24 @@ func (s *ProductApplicationServiceImpl) ExportProductDictionary(ctx context.Cont
|
||||
return nil, fmt.Errorf("没有找到符合条件的产品数据")
|
||||
}
|
||||
|
||||
// 按分类分组整理数据
|
||||
categoryGroups := make(map[string][]map[string]interface{})
|
||||
categoryOrder := []string{} // 保持分类顺序
|
||||
// 按分类名称分组整理数据
|
||||
type categoryInfo struct {
|
||||
name string
|
||||
sort int
|
||||
}
|
||||
|
||||
categoryGroups := make(map[string][]map[string]interface{}) // 使用分类名称作为key
|
||||
categorySortMap := make(map[string]int) // 分类名称到sort值的映射
|
||||
var categoryNames []string // 保持分类顺序
|
||||
|
||||
for _, product := range products {
|
||||
// 获取分类名称
|
||||
// 获取分类信息
|
||||
categoryName := "未分类"
|
||||
categorySort := 999999 // 默认排序值,确保未分类的产品排在最后
|
||||
|
||||
if product.Category != nil {
|
||||
categoryName = product.Category.Name
|
||||
categorySort = product.Category.Sort
|
||||
// 如果有二级分类,添加到分类名称中
|
||||
if product.SubCategory != nil && product.SubCategory.Name != "" {
|
||||
categoryName = categoryName + " / " + product.SubCategory.Name
|
||||
@@ -1276,7 +1286,8 @@ func (s *ProductApplicationServiceImpl) ExportProductDictionary(ctx context.Cont
|
||||
// 如果分类不存在,初始化并添加到顺序列表
|
||||
if _, exists := categoryGroups[categoryName]; !exists {
|
||||
categoryGroups[categoryName] = []map[string]interface{}{}
|
||||
categoryOrder = append(categoryOrder, categoryName)
|
||||
categoryNames = append(categoryNames, categoryName)
|
||||
categorySortMap[categoryName] = categorySort
|
||||
}
|
||||
|
||||
// 添加产品到对应分类
|
||||
@@ -1289,19 +1300,24 @@ func (s *ProductApplicationServiceImpl) ExportProductDictionary(ctx context.Cont
|
||||
categoryGroups[categoryName] = append(categoryGroups[categoryName], productInfo)
|
||||
}
|
||||
|
||||
// 按分类的sort值对分类名称进行排序
|
||||
sort.Slice(categoryNames, func(i, j int) bool {
|
||||
return categorySortMap[categoryNames[i]] < categorySortMap[categoryNames[j]]
|
||||
})
|
||||
|
||||
// 准备导出数据
|
||||
headers := []string{"分类", "产品编码", "产品名称", "产品简介"}
|
||||
columnWidths := []float64{25, 15, 20, 40}
|
||||
|
||||
// 构建数据行
|
||||
var data [][]interface{}
|
||||
for _, categoryName := range categoryOrder {
|
||||
for _, categoryName := range categoryNames {
|
||||
productsInCategory := categoryGroups[categoryName]
|
||||
for i, product := range productsInCategory {
|
||||
// 只有每个分类的第一个产品才显示分类名称
|
||||
var categoryNameForRow interface{}
|
||||
if i == 0 {
|
||||
categoryNameForRow = product["category"]
|
||||
categoryNameForRow = categoryName
|
||||
} else {
|
||||
categoryNameForRow = ""
|
||||
}
|
||||
@@ -1319,7 +1335,7 @@ func (s *ProductApplicationServiceImpl) ExportProductDictionary(ctx context.Cont
|
||||
// 计算需要合并的行
|
||||
mergedRegions := [][]int{}
|
||||
currentRow := 1 // 从第1行开始(第0行是表头)
|
||||
for _, categoryName := range categoryOrder {
|
||||
for _, categoryName := range categoryNames {
|
||||
productsCount := len(categoryGroups[categoryName])
|
||||
if productsCount > 1 {
|
||||
// 合并相同分类的单元格:从当前行开始,合并productsCount行,第0列
|
||||
|
||||
Reference in New Issue
Block a user