SET NAMES utf8mb4;

-- 目的：将历史正常生产工单的领料记录迁移到生产台账。
-- 幂等性：生产台账按“材料库存批次 + 产品”唯一键更新，流水按历史工单领料明细防重复插入。

INSERT INTO pp_production_batch_ledger (
  material_lot_id,
  material_lot_no,
  material_item_id,
  product_item_id,
  status,
  miniapp_selectable,
  total_issued_weight_kg,
  outside_weight_kg,
  finished_inbound_qty,
  surplus_inbound_weight_kg,
  scrap_inbound_weight_kg,
  writeoff_weight_kg,
  first_issue_time,
  last_issue_time,
  remark
)
SELECT
  issue.source_lot_id,
  lot.lot_no,
  issue.material_item_id,
  wo.product_item_id,
  CASE WHEN UPPER(COALESCE(wo.status, '')) = 'SETTLED' THEN '锁单' ELSE '在生产' END,
  CASE WHEN UPPER(COALESCE(wo.status, '')) = 'SETTLED' THEN 0 ELSE 1 END,
  SUM(COALESCE(issue.issued_weight_kg, 0)),
  GREATEST(SUM(COALESCE(issue.issued_weight_kg, 0)) - COALESCE(MAX(wom.returned_weight_kg), 0), 0),
  COALESCE(MAX(wo.finished_qty), 0),
  COALESCE(MAX(wom.returned_weight_kg), 0),
  0,
  0,
  MIN(issue.issue_time),
  MAX(issue.issue_time),
  '由历史生产工单迁移生成'
FROM pp_work_order wo
JOIN pp_work_order_material_issue issue ON issue.work_order_id = wo.id
JOIN wh_stock_lot lot ON lot.id = issue.source_lot_id
LEFT JOIN pp_work_order_material wom
  ON wom.work_order_id = wo.id
 AND wom.material_item_id = issue.material_item_id
WHERE wo.work_order_type = 'NORMAL'
GROUP BY
  issue.source_lot_id,
  lot.lot_no,
  issue.material_item_id,
  wo.product_item_id,
  wo.status
ON DUPLICATE KEY UPDATE
  material_lot_no = VALUES(material_lot_no),
  material_item_id = VALUES(material_item_id),
  status = VALUES(status),
  miniapp_selectable = VALUES(miniapp_selectable),
  total_issued_weight_kg = VALUES(total_issued_weight_kg),
  outside_weight_kg = VALUES(outside_weight_kg),
  finished_inbound_qty = VALUES(finished_inbound_qty),
  surplus_inbound_weight_kg = VALUES(surplus_inbound_weight_kg),
  scrap_inbound_weight_kg = VALUES(scrap_inbound_weight_kg),
  writeoff_weight_kg = VALUES(writeoff_weight_kg),
  first_issue_time = VALUES(first_issue_time),
  last_issue_time = VALUES(last_issue_time),
  remark = CASE
    WHEN pp_production_batch_ledger.remark IS NULL OR pp_production_batch_ledger.remark = '' THEN '历史工单迁移已更新'
    WHEN pp_production_batch_ledger.remark LIKE '%历史工单迁移已更新%' THEN pp_production_batch_ledger.remark
    ELSE CONCAT(pp_production_batch_ledger.remark, '；历史工单迁移已更新')
  END;

-- 目的：为每条历史领料明细补充生产台账流水，保留原始工单与明细来源。
INSERT INTO pp_production_batch_ledger_txn (
  production_ledger_id,
  txn_type,
  qty_delta,
  weight_delta_kg,
  outside_weight_after_kg,
  source_doc_type,
  source_doc_id,
  source_line_id,
  biz_time,
  operator_user_id,
  remark
)
SELECT
  ledger.id,
  '生产出库',
  0,
  COALESCE(issue.issued_weight_kg, 0),
  ledger.outside_weight_kg,
  '历史生产工单',
  issue.work_order_id,
  issue.id,
  issue.issue_time,
  issue.operator_user_id,
  '由历史生产工单领料记录迁移'
FROM pp_work_order_material_issue issue
JOIN pp_work_order wo ON wo.id = issue.work_order_id
JOIN pp_production_batch_ledger ledger
  ON ledger.material_lot_id = issue.source_lot_id
 AND ledger.product_item_id = wo.product_item_id
WHERE wo.work_order_type = 'NORMAL'
  AND NOT EXISTS (
    SELECT 1
    FROM pp_production_batch_ledger_txn existing
    WHERE existing.production_ledger_id = ledger.id
      AND existing.source_doc_type = '历史生产工单'
      AND existing.source_doc_id = issue.work_order_id
      AND existing.source_line_id = issue.id
      AND existing.txn_type = '生产出库'
  );
