1、增加钱包余额

This commit is contained in:
2024-10-15 23:58:36 +08:00
parent 8609f8566d
commit fdfdbb5ff6
10 changed files with 313 additions and 285 deletions

View File

@@ -32,27 +32,30 @@ func (l *UserInfoLogic) UserInfo(in *user.UserInfoReq) (*user.UserInfoResp, erro
return nil, errors.New("用户不存在")
}
// 查询企业信息
enterprise, err := l.svcCtx.EnterpriseModel.FindOneByUserId(l.ctx, users.Id)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, errors.New("failed to query enterprise auth info")
}
if enterprise == nil {
if users.AuthStatus == "approved" {
enterprise, findEntErr := l.svcCtx.EnterpriseModel.FindOneByUserId(l.ctx, users.Id)
if findEntErr != nil && !errors.Is(findEntErr, sql.ErrNoRows) {
return nil, errors.New("failed to query enterprise auth info")
}
wallet, findWalletErr := l.svcCtx.WalletsModel.FindOne(l.ctx, in.UserId)
if findWalletErr != nil {
return nil, findWalletErr
}
// 正常返回用户和企业信息
return &user.UserInfoResp{
Username: users.Username,
Phone: users.Phone,
EnterpriseAuthStatus: users.AuthStatus,
EnterpriseName: enterprise.EnterpriseName,
CreditCode: enterprise.CreditCode,
LegalPerson: enterprise.LegalPerson,
Balance: wallet.Balance,
}, nil
}
// 正常返回用户和企业信息
return &user.UserInfoResp{
Username: users.Username,
Phone: users.Phone,
EnterpriseAuthStatus: users.AuthStatus,
EnterpriseName: enterprise.EnterpriseName,
CreditCode: enterprise.CreditCode,
LegalPerson: enterprise.LegalPerson,
}, nil
}

View File

@@ -25,7 +25,12 @@ func NewGetWalletLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWall
// 查询钱包信息
func (l *GetWalletLogic) GetWallet(in *user.GetWalletRequest) (*user.GetWalletResponse, error) {
// todo: add your logic here and delete this line
return &user.GetWalletResponse{}, nil
wallet, err := l.svcCtx.WalletsModel.FindOne(l.ctx, in.Id)
if err != nil {
return nil, err
}
return &user.GetWalletResponse{
UserId: wallet.UserId,
Balance: wallet.Balance,
}, nil
}

View File

@@ -51,8 +51,14 @@ func (m *customWalletsModel) UpdateBalance(session sqlx.Session, ctx context.Con
return err
}
// 使用乐观锁更新余额
result, err := session.Exec("UPDATE wallets SET balance = balance + ?, version = version + 1 WHERE user_id = ? AND version = ?", amount, userId, wallet.Version)
// 检查当前版本是否达到了上限
newVersion := wallet.Version + 1
if newVersion > 100000000 { // 你可以根据实际需要设定上限,比如 UNSIGNED INT 的最大值
newVersion = 1 // 或者设置为其他重置值,比如 0
}
// 使用乐观锁更新余额和版本
result, err := session.Exec("UPDATE wallets SET balance = balance + ?, version = ? WHERE user_id = ? AND version = ?", amount, newVersion, userId, wallet.Version)
if err != nil {
return err
}