-- Active: 1763990602551@@192.144.167.231@3306@autoaiworksys
-- ============================================
-- 数据迁移脚本（简化版）：将 device_status 表中的 device_id 迁移到 pla_account 表
-- ============================================

-- 查看 device_status 表的数据
SELECT sn_code, device_id FROM device_status;

-- 查看迁移前的 pla_account 数据
SELECT id, sn_code, device_id, name FROM pla_account WHERE sn_code IN (SELECT sn_code FROM device_status);

-- 执行迁移：根据 sn_code 匹配，将 device_status.device_id 更新到 pla_account.device_id
UPDATE pla_account pa
INNER JOIN device_status ds ON pa.sn_code = ds.sn_code
SET pa.device_id = ds.device_id
WHERE ds.device_id IS NOT NULL 
  AND ds.device_id != ''
  AND (pa.device_id IS NULL OR pa.device_id = '');

-- 查看迁移后的结果
SELECT id, sn_code, device_id, name, 
       CASE WHEN device_id IS NOT NULL AND device_id != '' THEN '已迁移' ELSE '未迁移' END AS status
FROM pla_account 
WHERE sn_code IN (SELECT sn_code FROM device_status);

