<?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.SysOaWarehouseDetailMapper">

    <resultMap type="com.ruoyi.oa.domain.SysOaWarehouseDetail" id="SysOaOutWarehouseResult">
        <result property="id" column="id"/>
        <result property="projectId" column="project_id"/>
        <result property="amount" column="amount"/>
        <result property="remark" column="remark"/>
        <result property="warehouseId" column="warehouse_id"/>
        <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"/>
        <result property="signPrice" column="sign_price"/>
        <result property="masterId" column="master_id"/>
    </resultMap>


    <!-- 仓库出库筛选(联查) -->
    <select id="defineSelectList" resultType="com.ruoyi.oa.domain.vo.SysOaOutWarehouseListVo">
        select
        ware.id as id,
        ware.project_id as projectId,
        ware.amount as amount,
        ware.remark as remark,
        ware.warehouse_id as warehouseId,
        project.project_name as projectName,
        warehouse.name as warehouseName,
        ware.create_time,
        ware.create_by,
        ware.master_id,
        ware.sign_price
        from
        sys_oa_warehouse_detail ware
        left join sys_oa_project project on ware.project_id = project.project_id
        left join sys_oa_warehouse warehouse on warehouse.id = ware.warehouse_id
        <where>
             ware.del_flag='0'
            <if test="bo.projectName != null and bo.projectName != ''">
                and project.project_name like concat('%', #{bo.projectName}, '%')
            </if>
            <if test="bo.id != null and bo.id != ''">
                and ware.id = #{bo.id}
            </if>
            <if test="bo.projectId != null and bo.projectId != ''">
                and project.project_id = #{bo.projectId}
            </if>
            <if test="bo.amount != null and bo.amount != ''">
                and ware.amount = #{bo.amount}
            </if>
            <if test="bo.remark != null and bo.remark != ''">
                and ware.remark like concat('%', #{bo.remark}, '%')
            </if>
            <if test="bo.warehouseId != null and bo.warehouseId != ''">
                and ware.warehouse_id = #{bo.warehouseId}
            </if>
            <if test="bo.warehouseName != null and bo.warehouseName != ''">
                and warehouse.name like concat('%', #{bo.warehouseName}, '%')
            </if>
        </where>
        order by ware.create_time desc
        <if test="page != null and page.pageNum != null and page.pageSize != null">
            limit #{page.pageNum}, #{page.pageSize}
        </if>

    </select>

    <select id="defineQueryTotal" resultType="Long">
        select
         count(*)
        from
        sys_oa_warehouse_detail ware
        left join sys_oa_project project on ware.project_id = project.project_id

        left join sys_oa_warehouse warehouse on warehouse.id = ware.warehouse_id
        <if test="bo != null">
        <where>
            ware.del_flag = '0'
            <if test="bo.projectName != null and bo.projectName != ''">
                and project.project_name like concat('%', #{bo.projectName}, '%')
            </if>
            <if test="bo.id != null and bo.id != ''">
                and ware.id = #{bo.id}
            </if>
            <if test="bo.projectId != null and bo.projectId != ''">
                and project.project_id = #{bo.projectId}
            </if>
            <if test="bo.amount != null and bo.amount != ''">
                and ware.amount = #{bo.amount}
            </if>
            <if test="bo.remark != null and bo.remark != ''">
                and ware.remark like concat('%', #{bo.remark}, '%')
            </if>
            <if test="bo.warehouseId != null and bo.warehouseId != ''">
                and ware.warehouse_id = #{bo.warehouseId}
            </if>
            <if test="bo.warehouseName != null and bo.warehouseName != ''">
                and warehouse.name like concat('%', #{bo.warehouseName}, '%')
            </if>
        </where>
        </if>

    </select>


    <select id="selectRecentOutbound" resultType="com.ruoyi.oa.domain.vo.SysOaWarehouseVo">
        SELECT w.id             AS id,
               w.name           AS name,
               w.model          AS model,
               w.price          AS price,
               w.inventory      AS inventory,
               w.unit           AS unit,
               w.brand          AS brand,
               w.specifications AS specifications,
               w.remark         AS remark,
               w.threshold      AS threshold,
               d.amount         AS taskInventory,

               -- 最近一次出库数量（type=0）
               (SELECT d2.amount
                FROM sys_oa_warehouse_detail d2
                         INNER JOIN sys_oa_warehouse_master m2
                                    ON d2.master_id = m2.master_id
                WHERE m2.type = 0
                  AND d2.warehouse_id = w.id
                ORDER BY d2.create_time DESC
                LIMIT 1)        AS lastOutbound,

               -- 最近一次入库数量（type=1）
               (SELECT d3.amount
                FROM sys_oa_warehouse_detail d3
                         INNER JOIN sys_oa_warehouse_master m3
                                    ON d3.master_id = m3.master_id
                WHERE m3.type = 1
                  AND d3.warehouse_id = w.id
                ORDER BY d3.create_time DESC
                LIMIT 1)        AS lastInbound,

               ''               AS temp
        FROM sys_oa_warehouse_detail d
                 INNER JOIN sys_oa_warehouse_master m
                            ON d.master_id = m.master_id
                 INNER JOIN sys_oa_warehouse w
                            ON d.warehouse_id = w.id
        WHERE m.type = 0
        ORDER BY d.create_time DESC
        LIMIT #{limit}
    </select>


</mapper>
