USE jiaheng_erp;

SET NAMES utf8mb4;

START TRANSACTION;

-- 垃圾桶垫默认 BOM
INSERT INTO md_bom (
  bom_code, product_item_id, version_no, is_default, status, effective_date, expire_date, remark
)
SELECT
  'BOM-BT-002',
  (SELECT id FROM md_item WHERE item_code = 'FG-BT-002'),
  'V1.0', 1, 'ACTIVE', CURRENT_DATE(), NULL,
  '垃圾桶垫默认BOM：单件净重1.6kg，按SAP440钢板毛重1.82kg计入MRP'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1
  FROM md_bom
  WHERE product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002')
    AND is_default = 1
    AND status = 'ACTIVE'
);

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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002') AND is_default = 1 AND status = 'ACTIVE' ORDER BY id DESC LIMIT 1),
  10,
  (SELECT id FROM md_item WHERE item_code = 'RM-SAP440-500600-2.5'),
  'WEIGHT', 1.000000, 1.600000, 1.820000, 0.1375, 0.0200,
  '垃圾桶垫按SAP440钢板计料；MRP按1.82kg/件计算，采购仍按2吨倍数向上取整'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1
  FROM md_bom_item
  WHERE bom_id = (
    SELECT id FROM md_bom
    WHERE product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002')
      AND is_default = 1
      AND status = 'ACTIVE'
    ORDER BY id DESC LIMIT 1
  )
    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-002', '垃圾桶垫标准工艺路线',
  (SELECT id FROM md_item WHERE item_code = 'FG-BT-002'),
  'V1.0', 1, 'ACTIVE', CURRENT_DATE(), NULL,
  '适用于垃圾桶垫的默认冲压件六道工序路线'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1
  FROM md_process_route
  WHERE product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002')
    AND is_default = 1
    AND status = 'ACTIVE'
);

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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002') AND is_default = 1 AND status = 'ACTIVE' ORDER BY id DESC LIMIT 1),
  10,
  (SELECT id FROM md_process WHERE process_code = 'PROC-KP'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-KP-01'),
  '开平',
  12.00, 4.00, 0.08, 0.10, 0.00, 0.0030, 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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002')
      AND is_default = 1
      AND status = 'ACTIVE'
    ORDER BY id DESC LIMIT 1
  )
    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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002') AND is_default = 1 AND status = 'ACTIVE' ORDER BY id DESC LIMIT 1),
  20,
  (SELECT id FROM md_process WHERE process_code = 'PROC-CY'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-CY-01'),
  '冲压',
  20.00, 6.00, 0.18, 0.24, 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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002')
      AND is_default = 1
      AND status = 'ACTIVE'
    ORDER BY id DESC LIMIT 1
  )
    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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002') AND is_default = 1 AND status = 'ACTIVE' ORDER BY id DESC LIMIT 1),
  30,
  (SELECT id FROM md_process WHERE process_code = 'PROC-MC'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-MC-01'),
  '去毛刺',
  10.00, 4.00, 0.06, 0.05, 0.00, 0.0040, 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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002')
      AND is_default = 1
      AND status = 'ACTIVE'
    ORDER BY id DESC LIMIT 1
  )
    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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002') AND is_default = 1 AND status = 'ACTIVE' ORDER BY id DESC LIMIT 1),
  40,
  (SELECT id FROM md_process WHERE process_code = 'PROC-ZW'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-ZW-01'),
  '折弯',
  14.00, 5.00, 0.12, 0.12, 0.00, 0.0060, 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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002')
      AND is_default = 1
      AND status = 'ACTIVE'
    ORDER BY id DESC LIMIT 1
  )
    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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002') AND is_default = 1 AND status = 'ACTIVE' ORDER BY id DESC LIMIT 1),
  50,
  (SELECT id FROM md_process WHERE process_code = 'PROC-HJ'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-HJ-01'),
  '焊接',
  18.00, 8.00, 0.20, 0.10, 0.00, 0.0050, 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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002')
      AND is_default = 1
      AND status = 'ACTIVE'
    ORDER BY id DESC LIMIT 1
  )
    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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002') AND is_default = 1 AND status = 'ACTIVE' ORDER BY id DESC LIMIT 1),
  60,
  (SELECT id FROM md_process WHERE process_code = 'PROC-QA'),
  (SELECT id FROM md_work_center WHERE center_code = 'WC-QA-01'),
  '检验包装',
  8.00, 6.00, 0.09, 0.02, 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 product_item_id = (SELECT id FROM md_item WHERE item_code = 'FG-BT-002')
      AND is_default = 1
      AND status = 'ACTIVE'
    ORDER BY id DESC LIMIT 1
  )
    AND seq_no = 60
);

COMMIT;
