/* 一、二级位置：机台（H1202 ~ H1404），父级为车间 location_code='1' */
INSERT INTO rfid_location (
    location_code,
    location_alias,
    location_type,
    parent_id,
    ancestors,
    is_marked,
    remark,
    create_dept,
    create_by,
    create_time,
    update_by,
    update_time
)
SELECT
    t.location_code,
    t.location_alias,
    '2' AS location_type,
    p.id AS parent_id,
    CONCAT(p.ancestors, ',', p.id) AS ancestors,
    '1' AS is_marked,
    NULL AS remark,
    NULL AS create_dept,
    1994219831517650945 AS create_by,
    '2025-12-01 14:01:57' AS create_time,
    1994219831517650945 AS update_by,
    '2025-12-01 14:01:57' AS update_time
FROM (
         SELECT 'H1202' AS location_code, 'H1202' AS location_alias UNION ALL
         SELECT 'H1203', 'H1203' UNION ALL
         SELECT 'H1204', 'H1204' UNION ALL
         SELECT 'H1205', 'H1205' UNION ALL
         SELECT 'H1206', 'H1206' UNION ALL
         SELECT 'H1301', 'H1301' UNION ALL
         SELECT 'H1302', 'H1302' UNION ALL
         SELECT 'H1303', 'H1303' UNION ALL
         SELECT 'H1304', 'H1304' UNION ALL
         SELECT 'H1305', 'H1305' UNION ALL
         SELECT 'H1306', 'H1306' UNION ALL
         SELECT 'H1401', 'H1401' UNION ALL
         SELECT 'H1402', 'H1402' UNION ALL
         SELECT 'H1403', 'H1403' UNION ALL
         SELECT 'H1404', 'H1404'
     ) t
         JOIN rfid_location p
              ON p.location_code = '1';
/* 二、三级位置：工位（RFID 点位），父级为对应机台，跳过已存在的 H1201-胎侧 */
INSERT INTO rfid_location (
    location_code,
    location_alias,
    location_type,
    parent_id,
    ancestors,
    is_marked,
    remark,
    create_dept,
    create_by,
    create_time,
    update_by,
    update_time
)
SELECT
    CONCAT(m.machine_code, '-', p.point_name) AS location_code,
    CONCAT(m.machine_code, '-', p.point_name) AS location_alias,
    '3' AS location_type,
    parentLoc.id AS parent_id,
    CONCAT(parentLoc.ancestors, ',', parentLoc.id) AS ancestors,
    '1' AS is_marked,
    NULL AS remark,
    NULL AS create_dept,
    1994219831517650945 AS create_by,
    '2025-12-01 14:01:57' AS create_time,
    1994219831517650945 AS update_by,
    '2025-12-01 14:01:57' AS update_time
FROM (
         SELECT 'H1201' AS machine_code UNION ALL
         SELECT 'H1202' UNION ALL
         SELECT 'H1203' UNION ALL
         SELECT 'H1204' UNION ALL
         SELECT 'H1205' UNION ALL
         SELECT 'H1206' UNION ALL
         SELECT 'H1301' UNION ALL
         SELECT 'H1302' UNION ALL
         SELECT 'H1303' UNION ALL
         SELECT 'H1304' UNION ALL
         SELECT 'H1305' UNION ALL
         SELECT 'H1306' UNION ALL
         SELECT 'H1401' UNION ALL
         SELECT 'H1402' UNION ALL
         SELECT 'H1403' UNION ALL
         SELECT 'H1404'
     ) m
         CROSS JOIN (
    SELECT '胎侧' AS point_name UNION ALL
    SELECT '内衬' UNION ALL
    SELECT '胎体1' UNION ALL
    SELECT '胎体2' UNION ALL
    SELECT '带束1' UNION ALL
    SELECT '带束2' UNION ALL
    SELECT '胎面'
) p
         JOIN rfid_location parentLoc
              ON parentLoc.location_code = m.machine_code
WHERE NOT (m.machine_code = 'H1201' AND p.point_name = '胎侧');
/* 三、设备表：每个工位一个设备，IP 与端口来自 ddd.md
   IP 规则：10.82.104.(80 ~ 191)，按机台从 H1201 到 H1404，
   每台 7 个点位，顺序：胎侧、内衬、胎体1、胎体2、带束1、带束2、胎面
*/
INSERT INTO rfid_device (
    device_code,
    device_name,
    location_id,
    device_address,
    device_port,
    read_frequency,
    online_status,
    alarm_status,
    is_marked,
    remark,
    create_dept,
    create_by,
    create_time,
    update_by,
    update_time
)
SELECT
    loc.location_code AS device_code,
    loc.location_code AS device_name,
    loc.id AS location_id,
    CONCAT('10.82.104.', 80 + m.base_offset + p.point_offset) AS device_address,
    20108 AS device_port,
    5 AS read_frequency,
    '0' AS online_status,
    '0' AS alarm_status,
    '1' AS is_marked,
    NULL AS remark,
    NULL AS create_dept,
    1994219831517650945 AS create_by,
    '2025-12-01 14:01:57' AS create_time,
    1994219831517650945 AS update_by,
    '2025-12-01 14:01:57' AS update_time
FROM (
         SELECT 'H1201' AS machine_code, 0 AS base_offset UNION ALL
         SELECT 'H1202', 7 UNION ALL
         SELECT 'H1203', 14 UNION ALL
         SELECT 'H1204', 21 UNION ALL
         SELECT 'H1205', 28 UNION ALL
         SELECT 'H1206', 35 UNION ALL
         SELECT 'H1301', 42 UNION ALL
         SELECT 'H1302', 49 UNION ALL
         SELECT 'H1303', 56 UNION ALL
         SELECT 'H1304', 63 UNION ALL
         SELECT 'H1305', 70 UNION ALL
         SELECT 'H1306', 77 UNION ALL
         SELECT 'H1401', 84 UNION ALL
         SELECT 'H1402', 91 UNION ALL
         SELECT 'H1403', 98 UNION ALL
         SELECT 'H1404', 105
     ) m
         CROSS JOIN (
    SELECT '胎侧' AS point_name, 0 AS point_offset UNION ALL
    SELECT '内衬', 1 UNION ALL
    SELECT '胎体1', 2 UNION ALL
    SELECT '胎体2', 3 UNION ALL
    SELECT '带束1', 4 UNION ALL
    SELECT '带束2', 5 UNION ALL
    SELECT '胎面', 6
) p
         JOIN rfid_location loc
              ON loc.location_code = CONCAT(m.machine_code, '-', p.point_name);