<?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.hys.app.mapper.erp.ProductStockMapper">


    <select id="getOne" resultType="com.hys.app.model.erp.dos.ProductStockDO">
        select * from erp_product_stock where id=#{id}
    </select>

    <!-- 查询产品预警库存信息分页列表 -->
    <select id="queryList" resultType="com.hys.app.model.erp.vo.ProductStockV1" useCache="false">
        select
        ps.id,
        ps.actual_stock,
        ps.usable_stock,
        ps.transit_Stock,
        ps.warning_stock,
        ps.locked_stock,
        w.id as warehouse_id,
        w.name as warehouse_name,
        g.id as goods_id,
        g.name as goodsName,
        g.sn ,
        p.id as product_id,
        p.name as product_name,
        p.unit goodsUnit
        from erp_product_stock ps
        left join erp_warehouse w on ps.warehouse_id = w.id
        left join erp_product p on ps.product_id = p.id
        left join erp_goods g on ps.goods_id = g.id
        <where>
            <if test="request.companyId != null and request.companyId != 0">
                and ps.company_id = #{request.companyId}
            </if>
            <if test="request.warehouseId != null and request.warehouseId != 0">
                and ps.id = #{request.warehouseId}
            </if>
            <if test="request.keyword != null and request.keyword != ''">
                and (
                g.name like concat("%",#{request.keyword },"%")
                or g.name like concat("%",#{request.keyword },"%")
                )
            </if>
        </where>
        order by ps.id desc
        limit ${(request.pageNo- 1) * request.pageSize} , #{request.pageSize}

    </select>


    <!-- 查询产品预警库存信息分页列表 -->
    <select id="selectCount" resultType="Long" useCache="false">
        select

        count(ps.id)

        from erp_product_stock ps
        left join erp_warehouse w on ps.warehouse_id = w.id
        left join erp_product p on ps.product_id = p.id
        left join erp_goods g on ps.goods_id = g.id
        left join erp_warehouse_area a on ps.area_id = a.id
        <where>
            <if test="request.warehouseId != null and request.warehouseId != 0">
                and ps.id = #{request.warehouseId}
            </if>
            <if test="request.keyword != null and request.keyword != ''">
                and (
                g.name like concat("%",#{request.keyword },"%")
                or g.name like concat("%",#{request.keyword },"%")
                )
            </if>
        </where>

    </select>



    <!-- 查询产品预警库存信息分页列表 -->
    <select id="listWarningStockPage" resultType="com.hys.app.model.erp.vo.ProductStockWarningVO" useCache="false">
        select p.*,ps.warehouse_id,w.`name` as warehouse_name,w.dept_id,sd.`name` as dept_name,ps.num as stock_num from erp_product_stock ps
        left join erp_warehouse w on ps.warehouse_id = w.id
        left join erp_product p on ps.product_id = p.id
        left join erp_company_dept sd on w.dept_id = sd.id
        <where>
            ps.num &lt;= p.warning_value
            <if test="deptId != null and deptId != 0">
                and w.dept_id = #{deptId}
            </if>
            <if test="warehouseId != null and warehouseId != 0">
                and ps.warehouse_id = #{warehouseId}
            </if>
            <if test="name != null and name != ''">
                and p.name like concat('%',#{name},'%')
            </if>
        </where>
    </select>
    <select id="transitDetail" resultType="com.hys.app.model.erp.vo.TransitVO">
        SELECT
            eo.sn order_sn,
            eoi.num,
            eoi.end_time
        FROM
            erp_order_item eoi
                LEFT JOIN erp_order eo ON eoi.order_id = eo.id
        WHERE
            eoi.product_stock_id = #{stockId}
          and eoi.has_return = 0
          AND eo.type = "TO_C"
    </select>

</mapper>