-- 恭学教育 — 旧库 → 新库 迁移（匹配 2026-06-26 dump）
SET FOREIGN_KEY_CHECKS = 0;

-- ── 1. tenants 直接迁移 ──
INSERT IGNORE INTO gongxue.tenants (id, name, contact, phone, color, notes, status, created_at, updated_at)
SELECT id, name, contact, phone, color, notes, status, created_at, updated_at
FROM dorm_billing.tenants;

-- ── 2. rooms → rooms ──
INSERT IGNORE INTO gongxue.rooms (id, room_number, building, floor, capacity, status, room_type, gender, rental_category, monthly_rate, department_id, created_at)
SELECT r.id, r.room_number, COALESCE(r.building, 'A栋'), r.floor, r.capacity,
  CASE WHEN r.status = 'archived' THEN 'archived' ELSE 'available' END,
  NULLIF(r.room_type, ''), NULLIF(r.gender, ''),
  'short', 0, 1,
  COALESCE(r.created_at, NOW())
FROM dorm_billing.rooms r
WHERE r.status != 'archived';

-- ── 3. students → students ──
INSERT IGNORE INTO gongxue.students (id, name, phone, id_number, student_no, gender, ethnicity,
  emergency_contact, emergency_phone, supervisor, status, tenant_id, department_id, created_at, updated_at)
SELECT s.id, s.name, NULLIF(s.phone, ''), NULLIF(s.id_number, ''),
  NULL, -- 旧 dump 无 student_no
  NULLIF(s.gender, ''), NULLIF(s.ethnicity, ''),
  NULLIF(s.emergency_contact, ''), NULLIF(s.emergency_phone, ''),
  NULLIF(s.supervisor, ''),
  CASE s.status WHEN 'archived' THEN 'graduated' WHEN 'inactive' THEN 'inactive' ELSE 'active' END,
  (SELECT t.id FROM dorm_billing.tenants t WHERE t.name = s.organization LIMIT 1),
  1, -- 默认校区
  s.created_at, s.updated_at
FROM dorm_billing.students s
WHERE s.status != 'archived';

-- ── 4. occupancies → occupancies ──
INSERT IGNORE INTO gongxue.occupancies (id, student_id, room_id, check_in_date, check_out_date, billing_start_date, billing_end_date, check_out_reason, notes, rental_type, department_id, created_at)
SELECT o.id, o.student_id, o.room_id, o.check_in_date, o.check_out_date,
  o.billing_start_date, o.billing_end_date, o.check_out_reason, o.notes,
  'short', 1, o.created_at
FROM dorm_billing.occupancies o
WHERE EXISTS (SELECT 1 FROM gongxue.students WHERE id = o.student_id)
  AND EXISTS (SELECT 1 FROM gongxue.rooms WHERE id = o.room_id);

-- ── 5. bills → bills ──
INSERT IGNORE INTO gongxue.bills (id, student_id, period_start, period_end, shared_amount, personal_amount, total_amount, status, department_id, generated_at)
SELECT b.id, b.student_id, b.period_start, b.period_end,
  b.shared_amount, b.personal_amount, b.total_amount, b.status,
  1, b.generated_at
FROM dorm_billing.bills b
WHERE EXISTS (SELECT 1 FROM gongxue.students WHERE id = b.student_id);

-- ── 6. bill_items → bill_items ──
INSERT IGNORE INTO gongxue.bill_items (id, bill_id, room_id, expense_type, description, days, total_room_days, room_total_amount, student_amount)
SELECT bi.id, bi.bill_id, bi.room_id, bi.expense_type, bi.description, bi.days, bi.total_room_days, bi.room_total_amount, bi.student_amount
FROM dorm_billing.bill_items bi
WHERE EXISTS (SELECT 1 FROM gongxue.bills WHERE id = bi.bill_id);

-- ── 7. deposits → deposits ──
INSERT IGNORE INTO gongxue.deposits (id, student_id, amount, status, paid_date, refund_date, refund_amount, deduction_amount, deduction_reason, notes, recorded_by, department_id, created_at)
SELECT d.id, d.student_id, d.amount, d.status, d.paid_date, d.refund_date, d.refund_amount, d.deduction_amount, d.deduction_reason, d.notes, d.recorded_by, 1, d.created_at
FROM dorm_billing.deposits d
WHERE EXISTS (SELECT 1 FROM gongxue.students WHERE id = d.student_id);

-- ── 8. classrooms / classroom_rentals ──
INSERT IGNORE INTO gongxue.classrooms (id, name, building, floor, capacity, room_type, course_type, supervisor, status, notes, department_id, created_at)
SELECT id, name, building, floor, capacity, room_type, course_type, supervisor, status, notes, 1, created_at
FROM dorm_billing.classrooms;

INSERT IGNORE INTO gongxue.classroom_rentals (id, classroom_id, tenant_id, start_date, end_date, contract_path, contract_original_name, daily_rate, total_amount, status, notes, created_by, department_id, created_at, updated_at)
SELECT id, classroom_id, tenant_id, start_date, end_date, contract_path, contract_original_name, daily_rate, total_amount, status, notes, created_by, 1, created_at, updated_at
FROM dorm_billing.classroom_rentals;

-- ── 9. room_expenses / personal_expenses ──
INSERT IGNORE INTO gongxue.room_expenses (id, room_id, expense_type, amount, period_start, period_end, description, recorded_by, department_id, created_at)
SELECT id, room_id, expense_type, amount, period_start, period_end, description, recorded_by, 1, created_at
FROM dorm_billing.room_expenses
WHERE EXISTS (SELECT 1 FROM gongxue.rooms WHERE id = room_id);

INSERT IGNORE INTO gongxue.personal_expenses (id, student_id, room_id, expense_type, amount, expense_date, description, recorded_by, department_id, created_at)
SELECT id, student_id, room_id, expense_type, amount, expense_date, description, recorded_by, 1, created_at
FROM dorm_billing.personal_expenses
WHERE EXISTS (SELECT 1 FROM gongxue.students WHERE id = student_id);

-- ── 10. users → users（保留原始 bcrypt hash） ──
INSERT IGNORE INTO gongxue.users (id, username, password_hash, name, is_active, last_login_at, created_at, updated_at)
SELECT u.id, u.username, u.password_hash,
  COALESCE(NULLIF(u.name, ''), u.username),
  u.is_active, u.last_login_at,
  u.created_at, u.updated_at
FROM dorm_billing.users u;

-- ── 11. 用户-校区关联 ──
INSERT IGNORE INTO gongxue.user_departments (user_id, department_id, is_default)
SELECT u.id, 1, 1 FROM gongxue.users u;

-- ── 12. operation_logs → operation_logs ──
INSERT IGNORE INTO gongxue.operation_logs (id, user_id, username, module, action, target_id, target_type, detail, ip_address, user_agent, status, created_at)
SELECT id, user_id, username, module, action, target_id, target_type, detail, ip_address, user_agent, status, created_at
FROM dorm_billing.operation_logs;

SET FOREIGN_KEY_CHECKS = 1;

SELECT 'OK' AS status;
SELECT 'students' AS tbl, COUNT(*) AS cnt FROM gongxue.students
UNION ALL SELECT 'occupancies', COUNT(*) FROM gongxue.occupancies
UNION ALL SELECT 'rooms', COUNT(*) FROM gongxue.rooms
UNION ALL SELECT 'bills', COUNT(*) FROM gongxue.bills
UNION ALL SELECT 'deposits', COUNT(*) FROM gongxue.deposits
UNION ALL SELECT 'users', COUNT(*) FROM gongxue.users
UNION ALL SELECT 'tenants', COUNT(*) FROM gongxue.tenants
UNION ALL SELECT 'classrooms', COUNT(*) FROM gongxue.classrooms;
