fix query
This commit is contained in:
parent
8af7189a5f
commit
0f8503984b
@ -5,6 +5,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
"tyc-server/common/xerr"
|
"tyc-server/common/xerr"
|
||||||
"tyc-server/pkg/lzkit/crypto"
|
"tyc-server/pkg/lzkit/crypto"
|
||||||
@ -137,14 +138,18 @@ func (l *QueryDetailByOrderIdLogic) QueryDetailByOrderId(req *types.QueryDetailB
|
|||||||
if processErr != nil {
|
if processErr != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结果处理失败: %v", processErr)
|
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)
|
err = copier.Copy(&query, queryModel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结构体复制失败, %+v", err)
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结构体复制失败, %v", err)
|
||||||
}
|
}
|
||||||
product, err := l.svcCtx.ProductModel.FindOne(l.ctx, queryModel.ProductId)
|
product, err := l.svcCtx.ProductModel.FindOne(l.ctx, queryModel.ProductId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 获取商品信息失败, %+v", err)
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 获取商品信息失败, %v", err)
|
||||||
}
|
}
|
||||||
query.ProductName = product.ProductName
|
query.ProductName = product.ProductName
|
||||||
return &types.QueryDetailByOrderIdResp{
|
return &types.QueryDetailByOrderIdResp{
|
||||||
@ -189,3 +194,56 @@ func ProcessQueryParams(QueryParams string, target *map[string]interface{}, key
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func (l *QueryDetailByOrderIdLogic) 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
|
||||||
|
sort := 0
|
||||||
|
for _, pf := range productFeatures {
|
||||||
|
if pf.FeatureId == feature.Id { // 确保和 Feature 关联
|
||||||
|
sort = int(pf.Sort)
|
||||||
|
break // 找到第一个符合条件的就退出循环
|
||||||
|
}
|
||||||
|
}
|
||||||
|
featureData = map[string]interface{}{
|
||||||
|
"featureName": feature.Name,
|
||||||
|
"sort": sort,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新 queryItem 的 Feature 字段(不是数组)
|
||||||
|
queryItem.Feature = featureData
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@ package query
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
"tyc-server/common/ctxdata"
|
"tyc-server/common/ctxdata"
|
||||||
"tyc-server/common/xerr"
|
"tyc-server/common/xerr"
|
||||||
@ -135,16 +136,74 @@ func (l *QueryDetailByOrderNoLogic) QueryDetailByOrderNo(req *types.QueryDetailB
|
|||||||
if processErr != nil {
|
if processErr != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结果处理失败: %v", processErr)
|
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)
|
err = copier.Copy(&query, queryModel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结构体复制失败, %+v", err)
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结构体复制失败, %v", err)
|
||||||
}
|
}
|
||||||
product, err := l.svcCtx.ProductModel.FindOne(l.ctx, queryModel.ProductId)
|
product, err := l.svcCtx.ProductModel.FindOne(l.ctx, queryModel.ProductId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 获取商品信息失败, %+v", err)
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 获取商品信息失败, %v", err)
|
||||||
}
|
}
|
||||||
query.ProductName = product.ProductName
|
query.ProductName = product.ProductName
|
||||||
return &types.QueryDetailByOrderNoResp{
|
return &types.QueryDetailByOrderNoResp{
|
||||||
Query: query,
|
Query: query,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
func (l *QueryDetailByOrderNoLogic) 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
|
||||||
|
sort := 0
|
||||||
|
for _, pf := range productFeatures {
|
||||||
|
if pf.FeatureId == feature.Id { // 确保和 Feature 关联
|
||||||
|
sort = int(pf.Sort)
|
||||||
|
break // 找到第一个符合条件的就退出循环
|
||||||
|
}
|
||||||
|
}
|
||||||
|
featureData = map[string]interface{}{
|
||||||
|
"featureName": feature.Name,
|
||||||
|
"sort": sort,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新 queryItem 的 Feature 字段(不是数组)
|
||||||
|
queryItem.Feature = featureData
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user