-- 初始角色（如果不存在则插入）
INSERT INTO bank_roles (name, display_name, level, is_system, status)
SELECT 'admin','系统管理员',100,1,'active'
WHERE NOT EXISTS (SELECT 1 FROM bank_roles WHERE name='admin');

INSERT INTO bank_roles (name, display_name, level, is_system, status)
SELECT 'user','普通用户',20,0,'active'
WHERE NOT EXISTS (SELECT 1 FROM bank_roles WHERE name='user');

-- 管理员账户，密码由脚本动态替换为 bcrypt 哈希（REPLACE_ADMIN_BCRYPT）
INSERT INTO bank_users (username,email,password,real_name,id_card,role_id,status)
SELECT 'admin','admin@bank.com','REPLACE_ADMIN_BCRYPT','系统管理员','110101199001011234', r.id,'active'
FROM bank_roles r WHERE r.name='admin'
AND NOT EXISTS (SELECT 1 FROM bank_users WHERE username='admin');

-- 测试用户
INSERT INTO bank_users (username,email,password,real_name,id_card,role_id,status)
SELECT 'testuser','test@bank.com','REPLACE_ADMIN_BCRYPT','测试用户','110101199001011235', r.id,'active'
FROM bank_roles r WHERE r.name='user'
AND NOT EXISTS (SELECT 1 FROM bank_users WHERE username='testuser');

-- 测试账户（admin名下）
INSERT INTO bank_accounts (account_number,user_id,account_type,balance,available_balance,frozen_amount,currency,interest_rate,status)
SELECT '001' || CAST(FLOOR(RAND()*90000000)+10000000 AS CHAR) || '0001', u.id, 'savings', 100000, 100000, 0, 'CNY', 0.035, 'active'
FROM bank_users u WHERE u.username='admin'
AND NOT EXISTS (SELECT 1 FROM bank_accounts a JOIN bank_users u2 ON a.user_id=u2.id WHERE u2.username='admin');

