-- ============================================================
-- 桃育种业务模块 · 菜单树（单一权威种子，重构版）
-- ------------------------------------------------------------
-- 本文件为全系统唯一菜单种子。其余种子源已收敛：
--   bre_menu_extra.sql  -> 已废弃（旧版 900130 段与 900200-900318 段冲突），
--                          由 _gen_breeding_be.py 生成器输出改为占位说明。
--   _gen_breeding_be.py -> 生成器仍可生成后端骨架，但不再负责菜单种子。
--
-- 拥有 id 段：
--   900001-900199  一级菜单 + 子项 + 按钮 A 段（10 个已实装模块）
--   900200-900299  预留树（分子育种/品种保护/AI增强）—— 保留段！
--   900300-900499  按钮 C 段（单株/评价/试验/统计 11 个模块）
--   900500-900599  按钮 D 段（系谱/性状/选择规则/性状观测/无性系/砧木/种子批 7 模块补缺）
--   900600-900699  按钮 E 段（花粉档案 1 个模块）
--   900700-900799  按钮 F 段（seq 0-3 分子层 marker/genotype_dataset/genotype_sample/genotype_call；
--                   seq 4-7 批2 通用观测/农事操作/育种报告/分析数据集）
--
-- 互删防护（H8）：DELETE 只清本文件拥有段，绝不触碰 900200-900299 预留树。
-- 预留树以线上库为权威，本文件以 ON CONFLICT (id) DO NOTHING 幂等补灌，
-- 保证全新环境可重建、重跑不摧毁线上已部署的预留菜单。
--
-- 列说明："order" 是 PostgreSQL 保留字，用双引号引用；
--   uuid/created_time/updated_time/is_deleted 均 NOT NULL 且无默认值，须显式提供。
-- ============================================================

-- 清理本文件拥有段（不触碰 900200-900299 预留树）
DELETE FROM sys_menu WHERE id BETWEEN 900001 AND 900199 OR id BETWEEN 900300 AND 900599 OR id BETWEEN 900600 AND 900699;

-- 1) 菜单与子项（以线上库为权威，含阶段容器与已实装模块组件路径）
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
VALUES
    (gen_random_uuid(), now(), now(), '900001', '基础数据', '1', 'ri:database-2-line', '1', NULL, 'BasicData', '/breeding', NULL, '/breeding/site', false, true, false, '基础数据', '0', false, 'web', '基础主数据与配置', NULL, NULL, false, false, false, false, NULL, NULL, NULL),
    (gen_random_uuid(), now(), now(), '900002', '种质资源', '1', 'ri:seedling-line', '2', NULL, 'GermplasmRoot', '/breeding', NULL, '/breeding/germplasm', false, true, false, '种质资源', '0', false, 'web', '亲本档案与系谱', NULL, NULL, false, false, false, false, NULL, NULL, NULL),
    (gen_random_uuid(), now(), now(), '900003', '育种流程', '1', 'ri:git-branch-line', '3', NULL, 'BreedingFlow', '/breeding', NULL, '/breeding/stage_cross', false, true, false, '育种流程', '0', false, 'web', '核心育种时序（阶段页）', NULL, NULL, false, false, false, false, NULL, NULL, NULL),
    (gen_random_uuid(), now(), now(), '900004', '试验管理', '1', 'ri:flask-line', '4', NULL, 'TrialRoot', '/breeding', NULL, '/breeding/program', false, true, false, '试验管理', '0', false, 'web', '多年多点试验（MET，待建）', NULL, NULL, false, false, false, false, NULL, NULL, NULL),
    (gen_random_uuid(), now(), now(), '900005', '统计分析', '1', 'ri:bar-chart-2-line', '5', NULL, 'StatisticsRoot', '/breeding', NULL, '/breeding/statistics', false, true, false, '统计分析', '0', false, 'web', '统计决策（待建）', NULL, NULL, false, false, false, false, NULL, NULL, NULL),
    (gen_random_uuid(), now(), now(), '900010', '试验基地', '2', 'ri:map-pin-line', '1', 'module_bre:site:query', 'Site', 'site', 'module_bre/site/index', NULL, false, true, false, '试验基地', '0', false, 'web', '育种基地/试验站', '900001', NULL, false, false, false, false, NULL, '/bre/site', NULL),
    (gen_random_uuid(), now(), now(), '900011', '试验地块', '2', 'ri:grid-line', '2', 'module_bre:plot:query', 'Plot', 'plot', 'module_bre/plot/index', NULL, false, true, false, '试验地块', '0', false, 'web', '试验地块（隶属基地）', '900001', NULL, false, false, false, false, NULL, '/bre/plot', NULL),
    (gen_random_uuid(), now(), now(), '900012', '育种人员', '2', 'ri:user-line', '3', 'module_bre:personnel:query', 'Personnel', 'personnel', 'module_bre/personnel/index', NULL, false, true, false, '育种人员', '0', false, 'web', '人员花名册', '900001', NULL, false, false, false, false, NULL, '/bre/personnel', NULL),
    (gen_random_uuid(), now(), now(), '900013', '育种目标', '2', 'ri:target-line', '4', 'module_bre:target:query', 'Target', 'target', 'module_bre/target/index', NULL, false, true, false, '育种目标', '0', false, 'web', '育种目标', '900001', NULL, false, false, false, false, NULL, '/bre/target', NULL),
    (gen_random_uuid(), now(), now(), '900014', '性状字典', '2', 'ri:list-settings-line', '5', 'module_bre:trait:query', 'Trait', 'trait', 'module_bre/trait/index', NULL, false, true, false, '性状字典', '0', false, 'web', '性状定义（待建）', '900001', NULL, false, false, false, false, NULL, '/bre/trait', NULL),
    (gen_random_uuid(), now(), now(), '900015', '选择规则', '2', 'ri:shield-check-line', '6', 'module_bre:selection_rule:query', 'SelectionRule', 'selection_rule', 'module_bre/selection_rule/index', NULL, false, true, false, '选择规则', '0', false, 'web', '选择规则（待建）', '900001', NULL, false, false, false, false, NULL, '/bre/selection_rule', NULL),
    (gen_random_uuid(), now(), now(), '900016', '砧木管理', '2', 'ri:tree-line', '7', 'module_bre:rootstock:query', 'Rootstock', 'rootstock', 'module_bre/rootstock/index', NULL, false, true, false, '砧木管理', '0', false, 'web', '砧木管理（规格 §3.17）', '900001', NULL, false, false, false, false, NULL, '/bre/rootstock', NULL),
    (gen_random_uuid(), now(), now(), '900020', '亲本资源', '2', 'ri:seedling-line', '1', 'module_bre:germplasm:query', 'Germplasm', 'germplasm', 'module_bre/germplasm/index', NULL, false, true, false, '亲本资源', '0', false, 'web', '亲本/种质档案', '900002', NULL, false, false, false, false, NULL, '/bre/germplasm', NULL),
    (gen_random_uuid(), now(), now(), '900021', '系谱管理', '2', 'ri:git-merge-line', '2', 'module_bre:pedigree:query', 'Pedigree', 'pedigree', 'module_bre/pedigree/index', NULL, false, true, false, '系谱管理', '0', false, 'web', '系谱（待建）', '900002', NULL, false, false, false, false, NULL, '/bre/pedigree', NULL),
    (gen_random_uuid(), now(), now(), '900030', '杂交配组', '2', 'ri:git-branch-line', '1', NULL, 'StageCross', 'stage_cross', 'module_bre/stage_cross/index', NULL, false, true, false, '杂交配组', '0', false, 'web', '杂交组合 / 授粉', '900003', NULL, false, false, false, false, NULL, '/bre/stage_cross', NULL),
    (gen_random_uuid(), now(), now(), '900031', '种子与繁育', '2', 'ri:test-tube-line', '2', NULL, 'StageSeed', 'stage_seed', 'module_bre/stage_seed/index', NULL, false, true, false, '种子与繁育', '0', false, 'web', '种子处理 / 育苗 / 克隆扩繁', '900003', NULL, false, false, false, false, NULL, '/bre/stage_seed', NULL),
    (gen_random_uuid(), now(), now(), '900032', '田间植株', '2', 'ri:tree-line', '3', NULL, 'StageField', 'stage_field', 'module_bre/stage_field/index', NULL, false, true, false, '田间植株', '0', false, 'web', '定植 / 育种单株 / 环境气象', '900003', NULL, false, false, false, false, NULL, '/bre/stage_field', NULL),
    (gen_random_uuid(), now(), now(), '900033', '表型鉴定', '2', 'ri:microscope-line', '4', NULL, 'StagePhenotype', 'stage_phenotype', 'module_bre/stage_phenotype/index', NULL, false, true, false, '表型鉴定', '0', false, 'web', '单株评价 / 照片 / 通用观测 / 农事', '900003', NULL, false, false, false, false, NULL, '/bre/stage_phenotype', NULL),
    (gen_random_uuid(), now(), now(), '900034', '评价选择', '2', 'ri:award-line', '5', NULL, 'StageSelection', 'stage_selection', 'module_bre/stage_selection/index', NULL, false, true, false, '评价选择', '0', false, 'web', '选育结果', '900003', NULL, false, false, false, false, NULL, '/bre/stage_selection', NULL),
    (gen_random_uuid(), now(), now(), '900040', '杂交组合', '2', 'ri:git-branch-line', '1', 'module_bre:cross_combination:query', 'CrossCombination', 'cross_combination', 'module_bre/cross_combination/index', NULL, true, true, false, '杂交组合', '0', false, 'web', '杂交组合', '900030', NULL, false, false, false, false, NULL, '/bre/cross_combination', NULL),
    (gen_random_uuid(), now(), now(), '900041', '授粉管理', '2', 'ri:flower-line', '2', 'module_bre:pollination:query', 'Pollination', 'pollination', 'module_bre/pollination/index', NULL, true, true, false, '授粉管理', '0', false, 'web', '授粉管理', '900030', NULL, false, false, false, false, NULL, '/bre/pollination', NULL),
    (gen_random_uuid(), now(), now(), '900056', '花粉档案', '2', 'ri:flower-fill', '3', 'module_bre:pollen:query', 'Pollen', 'pollen', 'module_bre/pollen/index', NULL, true, true, false, '花粉档案', '0', false, 'web', '花粉档案（规格 §3.19）：采集/贮藏/活力/有效期，授粉窗口地基', '900030', NULL, false, false, false, false, NULL, '/bre/pollen', NULL),
    (gen_random_uuid(), now(), now(), '900042', '种子处理', '2', 'ri:test-tube-line', '1', 'module_bre:seed_treatment:query', 'SeedTreatment', 'seed_treatment', 'module_bre/seed_treatment/index', NULL, true, true, false, '种子处理', '0', false, 'web', '种子处理', '900031', NULL, false, false, false, false, NULL, '/bre/seed_treatment', NULL),
    (gen_random_uuid(), now(), now(), '900043', '育苗管理', '2', 'ri:sprout-line', '3', 'module_bre:seedling:query', 'Seedling', 'seedling', 'module_bre/seedling/index', NULL, true, true, false, '育苗管理', '0', false, 'web', '育苗管理', '900031', NULL, false, false, false, false, NULL, '/bre/seedling', NULL),
    (gen_random_uuid(), now(), now(), '900054', '种子批', '2', 'ri:archive-drawer-line', '2', 'module_bre:seed_lot:query', 'SeedLot', 'seed_lot', 'module_bre/seed_lot/index', NULL, true, true, false, '种子批', '0', false, 'web', '种子批（选择强度链源头，规格 §3.13）', '900031', NULL, false, false, false, false, NULL, '/bre/seed_lot', NULL),
    (gen_random_uuid(), now(), now(), '900044', '无性系', '2', 'ri:node-tree', '4', 'module_bre:clone:query', 'Clone', 'clone', 'module_bre/clone/index', NULL, true, true, false, '无性系', '0', false, 'web', '无性系（单株系）管理', '900031', NULL, false, false, false, false, NULL, '/bre/clone', NULL),
    (gen_random_uuid(), now(), now(), '900055', '克隆扩繁', '2', 'ri:sprout-line', '5', 'module_bre:propagation:query', 'Propagation', 'propagation', 'module_bre/propagation/index', NULL, true, true, false, '克隆扩繁', '0', false, 'web', '克隆扩繁批次（§3.8）', '900031', NULL, false, false, false, false, NULL, '/bre/propagation', NULL),
    (gen_random_uuid(), now(), now(), '900045', '定植管理', '2', 'ri:tree-line', '1', 'module_bre:planting:query', 'Planting', 'planting', 'module_bre/planting/index', NULL, true, true, false, '定植管理', '0', false, 'web', '定植管理', '900032', NULL, false, false, false, false, NULL, '/bre/planting', NULL),
    (gen_random_uuid(), now(), now(), '900046', '育种单株', '2', 'ri:forest-line', '2', 'module_bre:tree:query', 'Tree', 'tree', 'module_bre/tree/index', NULL, true, true, false, '育种单株', '0', false, 'web', '育种单株', '900032', NULL, false, false, false, false, NULL, '/bre/tree', NULL),
    (gen_random_uuid(), now(), now(), '900047', '环境气象', '2', 'ri:cloud-line', '3', 'module_bre:environment_condition:query', 'EnvironmentCondition', 'environment_condition', 'module_bre/environment_condition/index', NULL, true, true, false, '环境气象', '0', false, 'web', '环境因子（G×E 协变量，§3.9）', '900032', NULL, false, false, false, false, NULL, '/bre/environment_condition', NULL),
    (gen_random_uuid(), now(), now(), '900048', '单株评价', '2', 'ri:clipboard-line', '1', 'module_bre:tree_evaluation:query', 'TreeEvaluation', 'tree_evaluation', 'module_bre/tree_evaluation/index', NULL, true, true, false, '单株评价', '0', false, 'web', '单株评价', '900033', NULL, false, false, false, false, NULL, '/bre/tree_evaluation', NULL),
    (gen_random_uuid(), now(), now(), '900049', '单株照片', '2', 'ri:camera-line', '2', 'module_bre:tree_photo:query', 'TreePhoto', 'tree_photo', 'module_bre/tree_photo/index', NULL, true, true, false, '单株照片', '0', false, 'web', '单株照片', '900033', NULL, false, false, false, false, NULL, '/bre/tree_photo', NULL),
    (gen_random_uuid(), now(), now(), '900050', '通用观测', '2', 'ri:eye-line', '3', 'module_bre:observation:query', 'Observation', 'observation', 'module_bre/_coming_soon/index', NULL, true, true, false, '通用观测', '0', false, 'web', '通用观测（待建）', '900033', NULL, false, false, false, false, NULL, '/bre/observation', NULL),
    (gen_random_uuid(), now(), now(), '900051', '农事操作', '2', 'ri:tools-line', '4', 'module_bre:field_operation:query', 'FieldOperation', 'field_operation', 'module_bre/_coming_soon/index', NULL, true, true, false, '农事操作', '0', false, 'web', '农事操作（待建）', '900033', NULL, false, false, false, false, NULL, '/bre/field_operation', NULL),
    (gen_random_uuid(), now(), now(), '900053', '性状观测', '2', 'ri:ruler-line', '5', 'module_bre:trait_observation:query', 'TraitObservation', 'trait_observation', 'module_bre/_coming_soon/index', NULL, true, true, false, '性状观测', '0', false, 'web', '性状观测（嵌入单株评价页，无独立路由，隐藏占位）', '900033', NULL, false, false, false, false, NULL, '/bre/tree_evaluation', NULL),
    (gen_random_uuid(), now(), now(), '900052', '选育结果', '2', 'ri:award-line', '1', 'module_bre:selection_result:query', 'SelectionResult', 'selection_result', 'module_bre/selection_result/index', NULL, true, true, false, '选育结果', '0', false, 'web', '选育结果', '900034', NULL, false, false, false, false, NULL, '/bre/selection_result', NULL),
    (gen_random_uuid(), now(), now(), '900060', '育种计划', '2', 'ri:flag-line', '1', 'module_bre:plan:query', 'Plan', 'plan', 'module_bre/plan/index', NULL, false, true, false, '育种计划', '0', false, 'web', '育种计划/项目', '900004', NULL, false, false, false, false, NULL, '/bre/plan', NULL),
    (gen_random_uuid(), now(), now(), '900061', '试验', '2', 'ri:flask-line', '2', 'module_bre:trial:query', 'Trial', 'trial', 'module_bre/trial/index', NULL, false, true, false, '试验', '0', false, 'web', '试验（待建）', '900004', NULL, false, false, false, false, NULL, '/bre/trial', NULL),
    (gen_random_uuid(), now(), now(), '900062', '试验执行', '2', 'ri:play-circle-line', '3', 'module_bre:trial_study:query', 'TrialStudy', 'trial_study', 'module_bre/trial_study/index', NULL, false, true, false, '试验执行', '0', false, 'web', '试验执行', '900004', NULL, false, false, false, false, NULL, '/bre/trial_study', NULL),
    (gen_random_uuid(), now(), now(), '900063', '参试条目', '2', 'ri:play-list-add-line', '4', 'module_bre:trial_study_entry:query', 'TrialEntry', 'trial_study_entry', 'module_bre/trial_study_entry/index', NULL, false, true, false, '参试条目', '0', false, 'web', '参试条目（待建）', '900004', NULL, false, false, false, false, NULL, '/bre/trial_entry', NULL),
    (gen_random_uuid(), now(), now(), '900064', '试验处理', '2', 'ri:equalizer-line', '5', 'module_bre:treatment:query', 'Treatment', 'treatment', 'module_bre/treatment/index', NULL, false, true, false, '试验处理', '0', false, 'web', '试验处理', '900004', NULL, false, false, false, false, NULL, '/bre/treatment', NULL),
    (gen_random_uuid(), now(), now(), '900065', '分组管理', '2', 'ri:group-line', '6', 'module_bre:group:query', 'Group', 'group', 'module_bre/group/index', NULL, false, true, false, '分组管理', '0', false, 'web', '分组管理（待建）', '900004', NULL, false, false, false, false, NULL, '/bre/group', NULL),
    (gen_random_uuid(), now(), now(), '900070', '分析数据集', '2', 'ri:database-line', '1', 'module_bre:analysis_dataset:query', 'AnalysisDataset', 'analysis_dataset', 'module_bre/_coming_soon/index', NULL, true, true, false, '分析数据集', '0', false, 'web', '统一性状值视图/分析集（待建）', '900005', NULL, false, false, false, false, NULL, '/bre/analysis_dataset', NULL),
    (gen_random_uuid(), now(), now(), '900071', '统计计算', '2', 'ri:calculator-line', '2', 'module_bre:statistics:query', 'StatisticsCalc', 'statistics', 'module_bre/statistics/index', NULL, false, true, false, '统计计算（EBV/配合力/描述/相关/选择指数）', '0', false, 'web', '育种值 EBV 与配合力 GCA/SCA 估计 + 描述/相关/选择指数', '900005', NULL, false, false, false, false, NULL, '/bre/statistics_calc', NULL),
    (gen_random_uuid(), now(), now(), '900072', '育种值', '2', 'ri:line-chart-line', '3', 'module_bre:statistics:query', 'PredictionValue', 'prediction_value', 'module_bre/statistics/index', NULL, false, true, false, '育种值', '0', false, 'web', 'ABLUP-EBV（待建）', '900005', NULL, false, false, false, false, NULL, '/bre/prediction_value', NULL),
    (gen_random_uuid(), now(), now(), '900073', '选择指数与决策', '2', 'ri:focus-2-line', '4', 'module_bre:statistics:query', 'SelectionDecision', 'selection_decision', 'module_bre/statistics/index', NULL, false, true, false, '选择指数与决策', '0', false, 'web', '选择指数与决策（待建）', '900005', NULL, false, false, false, false, NULL, '/bre/selection_decision', NULL),
    (gen_random_uuid(), now(), now(), '900074', '图形分析', '2', 'ri:pie-chart-line', '5', 'module_bre:statistics:query', 'StatisticsChart', 'statistics_chart', 'module_bre/statistics/index', NULL, false, true, false, '图形分析', '0', false, 'web', '分布/PCA/BLUP 图表（待建）', '900005', NULL, false, false, false, false, NULL, '/bre/statistics_chart', NULL),
    (gen_random_uuid(), now(), now(), '900075', '育种报告', '2', 'ri:file-text-line', '6', 'module_bre:breeding_report:query', 'BreedingReport', 'breeding_report', 'module_bre/_coming_soon/index', NULL, true, true, false, '育种报告', '0', false, 'web', '育种报告（待建）', '900005', NULL, false, false, false, false, NULL, '/bre/breeding_report', NULL);

-- 2) 按钮 A 段（基础数据/种质/杂交配组/种子/定植 10 个模块）
--    id = 900100 + seq*10 + border；border: 1查看 2查询 3新增 4编辑 5删除 6导出 7导入 8下载模板
WITH modules(seq, mod_id, perm) AS (VALUES
    (0, 900010, 'module_bre:site'),
    (1, 900011, 'module_bre:plot'),
    (2, 900012, 'module_bre:personnel'),
    (3, 900013, 'module_bre:target'),
    (4, 900020, 'module_bre:germplasm'),
    (5, 900040, 'module_bre:cross_combination'),
    (6, 900041, 'module_bre:pollination'),
    (7, 900042, 'module_bre:seed_treatment'),
    (8, 900043, 'module_bre:seedling'),
    (9, 900045, 'module_bre:planting')
),
btns(border, bname, suffix) AS (VALUES
    (1, '查看', 'detail'),
    (2, '查询', 'query'),
    (3, '新增', 'create'),
    (4, '编辑', 'update'),
    (5, '删除', 'delete'),
    (6, '导出', 'export'),
    (7, '导入', 'import'),
    (8, '下载模板', 'download')
)
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
SELECT
    gen_random_uuid(), now(), now(),
    900100 + m.seq * 10 + b.border,
    b.bname, 3, NULL, b.border,
    m.perm || ':' || b.suffix,
    NULL, NULL, NULL, NULL, false, true, false, b.bname, 0, false, 'web', NULL,
    m.mod_id, NULL, false, false, false, false, NULL, NULL, NULL
FROM modules m
CROSS JOIN btns b;

-- 3) 预留树：分子育种 / 品种保护 / AI 增强（线上库权威，幂等补灌，重跑不删除不覆盖）
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
VALUES
    (gen_random_uuid(), now(), now(), '900200', '分子育种', '1', 'ri:dna-line', '6', NULL, 'MolecularRoot', '/breeding', NULL, '/breeding/marker', false, true, false, '分子育种', '0', false, 'web', '基因型与标记（二期）', NULL, NULL, false, false, false, false, NULL, NULL, NULL),
    (gen_random_uuid(), now(), now(), '900210', '分子标记', '2', 'ri:dna-line', '1', 'module_bre:marker:query', 'Marker', 'marker', 'module_bre/_coming_soon/index', NULL, false, true, false, '分子标记', '0', false, 'web', '分子标记（待建）', '900200', NULL, false, false, false, false, NULL, '/bre/marker', NULL),
    (gen_random_uuid(), now(), now(), '900211', '基因型数据集', '2', 'ri:database-2-line', '2', 'module_bre:genotype_dataset:query', 'GenotypeDataset', 'genotype_dataset', 'module_bre/_coming_soon/index', NULL, false, true, false, '基因型数据集', '0', false, 'web', '基因型数据集（待建）', '900200', NULL, false, false, false, false, NULL, '/bre/genotype_dataset', NULL),
    (gen_random_uuid(), now(), now(), '900212', '分型样本', '2', 'ri:test-tube-line', '3', 'module_bre:genotype_sample:query', 'GenotypeSample', 'genotype_sample', 'module_bre/_coming_soon/index', NULL, false, true, false, '分型样本', '0', false, 'web', '分型样本（待建）', '900200', NULL, false, false, false, false, NULL, '/bre/genotype_sample', NULL),
    (gen_random_uuid(), now(), now(), '900213', '分型结果', '2', 'ri:list-check', '4', 'module_bre:genotype_call:query', 'GenotypeCall', 'genotype_call', 'module_bre/_coming_soon/index', NULL, false, true, false, '分型结果', '0', false, 'web', '分型结果（待建）', '900200', NULL, false, false, false, false, NULL, '/bre/genotype_call', NULL),
    (gen_random_uuid(), now(), now(), '900214', 'GWAS / QTL', '2', 'ri:line-chart-line', '5', 'module_bre:gwas:query', 'Gwas', 'gwas', 'module_bre/_coming_soon/index', NULL, false, true, false, 'GWAS / QTL', '0', false, 'web', 'GWAS/QTL（待建）', '900200', NULL, false, false, false, false, NULL, '/bre/gwas', NULL),
    (gen_random_uuid(), now(), now(), '900220', '品种保护', '1', 'ri:shield-line', '7', NULL, 'ProtectionRoot', '/breeding', NULL, '/breeding/dus_test', false, true, false, '品种保护', '0', false, 'web', 'DUS/登记/PVP（三期）', NULL, NULL, false, false, false, false, NULL, NULL, NULL),
    (gen_random_uuid(), now(), now(), '900230', 'DUS 测试', '2', 'ri:clipboard-line', '1', 'module_bre:dus_test:query', 'DusTest', 'dus_test', 'module_bre/_coming_soon/index', NULL, false, true, false, 'DUS 测试', '0', false, 'web', 'DUS 测试（待建）', '900220', NULL, false, false, false, false, NULL, '/bre/dus_test', NULL),
    (gen_random_uuid(), now(), now(), '900231', '品种登记', '2', 'ri:file-text-line', '2', 'module_bre:variety_registration:query', 'VarietyRegistration', 'variety_registration', 'module_bre/_coming_soon/index', NULL, false, true, false, '品种登记', '0', false, 'web', '品种登记（待建）', '900220', NULL, false, false, false, false, NULL, '/bre/variety_registration', NULL),
    (gen_random_uuid(), now(), now(), '900232', '品种权保护', '2', 'ri:shield-check-line', '3', 'module_bre:pvp:query', 'Pvp', 'pvp', 'module_bre/_coming_soon/index', NULL, false, true, false, '品种权保护', '0', false, 'web', '品种权保护 PVP（待建）', '900220', NULL, false, false, false, false, NULL, '/bre/pvp', NULL),
    (gen_random_uuid(), now(), now(), '900240', 'AI 增强', '1', 'ri:robot-line', '8', NULL, 'AiRoot', '/breeding', NULL, '/breeding/knowledge_graph', false, true, false, 'AI 增强', '0', false, 'web', '知识图谱/候选亲本/表型识别（三期）', NULL, NULL, false, false, false, false, NULL, NULL, NULL),
    (gen_random_uuid(), now(), now(), '900250', '知识图谱', '2', 'ri:node-tree', '1', 'module_bre:knowledge_graph:query', 'KnowledgeGraph', 'knowledge_graph', 'module_bre/_coming_soon/index', NULL, false, true, false, '知识图谱', '0', false, 'web', '育种知识图谱（待建）', '900240', NULL, false, false, false, false, NULL, '/bre/knowledge_graph', NULL),
    (gen_random_uuid(), now(), now(), '900251', '候选亲本生成', '2', 'ri:seedling-line', '2', 'module_bre:candidate_parent:query', 'CandidateParent', 'candidate_parent', 'module_bre/_coming_soon/index', NULL, false, true, false, '候选亲本生成', '0', false, 'web', '候选亲本生成（待建）', '900240', NULL, false, false, false, false, NULL, '/bre/candidate_parent', NULL),
    (gen_random_uuid(), now(), now(), '900252', 'AI 表型识别', '2', 'ri:camera-line', '3', 'module_bre:ai_phenotype:query', 'AiPhenotype', 'ai_phenotype', 'module_bre/_coming_soon/index', NULL, false, true, false, 'AI 表型识别', '0', false, 'web', 'AI 表型识别（待建）', '900240', NULL, false, false, false, false, NULL, '/bre/ai_phenotype', NULL)
ON CONFLICT (id) DO NOTHING;

-- 4) 按钮 C 段（育种单株/评价/照片/选育/计划/试验/统计 11 个模块）
--    id = 900300 + seq*10 + border；补全此前缺失的 tree/tree_evaluation/tree_photo/selection_result 按钮
WITH modules(seq, mod_id, perm) AS (VALUES
    (0, 900046, 'module_bre:tree'),
    (1, 900048, 'module_bre:tree_evaluation'),
    (2, 900049, 'module_bre:tree_photo'),
    (3, 900052, 'module_bre:selection_result'),
    (4, 900060, 'module_bre:plan'),
    (5, 900061, 'module_bre:trial'),
    (6, 900062, 'module_bre:trial_study'),
    (7, 900063, 'module_bre:trial_study_entry'),
    (8, 900064, 'module_bre:treatment'),
    (9, 900065, 'module_bre:group'),
    (10, 900071, 'module_bre:statistics')
),
btns(border, bname, suffix) AS (VALUES
    (1, '查看', 'detail'),
    (2, '查询', 'query'),
    (3, '新增', 'create'),
    (4, '编辑', 'update'),
    (5, '删除', 'delete'),
    (6, '导出', 'export'),
    (7, '导入', 'import'),
    (8, '下载模板', 'download')
)
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
SELECT
    gen_random_uuid(), now(), now(),
    900300 + m.seq * 10 + b.border,
    b.bname, 3, NULL, b.border,
    m.perm || ':' || b.suffix,
    NULL, NULL, NULL, NULL, false, true, false, b.bname, 0, false, 'web', NULL,
    m.mod_id, NULL, false, false, false, false, NULL, NULL, NULL
FROM modules m
CROSS JOIN btns b;

-- 4b) 按钮 D 段（系谱/性状/选择规则/性状观测/无性系/砧木/种子批/克隆扩繁/环境因子 9 模块补缺）
--    id = 900500 + seq*10 + border；补全此前遗漏的 pedigree/trait/selection_rule 按钮与 trait_observation 按钮
WITH modules(seq, mod_id, perm) AS (VALUES
    (0, 900021, 'module_bre:pedigree'),
    (1, 900014, 'module_bre:trait'),
    (2, 900015, 'module_bre:selection_rule'),
    (3, 900053, 'module_bre:trait_observation'),
    (4, 900044, 'module_bre:clone'),
    (5, 900016, 'module_bre:rootstock'),
    (6, 900054, 'module_bre:seed_lot'),
    (7, 900055, 'module_bre:propagation'),
    (8, 900047, 'module_bre:environment_condition')
),
btns(border, bname, suffix) AS (VALUES
    (1, '查看', 'detail'),
    (2, '查询', 'query'),
    (3, '新增', 'create'),
    (4, '编辑', 'update'),
    (5, '删除', 'delete'),
    (6, '导出', 'export'),
    (7, '导入', 'import'),
    (8, '下载模板', 'download')
)
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
SELECT
    gen_random_uuid(), now(), now(),
    900500 + m.seq * 10 + b.border,
    b.bname, 3, NULL, b.border,
    m.perm || ':' || b.suffix,
    NULL, NULL, NULL, NULL, false, true, false, b.bname, 0, false, 'web', NULL,
    m.mod_id, NULL, false, false, false, false, NULL, NULL, NULL
FROM modules m
CROSS JOIN btns b;

-- 4c) 按钮 E 段（花粉档案 1 个模块）
--    id = 900600 + seq*10 + border
WITH modules(seq, mod_id, perm) AS (VALUES
    (0, 900056, 'module_bre:pollen')
),
btns(border, bname, suffix) AS (VALUES
    (1, '查看', 'detail'),
    (2, '查询', 'query'),
    (3, '新增', 'create'),
    (4, '编辑', 'update'),
    (5, '删除', 'delete'),
    (6, '导出', 'export'),
    (7, '导入', 'import'),
    (8, '下载模板', 'download')
)
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
SELECT
    gen_random_uuid(), now(), now(),
    900600 + m.seq * 10 + b.border,
    b.bname, 3, NULL, b.border,
    m.perm || ':' || b.suffix,
    NULL, NULL, NULL, NULL, false, true, false, b.bname, 0, false, 'web', NULL,
    m.mod_id, NULL, false, false, false, false, NULL, NULL, NULL
FROM modules m
CROSS JOIN btns b;

-- 5) 关联角色：超级管理员 + 管理员 → 全树（含预留树与 C/D/E 段按钮）
INSERT INTO sys_role_menus (role_id, menu_id)
SELECT r.id, m.id
FROM sys_role r
CROSS JOIN sys_menu m
WHERE r.code IN ('SUPER_ADMIN', 'ADMIN')
  AND (m.id BETWEEN 900001 AND 900199 OR m.id BETWEEN 900200 AND 900299 OR m.id BETWEEN 900300 AND 900499 OR m.id BETWEEN 900500 AND 900599 OR m.id BETWEEN 900600 AND 900699 OR m.id BETWEEN 900700 AND 900799)
ON CONFLICT DO NOTHING;

-- 6) 分子层「建数据能力」落地：component_path 翻转（900210-900213 → 真实页面；900214 GWAS/QTL → §6d 翻转）
UPDATE sys_menu SET component_path = 'module_bre/marker/index', description = '分子标记' WHERE id = 900210;
UPDATE sys_menu SET component_path = 'module_bre/genotype_dataset/index', description = '基因型数据集' WHERE id = 900211;
UPDATE sys_menu SET component_path = 'module_bre/genotype_sample/index', description = '分型样本' WHERE id = 900212;
UPDATE sys_menu SET component_path = 'module_bre/genotype_call/index', description = '分型结果' WHERE id = 900213;

-- 6b) 按钮 F 段（分子层 4 模块）
--    id = 900700 + seq*10 + border；parent_id 挂在 900210-900213 上
WITH modules(seq, mod_id, perm) AS (VALUES
    (0, 900210, 'module_bre:marker'),
    (1, 900211, 'module_bre:genotype_dataset'),
    (2, 900212, 'module_bre:genotype_sample'),
    (3, 900213, 'module_bre:genotype_call')
),
btns(border, bname, suffix) AS (VALUES
    (1, '查看', 'detail'),
    (2, '查询', 'query'),
    (3, '新增', 'create'),
    (4, '编辑', 'update'),
    (5, '删除', 'delete'),
    (6, '导出', 'export'),
    (7, '导入', 'import'),
    (8, '下载模板', 'download')
)
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
SELECT
    gen_random_uuid(), now(), now(),
    900700 + m.seq * 10 + b.border,
    b.bname, 3, NULL, b.border,
    m.perm || ':' || b.suffix,
    NULL, NULL, NULL, NULL, false, true, false, b.bname, 0, false, 'web', NULL,
    m.mod_id, NULL, false, false, false, false, NULL, NULL, NULL
FROM modules m
CROSS JOIN btns b
ON CONFLICT (id) DO NOTHING;

-- 6c) 关联角色：F 段按钮也授给 超级管理员 + 管理员
--    §5 的角色关联在 §6b 插按钮之前执行（脚本顺序），F 段须单独补关联；幂等可重跑。
INSERT INTO sys_role_menus (role_id, menu_id)
SELECT r.id, m.id
FROM sys_role r
CROSS JOIN sys_menu m
WHERE r.code IN ('SUPER_ADMIN', 'ADMIN')
  AND m.id BETWEEN 900700 AND 900799
ON CONFLICT DO NOTHING;

-- 6d) GWAS/QTL/MAS 闭环落地：900214 占位页 → 真实页面。
--     GWAS/QTL/MAS 端点复用统计模块权限（module_bre:statistics:query/create/delete，
--     已在 A 段 900071 按钮行授予 ADMIN），故无需新增按钮行；仅翻转 component_path。
UPDATE sys_menu SET component_path = 'module_bre/gwas/index', description = 'GWAS / QTL' WHERE id = 900214;

-- 7) 批2 四模块落地（2026-08-05）：通用观测/农事操作/分析数据集/育种报告。
-- 7a) 菜单翻转：900050/900051/900070/900075 占位页 → 真实页面，文案去「待建」。
UPDATE sys_menu SET component_path = 'module_bre/observation/index', description = '通用观测（EAV 长表，obs_type: numeric/text/date）' WHERE id = 900050;
UPDATE sys_menu SET component_path = 'module_bre/field_operation/index', description = '农事操作（field_operation 字典：施肥/喷药/修剪/灌溉/疏果/套袋/采收）' WHERE id = 900051;
UPDATE sys_menu SET component_path = 'module_bre/analysis_dataset/index', description = '分析数据集（性状值视图切片/分析集配置）' WHERE id = 900070;
UPDATE sys_menu SET component_path = 'module_bre/breeding_report/index', description = '育种报告（prediction/stability/cv 聚合生成）' WHERE id = 900075;

-- 7b) 文案清理：既有真实页面但 description 残留「待建」的菜单（对照 35 模块现状盘点）。
--     900014 性状字典 / 900015 选择规则 / 900021 系谱管理 / 900061 试验 /
--     900063 参试条目 / 900065 分组管理 / 900072 育种值 / 900073 选择指数与决策 / 900074 图形分析。
UPDATE sys_menu SET description = '性状定义与计量单位' WHERE id = 900014;
UPDATE sys_menu SET description = '选择规则（阶段感知，供选育结果/决选参考）' WHERE id = 900015;
UPDATE sys_menu SET description = '系谱（亲本-子代关系，A 矩阵构建地基）' WHERE id = 900021;
UPDATE sys_menu SET description = '试验设计（育种试验元数据）' WHERE id = 900061;
UPDATE sys_menu SET description = '参试条目（试验-种质/单株 关联）' WHERE id = 900063;
UPDATE sys_menu SET description = '分组管理（项目/家系/类目/临时集/自定义）' WHERE id = 900065;
UPDATE sys_menu SET description = '育种值 ABLUP-EBV（统计计算页）' WHERE id = 900072;
UPDATE sys_menu SET description = '选择指数与决策（统计计算页）' WHERE id = 900073;
UPDATE sys_menu SET description = '图形分析（分布/PCA/BLUP 图表，统计计算页）' WHERE id = 900074;

-- 7c) 按钮 F 段补缺（seq 4-7）：通用观测/农事操作/育种报告/分析数据集。
--     id = 900700 + seq*10 + border；F 段 seq 0-3 已归分子层四模块，本段用 4-7。
WITH modules(seq, mod_id, perm) AS (VALUES
    (4, 900051, 'module_bre:field_operation'),
    (5, 900050, 'module_bre:observation'),
    (6, 900075, 'module_bre:breeding_report'),
    (7, 900070, 'module_bre:analysis_dataset')
),
btns(border, bname, suffix) AS (VALUES
    (1, '查看', 'detail'),
    (2, '查询', 'query'),
    (3, '新增', 'create'),
    (4, '编辑', 'update'),
    (5, '删除', 'delete'),
    (6, '导出', 'export'),
    (7, '导入', 'import'),
    (8, '下载模板', 'download')
)
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
SELECT
    gen_random_uuid(), now(), now(),
    900700 + m.seq * 10 + b.border,
    b.bname, 3, NULL, b.border,
    m.perm || ':' || b.suffix,
    NULL, NULL, NULL, NULL, false, true, false, b.bname, 0, false, 'web', NULL,
    m.mod_id, NULL, false, false, false, false, NULL, NULL, NULL
FROM modules m
CROSS JOIN btns b
ON CONFLICT (id) DO NOTHING;

-- 7d) 角色关联：批2 新按钮授给 超级管理员 + 管理员（§5/6c 的 900700-900799 范围已覆盖，
--     本段显式补关联，保证 7c 在 §5/6c 之后执行也安全；幂等可重跑）。
INSERT INTO sys_role_menus (role_id, menu_id)
SELECT r.id, m.id
FROM sys_role r
CROSS JOIN sys_menu m
WHERE r.code IN ('SUPER_ADMIN', 'ADMIN')
  AND m.id BETWEEN 900700 AND 900799
ON CONFLICT DO NOTHING;


-- 8) 批3 超期预警落地（2026-08-05）：预警中心菜单 + 按钮。
-- 8a) 菜单：预警中心 归统计分析(900005)，order 7；component 指向 module_bre/alert/index。
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
SELECT
    gen_random_uuid(), now(), now(), '900076', '预警中心', '2', 'ri:alert-line', '7',
    'module_bre:alert:query', 'AlertCenter', 'alert', 'module_bre/alert/index', NULL,
    false, true, false, '预警中心', '0', false, 'web',
    '超期预警（单株多年未决策/花粉批次过期/种子批超期，每日扫描）', '900005',
    NULL, false, false, false, false, NULL, '/bre/alert', NULL
ON CONFLICT (id) DO NOTHING;

-- 8b) 按钮：查看/查询/处理（resolve 复用 update 权限后缀）。
--     id = 900780 + border（F 段尾段 900780-900789）。
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
SELECT
    gen_random_uuid(), now(), now(), 900780 + b.border,
    b.bname, 3, NULL, b.border,
    'module_bre:alert:' || b.suffix,
    NULL, NULL, NULL, NULL, false, true, false, b.bname, 0, false, 'web', NULL,
    900076, NULL, false, false, false, false, NULL, NULL, NULL
FROM (VALUES (1, '查看', 'detail'), (2, '查询', 'query'), (3, '处理', 'update')) AS b(border, bname, suffix)
ON CONFLICT (id) DO NOTHING;

-- 8c) 角色关联：预警中心菜单 + 按钮授给 超级管理员 + 管理员。
--     900076 属 900001-900199 段（文件头 DELETE 会清后重建），本段显式补关联。
INSERT INTO sys_role_menus (role_id, menu_id)
SELECT r.id, m.id
FROM sys_role r
CROSS JOIN sys_menu m
WHERE r.code IN ('SUPER_ADMIN', 'ADMIN')
  AND (m.id = 900076 OR (m.id BETWEEN 900780 AND 900789))
ON CONFLICT DO NOTHING;
