<?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="com.zg.project.wisdom.mapper.WarehouseStatMapper">

    <!-- 1) 启用仓库列表 -->
    <resultMap id="WarehouseLiteMap" type="com.zg.project.wisdom.domain.vo.WarehouseStatVO">
        <result property="warehouseCode" column="warehouse_code"/>
        <result property="warehouseName" column="warehouse_name"/>
    </resultMap>

    <select id="selectEnabledWarehouses" resultMap="WarehouseLiteMap">
        SELECT wi.warehouse_code,
               wi.warehouse_name
        FROM warehouse_info wi
        WHERE wi.status = 1
        ORDER BY wi.warehouse_code
    </select>

    <!-- 2) 指定仓库的场景库位统计（总库位/已使用） -->
    <resultMap id="SceneUsageMap" type="com.zg.project.wisdom.domain.vo.SceneUsageVO">
        <result property="sceneCode" column="scene_code"/>
        <result property="sceneName" column="scene_name"/>
        <result property="totalPositions" column="total_positions"/>
        <result property="usedPositions" column="used_positions"/>
    </resultMap>

    <select id="selectSceneUsageByWarehouse" resultMap="SceneUsageMap">
        SELECT
            sm.scene_code,
            sm.scene_name,
            COUNT(DISTINCT pd.pcode) AS total_positions,
            COUNT(DISTINCT CASE WHEN ri.is_chuku = 0 THEN pd.pcode END) AS used_positions
        FROM pcde_detail pd
                 LEFT JOIN scene_mapping sm
                           ON pd.scene = sm.scene_code
                 LEFT JOIN rk_info ri
                           ON pd.pcode = ri.pcode
        WHERE pd.warehouse_code = #{warehouseCode}
          AND (pd.is_delete IS NULL OR pd.is_delete = '0')
        GROUP BY sm.scene_code, sm.scene_name
        ORDER BY sm.scene_code
    </select>

    <!-- 3) 启用仓库总数 -->
    <select id="countEnabledWarehouses" resultType="int">
        SELECT COUNT(*)
        FROM warehouse_info wi
        WHERE wi.status = 1
    </select>

</mapper>
