first commit
This commit is contained in:
		| @@ -0,0 +1,135 @@ | ||||
| package query | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"encoding/hex" | ||||
| 	"github.com/jinzhu/copier" | ||||
| 	"github.com/pkg/errors" | ||||
| 	"time" | ||||
| 	"tydata-server/common/xerr" | ||||
| 	"tydata-server/pkg/lzkit/delay" | ||||
|  | ||||
| 	"tydata-server/app/user/cmd/api/internal/svc" | ||||
| 	"tydata-server/app/user/cmd/api/internal/types" | ||||
|  | ||||
| 	"github.com/zeromicro/go-zero/core/logx" | ||||
| ) | ||||
|  | ||||
| type QueryDetailByOrderNoLogic struct { | ||||
| 	logx.Logger | ||||
| 	ctx    context.Context | ||||
| 	svcCtx *svc.ServiceContext | ||||
| } | ||||
|  | ||||
| func NewQueryDetailByOrderNoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryDetailByOrderNoLogic { | ||||
| 	return &QueryDetailByOrderNoLogic{ | ||||
| 		Logger: logx.WithContext(ctx), | ||||
| 		ctx:    ctx, | ||||
| 		svcCtx: svcCtx, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (l *QueryDetailByOrderNoLogic) QueryDetailByOrderNo(req *types.QueryDetailByOrderNoReq) (resp *types.QueryDetailByOrderNoResp, err error) { | ||||
| 	// 获取订单信息 | ||||
| 	order, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo) | ||||
| 	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.FindOneByOrderNo(l.ctx, req.OrderNo) | ||||
| 		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, order.Id) | ||||
| 	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, order.Id) | ||||
| 		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) | ||||
| 	} | ||||
| 	// 复制报告数据 | ||||
| 	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.QueryDetailByOrderNoResp{ | ||||
| 		Query: query, | ||||
| 	}, nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user