2025-01-10 00:09:25 +08:00
|
|
|
|
package query
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-06-09 12:34:52 +08:00
|
|
|
|
"qnc-server/app/main/api/internal/svc"
|
|
|
|
|
"qnc-server/app/main/api/internal/types"
|
|
|
|
|
"qnc-server/app/main/model"
|
2025-04-11 13:10:17 +08:00
|
|
|
|
"qnc-server/common/ctxdata"
|
|
|
|
|
"qnc-server/common/xerr"
|
|
|
|
|
|
2025-01-10 00:09:25 +08:00
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type QueryListLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewQueryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryListLogic {
|
|
|
|
|
return &QueryListLogic{
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *QueryListLogic) QueryList(req *types.QueryListReq) (resp *types.QueryListResp, err error) {
|
|
|
|
|
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
|
|
|
|
|
if getUidErr != nil {
|
2025-05-24 14:26:20 +08:00
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "订单列表查询, 获取用户信息失败, %+v", getUidErr)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
2025-04-14 17:06:47 +08:00
|
|
|
|
|
2025-06-09 13:39:21 +08:00
|
|
|
|
// 构建子查询:查找在query表中有记录的order_id
|
|
|
|
|
querySubQuery := squirrel.Select("order_id").From("query")
|
|
|
|
|
|
|
|
|
|
// 构建子查询:查找在authorization表中状态为pending的order_id
|
|
|
|
|
authSubQuery := squirrel.Select("order_id").
|
|
|
|
|
From("authorization").
|
|
|
|
|
Where(squirrel.Eq{"status": model.AuthorizationStatusPending})
|
|
|
|
|
|
|
|
|
|
// 构建主查询条件
|
2025-05-24 14:26:20 +08:00
|
|
|
|
build := l.svcCtx.OrderModel.SelectBuilder().Where(squirrel.And{
|
|
|
|
|
squirrel.Eq{
|
|
|
|
|
"user_id": userID,
|
|
|
|
|
},
|
|
|
|
|
squirrel.NotEq{
|
|
|
|
|
"status": model.OrderStatusPending,
|
|
|
|
|
},
|
2025-06-09 13:39:21 +08:00
|
|
|
|
squirrel.Or{
|
|
|
|
|
// 在query表中有记录
|
|
|
|
|
squirrel.Expr("id IN (?)", querySubQuery),
|
|
|
|
|
// 或者在query表中没有记录,但在authorization表中状态为pending
|
|
|
|
|
squirrel.And{
|
|
|
|
|
squirrel.Expr("id NOT IN (?)", querySubQuery),
|
|
|
|
|
squirrel.Expr("id IN (?)", authSubQuery),
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-01-10 00:09:25 +08:00
|
|
|
|
})
|
2025-04-14 17:06:47 +08:00
|
|
|
|
|
2025-05-24 14:26:20 +08:00
|
|
|
|
// 从订单表分页查询
|
|
|
|
|
orderList, total, err := l.svcCtx.OrderModel.FindPageListByPageWithTotal(l.ctx, build, req.Page, req.PageSize, "create_time DESC")
|
2025-01-10 00:09:25 +08:00
|
|
|
|
if err != nil {
|
2025-05-24 14:26:20 +08:00
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "订单列表查询, 查找订单列表错误, %+v", err)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var list []types.Query
|
2025-05-24 14:26:20 +08:00
|
|
|
|
if len(orderList) > 0 {
|
|
|
|
|
for _, orderModel := range orderList {
|
2025-01-10 00:09:25 +08:00
|
|
|
|
var query types.Query
|
2025-05-24 14:26:20 +08:00
|
|
|
|
query.CreateTime = orderModel.CreateTime.Format("2006-01-02 15:04:05")
|
|
|
|
|
query.UpdateTime = orderModel.UpdateTime.Format("2006-01-02 15:04:05")
|
|
|
|
|
// 设置订单ID
|
|
|
|
|
query.OrderId = orderModel.Id
|
|
|
|
|
query.UserId = orderModel.UserId
|
2025-05-28 17:57:22 +08:00
|
|
|
|
query.OrderNo = orderModel.OrderNo
|
2025-05-24 14:26:20 +08:00
|
|
|
|
// 获取商品信息
|
|
|
|
|
product, findProductErr := l.svcCtx.ProductModel.FindOne(l.ctx, orderModel.ProductId)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
if findProductErr != nil {
|
2025-05-24 14:26:20 +08:00
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "订单列表查询, 获取商品信息失败, %+v", findProductErr)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
query.ProductName = product.ProductName
|
2025-05-24 14:26:20 +08:00
|
|
|
|
|
|
|
|
|
// 设置订单支付状态
|
|
|
|
|
query.IsPaid = orderModel.Status == model.OrderStatusPaid
|
|
|
|
|
|
2025-05-28 18:08:47 +08:00
|
|
|
|
// 判断订单是否已退款
|
|
|
|
|
query.IsRefunded = orderModel.Status == model.OrderStatusRefunded
|
|
|
|
|
|
2025-05-24 14:26:20 +08:00
|
|
|
|
// 查询查询状态
|
|
|
|
|
queryInfo, findQueryErr := l.svcCtx.QueryModel.FindOneByOrderId(l.ctx, orderModel.Id)
|
|
|
|
|
if findQueryErr == nil {
|
|
|
|
|
// 查询存在
|
|
|
|
|
query.Id = queryInfo.Id
|
|
|
|
|
query.QueryState = queryInfo.QueryState
|
|
|
|
|
query.IsQueryCompleted = queryInfo.QueryState == model.QueryStateSuccess
|
|
|
|
|
|
2025-06-09 13:39:21 +08:00
|
|
|
|
// 获取授权状态
|
|
|
|
|
authInfo, findAuthErr := l.svcCtx.AuthorizationModel.FindOneByOrderId(l.ctx, orderModel.Id)
|
|
|
|
|
if findAuthErr != nil && !errors.Is(findAuthErr, model.ErrNotFound) {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "订单列表查询, 获取授权信息失败, %+v", findAuthErr)
|
|
|
|
|
}
|
|
|
|
|
if errors.Is(findAuthErr, model.ErrNotFound) {
|
|
|
|
|
// 如果query存在但authorization不存在,说明已经授权完成
|
|
|
|
|
query.IsAuthCompleted = true
|
|
|
|
|
}
|
2025-05-24 14:26:20 +08:00
|
|
|
|
// 授权存在
|
|
|
|
|
query.IsAuthCompleted = authInfo.Status == model.AuthorizationStatusSuccess
|
2025-06-09 13:39:21 +08:00
|
|
|
|
|
2025-05-24 14:26:20 +08:00
|
|
|
|
} else {
|
2025-06-09 13:39:21 +08:00
|
|
|
|
query.QueryState = "未创建"
|
|
|
|
|
query.IsQueryCompleted = false
|
|
|
|
|
|
|
|
|
|
// 获取授权状态
|
|
|
|
|
authInfo, findAuthErr := l.svcCtx.AuthorizationModel.FindOneByOrderId(l.ctx, orderModel.Id)
|
|
|
|
|
if findAuthErr == nil {
|
|
|
|
|
// 授权存在
|
|
|
|
|
query.IsAuthCompleted = authInfo.Status == model.AuthorizationStatusSuccess
|
|
|
|
|
} else {
|
|
|
|
|
query.IsAuthCompleted = false
|
|
|
|
|
}
|
2025-05-24 14:26:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 00:09:25 +08:00
|
|
|
|
list = append(list, query)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &types.QueryListResp{
|
|
|
|
|
Total: total,
|
|
|
|
|
List: list,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|