<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.dromara.rfid.mapper.StatisticsMapper">

    <!-- 位置统计（轻量级，仅查询 rfid_location 表） -->
    <select id="selectLocationStats" resultType="org.dromara.rfid.domain.vo.HomeStatsVO$LocationStats">
        SELECT
            COUNT(*) as totalLocations,
            SUM(CASE WHEN location_type = '1' THEN 1 ELSE 0 END) as workshopCount,
            SUM(CASE WHEN location_type = '2' THEN 1 ELSE 0 END) as processCount,
            SUM(CASE WHEN location_type = '3' THEN 1 ELSE 0 END) as stationCount
        FROM rfid_location
    </select>

    <!-- 顶级位置列表（parent_id = 0 或 NULL） -->
    <select id="selectTopLocations" resultType="org.dromara.rfid.domain.vo.HomeStatsVO$LocationItem">
        SELECT
            l.id,
            l.location_code as locationCode,
            l.location_alias as locationAlias,
            l.location_type as locationType,
            (SELECT COUNT(*) FROM rfid_location c WHERE c.parent_id = l.id) as childCount
        FROM rfid_location l
        WHERE l.parent_id = 0 OR l.parent_id IS NULL
        ORDER BY l.id
    </select>

</mapper>
