业务背景:电商业务中,需要给电商app设计一个用户钱包,用户可以往钱包中充值,购买商品时用户可以使用钱包中的钱消费,商品申请退款成功后钱会退回钱包中,用户也可以申请提现把钱提到银行卡中 用程序实现如下api接口 1. 查询用户钱包余额 2. 用户消费100元的接口 3. 用户退款20元接口 4. 查询用户钱包金额变动明细的接口 请给出建表语句和对应的代码(只要能实现上面api接口要求即可,不相关的表和代码不用写)
时间: 2024-03-11 08:51:18 浏览: 56
建表语句如下:
```
CREATE TABLE user_wallet (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
balance DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
created_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE wallet_transaction (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
type ENUM('RECHARGE', 'CONSUME', 'REFUND', 'WITHDRAW') NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
balance DECIMAL(10, 2) NOT NULL,
remark VARCHAR(255) DEFAULT NULL,
created_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
```
对应的代码如下:
```
// 查询用户钱包余额
public BigDecimal getUserWalletBalance(int userId) {
String sql = "SELECT balance FROM user_wallet WHERE user_id = ?";
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, userId);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
return rs.getBigDecimal("balance");
}
}
} catch (SQLException e) {
// 异常处理
}
return BigDecimal.ZERO;
}
// 用户消费100元的接口
public void consumeFromUserWallet(int userId, BigDecimal amount) {
String sql = "UPDATE user_wallet SET balance = balance - ? WHERE user_id = ?";
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setBigDecimal(1, amount);
ps.setInt(2, userId);
ps.executeUpdate();
// 记录交易明细
recordTransaction(userId, WalletTransactionType.CONSUME, amount.negate(), "用户消费");
} catch (SQLException e) {
// 异常处理
}
}
// 用户退款20元接口
public void refundToUserWallet(int userId, BigDecimal amount) {
String sql = "UPDATE user_wallet SET balance = balance + ? WHERE user_id = ?";
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setBigDecimal(1, amount);
ps.setInt(2, userId);
ps.executeUpdate();
// 记录交易明细
recordTransaction(userId, WalletTransactionType.REFUND, amount, "用户退款");
} catch (SQLException e) {
// 异常处理
}
}
// 查询用户钱包金额变动明细的接口
public List<WalletTransaction> getUserWalletTransactions(int userId) {
List<WalletTransaction> transactions = new ArrayList<>();
String sql = "SELECT type, amount, balance, remark, created_time FROM wallet_transaction WHERE user_id = ? ORDER BY created_time DESC";
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, userId);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
WalletTransaction transaction = new WalletTransaction();
transaction.setType(WalletTransactionType.valueOf(rs.getString("type")));
transaction.setAmount(rs.getBigDecimal("amount"));
transaction.setBalance(rs.getBigDecimal("balance"));
transaction.setRemark(rs.getString("remark"));
transaction.setCreatedTime(rs.getTimestamp("created_time").toLocalDateTime());
transactions.add(transaction);
}
}
} catch (SQLException e) {
// 异常处理
}
return transactions;
}
// 记录交易明细
private void recordTransaction(int userId, WalletTransactionType type, BigDecimal amount, String remark) {
BigDecimal balance = getUserWalletBalance(userId);
String sql = "INSERT INTO wallet_transaction (user_id, type, amount, balance, remark) VALUES (?, ?, ?, ?, ?)";
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, userId);
ps.setString(2, type.name());
ps.setBigDecimal(3, amount);
ps.setBigDecimal(4, balance);
ps.setString(5, remark);
ps.executeUpdate();
} catch (SQLException e) {
// 异常处理
}
}
```
阅读全文