2025-01-10 00:09:25 +08:00
|
|
|
|
package query
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"encoding/json"
|
2025-01-26 14:28:36 +08:00
|
|
|
|
"fmt"
|
2025-01-10 00:09:25 +08:00
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"time"
|
|
|
|
|
"tydata-server/common/xerr"
|
|
|
|
|
"tydata-server/pkg/lzkit/crypto"
|
|
|
|
|
"tydata-server/pkg/lzkit/delay"
|
|
|
|
|
"tydata-server/pkg/lzkit/lzUtils"
|
|
|
|
|
|
|
|
|
|
"tydata-server/app/user/cmd/api/internal/svc"
|
|
|
|
|
"tydata-server/app/user/cmd/api/internal/types"
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type QueryDetailByOrderIdLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewQueryDetailByOrderIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryDetailByOrderIdLogic {
|
|
|
|
|
return &QueryDetailByOrderIdLogic{
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *QueryDetailByOrderIdLogic) QueryDetailByOrderId(req *types.QueryDetailByOrderIdReq) (resp *types.QueryDetailByOrderIdResp, err error) {
|
|
|
|
|
// 获取订单信息
|
|
|
|
|
order, err := l.svcCtx.OrderModel.FindOne(l.ctx, req.OrderId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "报告查询, 查找报告错误: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建渐进式延迟策略实例
|
|
|
|
|
progressiveDelayOrder, err := delay.New(200*time.Millisecond, 3*time.Second, 10*time.Second, 1.5)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "初始化渐进式延迟策略失败: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 等待订单状态变为 "paid"
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
for order.Status == "pending" {
|
|
|
|
|
if time.Since(startTime) > 10*time.Second {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.LOGIC_QUERY_WAIT, ""), "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用渐进式延迟,获取下次延迟时间
|
|
|
|
|
nextDelay, _ := progressiveDelayOrder.NextDelay()
|
|
|
|
|
|
|
|
|
|
// 等待一段时间后再查一次订单状态
|
|
|
|
|
time.Sleep(nextDelay)
|
|
|
|
|
|
|
|
|
|
// 再次查找订单
|
|
|
|
|
order, err = l.svcCtx.OrderModel.FindOne(l.ctx, req.OrderId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "报告查询, 查找订单错误: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if order.Status != "paid" {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.LOGIC_QUERY_ERROR, ""), "")
|
|
|
|
|
}
|
|
|
|
|
// 获取报告信息
|
|
|
|
|
queryModel, err := l.svcCtx.QueryModel.FindOneByOrderId(l.ctx, req.OrderId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "报告查询, 查找报告错误: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建渐进式延迟实例
|
|
|
|
|
progressiveDelayQuery, err := delay.New(200*time.Millisecond, 3*time.Second, 10*time.Second, 1.5)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "初始化渐进式延迟策略失败: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 等待 queryModel.QueryState 不再是 "pending"
|
|
|
|
|
startTime = time.Now()
|
|
|
|
|
for queryModel.QueryState == "pending" {
|
|
|
|
|
if time.Since(startTime) > 10*time.Second {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询超时,查询状态长时间为 'pending'")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用渐进式延迟,获取下次延迟时间
|
|
|
|
|
nextDelay, _ := progressiveDelayQuery.NextDelay()
|
|
|
|
|
|
|
|
|
|
// 每隔一段时间检查一次查询状态
|
|
|
|
|
time.Sleep(nextDelay)
|
|
|
|
|
|
|
|
|
|
// 再次查询 report 状态
|
|
|
|
|
queryModel, err = l.svcCtx.QueryModel.FindOneByOrderId(l.ctx, req.OrderId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "报告查询, 查找报告错误: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据 QueryState 做后续处理
|
|
|
|
|
if queryModel.QueryState == "failed" {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.LOGIC_QUERY_ERROR, ""), "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query types.Query
|
|
|
|
|
query.CreateTime = queryModel.CreateTime.Format("2006-01-02 15:04:05")
|
|
|
|
|
query.UpdateTime = queryModel.UpdateTime.Format("2006-01-02 15:04:05")
|
|
|
|
|
|
|
|
|
|
// 解密查询数据
|
|
|
|
|
secretKey := l.svcCtx.Config.Encrypt.SecretKey
|
|
|
|
|
key, decodeErr := hex.DecodeString(secretKey)
|
|
|
|
|
if decodeErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 获取AES解密解药失败, %+v", err)
|
|
|
|
|
}
|
|
|
|
|
processParamsErr := ProcessQueryParams(queryModel.QueryParams, &query.QueryParams, key)
|
|
|
|
|
if processParamsErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告参数处理失败: %v", processParamsErr)
|
|
|
|
|
}
|
|
|
|
|
processErr := ProcessQueryData(queryModel.QueryData, &query.QueryData, key)
|
|
|
|
|
if processErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结果处理失败: %v", processErr)
|
|
|
|
|
}
|
2025-01-26 14:28:36 +08:00
|
|
|
|
updateFeatureAndProductFeatureErr := l.UpdateFeatureAndProductFeature(queryModel.ProductId, &query.QueryData)
|
|
|
|
|
if updateFeatureAndProductFeatureErr != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结果处理失败: %v", updateFeatureAndProductFeatureErr)
|
|
|
|
|
}
|
2025-01-10 00:09:25 +08:00
|
|
|
|
// 复制报告数据
|
|
|
|
|
err = copier.Copy(&query, queryModel)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结构体复制失败, %+v", err)
|
|
|
|
|
}
|
|
|
|
|
product, err := l.svcCtx.ProductModel.FindOne(l.ctx, queryModel.ProductId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 获取商品信息失败, %+v", err)
|
|
|
|
|
}
|
|
|
|
|
query.ProductName = product.ProductName
|
|
|
|
|
return &types.QueryDetailByOrderIdResp{
|
|
|
|
|
Query: query,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ProcessQueryData 解密和反序列化 QueryData
|
2025-01-26 14:28:36 +08:00
|
|
|
|
func ProcessQueryData(queryData sql.NullString, target *[]types.QueryItem, key []byte) error {
|
2025-01-10 00:09:25 +08:00
|
|
|
|
queryDataStr := lzUtils.NullStringToString(queryData)
|
|
|
|
|
if queryDataStr == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-01-26 14:28:36 +08:00
|
|
|
|
|
|
|
|
|
// 解密数据
|
2025-01-10 00:09:25 +08:00
|
|
|
|
decryptedData, decryptErr := crypto.AesDecrypt(queryDataStr, key)
|
|
|
|
|
if decryptErr != nil {
|
|
|
|
|
return decryptErr
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-26 14:28:36 +08:00
|
|
|
|
// 解析 JSON 数组
|
|
|
|
|
var decryptedArray []map[string]interface{}
|
|
|
|
|
unmarshalErr := json.Unmarshal(decryptedData, &decryptedArray)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
if unmarshalErr != nil {
|
|
|
|
|
return unmarshalErr
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-26 14:28:36 +08:00
|
|
|
|
// 确保 target 具有正确的长度
|
|
|
|
|
if len(*target) == 0 {
|
|
|
|
|
*target = make([]types.QueryItem, len(decryptedArray))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 填充解密后的数据到 target
|
|
|
|
|
for i := 0; i < len(decryptedArray); i++ {
|
|
|
|
|
// 直接填充解密数据到 Data 字段
|
|
|
|
|
(*target)[i].Data = decryptedArray[i]
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 00:09:25 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ProcessQueryParams解密和反序列化 QueryParams
|
|
|
|
|
func ProcessQueryParams(QueryParams string, target *map[string]interface{}, key []byte) error {
|
|
|
|
|
// 解密 QueryParams
|
|
|
|
|
decryptedData, decryptErr := crypto.AesDecrypt(QueryParams, key)
|
|
|
|
|
if decryptErr != nil {
|
|
|
|
|
return decryptErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 反序列化解密后的数据
|
|
|
|
|
unmarshalErr := json.Unmarshal(decryptedData, target)
|
|
|
|
|
if unmarshalErr != nil {
|
|
|
|
|
return unmarshalErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|