<?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.warehouse.mapper.StockInfoMapper">
    <resultMap type="StockInfo" id="StockInfoResult">
        <id property="id" column="id"/>
        <result property="areaId" column="area_id"/>
        <result property="productId" column="product_id"/>
        <result property="stockNum" column="stock_num"/>
        <result property="lastCheckTime" column="last_check_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="productCode" column="product_code"/>
        <result property="productName" column="product_name"/>
        <result property="brand" column="brand"/>
        <result property="category" column="category"/>
        <result property="spec" column="spec"/>
        <result property="unit" column="unit"/>
    </resultMap>

    <sql id="selectStockInfoVo">
        select si.id, si.area_id, si.product_id, si.stock_num, si.last_check_time, si.create_time, si.update_time,
               wa.area_code, wa.area_name, pi.product_code, pi.product_name, pi.brand, pi.category, pi.spec, pi.unit
        from stock_info si
        left join warehouse_area wa on wa.id = si.area_id
        left join product_info pi on pi.id = si.product_id
    </sql>

    <select id="selectStockInfoList" parameterType="StockInfo" resultMap="StockInfoResult">
        <include refid="selectStockInfoVo"/>
        <where>
            <if test="areaId != null">and si.area_id = #{areaId}</if>
            <if test="productId != null">and si.product_id = #{productId}</if>
            <if test="areaName != null and areaName != ''">and wa.area_name like concat('%', #{areaName}, '%')</if>
            <if test="productName != null and productName != ''">
                and lower(concat_ws(' ', pi.product_name, pi.brand, pi.category, pi.spec, pi.unit))
                    like concat('%', lower(#{productName}), '%')
            </if>
        </where>
        order by si.id desc
    </select>

    <select id="selectStockInfoById" parameterType="Long" resultMap="StockInfoResult">
        <include refid="selectStockInfoVo"/> where si.id = #{id}
    </select>

    <select id="selectStockInfoByAreaProduct" resultMap="StockInfoResult">
        <include refid="selectStockInfoVo"/> where si.area_id = #{areaId} and si.product_id = #{productId}
    </select>

    <select id="selectStockInfoByAreaProductForUpdate" resultMap="StockInfoResult">
        select id, area_id, product_id, stock_num, last_check_time, create_time, update_time
        from stock_info where area_id = #{areaId} and product_id = #{productId} for update
    </select>

    <insert id="upsertIncreaseStock">
        insert into stock_info(area_id, product_id, stock_num, create_time, update_time)
        values(#{areaId}, #{productId}, #{stockNum}, sysdate(), sysdate())
        on duplicate key update stock_num = stock_num + values(stock_num), update_time = sysdate()
    </insert>

    <update id="deductStock">
        update stock_info
        set stock_num = stock_num - #{stockNum}, update_time = sysdate()
        where area_id = #{areaId} and product_id = #{productId} and stock_num >= #{stockNum}
    </update>

    <insert id="upsertSetStock">
        insert into stock_info(area_id, product_id, stock_num, last_check_time, create_time, update_time)
        values(#{areaId}, #{productId}, #{stockNum}, sysdate(), sysdate(), sysdate())
        on duplicate key update stock_num = values(stock_num), last_check_time = sysdate(), update_time = sysdate()
    </insert>
</mapper>
