-- 种子质检初始化脚本（无租户字段，可重复执行）
CREATE TABLE IF NOT EXISTS mes_qc_seed_quality (
  id bigint NOT NULL AUTO_INCREMENT COMMENT '编号', code varchar(64) NOT NULL COMMENT '袋码', item_id bigint NOT NULL COMMENT '物料', batch_code varchar(64) DEFAULT NULL COMMENT '批次号', result tinyint NOT NULL DEFAULT 0 COMMENT '0处理中1合格2不合格', reason varchar(500) DEFAULT NULL COMMENT '原因', inspector_id bigint DEFAULT NULL COMMENT '质检员', inspected_at datetime DEFAULT NULL COMMENT '质检时间', creator varchar(64) DEFAULT NULL, create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, updater varchar(64) DEFAULT NULL, update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, deleted bit NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY uk_code (code)
) ENGINE=InnoDB COMMENT='种子质检主表';
CREATE TABLE IF NOT EXISTS mes_qc_seed_quality_indicator (
  id bigint NOT NULL AUTO_INCREMENT, quality_id bigint NOT NULL, indicator_code varchar(32) NOT NULL, value decimal(18,6) NOT NULL, min_value decimal(18,6) NOT NULL, max_value decimal(18,6) NOT NULL, creator varchar(64) DEFAULT NULL, create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, updater varchar(64) DEFAULT NULL, update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, deleted bit NOT NULL DEFAULT 0, PRIMARY KEY(id), UNIQUE KEY uk_quality_indicator(quality_id,indicator_code)
) ENGINE=InnoDB COMMENT='种子质检指标结果';
CREATE TABLE IF NOT EXISTS mes_qc_seed_quality_source (
  id bigint NOT NULL AUTO_INCREMENT, quality_id bigint NOT NULL, bag_code_id bigint DEFAULT NULL, bag_code varchar(64) NOT NULL, material_stock_id bigint DEFAULT NULL, quantity decimal(18,6) NOT NULL DEFAULT 0, source_warehouse_id bigint DEFAULT NULL, source_location_id bigint DEFAULT NULL, source_area_id bigint DEFAULT NULL, target_warehouse_id bigint DEFAULT NULL, target_location_id bigint DEFAULT NULL, target_area_id bigint DEFAULT NULL, creator varchar(64) DEFAULT NULL, create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, updater varchar(64) DEFAULT NULL, update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, deleted bit NOT NULL DEFAULT 0, PRIMARY KEY(id), UNIQUE KEY uk_quality_bag(quality_id,bag_code)
) ENGINE=InnoDB COMMENT='种子质检库存袋码来源明细';
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '种子质检', '', 2, 90, 5500, 'seed-quality', 'ep:checked', 'mes/qc/seedQuality/index', 'MesQcSeedQuality', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE parent_id = 5500 AND path = 'seed-quality' AND deleted = 0);

INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '查询质检', 'mes:qc-seed-quality:query', 3, 1, id, '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu
WHERE parent_id = 5500 AND path = 'seed-quality' AND deleted = 0
  AND NOT EXISTS (
    SELECT 1 FROM system_menu child
    WHERE child.parent_id = system_menu.id
      AND child.permission = 'mes:qc-seed-quality:query'
      AND child.deleted = 0
  );

INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '提交质检', 'mes:qc-seed-quality:submit', 3, 2, id, '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu
WHERE parent_id = 5500 AND path = 'seed-quality' AND deleted = 0
  AND NOT EXISTS (
    SELECT 1 FROM system_menu child
    WHERE child.parent_id = system_menu.id
      AND child.permission = 'mes:qc-seed-quality:submit'
      AND child.deleted = 0
  );
