<?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="cn.com.xingxi.store.data.mapper.WxaStoreStockMapper">

    <select id="selectProdStockLock" resultType="ProdStock">
        SELECT ps.*
        FROM t_prod_stock ps
        WHERE ps.delFlag = '0'
          AND ps.vendorId = #{vendorId}
          AND ps.prodId = #{prodId}
          AND ps.pprId = #{pprId}
        ORDER BY ps.availQty DESC LIMIT 1
        FOR UPDATE
    </select>

    <update id="updateSubProdStock" parameterType="ProdStock">
        update t_prod_stock
        <trim prefix="SET" suffixOverrides=",">
            <if test="availQty != null">availQty = availQty - #{availQty},</if>
            <if test="updateUser != null and updateUser != ''">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where stockId = #{stockId} and availQty - #{availQty} >= 0
    </update>

    <update id="updateAddProdStock" parameterType="ProdStock">
        update t_prod_stock
        <trim prefix="SET" suffixOverrides=",">
            <if test="availQty != null">availQty = availQty + #{availQty},</if>
            <if test="updateUser != null and updateUser != ''">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where stockId = #{stockId}
    </update>

</mapper>
