This commit is contained in:
Mrx
2026-05-27 15:18:43 +08:00
parent b16e68f3bd
commit 6cde82ee69
2 changed files with 26 additions and 10 deletions

View File

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

View File

@@ -449,8 +449,8 @@ func (s *ProductManagementService) GetAllProductsForDictionary(ctx context.Conte
options := interfaces.ListOptions{ options := interfaces.ListOptions{
Page: 1, Page: 1,
PageSize: 1000, // 获取所有产品 PageSize: 1000, // 获取所有产品
Sort: "category.sort", // 按分类的排序字段排序 Sort: "id", // 按产品ID排序避免JOIN问题
Order: "asc", Order: "asc",
} }