<?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.web.stock.mapper.WarehouseInventoryMapper">

    <resultMap id="WarehouseInventoryResult" type="com.ruoyi.web.stock.domain.WarehouseInventory">
        <id property="id" column="id"/>
        <result property="warehouseNo" column="warehouse_no"/>
        <result property="warehouseName" column="warehouse_name"/>
        <result property="goodsNum" column="goods_num"/>
        <result property="goodsName" column="goods_name"/>
        <result property="batchNo" column="batch_no"/>
        <result property="stockQty" column="stock_qty"/>
        <result property="assistStockQty" column="assist_stock_qty"/>
        <result property="costAmount" column="cost_amount"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
        <result property="remark" column="remark"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap id="StockOptionResult" type="com.ruoyi.web.stock.domain.vo.StockOptionVo">
        <result property="code" column="code"/>
        <result property="name" column="name"/>
    </resultMap>

    <resultMap id="InventoryLineOptionResult" type="com.ruoyi.web.stock.domain.vo.InventoryLineOptionVo">
        <result property="inventoryId" column="inventory_id"/>
        <result property="goodsNum" column="goods_num"/>
        <result property="goodsName" column="goods_name"/>
        <result property="goodsSpec" column="goods_spec"/>
        <result property="batchNo" column="batch_no"/>
        <result property="warehouseNo" column="warehouse_no"/>
        <result property="warehouseName" column="warehouse_name"/>
        <result property="baseUnit" column="base_unit"/>
        <result property="assistantUnit" column="assistant_unit"/>
        <result property="bookBaseQty" column="book_base_qty"/>
        <result property="bookAssistQty" column="book_assist_qty"/>
    </resultMap>

    <sql id="baseSelect">
        select id, warehouse_no, warehouse_name, goods_num, goods_name, batch_no,
               stock_qty, assist_stock_qty, cost_amount, del_flag, create_by, create_time, update_by, update_time, remark, org_id
        from warehouse_inventory
    </sql>

    <select id="selectWarehouseInventoryById" parameterType="Long" resultMap="WarehouseInventoryResult">
        <include refid="baseSelect"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectWarehouseInventoryList" parameterType="com.ruoyi.web.stock.domain.WarehouseInventory" resultMap="WarehouseInventoryResult">
        <include refid="baseSelect"/>
        <where>
            del_flag = '0'
            <if test="warehouseNo != null and warehouseNo != ''">
                and warehouse_no like concat('%', #{warehouseNo}, '%')
            </if>
            <if test="warehouseName != null and warehouseName != ''">
                and warehouse_name like concat('%', #{warehouseName}, '%')
            </if>
            <if test="goodsNum != null and goodsNum != ''">
                and goods_num like concat('%', #{goodsNum}, '%')
            </if>
            <if test="goodsName != null and goodsName != ''">
                and goods_name like concat('%', #{goodsName}, '%')
            </if>
            <if test="batchNo != null and batchNo != ''">
                and batch_no like concat('%', #{batchNo}, '%')
            </if>
            <if test="orgId != null and orgId != ''">
                and org_id = #{orgId}
            </if>
        </where>
        order by id desc
    </select>

    <select id="selectWarehouseInventoryListByWarehouseNo" resultMap="WarehouseInventoryResult">
        <include refid="baseSelect"/>
        where del_flag = '0'
          and org_id = #{orgId}
          and warehouse_no = #{warehouseNo}
        order by id desc
    </select>

    <insert id="insertWarehouseInventory" parameterType="com.ruoyi.web.stock.domain.WarehouseInventory" useGeneratedKeys="true" keyProperty="id">
        insert into warehouse_inventory (
            warehouse_no, warehouse_name, goods_num, goods_name, batch_no,
            stock_qty, assist_stock_qty, cost_amount, del_flag,
            create_by, create_time, update_by, update_time, remark, org_id
        ) values (
            #{warehouseNo}, #{warehouseName}, #{goodsNum}, #{goodsName}, #{batchNo},
            #{stockQty}, #{assistStockQty}, #{costAmount}, #{delFlag},
            #{createBy}, now(), #{updateBy}, now(), #{remark}, #{orgId}
        )
    </insert>

    <update id="updateWarehouseInventory" parameterType="com.ruoyi.web.stock.domain.WarehouseInventory">
        update warehouse_inventory
        <set>
            warehouse_no = #{warehouseNo},
            warehouse_name = #{warehouseName},
            goods_num = #{goodsNum},
            goods_name = #{goodsName},
            batch_no = #{batchNo},
            stock_qty = #{stockQty},
            assist_stock_qty = #{assistStockQty},
            cost_amount = #{costAmount},
            update_by = #{updateBy},
            update_time = now(),
            remark = #{remark},
            org_id = #{orgId}
        </set>
        where id = #{id}
          and del_flag = '0'
    </update>

    <update id="deleteWarehouseInventoryById" parameterType="Long">
        update warehouse_inventory
        set del_flag = '2',
            update_time = now()
        where id = #{id}
          and del_flag = '0'
    </update>

    <select id="selectByUniqueKey" resultMap="WarehouseInventoryResult">
        <include refid="baseSelect"/>
        where org_id = #{orgId}
          and warehouse_no = #{warehouseNo}
          and goods_num = #{goodsNum}
          and batch_no = #{batchNo}
          and del_flag = '0'
        limit 1
    </select>

    <update id="increaseInventory">
        update warehouse_inventory
        set stock_qty = ifnull(stock_qty, 0) + ifnull(#{stockQty}, 0),
            assist_stock_qty = ifnull(assist_stock_qty, 0) + ifnull(#{assistStockQty}, 0),
            cost_amount = ifnull(cost_amount, 0) + ifnull(#{costAmount}, 0),
            update_by = #{updateBy},
            update_time = now()
        where id = #{id}
          and del_flag = '0'
    </update>

    <update id="updateStockQtyById">
        update warehouse_inventory
        set stock_qty = ifnull(#{stockQty}, 0),
            assist_stock_qty = ifnull(#{assistStockQty}, 0),
            update_by = #{updateBy},
            update_time = now()
        where id = #{id}
          and del_flag = '0'
    </update>

    <select id="selectWarehouseInventoryByBatchNo" parameterType="String" resultMap="WarehouseInventoryResult">
        <include refid="baseSelect"/>
        where batch_no = #{batchNo}
          and org_id = #{orgId}
          and del_flag = '0'
        limit 1
    </select>

    <select id="selectMaterialByGoodsNum" parameterType="String" resultMap="StockOptionResult">
        select goods_num as code, goods_name as name
        from base_material
        where goods_num = #{goodsNum}
          and del_flag = '0'
        limit 1
    </select>

    <select id="selectMaterialOptions" resultMap="StockOptionResult">
        select goods_num as code, max(goods_name) as name
        from base_material
        where del_flag = '0'
          and org_id = #{orgId}
        group by goods_num
        order by goods_num asc
    </select>

    <select id="selectInventoryLinesForStockCheck" resultMap="InventoryLineOptionResult">
        select i.id as inventory_id,
               i.goods_num,
               i.goods_name,
               m.goods_spec,
               i.batch_no,
               i.warehouse_no,
               i.warehouse_name,
               i.stock_qty as book_base_qty,
               i.assist_stock_qty as book_assist_qty,
               m.base_unit,
               m.assistant_unit
        from warehouse_inventory i
        left join (
            select goods_num,
                   org_id,
                   max(unit) as base_unit,
                   max(assistant_unit) as assistant_unit,
                   max(goods_spec) as goods_spec
            from base_material
            where del_flag = '0'
            group by goods_num, org_id
        ) m on m.goods_num = i.goods_num and m.org_id = i.org_id
        where i.org_id = #{orgId}
          and i.del_flag = '0'
        order by i.id desc
    </select>
</mapper>
