<?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.fjhx.mapper.stock.StockMapper">

    <select id="pageByWarehouseId" resultType="java.util.Map">
        select pi.id,
               pi.code,
               pi.name,
               pi.type,
               pi.unit,
               ifnull(s.quantity, 0) inventoryQuantity
        from product_info pi
                 left join stock s on s.goods_id = pi.id and s.warehouse_id = #{warehouseId}
    </select>

    <select id="pageByWarehouse" resultType="com.fjhx.params.stock.PageByWarehouseVo">
        select w.id          warehouseId,
               w.name        warehouseName,
               s.id          stockId,
               pi.definition productDefinition,
               pi.id         productId,
               pi.code       productCode,
               pi.name       productName,
               pi.specs      productSpecs,
               pi.unit       productUnit,
               s.quantity,
               s.frozen_quantity
        from warehouse w
                 inner join stock s on w.id = s.warehouse_id
                 inner join product_info pi on s.goods_id = pi.id
            ${ew.customSqlSegment}
    </select>

    <select id="pageByProduct" resultType="com.fjhx.params.stock.PageByProductVo">
        <include refid="page"/>
    </select>

    <select id="pageByCombination" resultType="com.fjhx.params.stock.PageByCombinationVo">
        <include refid="page"/>
    </select>

    <sql id="page">
        select pi.definition          productDefinition,
               pi.id                  productId,
               pi.code                productCode,
               pi.name                productName,
               pi.specs               productSpecs,
               pi.unit                productUnit,
               sum(s.quantity)        quantity,
               sum(s.frozen_quantity) frozenQuantity
        from warehouse w
                 inner join stock s on w.id = s.warehouse_id
                 inner join product_info pi on s.goods_id = pi.id
            ${ew.customSqlSegment}
    </sql>

</mapper>
