You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.8 KiB
116 lines
2.8 KiB
package logic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"healthapi/internal/model"
|
|
"healthapi/internal/svc"
|
|
"healthapi/internal/types"
|
|
"healthapi/pkg/errorx"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type PayOrderLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewPayOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PayOrderLogic {
|
|
return &PayOrderLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *PayOrderLogic) PayOrder(req *types.PayOrderReq) (resp *types.CommonResp, err error) {
|
|
userID, err := GetUserIDFromCtx(l.ctx)
|
|
if err != nil {
|
|
return nil, errorx.ErrUnauthorized
|
|
}
|
|
|
|
var order model.Order
|
|
if err := l.svcCtx.DB.Where("id = ? AND user_id = ?", req.Id, userID).First(&order).Error; err != nil {
|
|
return nil, errorx.NewCodeError(404, "订单不存在")
|
|
}
|
|
|
|
if order.Status != model.OrderStatusPending {
|
|
return nil, errorx.NewCodeError(400, "订单状态不正确")
|
|
}
|
|
|
|
// 模拟支付(实际应调用支付网关)
|
|
now := time.Now()
|
|
err = l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
|
// 更新订单状态
|
|
if err := tx.Model(&order).Updates(map[string]interface{}{
|
|
"status": model.OrderStatusPaid,
|
|
"pay_method": req.PayMethod,
|
|
"pay_time": now,
|
|
}).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
// 更新用户累计消费和会员等级
|
|
var user model.User
|
|
if err := tx.First(&user, userID).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
newTotalSpent := user.TotalSpent + order.PayAmount
|
|
newLevel := model.GetMemberLevelBySpent(newTotalSpent)
|
|
|
|
updates := map[string]interface{}{
|
|
"total_spent": newTotalSpent,
|
|
"member_level": newLevel,
|
|
}
|
|
if user.MemberSince == nil {
|
|
updates["member_since"] = now
|
|
}
|
|
|
|
if err := tx.Model(&user).Updates(updates).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
// 增加获得积分
|
|
if order.PointsEarned > 0 {
|
|
if err := tx.Model(&user).Update("points", gorm.Expr("points + ?", order.PointsEarned)).Error; err != nil {
|
|
return err
|
|
}
|
|
expireAt := now.AddDate(1, 0, 0) // 积分1年后过期
|
|
tx.Create(&model.PointsRecord{
|
|
UserID: userID,
|
|
Type: model.PointsTypeEarn,
|
|
Points: order.PointsEarned,
|
|
Balance: user.Points + order.PointsEarned,
|
|
Source: model.PointsSourceOrder,
|
|
ReferenceID: uint(order.ID),
|
|
Remark: fmt.Sprintf("订单 %s 获得积分", order.OrderNo),
|
|
ExpireAt: &expireAt,
|
|
})
|
|
}
|
|
|
|
// 更新商品销量
|
|
var items []model.OrderItem
|
|
tx.Where("order_id = ?", order.ID).Find(&items)
|
|
for _, item := range items {
|
|
tx.Model(&model.Product{}).Where("id = ?", item.ProductID).
|
|
Update("sales_count", gorm.Expr("sales_count + ?", item.Quantity))
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &types.CommonResp{
|
|
Code: 0,
|
|
Message: "支付成功",
|
|
}, nil
|
|
}
|
|
|