-- BIT 样品仓储 v1.0
-- Run against besure_bit_dev after review.

-- 样品基础字段
SET @schema_name := DATABASE();

SET @column_exists := (
  SELECT COUNT(1)
  FROM information_schema.columns
  WHERE table_schema = @schema_name AND table_name = 'erp_product' AND column_name = 'is_sample'
);
SET @sql := IF(@column_exists = 0,
  'ALTER TABLE erp_product ADD COLUMN is_sample bit(1) NOT NULL DEFAULT b''0'' COMMENT ''是否样品'' AFTER bar_code',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @column_exists := (
  SELECT COUNT(1)
  FROM information_schema.columns
  WHERE table_schema = @schema_name AND table_name = 'erp_product' AND column_name = 'sample_category'
);
SET @sql := IF(@column_exists = 0,
  'ALTER TABLE erp_product ADD COLUMN sample_category varchar(64) NULL COMMENT ''样品分类'' AFTER is_sample',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @column_exists := (
  SELECT COUNT(1)
  FROM information_schema.columns
  WHERE table_schema = @schema_name AND table_name = 'erp_product' AND column_name = 'biz_unit'
);
SET @sql := IF(@column_exists = 0,
  'ALTER TABLE erp_product ADD COLUMN biz_unit varchar(64) NULL COMMENT ''所属事业部'' AFTER sample_category',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @column_exists := (
  SELECT COUNT(1)
  FROM information_schema.columns
  WHERE table_schema = @schema_name AND table_name = 'erp_product' AND column_name = 'customer_id'
);
SET @sql := IF(@column_exists = 0,
  'ALTER TABLE erp_product ADD COLUMN customer_id bigint NULL COMMENT ''客户ID'' AFTER biz_unit',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @column_exists := (
  SELECT COUNT(1)
  FROM information_schema.columns
  WHERE table_schema = @schema_name AND table_name = 'erp_product' AND column_name = 'customer_name'
);
SET @sql := IF(@column_exists = 0,
  'ALTER TABLE erp_product ADD COLUMN customer_name varchar(128) NULL COMMENT ''客户名称快照'' AFTER customer_id',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @index_exists := (
  SELECT COUNT(1)
  FROM information_schema.statistics
  WHERE table_schema = @schema_name AND table_name = 'erp_product' AND index_name = 'idx_erp_product_sample'
);
SET @sql := IF(@index_exists = 0,
  'CREATE INDEX idx_erp_product_sample ON erp_product (is_sample, biz_unit, category_id, sub_category_id)',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @index_exists := (
  SELECT COUNT(1)
  FROM information_schema.statistics
  WHERE table_schema = @schema_name AND table_name = 'erp_product' AND index_name = 'idx_erp_product_customer'
);
SET @sql := IF(@index_exists = 0,
  'CREATE INDEX idx_erp_product_customer ON erp_product (customer_id)',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 字典：样品分类、出库原因
INSERT INTO system_dict_type
  (name, type, status, remark, creator, create_time, updater, update_time, deleted)
SELECT 'BIT样品分类', 'bit_sample_category', 0, 'BIT样品仓样品分类', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM system_dict_type WHERE type = 'bit_sample_category');

INSERT INTO system_dict_type
  (name, type, status, remark, creator, create_time, updater, update_time, deleted)
SELECT 'BIT样品出库原因', 'bit_sample_out_reason', 0, 'BIT样品仓出库原因', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM system_dict_type WHERE type = 'bit_sample_out_reason');

INSERT INTO system_dict_data
  (sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
SELECT 10, '客户样品', 'CUSTOMER_SAMPLE', 'bit_sample_category', 0, 'primary', '', '', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM system_dict_data WHERE dict_type = 'bit_sample_category' AND value = 'CUSTOMER_SAMPLE');

INSERT INTO system_dict_data
  (sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
SELECT 20, '内部样品', 'INTERNAL_SAMPLE', 'bit_sample_category', 0, 'success', '', '', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM system_dict_data WHERE dict_type = 'bit_sample_category' AND value = 'INTERNAL_SAMPLE');

INSERT INTO system_dict_data
  (sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
SELECT 30, '测试样品', 'TEST_SAMPLE', 'bit_sample_category', 0, 'warning', '', '', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM system_dict_data WHERE dict_type = 'bit_sample_category' AND value = 'TEST_SAMPLE');

INSERT INTO system_dict_data
  (sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
SELECT 10, '测试', 'TEST', 'bit_sample_out_reason', 0, 'warning', '', '', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM system_dict_data WHERE dict_type = 'bit_sample_out_reason' AND value = 'TEST');

INSERT INTO system_dict_data
  (sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
SELECT 20, '寄样', 'SEND_SAMPLE', 'bit_sample_out_reason', 0, 'primary', '', '', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM system_dict_data WHERE dict_type = 'bit_sample_out_reason' AND value = 'SEND_SAMPLE');

INSERT INTO system_dict_data
  (sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
SELECT 30, '领用', 'USE', 'bit_sample_out_reason', 0, 'success', '', '', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM system_dict_data WHERE dict_type = 'bit_sample_out_reason' AND value = 'USE');

INSERT INTO system_dict_data
  (sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
SELECT 40, '报废', 'SCRAP', 'bit_sample_out_reason', 0, 'danger', '', '', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM system_dict_data WHERE dict_type = 'bit_sample_out_reason' AND value = 'SCRAP');

INSERT INTO system_dict_data
  (sort, label, value, dict_type, status, color_type, css_class, remark, creator, create_time, updater, update_time, deleted)
SELECT 50, '其他', 'OTHER', 'bit_sample_out_reason', 0, 'info', '', '', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM system_dict_data WHERE dict_type = 'bit_sample_out_reason' AND value = 'OTHER');

-- 编码规则：样品码、库位码，默认自动生成且后续可在系统中调整
INSERT INTO erp_autocode_rule
  (rule_code, rule_name, rule_desc, max_length, is_padded, padded_char, padded_method, remark, is_enable, creator, create_time, updater, update_time, deleted)
SELECT 'BIT_SAMPLE_CODE', 'BIT样品码', 'BIT样品仓样品码默认规则', 18, 'N', '0', 'L', '', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM erp_autocode_rule WHERE rule_code = 'BIT_SAMPLE_CODE');

SET @bit_sample_rule_id := (SELECT id FROM erp_autocode_rule WHERE rule_code = 'BIT_SAMPLE_CODE' ORDER BY id LIMIT 1);

INSERT INTO erp_autocode_part
  (rule_id, part_index, part_type, part_code, part_name, part_length, datetime_format, input_character, fix_character, seria_start_no, seria_step, seria_now_no, cycle_flag, cycle_method, remark, is_enable, creator, create_time, updater, update_time, deleted)
SELECT @bit_sample_rule_id, 1, 'FIXCHAR', 'BIT_SAMPLE_PREFIX', '固定前缀', 2, '', '', 'SP', NULL, NULL, NULL, 'N', 'OTHER', '', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_sample_rule_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM erp_autocode_part WHERE rule_id = @bit_sample_rule_id AND part_code = 'BIT_SAMPLE_PREFIX');

INSERT INTO erp_autocode_part
  (rule_id, part_index, part_type, part_code, part_name, part_length, datetime_format, input_character, fix_character, seria_start_no, seria_step, seria_now_no, cycle_flag, cycle_method, remark, is_enable, creator, create_time, updater, update_time, deleted)
SELECT @bit_sample_rule_id, 2, 'NOWDATE', 'BIT_SAMPLE_DATE', '年月日', 8, 'yyyyMMdd', '', '', NULL, NULL, NULL, 'N', 'OTHER', '', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_sample_rule_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM erp_autocode_part WHERE rule_id = @bit_sample_rule_id AND part_code = 'BIT_SAMPLE_DATE');

INSERT INTO erp_autocode_part
  (rule_id, part_index, part_type, part_code, part_name, part_length, datetime_format, input_character, fix_character, seria_start_no, seria_step, seria_now_no, cycle_flag, cycle_method, remark, is_enable, creator, create_time, updater, update_time, deleted)
SELECT @bit_sample_rule_id, 3, 'SERIALNO', 'BIT_SAMPLE_SERIAL', '流水号', 4, '', '', '', 1, 1, 0, 'Y', 'DAY', '', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_sample_rule_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM erp_autocode_part WHERE rule_id = @bit_sample_rule_id AND part_code = 'BIT_SAMPLE_SERIAL');

INSERT INTO erp_autocode_record
  (rule_id, gen_date, gen_index, last_result, last_serial_no, last_input_char, remark, is_enable, creator, create_time, updater, update_time, deleted)
SELECT @bit_sample_rule_id, DATE_FORMAT(NOW(), '%Y%m%d'), 0, '', 0, '', '', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_sample_rule_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM erp_autocode_record WHERE rule_id = @bit_sample_rule_id);

INSERT INTO erp_autocode_rule
  (rule_code, rule_name, rule_desc, max_length, is_padded, padded_char, padded_method, remark, is_enable, creator, create_time, updater, update_time, deleted)
SELECT 'BIT_WAREHOUSE_LOCATION_CODE', 'BIT库位码', 'BIT样品仓库位码默认规则', 18, 'N', '0', 'L', '', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE NOT EXISTS (SELECT 1 FROM erp_autocode_rule WHERE rule_code = 'BIT_WAREHOUSE_LOCATION_CODE');

SET @bit_location_rule_id := (SELECT id FROM erp_autocode_rule WHERE rule_code = 'BIT_WAREHOUSE_LOCATION_CODE' ORDER BY id LIMIT 1);

INSERT INTO erp_autocode_part
  (rule_id, part_index, part_type, part_code, part_name, part_length, datetime_format, input_character, fix_character, seria_start_no, seria_step, seria_now_no, cycle_flag, cycle_method, remark, is_enable, creator, create_time, updater, update_time, deleted)
SELECT @bit_location_rule_id, 1, 'FIXCHAR', 'BIT_LOCATION_PREFIX', '固定前缀', 3, '', '', 'LOC', NULL, NULL, NULL, 'N', 'OTHER', '', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_location_rule_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM erp_autocode_part WHERE rule_id = @bit_location_rule_id AND part_code = 'BIT_LOCATION_PREFIX');

INSERT INTO erp_autocode_part
  (rule_id, part_index, part_type, part_code, part_name, part_length, datetime_format, input_character, fix_character, seria_start_no, seria_step, seria_now_no, cycle_flag, cycle_method, remark, is_enable, creator, create_time, updater, update_time, deleted)
SELECT @bit_location_rule_id, 2, 'SERIALNO', 'BIT_LOCATION_SERIAL', '流水号', 5, '', '', '', 1, 1, 0, 'N', 'OTHER', '', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_location_rule_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM erp_autocode_part WHERE rule_id = @bit_location_rule_id AND part_code = 'BIT_LOCATION_SERIAL');

INSERT INTO erp_autocode_record
  (rule_id, gen_date, gen_index, last_result, last_serial_no, last_input_char, remark, is_enable, creator, create_time, updater, update_time, deleted)
SELECT @bit_location_rule_id, '', 0, '', 0, '', '', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_location_rule_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM erp_autocode_record WHERE rule_id = @bit_location_rule_id);

-- 菜单和按钮权限：后续 Web 页面与后端接口按这些权限标识实现
SET @menu_seed := GREATEST((SELECT IFNULL(MAX(id), 0) FROM system_menu), 900000);

SET @erp_menu_id := (SELECT id FROM system_menu WHERE path = '/erp' AND parent_id = 0 AND deleted = b'0' ORDER BY id LIMIT 1);

SET @bit_wms_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @erp_menu_id AND path = 'bitwms' AND deleted = b'0'
  ORDER BY id LIMIT 1
);
SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, 'BIT样品仓', '', 1, 90, @erp_menu_id, 'bitwms', 'ep:box', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @erp_menu_id IS NOT NULL AND @bit_wms_id IS NULL;
SET @bit_wms_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @erp_menu_id AND path = 'bitwms' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @bit_sample_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'sample' AND deleted = b'0'
  ORDER BY id LIMIT 1
);
SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '样品信息', 'erp:bit-wms-sample:query', 2, 10, @bit_wms_id, 'sample', 'ep:goods', 'erp/bitwms/sample/index', 'ErpBitWmsSample', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_wms_id IS NOT NULL AND @bit_sample_menu_id IS NULL;
SET @bit_sample_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'sample' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @bit_location_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'location' AND deleted = b'0'
  ORDER BY id LIMIT 1
);
SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '库位信息', 'erp:bit-wms-location:query', 2, 20, @bit_wms_id, 'location', 'ep:coordinate', 'erp/bitwms/location/index', 'ErpBitWmsLocation', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_wms_id IS NOT NULL AND @bit_location_menu_id IS NULL;
SET @bit_location_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'location' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @bit_in_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'in' AND deleted = b'0'
  ORDER BY id LIMIT 1
);
SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '样品入库', 'erp:bit-wms-in:query', 2, 30, @bit_wms_id, 'in', 'ep:download', 'erp/bitwms/in/index', 'ErpBitWmsIn', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_wms_id IS NOT NULL AND @bit_in_menu_id IS NULL;
SET @bit_in_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'in' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @bit_out_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'out' AND deleted = b'0'
  ORDER BY id LIMIT 1
);
SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '样品出库', 'erp:bit-wms-out:query', 2, 40, @bit_wms_id, 'out', 'ep:upload', 'erp/bitwms/out/index', 'ErpBitWmsOut', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_wms_id IS NOT NULL AND @bit_out_menu_id IS NULL;
SET @bit_out_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'out' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @bit_move_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'move' AND deleted = b'0'
  ORDER BY id LIMIT 1
);
SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '样品移库', 'erp:bit-wms-move:query', 2, 50, @bit_wms_id, 'move', 'ep:sort', 'erp/bitwms/move/index', 'ErpBitWmsMove', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_wms_id IS NOT NULL AND @bit_move_menu_id IS NULL;
SET @bit_move_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'move' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @bit_stock_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'stock' AND deleted = b'0'
  ORDER BY id LIMIT 1
);
SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '样品库存', 'erp:bit-wms-stock:query', 2, 60, @bit_wms_id, 'stock', 'ep:grid', 'erp/bitwms/stock/index', 'ErpBitWmsStock', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_wms_id IS NOT NULL AND @bit_stock_menu_id IS NULL;
SET @bit_stock_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'stock' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @bit_record_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'record' AND deleted = b'0'
  ORDER BY id LIMIT 1
);
SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '出入库明细', 'erp:bit-wms-record:query', 2, 70, @bit_wms_id, 'record', 'ep:document', 'erp/bitwms/record/index', 'ErpBitWmsRecord', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_wms_id IS NOT NULL AND @bit_record_menu_id IS NULL;
SET @bit_record_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'record' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '样品创建', 'erp:bit-wms-sample:create', 3, 10, @bit_sample_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_sample_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-sample:create' AND deleted = b'0');

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '样品更新', 'erp:bit-wms-sample:update', 3, 20, @bit_sample_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_sample_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-sample:update' AND deleted = b'0');

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '样品打印', 'erp:bit-wms-sample:print', 3, 30, @bit_sample_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_sample_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-sample:print' AND deleted = b'0');

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '库位创建', 'erp:bit-wms-location:create', 3, 10, @bit_location_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_location_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-location:create' AND deleted = b'0');

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '库位更新', 'erp:bit-wms-location:update', 3, 20, @bit_location_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_location_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-location:update' AND deleted = b'0');

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '库位打印', 'erp:bit-wms-location:print', 3, 30, @bit_location_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_location_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-location:print' AND deleted = b'0');

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '执行入库', 'erp:bit-wms-in:create', 3, 10, @bit_in_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_in_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-in:create' AND deleted = b'0');

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '执行出库', 'erp:bit-wms-out:create', 3, 10, @bit_out_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_out_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-out:create' AND deleted = b'0');

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '执行移库', 'erp:bit-wms-move:create', 3, 10, @bit_move_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_move_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-move:create' AND deleted = b'0');

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '库存导出', 'erp:bit-wms-stock:export', 3, 20, @bit_stock_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_stock_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-stock:export' AND deleted = b'0');

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, 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 @menu_seed, '明细导出', 'erp:bit-wms-record:export', 3, 20, @bit_record_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_record_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission = 'erp:bit-wms-record:export' AND deleted = b'0');
