<?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.ruoyi.oa.mapper.SysOaWarehouseMapper">

    <resultMap type="com.ruoyi.oa.domain.vo.SysOaWarehouseVo" id="SysOaWarehouseResult">
        <result property="id" column="id"/>
        <result property="inventory" column="inventory"/>
        <result property="model" column="model"/>
        <result property="price" column="price"/>
        <result property="unit" column="unit"/>
        <result property="name" column="name"/>
        <result property="brand" column="brand"/>
        <result property="specifications" column="specifications"/>
        <result property="remark" column="remark"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <select id="selectVoByIdAndXml" resultMap="SysOaWarehouseResult" parameterType="Long">
        select id, inventory, model, unit, name, brand, specifications, remark, create_time, create_by, update_time, update_by, del_flag
        from sys_oa_warehouse
        where id = #{id}
    </select>


    <select id="selectVoPageThreshold" resultType="com.ruoyi.oa.domain.vo.SysOaWarehouseVo">
        select id,
               inventory,
               model,
               unit,
               name,
               brand,
               specifications,
               remark,
               create_time,
               create_by,
               update_time,
               update_by,
               del_flag
        from sys_oa_warehouse

                 ${ew.getCustomSqlSegment}
    </select>
    <select id="selectListVoPage" resultType="com.ruoyi.oa.domain.vo.SysOaWarehouseVo">
        select
            sow.id,
            sow.inventory,
            sow.model,
            sow.unit,
            sow.name,
            sow.brand,
            sow.specifications,
            sow.remark,
            sow.create_time,
            sow.create_by,
            sow.update_time,
            sow.update_by,
            sow.del_flag,
            sow.price,
            sow.threshold,
            (
                select sum(sowt.task_inventory)
                from sys_oa_warehouse_task sowt
                         left join sys_oa_warehouse_master sowm on sowm.master_id = sowt.master_id
                where sowt.warehouse_id = sow.id
                  and sowm.type = 2
                and (sowt.task_status = 0 or sowt.task_status = 1)
                  and sowt.del_flag = '0'
                  and sowm.del_flag = '0'
                  and sowm.status = 0
            ) as task_inventory
        from sys_oa_warehouse sow
            ${ew.getCustomSqlSegment}

    </select>


    <!-- 1. 快照时点的库存总量 -->
    <select id="selectInventoryAt" resultType="long">
        SELECT IFNULL(SUM(w.inventory),0)
        FROM sys_oa_warehouse w
        <where>
            w.del_flag = 0
            <!-- 快照时点 -->
            <if test="f.endTime != null">
                AND w.update_time &lt;= #{f.endTime}
            </if>
            <!-- 可选物料名 -->
            <if test="f.name != null and f.name != ''">
                AND w.name LIKE CONCAT('%',#{f.name},'%')
            </if>
            <!-- 可选品牌 -->
            <if test="f.brand != null and f.brand != ''">
                AND w.brand LIKE CONCAT('%',#{f.brand},'%')
            </if>
            <!-- 可选供应商：假设用 master.sign_user 表示 -->
            <if test="f.supplier != null and f.supplier != ''">
                AND EXISTS (
                SELECT 1
                FROM sys_oa_warehouse_task t
                JOIN sys_oa_warehouse_master m
                ON t.master_id = m.master_id
                WHERE t.warehouse_id = w.id
                AND m.sign_user = #{f.supplier}
                )
            </if>
        </where>
    </select>

    <!-- 2. 区间内在途物料数量 -->
    <select id="selectInTransitBetween" resultType="long">
        SELECT IFNULL(SUM(t.task_inventory),0)
        FROM sys_oa_warehouse_task t
        JOIN sys_oa_warehouse_master m
        ON t.master_id = m.master_id
        <where>
            m.del_flag = 0
            and t.del_flag = 0
            and m.type     = 2
            and m.status   = 0
            <!-- 时间范围 -->
            <if test="f.beginTime != null">
                AND m.sign_time &gt;= #{f.beginTime}
            </if>
            <if test="f.endTime != null">
                AND m.sign_time &lt;= #{f.endTime}
            </if>
            <!-- 物料名/品牌/供应商 同理 -->
            <if test="f.name != null and f.name != ''">
                AND t.name LIKE CONCAT('%',#{f.name},'%')
            </if>
            <if test="f.brand != null and f.brand != ''">
                AND t.brand LIKE CONCAT('%',#{f.brand},'%')
            </if>
            <if test="f.supplier != null and f.supplier != ''">
                AND m.sign_user = #{f.supplier}
            </if>
        </where>
    </select>
    <!-- 3. 指定日期的入库量（单位统一为米/kg 并取整） -->
    <select id="selectInboundOn" resultType="long">
        SELECT
        IFNULL(
        SUM(
        CASE
        WHEN w.unit IN ('cm','厘米')
        THEN FLOOR(d.amount / 100)        -- 厘米→米，并去小数
        WHEN w.unit IN ('g','克')
        THEN FLOOR(d.amount / 1000)       -- 克→千克，并去小数
        ELSE
        FLOOR(d.amount)                   -- 其他单位，也取整
        END
        )
        , 0)
        FROM sys_oa_warehouse_detail d
        JOIN sys_oa_warehouse_master m ON d.master_id = m.master_id
        JOIN sys_oa_warehouse w        ON d.warehouse_id = w.id
        <where>
            m.del_flag = 0
            AND d.del_flag = 0
            AND m.type = 1
            <!-- 当日 -->
            <if test="f.endTime != null">
                AND DATE(m.sign_time) = DATE(#{f.endTime})
            </if>
            <!-- 物料名/品牌/供应商 -->
            <if test="f.name != null and f.name != ''">
                AND w.name  LIKE CONCAT('%',#{f.name},'%')
            </if>
            <if test="f.brand != null and f.brand != ''">
                AND w.brand LIKE CONCAT('%',#{f.brand},'%')
            </if>
            <if test="f.supplier != null and f.supplier != ''">
                AND m.sign_user = #{f.supplier}
            </if>
        </where>
    </select>

    <!-- 4. 指定日期的出库量（同上） -->
    <select id="selectOutboundOn"
            parameterType="com.ruoyi.oa.domain.dto.SummaryFilterDTO"
            resultType="long">
        SELECT
        IFNULL(
        SUM(
        CASE
        WHEN w.unit IN ('cm','厘米')
        THEN FLOOR(d.amount / 100)
        WHEN w.unit IN ('g','克')
        THEN FLOOR(d.amount / 1000)
        ELSE
        FLOOR(d.amount)
        END
        )
        , 0)
        FROM sys_oa_warehouse_detail d
        JOIN sys_oa_warehouse_master m ON d.master_id = m.master_id
        JOIN sys_oa_warehouse w        ON d.warehouse_id = w.id
        <where>
            m.del_flag = 0
            AND d.del_flag = 0
            AND m.type = 0
            <!-- “当日”条件，用 endTime 对比 -->
            <if test="f.endTime != null">
                AND DATE(m.sign_time) = DATE(#{f.endTime})
            </if>
            <!-- 可选过滤 -->
            <if test="f.name != null and f.name != ''">
                AND w.name  LIKE CONCAT('%',#{f.name},'%')
            </if>
            <if test="f.brand != null and f.brand != ''">
                AND w.brand LIKE CONCAT('%',#{f.brand},'%')
            </if>
            <if test="f.supplier != null and f.supplier != ''">
                AND EXISTS (
                SELECT 1
                FROM sys_oa_warehouse_task t
                JOIN sys_oa_warehouse_master m2 ON t.master_id = m2.master_id
                WHERE t.warehouse_id = w.id
                AND m2.sign_user = #{f.supplier}
                )
            </if>
        </where>
    </select>
    <!-- 5. 指定日期的预警数 -->
    <select id="selectWarningOn" resultType="int">
        SELECT COUNT(1)
        FROM sys_oa_warehouse w
        <where>
            w.del_flag = 0
            AND w.threshold >= w.inventory
            <!-- 当日快照 -->
            <if test="f.endTime != null">
                AND DATE(w.update_time) = DATE(#{f.endTime})
            </if>
            <if test="f.name != null and f.name != ''">
                AND w.name LIKE CONCAT('%',#{f.name},'%')
            </if>
            <if test="f.brand != null and f.brand != ''">
                AND w.brand LIKE CONCAT('%',#{f.brand},'%')
            </if>
            <if test="f.supplier != null and f.supplier != ''">
                AND EXISTS (
                SELECT 1
                FROM sys_oa_warehouse_task t
                JOIN sys_oa_warehouse_master m
                ON t.master_id = m.master_id
                WHERE t.warehouse_id = w.id
                AND m.sign_user = #{f.supplier}
                )
            </if>
        </where>
    </select>


    <!-- 1. 年度入库量按月分组 -->
    <select id="selectInboundByMonth" parameterType="int" resultType="map">
        SELECT MONTH(m.sign_time) AS month,
               IFNULL(SUM(d.amount),0)    AS total
        FROM sys_oa_warehouse_detail d
                 JOIN sys_oa_warehouse_master  m
                      ON d.master_id = m.master_id
        WHERE m.del_flag  = 0
          AND d.del_flag  = 0
          AND m.type      = 1
          AND YEAR(m.sign_time) = #{year}
        GROUP BY MONTH(m.sign_time)
        ORDER BY MONTH(m.sign_time)
    </select>

    <!-- 2. 年度出库量按月分组 -->
    <select id="selectOutboundByMonth" parameterType="int" resultType="map">
        SELECT MONTH(m.sign_time) AS month,
               IFNULL(SUM(d.amount),0)    AS total
        FROM sys_oa_warehouse_detail d
                 JOIN sys_oa_warehouse_master  m
                      ON d.master_id = m.master_id
        WHERE m.del_flag  = 0
          AND d.del_flag  = 0
          AND m.type      = 0
          AND YEAR(m.sign_time) = #{year}
        GROUP BY MONTH(m.sign_time)
        ORDER BY MONTH(m.sign_time)
    </select>

    <!-- 任意日期的库存快照 -->
    <select id="selectInventorySnapshot"
            parameterType="java.time.LocalDate"
            resultType="long">
        SELECT IFNULL(SUM(inventory),0)
        FROM sys_oa_warehouse
        WHERE del_flag = 0
        AND #{date} >= DATE(update_time)
    </select>

</mapper>
