您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
优惠券系统是电商平台、餐饮服务、零售等行业中常见的营销工具。通过优惠券系统,商家可以吸引用户、提升销售额、增加用户粘性。本文将介绍如何使用Java和MySQL设计一个简单的优惠券系统。
在设计优惠券系统之前,首先需要明确系统的需求。一个基本的优惠券系统通常包括以下功能:
优惠券系统的核心是数据库设计。以下是几个关键的表结构设计:
user
)CREATE TABLE user (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
coupon
)CREATE TABLE coupon (
id INT PRIMARY KEY AUTO_INCREMENT,
code VARCHAR(20) NOT NULL UNIQUE,
type ENUM('DISCOUNT', 'FIXED', 'PERCENTAGE') NOT NULL,
value DECIMAL(10, 2) NOT NULL,
min_order_amount DECIMAL(10, 2) DEFAULT 0,
start_date TIMESTAMP NOT NULL,
end_date TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
user_coupon
)CREATE TABLE user_coupon (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
coupon_id INT NOT NULL,
status ENUM('UNUSED', 'USED', 'EXPIRED') DEFAULT 'UNUSED',
used_at TIMESTAMP NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user(id),
FOREIGN KEY (coupon_id) REFERENCES coupon(id)
);
order
)CREATE TABLE order (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
total_amount DECIMAL(10, 2) NOT NULL,
coupon_id INT NULL,
discount_amount DECIMAL(10, 2) DEFAULT 0,
final_amount DECIMAL(10, 2) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user(id),
FOREIGN KEY (coupon_id) REFERENCES coupon(id)
);
public class CouponService {
private Connection connection;
public CouponService(Connection connection) {
this.connection = connection;
}
public void createCoupon(String code, String type, BigDecimal value, BigDecimal minOrderAmount, Timestamp startDate, Timestamp endDate) throws SQLException {
String sql = "INSERT INTO coupon (code, type, value, min_order_amount, start_date, end_date) VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, code);
stmt.setString(2, type);
stmt.setBigDecimal(3, value);
stmt.setBigDecimal(4, minOrderAmount);
stmt.setTimestamp(5, startDate);
stmt.setTimestamp(6, endDate);
stmt.executeUpdate();
}
}
}
public class UserCouponService {
private Connection connection;
public UserCouponService(Connection connection) {
this.connection = connection;
}
public void issueCoupon(int userId, int couponId) throws SQLException {
String sql = "INSERT INTO user_coupon (user_id, coupon_id) VALUES (?, ?)";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, userId);
stmt.setInt(2, couponId);
stmt.executeUpdate();
}
}
}
public class OrderService {
private Connection connection;
public OrderService(Connection connection) {
this.connection = connection;
}
public void createOrder(int userId, BigDecimal totalAmount, Integer couponId) throws SQLException {
BigDecimal discountAmount = BigDecimal.ZERO;
if (couponId != null) {
Coupon coupon = getCoupon(couponId);
if (coupon != null && coupon.isValid() && totalAmount.compareTo(coupon.getMinOrderAmount()) >= 0) {
discountAmount = coupon.calculateDiscount(totalAmount);
markCouponAsUsed(couponId, userId);
}
}
BigDecimal finalAmount = totalAmount.subtract(discountAmount);
String sql = "INSERT INTO order (user_id, total_amount, coupon_id, discount_amount, final_amount) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, userId);
stmt.setBigDecimal(2, totalAmount);
stmt.setObject(3, couponId);
stmt.setBigDecimal(4, discountAmount);
stmt.setBigDecimal(5, finalAmount);
stmt.executeUpdate();
}
}
private Coupon getCoupon(int couponId) throws SQLException {
String sql = "SELECT * FROM coupon WHERE id = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, couponId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return new Coupon(rs);
}
}
return null;
}
private void markCouponAsUsed(int couponId, int userId) throws SQLException {
String sql = "UPDATE user_coupon SET status = 'USED', used_at = NOW() WHERE user_id = ? AND coupon_id = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, userId);
stmt.setInt(2, couponId);
stmt.executeUpdate();
}
}
}
public class CouponExpirationService {
private Connection connection;
public CouponExpirationService(Connection connection) {
this.connection = connection;
}
public void expireCoupons() throws SQLException {
String sql = "UPDATE user_coupon SET status = 'EXPIRED' WHERE status = 'UNUSED' AND coupon_id IN (SELECT id FROM coupon WHERE end_date < NOW())";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.executeUpdate();
}
}
}
本文介绍了如何使用Java和MySQL设计一个简单的优惠券系统。通过合理的数据库设计和Java代码实现,可以满足基本的优惠券创建、发放、使用和过期处理等功能。实际应用中,系统可能会更加复杂,需要考虑更多的业务场景和性能优化。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。