<?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.jeeplus.psimanage.psiWareHouse.mapper.PsiWareHouseSummaryMapper">
    <delete id="deleteByDetailedId">
        update psi_management_warehouse_summary set del_flag = 1
    </delete>
    <select id="getInfoByTradeName"
            resultType="java.lang.Double">
        SELECT
            ( sum( a.trade_number )- c.num2 ) AS num1
        FROM
            psi_management_warehouse_detailed a
            LEFT JOIN (
            SELECT
                sum( b.collect_number ) AS num2,
                b.collect_type,
                b.goods_name
            FROM
                psi_management_collect_detailed b
                LEFT JOIN psi_management_collect_basics c ON b.basic_id = c.id
                AND c.del_flag = 0
            WHERE
                b.collect_type = #{wareHouseType}
                AND b.goods_name = #{tradeName}

            AND b.del_flag = 0
            AND c.`status` = 5
            ) AS c ON a.ware_house_type = c.collect_type
            AND a.trade_name = c.goods_name
        WHERE
            a.ware_house_type = #{wareHouseType}
            AND a.trade_name = #{tradeName}
            AND a.del_flag = 0
    </select>
    <select id="getInfoByBasicId"
            resultType="com.jeeplus.psimanage.psiWareHouse.domain.PsiWareHouseSummary">
        select * from psi_management_warehouse_summary where basic_id = #{id}
    </select>

    <select id="getInfoByProduceDateDesc"
            resultType="com.jeeplus.psimanage.psiWareHouse.domain.PsiWareHouseDetailed">
        SELECT
            a.id,
            a.trade_name,
            a.produce_date,
            a.spec,
            a.trade_number,
--             (a.trade_number - IFNULL(c.num2, 0)) AS currentInventory
            a.current_inventory,
            a.warn_num,
            a.warn_flag
        FROM
            psi_management_warehouse_detailed a
            -- 只查当前批次、当前生产日期对应的领用数量
                LEFT JOIN (
                SELECT
                    b.goods_name,
                    b.produce_date,  -- 必须加上生产日期
                    sum(b.collect_number) AS num2
                FROM
                    psi_management_collect_detailed b
                        LEFT JOIN psi_management_collect_basics c
                                  ON b.basic_id = c.id AND c.del_flag = 0
                WHERE
                    b.collect_type = #{wareHouseType}
                  AND b.goods_name = #{tradeName}
                  AND b.del_flag = 0
                  AND c.`status` = 5
                -- 按商品  生产日期分组
                GROUP BY b.goods_name, b.produce_date
            ) c
                          ON a.trade_name = c.goods_name
                              AND a.produce_date = c.produce_date  -- 关键：按生产日期匹配
        WHERE
            a.ware_house_type = #{wareHouseType}
          AND a.trade_name = #{tradeName}
          AND a.del_flag = 0

        GROUP BY a.id  -- 按库存分组
        ORDER BY a.produce_date asc  -- 按生产日期排序
    </select>
</mapper>
