add sort
This commit is contained in:
		| @@ -3,6 +3,7 @@ package query | ||||
| import ( | ||||
| 	"context" | ||||
| 	"encoding/hex" | ||||
| 	"fmt" | ||||
| 	"github.com/jinzhu/copier" | ||||
| 	"github.com/pkg/errors" | ||||
| 	"tydata-server/app/user/cmd/api/internal/svc" | ||||
| @@ -69,6 +70,10 @@ func (l *QueryExampleLogic) QueryExample(req *types.QueryExampleReq) (resp *type | ||||
| 	if processErr != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "示例报告, 报告结果处理失败: %v", processErr) | ||||
| 	} | ||||
| 	updateFeatureAndProductFeatureErr := l.UpdateFeatureAndProductFeature(queryModel.ProductId, &query.QueryData) | ||||
| 	if updateFeatureAndProductFeatureErr != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结果处理失败: %v", updateFeatureAndProductFeatureErr) | ||||
| 	} | ||||
| 	// 复制报告数据 | ||||
| 	err = copier.Copy(&query, queryModel) | ||||
| 	if err != nil { | ||||
| @@ -83,3 +88,64 @@ func (l *QueryExampleLogic) QueryExample(req *types.QueryExampleReq) (resp *type | ||||
| 		Query: query, | ||||
| 	}, nil | ||||
| } | ||||
| func (l *QueryExampleLogic) UpdateFeatureAndProductFeature(productID int64, target *[]types.QueryItem) error { | ||||
| 	// 遍历 target 数组,使用倒序遍历,以便删除元素时不影响索引 | ||||
| 	for i := len(*target) - 1; i >= 0; i-- { | ||||
| 		queryItem := &(*target)[i] | ||||
|  | ||||
| 		// 确保 Data 为 map 类型 | ||||
| 		data, ok := queryItem.Data.(map[string]interface{}) | ||||
| 		if !ok { | ||||
| 			return fmt.Errorf("queryItem.Data 必须是 map[string]interface{} 类型") | ||||
| 		} | ||||
|  | ||||
| 		// 从 Data 中获取 apiID | ||||
| 		apiID, ok := data["apiID"].(string) | ||||
| 		if !ok { | ||||
| 			return fmt.Errorf("queryItem.Data 中的 apiID 必须是字符串类型") | ||||
| 		} | ||||
|  | ||||
| 		// 查询 Feature | ||||
| 		feature, err := l.svcCtx.FeatureModel.FindOneByApiId(l.ctx, apiID) | ||||
| 		if err != nil { | ||||
| 			// 如果 Feature 查不到,也要删除当前 QueryItem | ||||
| 			*target = append((*target)[:i], (*target)[i+1:]...) | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		// 查询 ProductFeatureModel | ||||
| 		builder := l.svcCtx.ProductFeatureModel.SelectBuilder().Where("product_id = ?", productID) | ||||
| 		productFeatures, err := l.svcCtx.ProductFeatureModel.FindAll(l.ctx, builder, "") | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("查询 ProductFeatureModel 错误: %v", err) | ||||
| 		} | ||||
|  | ||||
| 		// 遍历 productFeatures,找到与 feature.ID 关联且 enable == 1 的项 | ||||
| 		var featureData map[string]interface{} | ||||
| 		foundFeature := false | ||||
|  | ||||
| 		for _, pf := range productFeatures { | ||||
| 			if pf.FeatureId == feature.Id { // 确保和 Feature 关联 | ||||
| 				foundFeature = true | ||||
| 				if pf.Enable == 1 { | ||||
| 					featureData = map[string]interface{}{ | ||||
| 						"featureName": feature.Name, | ||||
| 						"sort":        pf.Sort, | ||||
| 					} | ||||
| 					break // 找到第一个符合条件的就退出循环 | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		// 如果没有符合条件的 feature 或者 featureData 为空,则删除当前 queryItem | ||||
| 		if !foundFeature || featureData == nil { | ||||
| 			*target = append((*target)[:i], (*target)[i+1:]...) | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		// 更新 queryItem 的 Feature 字段(不是数组) | ||||
| 		queryItem.Feature = featureData | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user