Files
nxxmdata/bank-backend/scripts/seed-test-data.js

250 lines
7.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 数据库种子文件 - 添加测试数据
* @file seed-test-data.js
* @description 为银行系统添加测试数据
*/
const { sequelize } = require('../config/database');
const { User, Account, Transaction, Role } = require('../models');
const bcrypt = require('bcryptjs');
async function seedTestData() {
console.log('🌱 开始添加测试数据...');
try {
// 同步数据库
await sequelize.sync({ force: false });
console.log('✅ 数据库同步完成');
// 1. 创建角色数据
console.log('📝 创建角色数据...');
const roles = await Role.bulkCreate([
{
name: 'admin',
display_name: '系统管理员',
description: '系统管理员,拥有所有权限',
permissions: JSON.stringify(['*'])
},
{
name: 'manager',
display_name: '银行经理',
description: '银行经理,管理银行日常运营',
permissions: JSON.stringify(['users:read', 'users:write', 'accounts:read', 'accounts:write', 'transactions:read'])
},
{
name: 'teller',
display_name: '银行柜员',
description: '银行柜员,处理客户业务',
permissions: JSON.stringify(['accounts:read', 'transactions:read', 'transactions:write'])
},
{
name: 'user',
display_name: '普通用户',
description: '普通用户,查看自己的账户信息',
permissions: JSON.stringify(['accounts:read:own', 'transactions:read:own'])
}
], { ignoreDuplicates: true });
console.log(`✅ 创建了 ${roles.length} 个角色`);
// 2. 创建用户数据
console.log('👥 创建用户数据...');
const users = await User.bulkCreate([
{
username: 'admin',
email: 'admin@bank.com',
password: 'admin123',
phone: '13800138000',
real_name: '系统管理员',
id_card: '110101199001010001',
role_id: 1,
status: 'active'
},
{
username: 'manager1',
email: 'manager1@bank.com',
password: 'manager123',
phone: '13800138001',
real_name: '张经理',
id_card: '110101199001010002',
role_id: 2,
status: 'active'
},
{
username: 'teller1',
email: 'teller1@bank.com',
password: 'teller123',
phone: '13800138002',
real_name: '李柜员',
id_card: '110101199001010003',
role_id: 3,
status: 'active'
},
{
username: 'user1',
email: 'user1@bank.com',
password: 'user123',
phone: '13800138004',
real_name: '王用户',
id_card: '110101199001010004',
role_id: 4,
status: 'active'
},
{
username: 'user2',
email: 'user2@bank.com',
password: 'user123',
phone: '13800138005',
real_name: '赵客户',
id_card: '110101199001010005',
role_id: 4,
status: 'active'
}
], { ignoreDuplicates: true });
console.log(`✅ 创建了 ${users.length} 个用户`);
// 3. 创建账户数据
console.log('🏦 创建账户数据...');
const accounts = await Account.bulkCreate([
{
account_number: '6225123456789001',
account_name: '王用户储蓄账户',
account_type: 'savings',
user_id: 4,
balance: 5000000, // 50,000元
available_balance: 5000000,
frozen_amount: 0,
status: 'active',
currency: 'CNY'
},
{
account_number: '6225123456789002',
account_name: '王用户活期账户',
account_type: 'checking',
user_id: 4,
balance: 2000000, // 20,000元
available_balance: 2000000,
frozen_amount: 0,
status: 'active',
currency: 'CNY'
},
{
account_number: '6225123456789003',
account_name: '赵客户储蓄账户',
account_type: 'savings',
user_id: 5,
balance: 10000000, // 100,000元
available_balance: 10000000,
frozen_amount: 0,
status: 'active',
currency: 'CNY'
},
{
account_number: '6225123456789004',
account_name: '赵客户信用卡',
account_type: 'credit',
user_id: 5,
balance: -500000, // -5,000元信用卡欠款
available_balance: 4500000, // 45,000元可用额度
frozen_amount: 0,
status: 'active',
currency: 'CNY'
}
], { ignoreDuplicates: true });
console.log(`✅ 创建了 ${accounts.length} 个账户`);
// 4. 创建交易记录数据
console.log('💳 创建交易记录数据...');
const transactions = await Transaction.bulkCreate([
{
transaction_id: 'T20240922001',
type: 'deposit',
account_id: 1,
amount: 1000000, // 10,000元
balance_after: 6000000,
description: '现金存款',
status: 'completed',
channel: 'counter',
created_at: new Date('2024-09-22T08:00:00Z')
},
{
transaction_id: 'T20240922002',
type: 'withdrawal',
account_id: 2,
amount: 500000, // 5,000元
balance_after: 1500000,
description: 'ATM取款',
status: 'completed',
channel: 'atm',
created_at: new Date('2024-09-22T09:30:00Z')
},
{
transaction_id: 'T20240922003',
type: 'transfer',
account_id: 1,
target_account_id: 3,
amount: 2000000, // 20,000元
balance_after: 4000000,
description: '转账给赵客户',
status: 'completed',
channel: 'online',
created_at: new Date('2024-09-22T10:15:00Z')
},
{
transaction_id: 'T20240922004',
type: 'payment',
account_id: 4,
amount: 300000, // 3,000元
balance_after: -800000,
description: '信用卡消费',
status: 'completed',
channel: 'pos',
created_at: new Date('2024-09-22T11:45:00Z')
},
{
transaction_id: 'T20240922005',
type: 'interest',
account_id: 1,
amount: 5000, // 50元
balance_after: 4005000,
description: '储蓄利息',
status: 'completed',
channel: 'system',
created_at: new Date('2024-09-22T12:00:00Z')
}
], { ignoreDuplicates: true });
console.log(`✅ 创建了 ${transactions.length} 条交易记录`);
console.log('');
console.log('🎉 测试数据添加完成!');
console.log('📊 数据统计:');
console.log(` - 角色: ${roles.length}`);
console.log(` - 用户: ${users.length}`);
console.log(` - 账户: ${accounts.length}`);
console.log(` - 交易记录: ${transactions.length}`);
console.log('');
console.log('🔑 测试账号:');
console.log(' - 管理员: admin / admin123');
console.log(' - 经理: manager1 / manager123');
console.log(' - 柜员: teller1 / teller123');
console.log(' - 用户: user1 / user123');
} catch (error) {
console.error('❌ 添加测试数据失败:', error);
throw error;
}
}
// 如果直接运行此脚本
if (require.main === module) {
seedTestData()
.then(() => {
console.log('✅ 种子数据脚本执行完成');
process.exit(0);
})
.catch((error) => {
console.error('❌ 种子数据脚本执行失败:', error);
process.exit(1);
});
}
module.exports = seedTestData;