-- ============================================
-- 供应商登录隔离 - 数据库初始化
-- 1. 新增供应商角色
-- 2. 创建供应商门户菜单树
-- 3. 为每家供应商创建登录账号并关联
-- 4. 配置角色-菜单权限
-- ============================================

-- 1. 新增供应商角色
INSERT INTO sys_role (role_id, role_name, role_key, role_sort, data_scope, menu_check_strictly, dept_check_strictly, status, del_flag, create_by, create_time, remark)
VALUES (3, '供应商', 'supplier', 3, 1, 1, 1, '0', '0', 'admin', NOW(), '供应商角色');

-- 2. 创建供应商门户菜单树
-- 2.1 父菜单：供应商门户（根级）
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
VALUES (2100, '供应商门户', 0, 4, 'supplier-portal', NULL, NULL, NULL, 1, 0, 'M', '0', '0', '', 'user', 'admin', NOW(), '供应商门户（菜单容器）');

-- 2.2 子菜单：我的报价请求
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
VALUES (2101, '我的报价请求', 2100, 1, 'my-rfq', 'bid/rfq/index', NULL, NULL, 1, 0, 'C', '0', '0', 'supplier:rfq:list', 'form', 'admin', NOW(), '供应商查看自己被邀请的报价请求');

-- 2.3 按钮权限：查询
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
VALUES (2102, '查询', 2101, 1, '#', NULL, NULL, NULL, 1, 0, 'F', '0', '0', 'supplier:rfq:query', '#', 'admin', NOW(), '');

-- 3. 供应商角色-菜单关联
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 3, menu_id FROM sys_menu WHERE menu_id IN (2100, 2101, 2102);

-- 4. 为每家供应商创建登录账号
-- 密码统一为 123456（BCrypt加密）
-- 说明：逐个创建用户，然后关联到 biz_supplier.user_id

-- 4.1 深圳市华顺达电子有限公司 → 用户名: hsd
INSERT INTO sys_user (dept_id, user_name, nick_name, user_type, phonenumber, password, status, del_flag, create_by, create_time, remark, tenant_id)
VALUES (100, 'hsd', '华顺达', '00', '13800138001', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', 'admin', NOW(), '供应商-华顺达', 1);
UPDATE biz_supplier SET user_id = (SELECT user_id FROM sys_user WHERE user_name = 'hsd') WHERE supplier_id = 1;

-- 4.2 苏州博远精密设备有限公司 → 用户名: boyuan
INSERT INTO sys_user (dept_id, user_name, nick_name, user_type, phonenumber, password, status, del_flag, create_by, create_time, remark, tenant_id)
VALUES (100, 'boyuan', '博远精密', '00', '13900139002', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', 'admin', NOW(), '供应商-博远精密', 1);
UPDATE biz_supplier SET user_id = (SELECT user_id FROM sys_user WHERE user_name = 'boyuan') WHERE supplier_id = 2;

-- 4.3 上海瑞达工控科技有限公司 → 用户名: ruida
INSERT INTO sys_user (dept_id, user_name, nick_name, user_type, phonenumber, password, status, del_flag, create_by, create_time, remark, tenant_id)
VALUES (100, 'ruida', '瑞达工控', '00', '13700137003', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', 'admin', NOW(), '供应商-瑞达工控', 1);
UPDATE biz_supplier SET user_id = (SELECT user_id FROM sys_user WHERE user_name = 'ruida') WHERE supplier_id = 3;

-- 4.4 广州联盟电气集团股份有限公司 → 用户名: lianmeng
INSERT INTO sys_user (dept_id, user_name, nick_name, user_type, phonenumber, password, status, del_flag, create_by, create_time, remark, tenant_id)
VALUES (100, 'lianmeng', '联盟电气', '00', '15900159004', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', 'admin', NOW(), '供应商-联盟电气', 1);
UPDATE biz_supplier SET user_id = (SELECT user_id FROM sys_user WHERE user_name = 'lianmeng') WHERE supplier_id = 4;

-- 4.5 武汉华中精工机械有限公司 → 用户名: hzjg
INSERT INTO sys_user (dept_id, user_name, nick_name, user_type, phonenumber, password, status, del_flag, create_by, create_time, remark, tenant_id)
VALUES (100, 'hzjg', '华中精工', '00', '13600136005', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', 'admin', NOW(), '供应商-华中精工', 1);
UPDATE biz_supplier SET user_id = (SELECT user_id FROM sys_user WHERE user_name = 'hzjg') WHERE supplier_id = 5;

-- 5. 为每个供应商用户分配 supplier 角色
INSERT INTO sys_user_role (user_id, role_id)
SELECT u.user_id, 3 FROM sys_user u
WHERE u.user_name IN ('hsd', 'boyuan', 'ruida', 'lianmeng', 'hzjg');
