-- 商户财务视角：我的收入菜单 + 平台运营视角权限
--
-- 号段：1926061460000000101 ~ 1926061460000000108
-- 父菜单：2048654050841710593（业务管理）
-- subsystem_id：1820814889969582082
--
-- 说明：
-- 1. 「我的收入」与「商户应收账」共用同一页面组件，商户角色仅分配本脚本按钮（无 summary / platformView）。
-- 2. 平台财务角色在原有 finance_menu_permission.sql 基础上追加 platformView 按钮。
-- 3. 执行后请为商户角色授权本菜单及按钮；为平台角色追加 platformView 按钮。

-- 执行前检查
SELECT id, menu_name, menu_path
FROM sys_menu_info
WHERE del_flag = '0'
  AND menu_path IN ('businessFinanceMyIncome', 'businessFinanceMerchantReceivable');

-- 1. 商户侧菜单：我的收入
INSERT INTO sys_menu_info (
    id, create_by, create_date, update_by, update_date, remarks, del_flag,
    organize_id, tenant_id, tenant_code, sort_num, menu_name, menu_short_name,
    menu_path, target, menu_parent_ids, menu_parent_id, menu_icon, is_show,
    permission, menu_type, tip, subsystem_id, category, class_path, component, verification
) VALUES (
    '1926061460000000101', '1', NOW(), '1', NOW(), 'finance_merchant_finance_permission', '0',
    NULL, '1', '1', 31, '我的收入', '我的收入',
    'businessFinanceMyIncome', '', '0,2048654050841710593,', '2048654050841710593', 'Wallet', '1',
    '', '2', '', '1820814889969582082', 'WEB', '', 'business/finance/merchantReceivable/index', '0'
);

INSERT INTO sys_menu_button_info (
    id, create_date, create_by, update_date, update_by, del_flag, remarks,
    tenant_id, tenant_code, organize_id, button_code, button_name, enable, menu_id, sort_num, sys_data, source
) VALUES
('1926061460000000102', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantReceivable:info', '详情', '1', '1926061460000000101', 10, '0', '0'),
('1926061460000000103', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantReceivable:list', '列表', '1', '1926061460000000101', 20, '0', '0'),
('1926061460000000104', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantReceivable:export', '导出', '1', '1926061460000000101', 40, '0', '0'),
('1926061460000000106', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantReceivable:mySummary', '本店汇总', '1', '1926061460000000101', 30, '0', '0');

-- 2. 平台运营视角：可查看抽成与全商户汇总（挂到原「商户应收账」菜单）
INSERT INTO sys_menu_button_info (
    id, create_date, create_by, update_date, update_by, del_flag, remarks,
    tenant_id, tenant_code, organize_id, button_code, button_name, enable, menu_id, sort_num, sys_data, source
) VALUES
('1926061460000000105', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantReceivable:platformView', '平台视角', '1', '1926061430000000101', 50, '0', '0');

-- 角色授权示例（将 YOUR_MERCHANT_ROLE_ID / YOUR_PLATFORM_ROLE_ID 替换后执行）：
-- INSERT INTO sys_role_menu(role_id, menu_id) VALUES ('YOUR_MERCHANT_ROLE_ID', '1926061460000000101');
-- INSERT INTO sys_role_menu(role_id, menu_id)
-- SELECT 'YOUR_MERCHANT_ROLE_ID', id FROM sys_menu_button_info WHERE menu_id = '1926061460000000101';
-- INSERT INTO sys_role_menu(role_id, menu_id)
-- SELECT 'YOUR_PLATFORM_ROLE_ID', id FROM sys_menu_button_info WHERE id = '1926061460000000105';

-- 3. 阶段 B：商户侧「我的分账」（微信分账模式，裁剪版分账单列表）
INSERT INTO sys_menu_info (
    id, create_by, create_date, update_by, update_date, remarks, del_flag,
    organize_id, tenant_id, tenant_code, sort_num, menu_name, menu_short_name,
    menu_path, target, menu_parent_ids, menu_parent_id, menu_icon, is_show,
    permission, menu_type, tip, subsystem_id, category, class_path, component, verification
) VALUES (
    '1926061460000000107', '1', NOW(), '1', NOW(), 'finance_merchant_finance_permission', '0',
    NULL, '1', '1', 32, '我的分账', '我的分账',
    'businessFinanceMyProfitSharing', '', '0,2048654050841710593,', '2048654050841710593', 'Share', '1',
    '', '2', '', '1820814889969582082', 'WEB', '', 'business/finance/profitSharing/index', '0'
);

INSERT INTO sys_menu_button_info (
    id, create_date, create_by, update_date, update_by, del_flag, remarks,
    tenant_id, tenant_code, organize_id, button_code, button_name, enable, menu_id, sort_num, sys_data, source
) VALUES
('1926061460000000108', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:profitSharing:info', '详情', '1', '1926061460000000107', 10, '0', '0'),
('1926061460000000109', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:profitSharing:list', '列表', '1', '1926061460000000107', 20, '0', '0');

-- 4. 阶段 C：平台「商户结算单」+ 商户「我的结算单」
INSERT INTO sys_menu_info (
    id, create_by, create_date, update_by, update_date, remarks, del_flag,
    organize_id, tenant_id, tenant_code, sort_num, menu_name, menu_short_name,
    menu_path, target, menu_parent_ids, menu_parent_id, menu_icon, is_show,
    permission, menu_type, tip, subsystem_id, category, class_path, component, verification
) VALUES (
    '1926061470000000101', '1', NOW(), '1', NOW(), 'finance_merchant_finance_permission', '0',
    NULL, '1', '1', 33, '商户结算单', '商户结算单',
    'businessFinanceMerchantSettlementBill', '', '0,2048654050841710593,', '2048654050841710593', 'Tickets', '1',
    '', '2', '', '1820814889969582082', 'WEB', '', 'business/finance/merchantSettlementBill/index', '0'
);

INSERT INTO sys_menu_button_info (
    id, create_date, create_by, update_date, update_by, del_flag, remarks,
    tenant_id, tenant_code, organize_id, button_code, button_name, enable, menu_id, sort_num, sys_data, source
) VALUES
('1926061470000000102', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantSettlementBill:info', '详情', '1', '1926061470000000101', 10, '0', '0'),
('1926061470000000103', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantSettlementBill:list', '列表', '1', '1926061470000000101', 20, '0', '0'),
('1926061470000000104', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantSettlementBill:generate', '生成', '1', '1926061470000000101', 30, '0', '0'),
('1926061470000000105', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantSettlementBill:recordPayout', '登记打款', '1', '1926061470000000101', 40, '0', '0'),
('1926061470000000106', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantSettlementBill:cancel', '作废', '1', '1926061470000000101', 50, '0', '0');

INSERT INTO sys_menu_info (
    id, create_by, create_date, update_by, update_date, remarks, del_flag,
    organize_id, tenant_id, tenant_code, sort_num, menu_name, menu_short_name,
    menu_path, target, menu_parent_ids, menu_parent_id, menu_icon, is_show,
    permission, menu_type, tip, subsystem_id, category, class_path, component, verification
) VALUES (
    '1926061470000000107', '1', NOW(), '1', NOW(), 'finance_merchant_finance_permission', '0',
    NULL, '1', '1', 34, '我的结算单', '我的结算单',
    'businessFinanceMySettlementBill', '', '0,2048654050841710593,', '2048654050841710593', 'Tickets', '1',
    '', '2', '', '1820814889969582082', 'WEB', '', 'business/finance/merchantSettlementBill/index', '0'
);

INSERT INTO sys_menu_button_info (
    id, create_date, create_by, update_date, update_by, del_flag, remarks,
    tenant_id, tenant_code, organize_id, button_code, button_name, enable, menu_id, sort_num, sys_data, source
) VALUES
('1926061470000000108', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantSettlementBill:info', '详情', '1', '1926061470000000107', 10, '0', '0'),
('1926061470000000109', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantSettlementBill:list', '列表', '1', '1926061470000000107', 20, '0', '0'),
('1926061470000000110', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:merchantSettlementBill:merchantConfirm', '确认收款', '1', '1926061470000000107', 30, '0', '0');

-- 5. 平台收入日汇总：手动触发回填（挂「商户应收账」平台菜单）
INSERT INTO sys_menu_button_info (
    id, create_date, create_by, update_date, update_by, del_flag, remarks,
    tenant_id, tenant_code, organize_id, button_code, button_name, enable, menu_id, sort_num, sys_data, source
) VALUES
('1926061480000000101', NOW(), '1', NOW(), '1', '0', NULL, '1', '1', NULL, 'manager:business:finance:platformRevenue:rollup', '汇总回填', '1', '1926061430000000101', 60, '0', '0');

-- 6. 修正结算单菜单 component（须共用 merchantSettlementBill/index，勿写 mySettlementBill/index）
UPDATE sys_menu_info
SET component = 'business/finance/merchantSettlementBill/index',
    update_date = NOW(),
    update_by = '1',
    remarks = CONCAT(IFNULL(NULLIF(remarks, ''), 'finance_merchant_finance_permission'), '; fix settlement component')
WHERE del_flag = '0'
  AND menu_path IN ('businessFinanceMySettlementBill', 'businessFinanceMerchantSettlementBill')
  AND (
      component IS NULL
      OR TRIM(component) = ''
      OR component LIKE '%mySettlementBill%'
  );
