<?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.xingxi.mq.server.order.mapper.MqProdInventoryMapper">

    <select id="selectProdInventoryLock" resultType="ProdInventory">
        SELECT tpi.*
        FROM t_prod_inventory tpi
        WHERE tpi.delFlag = '0'
          AND tpi.mercId = #{mercId}
          AND tpi.prodId = #{prodId}
          AND tpi.prodAttrId = #{prodAttrId}
        LIMIT 1
        FOR UPDATE
    </select>

    <update id="updateSubtractProdStock" parameterType="ProdInventory">
        update t_prod_inventory
        <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 prodInventoryId = #{prodInventoryId} and availQty - #{availQty} >= 0
    </update>

    <update id="updatePlusProdStock" parameterType="ProdInventory">
        update t_prod_inventory
        <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 prodInventoryId = #{prodInventoryId}
    </update>

</mapper>