USE jiaheng_erp;

SET NAMES utf8mb4;

START TRANSACTION;

-- 基础组织
INSERT INTO sys_department (dept_code, dept_name, parent_id, dept_type, manager_name, status, sort_no, remark)
SELECT 'ADMIN', '综合管理部', NULL, 'ADMIN', '总经理', 'ACTIVE', 10, '负责主数据、权限、经营总览'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_department WHERE dept_code = 'ADMIN');

INSERT INTO sys_department (dept_code, dept_name, parent_id, dept_type, manager_name, status, sort_no, remark)
SELECT 'SALES', '销售部', NULL, 'SALES', '销售主管', 'ACTIVE', 20, '负责客户、订单、交期'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_department WHERE dept_code = 'SALES');

INSERT INTO sys_department (dept_code, dept_name, parent_id, dept_type, manager_name, status, sort_no, remark)
SELECT 'PURCHASE', '采购部', NULL, 'PURCHASE', '采购主管', 'ACTIVE', 30, '负责钢板采购与供应商协同'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_department WHERE dept_code = 'PURCHASE');

INSERT INTO sys_department (dept_code, dept_name, parent_id, dept_type, manager_name, status, sort_no, remark)
SELECT 'WAREHOUSE', '仓储部', NULL, 'WAREHOUSE', '仓库主管', 'ACTIVE', 40, '负责原材料、成品、退货、废料收发存'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_department WHERE dept_code = 'WAREHOUSE');

INSERT INTO sys_department (dept_code, dept_name, parent_id, dept_type, manager_name, status, sort_no, remark)
SELECT 'PRODUCTION', '生产部', NULL, 'PRODUCTION', '车间主任', 'ACTIVE', 50, '负责工单、工序、报工'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_department WHERE dept_code = 'PRODUCTION');

INSERT INTO sys_department (dept_code, dept_name, parent_id, dept_type, manager_name, status, sort_no, remark)
SELECT 'FINANCE', '财务部', NULL, 'FINANCE', '财务主管', 'ACTIVE', 60, '负责成本、对账、报表'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_department WHERE dept_code = 'FINANCE');

INSERT INTO sys_department (dept_code, dept_name, parent_id, dept_type, manager_name, status, sort_no, remark)
SELECT 'EQUIPMENT', '设备部', NULL, 'EQUIPMENT', '设备主管', 'ACTIVE', 70, '负责设备保养、维修、点检'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_department WHERE dept_code = 'EQUIPMENT');

-- 基础角色
INSERT INTO sys_role (role_code, role_name, role_scope, status, remark)
SELECT 'ADMIN', '系统管理员', 'SYSTEM', 'ACTIVE', '全局主数据、参数、异常中心'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_role WHERE role_code = 'ADMIN');

INSERT INTO sys_role (role_code, role_name, role_scope, status, remark)
SELECT 'SALES', '销售员', 'SYSTEM', 'ACTIVE', '客户与订单维护'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_role WHERE role_code = 'SALES');

INSERT INTO sys_role (role_code, role_name, role_scope, status, remark)
SELECT 'PURCHASER', '采购员', 'SYSTEM', 'ACTIVE', '采购申请、采购订单、到货跟踪'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_role WHERE role_code = 'PURCHASER');

INSERT INTO sys_role (role_code, role_name, role_scope, status, remark)
SELECT 'WAREHOUSE', '仓库员', 'SYSTEM', 'ACTIVE', '收货、上架、领料、盘点、发货'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_role WHERE role_code = 'WAREHOUSE');

INSERT INTO sys_role (role_code, role_name, role_scope, status, remark)
SELECT 'PLANNER', '计划员', 'SYSTEM', 'ACTIVE', 'MRP、工单下达、排产'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_role WHERE role_code = 'PLANNER');

INSERT INTO sys_role (role_code, role_name, role_scope, status, remark)
SELECT 'SUPERVISOR', '车间主管', 'SYSTEM', 'ACTIVE', '工单进度、异常审核、现场调度'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_role WHERE role_code = 'SUPERVISOR');

INSERT INTO sys_role (role_code, role_name, role_scope, status, remark)
SELECT 'OPERATOR', '加工员工', 'SYSTEM', 'ACTIVE', '扫码报工、报废反馈、停机反馈'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_role WHERE role_code = 'OPERATOR');

INSERT INTO sys_role (role_code, role_name, role_scope, status, remark)
SELECT 'FINANCE', '财务员', 'SYSTEM', 'ACTIVE', '费用归集、报表、核对'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_role WHERE role_code = 'FINANCE');

INSERT INTO sys_role (role_code, role_name, role_scope, status, remark)
SELECT 'EQUIP_ADMIN', '设备管理员', 'SYSTEM', 'ACTIVE', '设备台账、保养计划、维修'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_role WHERE role_code = 'EQUIP_ADMIN');

-- 单位
INSERT INTO md_unit (unit_code, unit_name, precision_digits)
SELECT 'PCS', '件', 0 FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_unit WHERE unit_code = 'PCS');

INSERT INTO md_unit (unit_code, unit_name, precision_digits)
SELECT 'KG', '千克', 3 FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_unit WHERE unit_code = 'KG');

INSERT INTO md_unit (unit_code, unit_name, precision_digits)
SELECT 'TON', '吨', 3 FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_unit WHERE unit_code = 'TON');

INSERT INTO md_unit (unit_code, unit_name, precision_digits)
SELECT 'SHEET', '张', 0 FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_unit WHERE unit_code = 'SHEET');

-- 物料分类
INSERT INTO md_item_category (category_code, category_name, parent_id, item_type, sort_no, status)
SELECT 'CAT-RM-PLATE', '钢板原材料', NULL, 'RAW_MATERIAL', 10, 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_item_category WHERE category_code = 'CAT-RM-PLATE');

INSERT INTO md_item_category (category_code, category_name, parent_id, item_type, sort_no, status)
SELECT 'CAT-FG-HARDWARE', '五金成品', NULL, 'FINISHED_GOOD', 20, 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_item_category WHERE category_code = 'CAT-FG-HARDWARE');

INSERT INTO md_item_category (category_code, category_name, parent_id, item_type, sort_no, status)
SELECT 'CAT-SCRAP-METAL', '金属废料', NULL, 'SCRAP', 30, 'ACTIVE'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_item_category WHERE category_code = 'CAT-SCRAP-METAL');

-- 供应商
INSERT INTO md_supplier (supplier_code, supplier_name, short_name, contact_name, contact_phone, address, lead_time_days, default_tax_rate, status, remark)
SELECT 'SUP-NB-JG-001', '宁波嘉钢材料有限公司', '嘉钢材料', '王经理', '13800001111', '宁波市镇海区钢材市场', 5, 0.1300, 'ACTIVE', '本体默认板材供应商'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_supplier WHERE supplier_code = 'SUP-NB-JG-001');

-- 仓库
INSERT INTO wh_warehouse (warehouse_code, warehouse_name, warehouse_type, manager_employee_id, status, remark)
SELECT 'WH-RM-01', '原材料库', 'RAW', NULL, 'ACTIVE', '自家料、客供料等生产主材库存'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM wh_warehouse WHERE warehouse_code = 'WH-RM-01');

INSERT INTO wh_warehouse (warehouse_code, warehouse_name, warehouse_type, manager_employee_id, status, remark)
SELECT 'WH-SEMI-01', '半成品库', 'SEMI', NULL, 'ACTIVE', '加工到一半、尚未完成全部工艺的半成品暂存'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM wh_warehouse WHERE warehouse_code = 'WH-SEMI-01');

INSERT INTO wh_warehouse (warehouse_code, warehouse_name, warehouse_type, manager_employee_id, status, remark)
SELECT 'WH-FG-01', '成品库', 'FINISHED', NULL, 'ACTIVE', '完工入库后可发货的成品库存'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM wh_warehouse WHERE warehouse_code = 'WH-FG-01');

INSERT INTO wh_warehouse (warehouse_code, warehouse_name, warehouse_type, manager_employee_id, status, remark)
SELECT 'WH-AUX-01', '辅料库', 'AUX', NULL, 'ACTIVE', '手套、薄膜、纸箱、螺丝钉等生产消耗辅料库存'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM wh_warehouse WHERE warehouse_code = 'WH-AUX-01');

INSERT INTO wh_warehouse (warehouse_code, warehouse_name, warehouse_type, manager_employee_id, status, remark)
SELECT 'WH-RT-01', '退货仓', 'RETURN', NULL, 'ACTIVE', '客户退货待判定区域'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM wh_warehouse WHERE warehouse_code = 'WH-RT-01');

INSERT INTO wh_warehouse (warehouse_code, warehouse_name, warehouse_type, manager_employee_id, status, remark)
SELECT 'WH-SCRAP-01', '废料库', 'SCRAP', NULL, 'ACTIVE', '生产废料、边角料、委外废料、报废品等废料库存'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM wh_warehouse WHERE warehouse_code = 'WH-SCRAP-01');

-- 默认库位
INSERT INTO wh_location (warehouse_id, location_code, location_name, zone_name, is_default, is_locked, status, remark)
SELECT (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-RM-01'), 'RM-A-01', '原料主库位', 'A区', 1, 0, 'ACTIVE', '默认收货库位'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM wh_location
  WHERE warehouse_id = (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-RM-01')
    AND location_code = 'RM-A-01'
);

INSERT INTO wh_location (warehouse_id, location_code, location_name, zone_name, is_default, is_locked, status, remark)
SELECT (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-SEMI-01'), 'SEMI-B-01', '半成品暂存位', 'B区', 1, 0, 'ACTIVE', '半成品暂存'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM wh_location
  WHERE warehouse_id = (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-SEMI-01')
    AND location_code = 'SEMI-B-01'
);

INSERT INTO wh_location (warehouse_id, location_code, location_name, zone_name, is_default, is_locked, status, remark)
SELECT (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-FG-01'), 'FG-C-01', '成品主库位', 'C区', 1, 0, 'ACTIVE', '成品默认库位'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM wh_location
  WHERE warehouse_id = (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-FG-01')
    AND location_code = 'FG-C-01'
);

INSERT INTO wh_location (warehouse_id, location_code, location_name, zone_name, is_default, is_locked, status, remark)
SELECT (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-AUX-01'), 'AUX-D-01', '辅料主库位', 'D区', 1, 0, 'ACTIVE', '辅料默认库位'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM wh_location
  WHERE warehouse_id = (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-AUX-01')
    AND location_code = 'AUX-D-01'
);

INSERT INTO wh_location (warehouse_id, location_code, location_name, zone_name, is_default, is_locked, status, remark)
SELECT (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-RT-01'), 'RT-D-01', '退货暂存位', 'D区', 1, 0, 'ACTIVE', '退货待返工或报废'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM wh_location
  WHERE warehouse_id = (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-RT-01')
    AND location_code = 'RT-D-01'
);

INSERT INTO wh_location (warehouse_id, location_code, location_name, zone_name, is_default, is_locked, status, remark)
SELECT (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-SCRAP-01'), 'SCR-E-01', '废料暂存位', 'E区', 1, 0, 'ACTIVE', '金属废料集中存放'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM wh_location
  WHERE warehouse_id = (SELECT id FROM wh_warehouse WHERE warehouse_code = 'WH-SCRAP-01')
    AND location_code = 'SCR-E-01'
);

-- 工序
INSERT INTO md_process (process_code, process_name, process_type, is_report_required, is_quality_gate, status, remark)
SELECT 'PROC-KP', '开平', 'PREP', 1, 0, 'ACTIVE', '钢板整平、裁片准备'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_process WHERE process_code = 'PROC-KP');

INSERT INTO md_process (process_code, process_name, process_type, is_report_required, is_quality_gate, status, remark)
SELECT 'PROC-CY', '冲压', 'PRESS', 1, 0, 'ACTIVE', '主成形工序，重点监控报废'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_process WHERE process_code = 'PROC-CY');

INSERT INTO md_process (process_code, process_name, process_type, is_report_required, is_quality_gate, status, remark)
SELECT 'PROC-MC', '去毛刺', 'FINISH', 1, 0, 'ACTIVE', '去除冲压后毛刺'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_process WHERE process_code = 'PROC-MC');

INSERT INTO md_process (process_code, process_name, process_type, is_report_required, is_quality_gate, status, remark)
SELECT 'PROC-ZW', '折弯', 'BEND', 1, 0, 'ACTIVE', '形成结构外形'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_process WHERE process_code = 'PROC-ZW');

INSERT INTO md_process (process_code, process_name, process_type, is_report_required, is_quality_gate, status, remark)
SELECT 'PROC-HJ', '焊接', 'WELD', 1, 0, 'ACTIVE', '关键连接工序'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_process WHERE process_code = 'PROC-HJ');

INSERT INTO md_process (process_code, process_name, process_type, is_report_required, is_quality_gate, status, remark)
SELECT 'PROC-QA', '检验包装', 'QA', 1, 1, 'ACTIVE', '终检、包装、入库前放行'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_process WHERE process_code = 'PROC-QA');

-- 工作中心
INSERT INTO md_work_center (center_code, center_name, dept_id, center_type, shift_pattern, capacity_hours_per_day, status, remark)
SELECT 'WC-KP-01', '开平工作中心', (SELECT id FROM sys_department WHERE dept_code = 'PRODUCTION'), 'PREP', '两班制', 16.00, 'ACTIVE', '本体板材预处理'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_work_center WHERE center_code = 'WC-KP-01');

INSERT INTO md_work_center (center_code, center_name, dept_id, center_type, shift_pattern, capacity_hours_per_day, status, remark)
SELECT 'WC-CY-01', '冲压工作中心', (SELECT id FROM sys_department WHERE dept_code = 'PRODUCTION'), 'PRESS', '两班制', 16.00, 'ACTIVE', '本体冲压成形'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_work_center WHERE center_code = 'WC-CY-01');

INSERT INTO md_work_center (center_code, center_name, dept_id, center_type, shift_pattern, capacity_hours_per_day, status, remark)
SELECT 'WC-MC-01', '去毛刺工作中心', (SELECT id FROM sys_department WHERE dept_code = 'PRODUCTION'), 'FINISH', '两班制', 16.00, 'ACTIVE', '去毛刺与修边'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_work_center WHERE center_code = 'WC-MC-01');

INSERT INTO md_work_center (center_code, center_name, dept_id, center_type, shift_pattern, capacity_hours_per_day, status, remark)
SELECT 'WC-ZW-01', '折弯工作中心', (SELECT id FROM sys_department WHERE dept_code = 'PRODUCTION'), 'BEND', '两班制', 16.00, 'ACTIVE', '折弯定型'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_work_center WHERE center_code = 'WC-ZW-01');

INSERT INTO md_work_center (center_code, center_name, dept_id, center_type, shift_pattern, capacity_hours_per_day, status, remark)
SELECT 'WC-HJ-01', '焊接工作中心', (SELECT id FROM sys_department WHERE dept_code = 'PRODUCTION'), 'WELD', '两班制', 16.00, 'ACTIVE', '焊接与补焊'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_work_center WHERE center_code = 'WC-HJ-01');

INSERT INTO md_work_center (center_code, center_name, dept_id, center_type, shift_pattern, capacity_hours_per_day, status, remark)
SELECT 'WC-QA-01', '检验包装工作中心', (SELECT id FROM sys_department WHERE dept_code = 'PRODUCTION'), 'QA', '白班', 8.00, 'ACTIVE', '终检、包装、贴标'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_work_center WHERE center_code = 'WC-QA-01');

-- 物料主档：原材料、成品、废料
INSERT INTO md_item (
  item_code, item_name, item_type, category_id, specification, model_no, material_type, material_grade,
  length_mm, width_mm, thickness_mm, density_kg_m3, theoretical_weight_kg, unit_weight_kg,
  sales_unit_id, stock_unit_id, purchase_unit_id, safety_stock_qty, safety_stock_weight_kg,
  scrap_sale_price, status, remark
)
SELECT
  'RM-SAP440-500600-2.5', 'SAP440钢板', 'RAW_MATERIAL',
  (SELECT id FROM md_item_category WHERE category_code = 'CAT-RM-PLATE'),
  '500*600*2.5', NULL, '钢板', 'SAP440',
  500.000, 600.000, 2.500, 7850.000, 5.887500, 5.887500,
  (SELECT id FROM md_unit WHERE unit_code = 'KG'),
  (SELECT id FROM md_unit WHERE unit_code = 'KG'),
  (SELECT id FROM md_unit WHERE unit_code = 'KG'),
  0.000000, 10000.000000,
  0.0000, 'ACTIVE', '本体标准板材；采购按重量结算，采购倍数按2吨'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_item WHERE item_code = 'RM-SAP440-500600-2.5');

INSERT INTO md_item (
  item_code, item_name, item_type, category_id, specification, model_no, material_type, material_grade,
  length_mm, width_mm, thickness_mm, density_kg_m3, theoretical_weight_kg, unit_weight_kg,
  sales_unit_id, stock_unit_id, purchase_unit_id, safety_stock_qty, safety_stock_weight_kg,
  scrap_sale_price, status, remark
)
SELECT
  'FG-BT-001', '本体', 'FINISHED_GOOD',
  (SELECT id FROM md_item_category WHERE category_code = 'CAT-FG-HARDWARE'),
  '标准结构件', 'BT-V1', '五金结构件', NULL,
  320.000, 145.000, 0.000, 0.000, 5.200000, 5.200000,
  (SELECT id FROM md_unit WHERE unit_code = 'PCS'),
  (SELECT id FROM md_unit WHERE unit_code = 'PCS'),
  (SELECT id FROM md_unit WHERE unit_code = 'PCS'),
  500.000000, 0.000000,
  0.0000, 'ACTIVE', '宁波百华智能本体示例成品，用于演示销售、MRP、工单、成本闭环'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_item WHERE item_code = 'FG-BT-001');

INSERT INTO md_item (
  item_code, item_name, item_type, category_id, specification, model_no, material_type, material_grade,
  length_mm, width_mm, thickness_mm, density_kg_m3, theoretical_weight_kg, unit_weight_kg,
  sales_unit_id, stock_unit_id, purchase_unit_id, safety_stock_qty, safety_stock_weight_kg,
  scrap_sale_price, status, remark
)
SELECT
  'SCR-SAP440-EDGE', 'SAP440边角废料', 'SCRAP',
  (SELECT id FROM md_item_category WHERE category_code = 'CAT-SCRAP-METAL'),
  '冲压边角料', NULL, '金属废料', 'SAP440',
  0.000, 0.000, 0.000, 7850.000, 1.000000, 1.000000,
  (SELECT id FROM md_unit WHERE unit_code = 'KG'),
  (SELECT id FROM md_unit WHERE unit_code = 'KG'),
  (SELECT id FROM md_unit WHERE unit_code = 'KG'),
  0.000000, 0.000000,
  1.8000, 'ACTIVE', '本体生产过程中产生的可售废料'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_item WHERE item_code = 'SCR-SAP440-EDGE');

-- 原材料扩展
INSERT INTO md_material (
  item_id, default_supplier_id, purchase_calc_mode, min_purchase_qty, purchase_multiple_qty,
  purchase_multiple_weight_kg, lead_time_days, quality_rule
)
SELECT
  (SELECT id FROM md_item WHERE item_code = 'RM-SAP440-500600-2.5'),
  (SELECT id FROM md_supplier WHERE supplier_code = 'SUP-NB-JG-001'),
  'BY_WEIGHT', 2000.000000, 0.000000, 2000.000000, 5,
  '需提供材质证明；来料需检验厚度、表面状态、重量偏差'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM md_material
  WHERE item_id = (SELECT id FROM md_item WHERE item_code = 'RM-SAP440-500600-2.5')
);

-- 产品扩展
INSERT INTO md_product (
  item_id, drawing_no, product_family, version_no, standard_output_qty,
  allowed_scrap_rate, quality_standard
)
SELECT
  (SELECT id FROM md_item WHERE item_code = 'FG-BT-001'),
  'JH-BT-001', '结构件', 'V1.0', 1.000000, 0.0350,
  '尺寸公差控制在图纸范围内；焊点连续；表面无明显压伤、裂纹、毛刺'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM md_product
  WHERE item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-001')
);

-- BOM
INSERT INTO md_bom (bom_code, product_item_id, version_no, is_default, status, effective_date, expire_date, remark)
SELECT
  'BOM-BT-001',
  (SELECT id FROM md_item WHERE item_code = 'FG-BT-001'),
  'V1.0', 1, 'ACTIVE', CURRENT_DATE(), NULL,
  '本体标准BOM：单件毛重5.9kg，单件净重5.2kg'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_bom WHERE bom_code = 'BOM-BT-001');

INSERT INTO md_bom_item (
  bom_id, seq_no, material_item_id, usage_type, qty_per_piece, net_weight_per_piece_kg,
  gross_weight_per_piece_kg, loss_rate, issue_over_tolerance_rate, remark
)
SELECT
  (SELECT id FROM md_bom WHERE bom_code = 'BOM-BT-001'),
  10,
  (SELECT id FROM md_item WHERE item_code = 'RM-SAP440-500600-2.5'),
  'WEIGHT', 1.000000, 5.200000, 5.900000, 0.1180, 0.0200,
  '按单件毛重5.9kg参与MRP；按采购2吨倍数向上取整'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM md_bom_item
  WHERE bom_id = (SELECT id FROM md_bom WHERE bom_code = 'BOM-BT-001')
    AND seq_no = 10
);

-- 工艺路线
INSERT INTO md_process_route (
  route_code, route_name, product_item_id, version_no, is_default, status, effective_date, expire_date, remark
)
SELECT
  'ROUTE-BT-001', '本体标准工艺路线',
  (SELECT id FROM md_item WHERE item_code = 'FG-BT-001'),
  'V1.0', 1, 'ACTIVE', CURRENT_DATE(), NULL,
  '适用于本体标准批量生产的六道工序路线'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM md_process_route WHERE route_code = 'ROUTE-BT-001');

INSERT INTO md_process_route_operation (
  route_id, seq_no, process_id, work_center_id, operation_name,
  std_setup_minutes, std_cycle_seconds, std_labor_cost, std_machine_cost, std_outsource_cost,
  allowed_scrap_rate, report_required, is_key_operation, status, remark
)
SELECT
  (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001'),
  10,
  (SELECT id FROM md_process WHERE process_code = 'PROC-KP'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-KP-01'),
  '开平',
  20.00, 6.00, 0.12, 0.18, 0.00, 0.0050, 1, 0, 'ACTIVE', '板材整平、送料准备'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM md_process_route_operation
  WHERE route_id = (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001')
    AND seq_no = 10
);

INSERT INTO md_process_route_operation (
  route_id, seq_no, process_id, work_center_id, operation_name,
  std_setup_minutes, std_cycle_seconds, std_labor_cost, std_machine_cost, std_outsource_cost,
  allowed_scrap_rate, report_required, is_key_operation, status, remark
)
SELECT
  (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001'),
  20,
  (SELECT id FROM md_process WHERE process_code = 'PROC-CY'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-CY-01'),
  '冲压',
  45.00, 9.00, 0.34, 0.46, 0.00, 0.0200, 1, 1, 'ACTIVE', '主成形工序，模具与板材波动需重点监控'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM md_process_route_operation
  WHERE route_id = (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001')
    AND seq_no = 20
);

INSERT INTO md_process_route_operation (
  route_id, seq_no, process_id, work_center_id, operation_name,
  std_setup_minutes, std_cycle_seconds, std_labor_cost, std_machine_cost, std_outsource_cost,
  allowed_scrap_rate, report_required, is_key_operation, status, remark
)
SELECT
  (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001'),
  30,
  (SELECT id FROM md_process WHERE process_code = 'PROC-MC'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-MC-01'),
  '去毛刺',
  15.00, 5.00, 0.08, 0.06, 0.00, 0.0080, 1, 0, 'ACTIVE', '清理毛刺与锐边'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM md_process_route_operation
  WHERE route_id = (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001')
    AND seq_no = 30
);

INSERT INTO md_process_route_operation (
  route_id, seq_no, process_id, work_center_id, operation_name,
  std_setup_minutes, std_cycle_seconds, std_labor_cost, std_machine_cost, std_outsource_cost,
  allowed_scrap_rate, report_required, is_key_operation, status, remark
)
SELECT
  (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001'),
  40,
  (SELECT id FROM md_process WHERE process_code = 'PROC-ZW'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-ZW-01'),
  '折弯',
  20.00, 8.00, 0.21, 0.22, 0.00, 0.0100, 1, 1, 'ACTIVE', '关键角度成形控制'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM md_process_route_operation
  WHERE route_id = (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001')
    AND seq_no = 40
);

INSERT INTO md_process_route_operation (
  route_id, seq_no, process_id, work_center_id, operation_name,
  std_setup_minutes, std_cycle_seconds, std_labor_cost, std_machine_cost, std_outsource_cost,
  allowed_scrap_rate, report_required, is_key_operation, status, remark
)
SELECT
  (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001'),
  50,
  (SELECT id FROM md_process WHERE process_code = 'PROC-HJ'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-HJ-01'),
  '焊接',
  30.00, 15.00, 0.48, 0.28, 0.00, 0.0150, 1, 1, 'ACTIVE', '焊缝质量对成品稳定性影响大'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM md_process_route_operation
  WHERE route_id = (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001')
    AND seq_no = 50
);

INSERT INTO md_process_route_operation (
  route_id, seq_no, process_id, work_center_id, operation_name,
  std_setup_minutes, std_cycle_seconds, std_labor_cost, std_machine_cost, std_outsource_cost,
  allowed_scrap_rate, report_required, is_key_operation, status, remark
)
SELECT
  (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001'),
  60,
  (SELECT id FROM md_process WHERE process_code = 'PROC-QA'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-QA-01'),
  '检验包装',
  12.00, 10.00, 0.14, 0.03, 0.00, 0.0020, 1, 1, 'ACTIVE', '终检不合格件转返工或报废'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM md_process_route_operation
  WHERE route_id = (SELECT id FROM md_process_route WHERE route_code = 'ROUTE-BT-001')
    AND seq_no = 60
);

-- 设备台账
INSERT INTO em_equipment (
  equipment_code, equipment_name, category_name, work_center_id, manufacturer,
  model_no, install_date, rated_power_kw, occupied_area_sqm, maintenance_cycle_hours,
  last_maintenance_at, next_maintenance_at, status, remark
)
SELECT
  'EQ-KP-01', '开平机 01', '开平机',
  (SELECT id FROM md_work_center WHERE center_code = 'WC-KP-01'),
  '宁波精虎机械', 'KP-1600', '2024-01-15', 18.00, 12.50, 220.00,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 20 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 10 DAY),
  'IDLE', '本体板材预处理设备'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_equipment WHERE equipment_code = 'EQ-KP-01');

INSERT INTO em_equipment (
  equipment_code, equipment_name, category_name, work_center_id, manufacturer,
  model_no, install_date, rated_power_kw, occupied_area_sqm, maintenance_cycle_hours,
  last_maintenance_at, next_maintenance_at, status, remark
)
SELECT
  'EQ-CY-01', '冲床 01', '冲床',
  (SELECT id FROM md_work_center WHERE center_code = 'WC-CY-01'),
  '扬力集团', 'JH21-125T', '2024-01-20', 22.00, 16.00, 280.00,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 15 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 7 DAY),
  'IDLE', '本体主冲压设备'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_equipment WHERE equipment_code = 'EQ-CY-01');

INSERT INTO em_equipment (
  equipment_code, equipment_name, category_name, work_center_id, manufacturer,
  model_no, install_date, rated_power_kw, occupied_area_sqm, maintenance_cycle_hours,
  last_maintenance_at, next_maintenance_at, status, remark
)
SELECT
  'EQ-MC-01', '去毛刺机 01', '去毛刺机',
  (SELECT id FROM md_work_center WHERE center_code = 'WC-MC-01'),
  '苏州锐虎机电', 'MC-800', '2024-02-08', 7.50, 8.00, 180.00,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 18 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 12 DAY),
  'IDLE', '本体去毛刺设备'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_equipment WHERE equipment_code = 'EQ-MC-01');

INSERT INTO em_equipment (
  equipment_code, equipment_name, category_name, work_center_id, manufacturer,
  model_no, install_date, rated_power_kw, occupied_area_sqm, maintenance_cycle_hours,
  last_maintenance_at, next_maintenance_at, status, remark
)
SELECT
  'EQ-ZW-01', '折弯机 01', '折弯机',
  (SELECT id FROM md_work_center WHERE center_code = 'WC-ZW-01'),
  '江苏亚威', 'PBH-110', '2024-02-16', 15.00, 14.00, 240.00,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 22 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 8 DAY),
  'IDLE', '本体折弯设备'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_equipment WHERE equipment_code = 'EQ-ZW-01');

INSERT INTO em_equipment (
  equipment_code, equipment_name, category_name, work_center_id, manufacturer,
  model_no, install_date, rated_power_kw, occupied_area_sqm, maintenance_cycle_hours,
  last_maintenance_at, next_maintenance_at, status, remark
)
SELECT
  'EQ-HJ-01', '焊机 01', '焊机',
  (SELECT id FROM md_work_center WHERE center_code = 'WC-HJ-01'),
  '松下焊机', 'YD-350', '2024-03-01', 9.00, 6.00, 160.00,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 12 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 14 DAY),
  'IDLE', '本体焊接设备'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_equipment WHERE equipment_code = 'EQ-HJ-01');

INSERT INTO em_equipment (
  equipment_code, equipment_name, category_name, work_center_id, manufacturer,
  model_no, install_date, rated_power_kw, occupied_area_sqm, maintenance_cycle_hours,
  last_maintenance_at, next_maintenance_at, status, remark
)
SELECT
  'EQ-QA-01', '终检包装工位 01', '检验工位',
  (SELECT id FROM md_work_center WHERE center_code = 'WC-QA-01'),
  '百华内部配置', 'QA-STATION', '2024-03-10', 1.50, 4.00, 300.00,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 25 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 20 DAY),
  'IDLE', '终检与包装工位'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_equipment WHERE equipment_code = 'EQ-QA-01');

-- 设备保养计划
INSERT INTO em_maintenance_plan (
  plan_code, equipment_id, plan_name, maintenance_type, cycle_hours, cycle_days,
  last_execute_at, next_execute_at, status, remark
)
SELECT
  'MP-EQ-KP-01',
  (SELECT id FROM em_equipment WHERE equipment_code = 'EQ-KP-01'),
  '开平机周期保养', 'PREVENTIVE', 220.00, 30,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 20 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 10 DAY),
  'ACTIVE', '每220小时或每30天执行保养'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_maintenance_plan WHERE plan_code = 'MP-EQ-KP-01');

INSERT INTO em_maintenance_plan (
  plan_code, equipment_id, plan_name, maintenance_type, cycle_hours, cycle_days,
  last_execute_at, next_execute_at, status, remark
)
SELECT
  'MP-EQ-CY-01',
  (SELECT id FROM em_equipment WHERE equipment_code = 'EQ-CY-01'),
  '冲床周期保养', 'PREVENTIVE', 280.00, 30,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 15 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 7 DAY),
  'ACTIVE', '冲床模具与润滑需重点保养'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_maintenance_plan WHERE plan_code = 'MP-EQ-CY-01');

INSERT INTO em_maintenance_plan (
  plan_code, equipment_id, plan_name, maintenance_type, cycle_hours, cycle_days,
  last_execute_at, next_execute_at, status, remark
)
SELECT
  'MP-EQ-MC-01',
  (SELECT id FROM em_equipment WHERE equipment_code = 'EQ-MC-01'),
  '去毛刺机周期保养', 'PREVENTIVE', 180.00, 30,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 18 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 12 DAY),
  'ACTIVE', '砂轮与集尘装置维护'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_maintenance_plan WHERE plan_code = 'MP-EQ-MC-01');

INSERT INTO em_maintenance_plan (
  plan_code, equipment_id, plan_name, maintenance_type, cycle_hours, cycle_days,
  last_execute_at, next_execute_at, status, remark
)
SELECT
  'MP-EQ-ZW-01',
  (SELECT id FROM em_equipment WHERE equipment_code = 'EQ-ZW-01'),
  '折弯机周期保养', 'PREVENTIVE', 240.00, 30,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 22 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 8 DAY),
  'ACTIVE', '液压系统与模具保养'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_maintenance_plan WHERE plan_code = 'MP-EQ-ZW-01');

INSERT INTO em_maintenance_plan (
  plan_code, equipment_id, plan_name, maintenance_type, cycle_hours, cycle_days,
  last_execute_at, next_execute_at, status, remark
)
SELECT
  'MP-EQ-HJ-01',
  (SELECT id FROM em_equipment WHERE equipment_code = 'EQ-HJ-01'),
  '焊机周期保养', 'PREVENTIVE', 160.00, 30,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 12 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 14 DAY),
  'ACTIVE', '焊枪与送丝机构检查'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_maintenance_plan WHERE plan_code = 'MP-EQ-HJ-01');

INSERT INTO em_maintenance_plan (
  plan_code, equipment_id, plan_name, maintenance_type, cycle_hours, cycle_days,
  last_execute_at, next_execute_at, status, remark
)
SELECT
  'MP-EQ-QA-01',
  (SELECT id FROM em_equipment WHERE equipment_code = 'EQ-QA-01'),
  '终检工位周期保养', 'PREVENTIVE', 300.00, 45,
  DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 25 DAY), DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 20 DAY),
  'ACTIVE', '量具点检与包装工位维护'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM em_maintenance_plan WHERE plan_code = 'MP-EQ-QA-01');

COMMIT;
