USE jiaheng_erp;

SET NAMES utf8mb4;

START TRANSACTION;

-- 菜单权限
INSERT INTO sys_permission (permission_code, permission_name, module_code, action_code, status)
SELECT 'MENU_DASHBOARD', '经营驾驶舱菜单', 'DASHBOARD', 'VIEW', 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD');

INSERT INTO sys_permission (permission_code, permission_name, module_code, action_code, status)
SELECT 'MENU_SALES_PLANNING', '订单与计划中心菜单', 'SALES_PLANNING', 'VIEW', 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'MENU_SALES_PLANNING');

INSERT INTO sys_permission (permission_code, permission_name, module_code, action_code, status)
SELECT 'MENU_PROCESS_CENTER', '流程与工序中心菜单', 'PROCESS_CENTER', 'VIEW', 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'MENU_PROCESS_CENTER');

INSERT INTO sys_permission (permission_code, permission_name, module_code, action_code, status)
SELECT 'MENU_DATA_MODEL', '模块联动与数据设计菜单', 'DATA_MODEL', 'VIEW', 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'MENU_DATA_MODEL');

-- 员工
INSERT INTO hr_employee (
  employee_code, employee_name, dept_id, mobile, gender, hire_date, job_title,
  shift_code, manager_employee_id, is_operator, is_workshop_staff, status, remark
)
SELECT
  'EMP-ADMIN-001', '系统管理员',
  (SELECT id FROM sys_department WHERE dept_code = 'ADMIN'),
  '13800000001', 'M', CURRENT_DATE(), 'ERP管理员',
  'DAY', NULL, 0, 0, 'ACTIVE', '系统初始化账号'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM hr_employee WHERE employee_code = 'EMP-ADMIN-001');

INSERT INTO hr_employee (
  employee_code, employee_name, dept_id, mobile, gender, hire_date, job_title,
  shift_code, manager_employee_id, is_operator, is_workshop_staff, status, remark
)
SELECT
  'EMP-SALES-001', '销售专员',
  (SELECT id FROM sys_department WHERE dept_code = 'SALES'),
  '13800000002', 'F', CURRENT_DATE(), '销售跟单',
  'DAY', NULL, 0, 0, 'ACTIVE', '样例销售账号'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM hr_employee WHERE employee_code = 'EMP-SALES-001');

INSERT INTO hr_employee (
  employee_code, employee_name, dept_id, mobile, gender, hire_date, job_title,
  shift_code, manager_employee_id, is_operator, is_workshop_staff, status, remark
)
SELECT
  'EMP-PLAN-001', '计划员',
  (SELECT id FROM sys_department WHERE dept_code = 'PRODUCTION'),
  '13800000003', 'M', CURRENT_DATE(), '生产计划',
  'DAY', NULL, 0, 0, 'ACTIVE', '样例计划账号'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM hr_employee WHERE employee_code = 'EMP-PLAN-001');

INSERT INTO hr_employee (
  employee_code, employee_name, dept_id, mobile, gender, hire_date, job_title,
  shift_code, manager_employee_id, is_operator, is_workshop_staff, status, remark
)
SELECT
  'EMP-WH-001', '仓库主管',
  (SELECT id FROM sys_department WHERE dept_code = 'WAREHOUSE'),
  '13800000004', 'M', CURRENT_DATE(), '仓储主管',
  'DAY', NULL, 0, 0, 'ACTIVE', '样例仓储账号'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM hr_employee WHERE employee_code = 'EMP-WH-001');

INSERT INTO hr_employee (
  employee_code, employee_name, dept_id, mobile, gender, hire_date, job_title,
  shift_code, manager_employee_id, is_operator, is_workshop_staff, status, remark
)
SELECT
  'EMP-SUP-001', '车间主管',
  (SELECT id FROM sys_department WHERE dept_code = 'PRODUCTION'),
  '13800000005', 'M', CURRENT_DATE(), '车间主管',
  'DAY', NULL, 0, 1, 'ACTIVE', '样例车间主管账号'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM hr_employee WHERE employee_code = 'EMP-SUP-001');

-- 用户
INSERT INTO sys_user (
  username, password_hash, employee_id, dept_id, nickname, email,
  is_super_admin, last_login_at, status
)
SELECT
  'admin',
  'pbkdf2_sha256$600000$wQvlXNnVpATAKxQ4TW41JQ==$KcuwnxttYYu1RyThhviUb4BaCztSkwZ_Z-oUkz3lwzs=',
  (SELECT id FROM hr_employee WHERE employee_code = 'EMP-ADMIN-001'),
  (SELECT id FROM sys_department WHERE dept_code = 'ADMIN'),
  '系统管理员', 'admin@jiaheng.local',
  1, NULL, 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_user WHERE username = 'admin');

INSERT INTO sys_user (
  username, password_hash, employee_id, dept_id, nickname, email,
  is_super_admin, last_login_at, status
)
SELECT
  'sales',
  'pbkdf2_sha256$600000$dK6ZnRGU7UWZGguuiTZ63g==$R3H8ds5shAvmwK9AblmKgufZvlJTIPWzK_6CvO_a73M=',
  (SELECT id FROM hr_employee WHERE employee_code = 'EMP-SALES-001'),
  (SELECT id FROM sys_department WHERE dept_code = 'SALES'),
  '销售专员', 'sales@jiaheng.local',
  0, NULL, 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_user WHERE username = 'sales');

INSERT INTO sys_user (
  username, password_hash, employee_id, dept_id, nickname, email,
  is_super_admin, last_login_at, status
)
SELECT
  'planner',
  'pbkdf2_sha256$600000$yJtJeB76-AWLXUKd8ex20A==$PyZUtFuPjGlb0nYLBx9IHunjhtnV-HjE5pauLD-ogHk=',
  (SELECT id FROM hr_employee WHERE employee_code = 'EMP-PLAN-001'),
  (SELECT id FROM sys_department WHERE dept_code = 'PRODUCTION'),
  '计划员', 'planner@jiaheng.local',
  0, NULL, 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_user WHERE username = 'planner');

INSERT INTO sys_user (
  username, password_hash, employee_id, dept_id, nickname, email,
  is_super_admin, last_login_at, status
)
SELECT
  'warehouse',
  'pbkdf2_sha256$600000$FmDxVJ07KoHNLocfUMilGw==$ZKuYJnKc_aUZ1el128w9Ukn3ofC_OQQrr-86iV412XY=',
  (SELECT id FROM hr_employee WHERE employee_code = 'EMP-WH-001'),
  (SELECT id FROM sys_department WHERE dept_code = 'WAREHOUSE'),
  '仓库主管', 'warehouse@jiaheng.local',
  0, NULL, 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_user WHERE username = 'warehouse');

INSERT INTO sys_user (
  username, password_hash, employee_id, dept_id, nickname, email,
  is_super_admin, last_login_at, status
)
SELECT
  'supervisor',
  'pbkdf2_sha256$600000$GsM4Kgs232UoRx-QoHmZ_w==$E33NOSOdhtd_-LqqxL9QAYjPmOhDqUkyxXNj5CRe1aY=',
  (SELECT id FROM hr_employee WHERE employee_code = 'EMP-SUP-001'),
  (SELECT id FROM sys_department WHERE dept_code = 'PRODUCTION'),
  '车间主管', 'supervisor@jiaheng.local',
  0, NULL, 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_user WHERE username = 'supervisor');

-- 用户角色
INSERT INTO sys_user_role (user_id, role_id)
SELECT
  (SELECT id FROM sys_user WHERE username = 'admin'),
  (SELECT id FROM sys_role WHERE role_code = 'ADMIN')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_user_role
  WHERE user_id = (SELECT id FROM sys_user WHERE username = 'admin')
    AND role_id = (SELECT id FROM sys_role WHERE role_code = 'ADMIN')
);

INSERT INTO sys_user_role (user_id, role_id)
SELECT
  (SELECT id FROM sys_user WHERE username = 'sales'),
  (SELECT id FROM sys_role WHERE role_code = 'SALES')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_user_role
  WHERE user_id = (SELECT id FROM sys_user WHERE username = 'sales')
    AND role_id = (SELECT id FROM sys_role WHERE role_code = 'SALES')
);

INSERT INTO sys_user_role (user_id, role_id)
SELECT
  (SELECT id FROM sys_user WHERE username = 'planner'),
  (SELECT id FROM sys_role WHERE role_code = 'PLANNER')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_user_role
  WHERE user_id = (SELECT id FROM sys_user WHERE username = 'planner')
    AND role_id = (SELECT id FROM sys_role WHERE role_code = 'PLANNER')
);

INSERT INTO sys_user_role (user_id, role_id)
SELECT
  (SELECT id FROM sys_user WHERE username = 'warehouse'),
  (SELECT id FROM sys_role WHERE role_code = 'WAREHOUSE')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_user_role
  WHERE user_id = (SELECT id FROM sys_user WHERE username = 'warehouse')
    AND role_id = (SELECT id FROM sys_role WHERE role_code = 'WAREHOUSE')
);

INSERT INTO sys_user_role (user_id, role_id)
SELECT
  (SELECT id FROM sys_user WHERE username = 'supervisor'),
  (SELECT id FROM sys_role WHERE role_code = 'SUPERVISOR')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_user_role
  WHERE user_id = (SELECT id FROM sys_user WHERE username = 'supervisor')
    AND role_id = (SELECT id FROM sys_role WHERE role_code = 'SUPERVISOR')
);

-- 角色菜单权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'ADMIN'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'ADMIN')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'ADMIN'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_SALES_PLANNING')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'ADMIN')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_SALES_PLANNING')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'ADMIN'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_PROCESS_CENTER')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'ADMIN')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_PROCESS_CENTER')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'ADMIN'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DATA_MODEL')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'ADMIN')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DATA_MODEL')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'SALES'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'SALES')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'SALES'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_SALES_PLANNING')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'SALES')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_SALES_PLANNING')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'PLANNER'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'PLANNER')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'PLANNER'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_SALES_PLANNING')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'PLANNER')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_SALES_PLANNING')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'PLANNER'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_PROCESS_CENTER')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'PLANNER')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_PROCESS_CENTER')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'WAREHOUSE'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'WAREHOUSE')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'WAREHOUSE'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_PROCESS_CENTER')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'WAREHOUSE')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_PROCESS_CENTER')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'SUPERVISOR'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'SUPERVISOR')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_DASHBOARD')
);

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT
  (SELECT id FROM sys_role WHERE role_code = 'SUPERVISOR'),
  (SELECT id FROM sys_permission WHERE permission_code = 'MENU_PROCESS_CENTER')
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM sys_role_permission
  WHERE role_id = (SELECT id FROM sys_role WHERE role_code = 'SUPERVISOR')
    AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'MENU_PROCESS_CENTER')
);

-- 细分菜单权限
INSERT INTO sys_permission (permission_code, permission_name, module_code, action_code, status)
SELECT p.permission_code, p.permission_name, p.module_code, p.action_code, 'ACTIVE'
FROM (
  SELECT 'MENU_SUPPLIER_MANAGEMENT' AS permission_code, '供应商管理菜单' AS permission_name, 'SUPPLIER_MANAGEMENT' AS module_code, 'VIEW' AS action_code
  UNION ALL
  SELECT 'MENU_PURCHASE_ORDER', '采购订单菜单', 'PURCHASE_ORDER', 'VIEW'
  UNION ALL
  SELECT 'MENU_PURCHASE_RECEIPT', '到货入库菜单', 'PURCHASE_RECEIPT', 'VIEW'
  UNION ALL
  SELECT 'MENU_INVENTORY_LEDGER', '原材料库存台账与批次菜单', 'INVENTORY_LEDGER', 'VIEW'
  UNION ALL
  SELECT 'MENU_WORK_ORDER', '生产工单菜单', 'WORK_ORDER', 'VIEW'
  UNION ALL
  SELECT 'MENU_OPERATION_REPORT', '工序报工菜单', 'OPERATION_REPORT', 'VIEW'
  UNION ALL
  SELECT 'MENU_SCRAP_REWORK', '报废返工处理菜单', 'SCRAP_REWORK', 'VIEW'
  UNION ALL
  SELECT 'MENU_COMPLETION_RECEIPT', '成品入库菜单', 'COMPLETION_RECEIPT', 'VIEW'
  UNION ALL
  SELECT 'MENU_DELIVERY_MANAGEMENT', '发货管理菜单', 'DELIVERY_MANAGEMENT', 'VIEW'
  UNION ALL
  SELECT 'MENU_EQUIPMENT_MANAGEMENT', '设备台账与保养菜单', 'EQUIPMENT_MANAGEMENT', 'VIEW'
  UNION ALL
  SELECT 'MENU_QUALITY_INSPECTION', '质量检验菜单', 'QUALITY_INSPECTION', 'VIEW'
  UNION ALL
  SELECT 'MENU_FINANCE_REPORT', '财务成本与报表菜单', 'FINANCE_REPORT', 'VIEW'
  UNION ALL
  SELECT 'MENU_RETURN_MANAGEMENT', '退货处理菜单', 'RETURN_MANAGEMENT', 'VIEW'
  UNION ALL
  SELECT 'MENU_MATERIAL_MANAGEMENT', '原材料管理菜单', 'MATERIAL_MANAGEMENT', 'VIEW'
  UNION ALL
  SELECT 'MENU_BOM_MANAGEMENT', 'BOM管理菜单', 'BOM_MANAGEMENT', 'VIEW'
  UNION ALL
  SELECT 'MENU_ROUTE_MANAGEMENT', '工艺路线管理菜单', 'ROUTE_MANAGEMENT', 'VIEW'
  UNION ALL
  SELECT 'MENU_EMPLOYEE_MANAGEMENT', '员工管理菜单', 'EMPLOYEE_MANAGEMENT', 'VIEW'
  UNION ALL
  SELECT 'MENU_PRODUCT_MANAGEMENT', '产品管理菜单', 'PRODUCT_MANAGEMENT', 'VIEW'
  UNION ALL
  SELECT 'MENU_SYSTEM_PERMISSION', '系统权限管理菜单', 'SYSTEM_PERMISSION', 'VIEW'
  UNION ALL
  SELECT 'MENU_SYSTEM_EXTENSION', '系统拓展菜单', 'SYSTEM_EXTENSION', 'VIEW'
) AS p
LEFT JOIN sys_permission existing ON existing.permission_code = p.permission_code
WHERE existing.id IS NULL;

-- 补充角色测试账号对应员工
INSERT INTO hr_employee (
  employee_code, employee_name, dept_id, mobile, gender, hire_date, job_title,
  shift_code, manager_employee_id, is_operator, is_workshop_staff, status, remark
)
SELECT
  e.employee_code, e.employee_name, d.id, e.mobile, e.gender, CURRENT_DATE(), e.job_title,
  e.shift_code, NULL, e.is_operator, e.is_workshop_staff, 'ACTIVE', e.remark
FROM (
  SELECT 'EMP-PUR-001' AS employee_code, '采购专员' AS employee_name, 'PURCHASE' AS dept_code, '13800000006' AS mobile, 'F' AS gender, '采购跟单' AS job_title, 'DAY' AS shift_code, 0 AS is_operator, 0 AS is_workshop_staff, '样例采购账号' AS remark
  UNION ALL
  SELECT 'EMP-FIN-001', '财务专员', 'FINANCE', '13800000007', 'F', '成本会计', 'DAY', 0, 0, '样例财务账号'
  UNION ALL
  SELECT 'EMP-EQP-001', '设备管理员', 'EQUIPMENT', '13800000008', 'M', '设备工程师', 'DAY', 0, 0, '样例设备管理员账号'
  UNION ALL
  SELECT 'EMP-OP-001', '冲压操作工', 'PRODUCTION', '13800000009', 'M', '冲压操作工', 'A', 1, 1, '样例加工员工账号'
) AS e
JOIN sys_department d ON d.dept_code = e.dept_code
LEFT JOIN hr_employee existing ON existing.employee_code = e.employee_code
WHERE existing.id IS NULL;

-- 补充系统账号
INSERT INTO sys_user (
  username, password_hash, employee_id, dept_id, nickname, email,
  is_super_admin, last_login_at, status
)
SELECT
  u.username, u.password_hash, e.id, d.id, u.nickname, u.email,
  0, NULL, 'ACTIVE'
FROM (
  SELECT 'purchaser' AS username, 'pbkdf2_sha256$600000$ddxZt8DlrH-U27hQprqEzg==$QKgRLC3hdLmUI9nZ6AaP0mlVoH5vtu6diHeq9uS2fTg=' AS password_hash, 'EMP-PUR-001' AS employee_code, 'PURCHASE' AS dept_code, '采购专员' AS nickname, 'purchaser@jiaheng.local' AS email
  UNION ALL
  SELECT 'finance', 'pbkdf2_sha256$600000$K8pAnzCmAYKS74hZQ7M-uQ==$M1eT-INdsarnu297oEJNU_muEMxY8GMIGuYq3aiK_8A=', 'EMP-FIN-001', 'FINANCE', '财务专员', 'finance@jiaheng.local'
  UNION ALL
  SELECT 'equipadmin', 'pbkdf2_sha256$600000$FpH9NnUxpcxyc10mHnTzsg==$EHCnopdhmaIPzCIixzE431wEZVj7uk8IUaoA-0vAl1Y=', 'EMP-EQP-001', 'EQUIPMENT', '设备管理员', 'equipadmin@jiaheng.local'
  UNION ALL
  SELECT 'operator', 'pbkdf2_sha256$600000$zvfrPJQL-fSdmSbuJNns7w==$7U1npAmi-5KLSW3BPFWDwqXb0Vx-ag5q6rlVJVxTDkU=', 'EMP-OP-001', 'PRODUCTION', '加工员工', 'operator@jiaheng.local'
) AS u
JOIN hr_employee e ON e.employee_code = u.employee_code
JOIN sys_department d ON d.dept_code = u.dept_code
LEFT JOIN sys_user existing ON existing.username = u.username
WHERE existing.id IS NULL;

-- 补充用户角色
INSERT INTO sys_user_role (user_id, role_id)
SELECT u.id, r.id
FROM (
  SELECT 'purchaser' AS username, 'PURCHASER' AS role_code
  UNION ALL
  SELECT 'finance', 'FINANCE'
  UNION ALL
  SELECT 'equipadmin', 'EQUIP_ADMIN'
  UNION ALL
  SELECT 'operator', 'OPERATOR'
) AS m
JOIN sys_user u ON u.username = m.username
JOIN sys_role r ON r.role_code = m.role_code
LEFT JOIN sys_user_role existing ON existing.user_id = u.id AND existing.role_id = r.id
WHERE existing.id IS NULL;

-- 细分角色菜单权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT r.id, p.id
FROM (
  SELECT 'ADMIN' AS role_code, 'MENU_SUPPLIER_MANAGEMENT' AS permission_code
  UNION ALL
  SELECT 'ADMIN', 'MENU_PURCHASE_ORDER'
  UNION ALL
  SELECT 'ADMIN', 'MENU_PURCHASE_RECEIPT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_INVENTORY_LEDGER'
  UNION ALL
  SELECT 'ADMIN', 'MENU_WORK_ORDER'
  UNION ALL
  SELECT 'ADMIN', 'MENU_OPERATION_REPORT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_SCRAP_REWORK'
  UNION ALL
  SELECT 'ADMIN', 'MENU_COMPLETION_RECEIPT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_DELIVERY_MANAGEMENT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_EQUIPMENT_MANAGEMENT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_QUALITY_INSPECTION'
  UNION ALL
  SELECT 'ADMIN', 'MENU_FINANCE_REPORT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_RETURN_MANAGEMENT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_MATERIAL_MANAGEMENT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_BOM_MANAGEMENT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_ROUTE_MANAGEMENT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_EMPLOYEE_MANAGEMENT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_PRODUCT_MANAGEMENT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_SYSTEM_PERMISSION'
  UNION ALL
  SELECT 'ADMIN', 'MENU_SYSTEM_EXTENSION'
  UNION ALL
  SELECT 'SALES', 'MENU_DELIVERY_MANAGEMENT'
  UNION ALL
  SELECT 'SALES', 'MENU_RETURN_MANAGEMENT'
  UNION ALL
  SELECT 'PURCHASER', 'MENU_DASHBOARD'
  UNION ALL
  SELECT 'PURCHASER', 'MENU_SUPPLIER_MANAGEMENT'
  UNION ALL
  SELECT 'PURCHASER', 'MENU_PURCHASE_ORDER'
  UNION ALL
  SELECT 'PURCHASER', 'MENU_INVENTORY_LEDGER'
  UNION ALL
  SELECT 'PURCHASER', 'MENU_MATERIAL_MANAGEMENT'
  UNION ALL
  SELECT 'WAREHOUSE', 'MENU_PURCHASE_RECEIPT'
  UNION ALL
  SELECT 'WAREHOUSE', 'MENU_INVENTORY_LEDGER'
  UNION ALL
  SELECT 'WAREHOUSE', 'MENU_COMPLETION_RECEIPT'
  UNION ALL
  SELECT 'WAREHOUSE', 'MENU_DELIVERY_MANAGEMENT'
  UNION ALL
  SELECT 'WAREHOUSE', 'MENU_RETURN_MANAGEMENT'
  UNION ALL
  SELECT 'WAREHOUSE', 'MENU_QUALITY_INSPECTION'
  UNION ALL
  SELECT 'PLANNER', 'MENU_WORK_ORDER'
  UNION ALL
  SELECT 'PLANNER', 'MENU_INVENTORY_LEDGER'
  UNION ALL
  SELECT 'PLANNER', 'MENU_MATERIAL_MANAGEMENT'
  UNION ALL
  SELECT 'PLANNER', 'MENU_BOM_MANAGEMENT'
  UNION ALL
  SELECT 'PLANNER', 'MENU_ROUTE_MANAGEMENT'
  UNION ALL
  SELECT 'PLANNER', 'MENU_PRODUCT_MANAGEMENT'
  UNION ALL
  SELECT 'SUPERVISOR', 'MENU_WORK_ORDER'
  UNION ALL
  SELECT 'SUPERVISOR', 'MENU_OPERATION_REPORT'
  UNION ALL
  SELECT 'SUPERVISOR', 'MENU_SCRAP_REWORK'
  UNION ALL
  SELECT 'SUPERVISOR', 'MENU_COMPLETION_RECEIPT'
  UNION ALL
  SELECT 'SUPERVISOR', 'MENU_QUALITY_INSPECTION'
  UNION ALL
  SELECT 'SUPERVISOR', 'MENU_BOM_MANAGEMENT'
  UNION ALL
  SELECT 'SUPERVISOR', 'MENU_ROUTE_MANAGEMENT'
  UNION ALL
  SELECT 'SUPERVISOR', 'MENU_PRODUCT_MANAGEMENT'
  UNION ALL
  SELECT 'OPERATOR', 'MENU_OPERATION_REPORT'
  UNION ALL
  SELECT 'FINANCE', 'MENU_DASHBOARD'
  UNION ALL
  SELECT 'FINANCE', 'MENU_INVENTORY_LEDGER'
  UNION ALL
  SELECT 'FINANCE', 'MENU_FINANCE_REPORT'
  UNION ALL
  SELECT 'FINANCE', 'MENU_DELIVERY_MANAGEMENT'
  UNION ALL
  SELECT 'FINANCE', 'MENU_RETURN_MANAGEMENT'
  UNION ALL
  SELECT 'EQUIP_ADMIN', 'MENU_DASHBOARD'
  UNION ALL
  SELECT 'EQUIP_ADMIN', 'MENU_EQUIPMENT_MANAGEMENT'
  UNION ALL
  SELECT 'ADMIN', 'MENU_DASHBOARD'
  UNION ALL
  SELECT 'ADMIN', 'MENU_SALES_PLANNING'
  UNION ALL
  SELECT 'ADMIN', 'MENU_PROCESS_CENTER'
  UNION ALL
  SELECT 'ADMIN', 'MENU_DATA_MODEL'
  UNION ALL
  SELECT 'SALES', 'MENU_DASHBOARD'
  UNION ALL
  SELECT 'SALES', 'MENU_SALES_PLANNING'
  UNION ALL
  SELECT 'PLANNER', 'MENU_DASHBOARD'
  UNION ALL
  SELECT 'PLANNER', 'MENU_SALES_PLANNING'
  UNION ALL
  SELECT 'PLANNER', 'MENU_PROCESS_CENTER'
  UNION ALL
  SELECT 'SUPERVISOR', 'MENU_DASHBOARD'
  UNION ALL
  SELECT 'SUPERVISOR', 'MENU_PROCESS_CENTER'
) AS rp
JOIN sys_role r ON r.role_code = rp.role_code
JOIN sys_permission p ON p.permission_code = rp.permission_code
LEFT JOIN sys_role_permission existing ON existing.role_id = r.id AND existing.permission_id = p.id
WHERE existing.id IS NULL;

-- 当前仓储角色不再展示流程与工序中心总菜单，改为按仓储页面细分展示
DELETE rp
FROM sys_role_permission rp
JOIN sys_role r ON r.id = rp.role_id
JOIN sys_permission p ON p.id = rp.permission_id
WHERE r.role_code = 'WAREHOUSE'
  AND p.permission_code = 'MENU_PROCESS_CENTER';

COMMIT;
