-- Ether E2E测试数据准备脚本
-- 执行时间: 2026-02-18
-- 用途: 为E2E测试准备必要的测试数据

-- ========================================
-- 1. 创建测试项目 (在ether_mdm数据库)
-- ========================================
\c ether_mdm

INSERT INTO mdm_project (id, code, name, address, status, project_type, initialized, created_at, updated_at) 
VALUES ('a1b2c3d4-e5f6-7890-abcd-ef1234567890', 'P2024001', 'E2E测试项目', '测试地址123号', 'ACTIVE', 'RESIDENTIAL', true, NOW(), NOW())
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, updated_at = NOW();

-- ========================================
-- 2. 创建测试空间节点
-- ========================================
INSERT INTO mdm_space_node (id, project_id, parent_id, code, name, node_type, status, created_at, updated_at) VALUES
('11111111-1111-1111-1111-111111111001', 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', NULL, 'BLD-001', '测试楼栋', 'BUILDING', 'ACTIVE', NOW(), NOW()),
('11111111-1111-1111-1111-111111111002', 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', '11111111-1111-1111-1111-111111111001', 'FLR-001', '测试楼层', 'FLOOR', 'ACTIVE', NOW(), NOW()),
('11111111-1111-1111-1111-111111111003', 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', '11111111-1111-1111-1111-111111111002', 'ROOM-001', '测试房间101', 'ROOM', 'ACTIVE', NOW(), NOW())
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name;

-- ========================================
-- 3. 创建测试业主
-- ========================================
INSERT INTO mdm_owner (id, project_id, name, phone, owner_type, status, created_at, updated_at) VALUES
('22222222-2222-2222-2222-222222222001', 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', '测试业主张三', '13800000020', 'PERSONAL', 'ACTIVE', NOW(), NOW()),
('22222222-2222-2222-2222-222222222002', 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', '测试业主李四', '13800000021', 'PERSONAL', 'ACTIVE', NOW(), NOW())
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name;

-- ========================================
-- 4. 创建测试设备
-- ========================================
INSERT INTO mdm_equipment (id, project_id, equipment_code, name, equipment_type, status, created_at, updated_at) VALUES
('33333333-3333-3333-3333-333333333001', 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', 'EQ-TEST-001', '测试电梯1号', 'ELEVATOR', 'NORMAL', NOW(), NOW()),
('33333333-3333-3333-3333-333333333002', 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', 'EQ-TEST-002', '测试门禁1号', 'ACCESS_CONTROL', 'NORMAL', NOW(), NOW())
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name;

-- ========================================
-- 5. 创建测试收费项目
-- ========================================
INSERT INTO fee_items (id, project_id, name, fee_type, billing_method, amount, unit, status, created_at, updated_at) VALUES
('44444444-4444-4444-4444-444444444001', 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', '测试物业费', 'PROPERTY_FEE', 'FIXED', 100.00, 'MONTH', 'ACTIVE', NOW(), NOW()),
('44444444-4444-4444-4444-444444444002', 'a1b2c3d4-e5f6-7890-abcd-ef1234567890', '测试停车费', 'PARKING_FEE', 'FIXED', 200.00, 'MONTH', 'ACTIVE', NOW(), NOW())
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name;

-- ========================================
-- 6. 创建测试用户 (在ether_auth数据库)
-- ========================================
\c ether_auth

-- 测试用户密码都是 Test@123
INSERT INTO auth_user (id, username, password, real_name, phone, email, status, created_at, updated_at) VALUES
('55555555-5555-5555-5555-555555555001', 'test_user1', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', '测试用户1', '13800000001', 'test1@e2e.test', 'ACTIVE', NOW(), NOW()),
('55555555-5555-5555-5555-555555555002', 'test_user2', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', '测试用户2', '13800000002', 'test2@e2e.test', 'ACTIVE', NOW(), NOW()),
('55555555-5555-5555-5555-555555555003', 'test_user3', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', '测试用户3', '13800000003', 'test3@e2e.test', 'ACTIVE', NOW(), NOW()),
('55555555-5555-5555-5555-555555555004', 'test_user4', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', '测试用户4', '13800000004', 'test4@e2e.test', 'ACTIVE', NOW(), NOW()),
('55555555-5555-5555-5555-555555555005', 'test_user5', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', '测试用户5', '13800000005', 'test5@e2e.test', 'ACTIVE', NOW(), NOW()),
('55555555-5555-5555-5555-555555555010', 'owner_user', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', '测试业主', '13800000010', 'owner@e2e.test', 'ACTIVE', NOW(), NOW()),
('55555555-5555-5555-5555-555555555011', 'worker_user', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', '测试员工', '13800000011', 'worker@e2e.test', 'ACTIVE', NOW(), NOW())
ON CONFLICT (username) DO NOTHING;

-- ========================================
-- 7. 创建测试角色
-- ========================================
INSERT INTO auth_role (id, name, code, description, is_system, status, created_at, updated_at) VALUES
('66666666-6666-6666-6666-666666666001', '测试角色', 'TEST_ROLE', 'E2E测试用角色', false, 'ACTIVE', NOW(), NOW())
ON CONFLICT (code) DO NOTHING;

-- ========================================
-- 验证数据
-- ========================================
\c ether_mdm
SELECT '项目' as type, COUNT(*) as count FROM mdm_project
UNION ALL
SELECT '空间节点' as type, COUNT(*) as count FROM mdm_space_node
UNION ALL
SELECT '业主' as type, COUNT(*) as count FROM mdm_owner
UNION ALL
SELECT '设备' as type, COUNT(*) as count FROM mdm_equipment
UNION ALL
SELECT '收费项目' as type, COUNT(*) as count FROM fee_items;

\c ether_auth
SELECT '用户' as type, COUNT(*) as count FROM auth_user
UNION ALL
SELECT '角色' as type, COUNT(*) as count FROM auth_role;
