客户预充值系统(一) - 数据库设计与数据插入
设计了一个SQL Server数据库结构来支持这个客户预充值系统。下面是完整的SQL表结构和示例数据。
数据库表设计:
-- 创建数据库
CREATE DATABASE CustomerRechargeSystem;
GOUSE CustomerRechargeSystem;
GO-- 客户信息表
CREATE TABLE Customers (CustomerID INT IDENTITY(1,1) PRIMARY KEY,Name NVARCHAR(100) NOT NULL,Phone NVARCHAR(20) NOT NULL,Email NVARCHAR(100),Address NVARCHAR(255),Balance DECIMAL(10,2) DEFAULT 0.00,CreatedDate DATETIME DEFAULT GETDATE(),LastModifiedDate DATETIME DEFAULT GETDATE()
);
GO-- 充值选项表
CREATE TABLE RechargeOptions (OptionID INT IDENTITY(1,1) PRIMARY KEY,Amount DECIMAL(10,2) NOT NULL,Bonus DECIMAL(10,2) NOT NULL DEFAULT 0.00,IsActive BIT DEFAULT 1
);
GO-- 支付方式表
CREATE TABLE PaymentMethods (MethodID INT IDENTITY(1,1) PRIMARY KEY,MethodName NVARCHAR(50) NOT NULL,IconClass NVARCHAR(100) NOT NULL,IsActive BIT DEFAULT 1
);
GO-- 充值记录表
CREATE TABLE RechargeRecords (RecordID INT IDENTITY(1,1) PRIMARY KEY,CustomerID INT NOT NULL,RechargeDate DATETIME DEFAULT GETDATE(),Amount DECIMAL(10,2) NOT NULL,Bonus DECIMAL(10,2) NOT NULL DEFAULT 0.00,PaymentMethodID INT NOT NULL,Status NVARCHAR(20) DEFAULT 'Completed',FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID),FOREIGN KEY (PaymentMethodID) REFERENCES PaymentMethods(MethodID)
);
GO-- 促销活动表
CREATE TABLE Promotions (PromotionID INT IDENTITY(1,1) PRIMARY KEY,Title NVARCHAR(100) NOT NULL,Description NVARCHAR(255),PaymentMethodID INT,MinimumAmount DECIMAL(10,2) DEFAULT 0.00,DiscountAmount DECIMAL(10,2) DEFAULT 0.00,DiscountPercent DECIMAL(5,2) DEFAULT 0.00,StartDate DATE,EndDate DATE,IsActive BIT DEFAULT 1,FOREIGN KEY (PaymentMethodID) REFERENCES PaymentMethods(MethodID)
);
GO
插入示例数据:
-- 插入客户信息
INSERT INTO Customers (Name, Phone, Email, Address, Balance)
VALUES
(N'张小明', '13800138000', 'zhangxm@example.com', N'北京市海淀区中关村大街1号', 158.50),
(N'李小红', '13900139000', 'lixh@example.com', N'上海市浦东新区张江高科技园区', 250.75),
(N'王小明', '13700137000', 'wangxm@example.com', N'广州市天河区天河路', 75.30);
GO-- 插入充值选项
INSERT INTO RechargeOptions (Amount, Bonus)
VALUES
(50.00, 5.00),
(100.00, 10.00),
(200.00, 25.00),
(500.00, 70.00),
(1000.00, 150.00),
(2000.00, 350.00);
GO-- 插入支付方式
INSERT INTO PaymentMethods (MethodName, IconClass)
VALUES
(N'支付宝', 'fab fa-alipay'),
(N'微信支付', 'fab fa-weixin'),
(N'银行卡', 'fas fa-credit-card'),
(N'网银转账', 'fas fa-university');
GO-- 插入充值记录
INSERT INTO RechargeRecords (CustomerID, RechargeDate, Amount, Bonus, PaymentMethodID)
VALUES
(1, '2023-06-15', 100.00, 10.00, 1),
(1, '2023-05-22', 200.00, 25.00, 2),
(1, '2023-04-10', 50.00, 5.00, 1),
(2, '2023-07-01', 500.00, 70.00, 1),
(2, '2023-06-20', 100.00, 10.00, 2),
(3, '2023-07-05', 200.00, 25.00, 4);
GO-- 插入促销活动
INSERT INTO Promotions (Title, Description, PaymentMethodID, MinimumAmount, DiscountAmount, DiscountPercent, StartDate, EndDate)
VALUES
(N'支付宝充值优惠', N'使用支付宝充值满100元立减5元', 1, 100.00, 5.00, 0.00, '2023-01-01', '2023-12-31'),
(N'微信支付首充优惠', N'使用微信支付首充赠送10%余额', 2, 0.00, 0.00, 10.00, '2023-01-01', '2023-12-31'),
(N'新用户专享优惠', N'新用户首次充值任意金额赠送20元', NULL, 0.00, 20.00, 0.00, '2023-07-01', '2023-07-31');
GO
查询示例:
-- 查询客户信息和余额
SELECT CustomerID, Name, Phone, Email, Address, Balance
FROM Customers;-- 查询客户的充值记录
SELECT c.Name, r.RechargeDate, r.Amount, r.Bonus, p.MethodName as PaymentMethod
FROM RechargeRecords r
JOIN Customers c ON r.CustomerID = c.CustomerID
JOIN PaymentMethods p ON r.PaymentMethodID = p.MethodID
ORDER BY r.RechargeDate DESC;-- 查询可用充值选项
SELECT Amount, Bonus
FROM RechargeOptions
WHERE IsActive = 1
ORDER BY Amount;-- 查询当前有效的促销活动
SELECT Title, Description, MinimumAmount, DiscountAmount, DiscountPercent
FROM Promotions
WHERE IsActive = 1 AND GETDATE() BETWEEN StartDate AND EndDate;
前端与数据库交互示例
以下是一个简化的前端代码示例,展示如何从数据库获取数据并显示在Vue.js应用中:
// 模拟从API获取数据
async function fetchCustomerData(customerId) {try {const response = await fetch(`/api/customers/${customerId}`);return await response.json();} catch (error) {console.error('Error fetching customer data:', error);return null;}
}// 模拟获取充值记录
async function fetchRechargeHistory(customerId) {try {const response = await fetch(`/api/recharge-records/${customerId}`);return await response.json();} catch (error) {console.error('Error fetching recharge history:', error);return [];}
}// 在Vue组件中使用
export default {data() {return {customerInfo: {},rechargeHistory: []};},async mounted() {// 假设当前客户ID为1this.customerInfo = await fetchCustomerData(1);this.rechargeHistory = await fetchRechargeHistory(1);}
};
这个数据库设计支持了客户预充值系统的所有核心功能,包括客户信息管理、充值选项、支付方式、充值记录和促销活动。您可以根据实际需求进一步扩展或修改这个设计。