<?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.iocoder.yudao.module.shop.mapper.UserBalanceMapper">

    <sql id="selectShopUserBalanceVo">
        select id, user_id, balance,all_balance from shop_user_balance
    </sql>

    <select id="selectShopUserBalanceList" parameterType="cn.iocoder.yudao.module.shop.entity.UserBalance" resultType="cn.iocoder.yudao.module.shop.entity.UserBalance">
            SELECT
            sub.id, sub.user_id,  u.real_name,u.phonenumber,sub.all_balance,sub.balance
            FROM
                shop_user_balance sub
                LEFT JOIN sys_user u on u.user_id = sub.user_id
            WHERE 1=1
            <if test="realName!=null">
                and  u.real_name like CONCAT('%',#{realName},'%')
            </if>
            <if test="phonenumber!=null">
                and  u.phonenumber like CONCAT('%',#{phonenumber},'%')
            </if>
            order by sub.balance desc
    </select>

    <select id="selectShopUserBalanceById" parameterType="Long" resultType="cn.iocoder.yudao.module.shop.entity.UserBalance">
        <include refid="selectShopUserBalanceVo"/>
        where id = #{id}
    </select>

    <insert id="insertShopUserBalance" parameterType="cn.iocoder.yudao.module.shop.entity.UserBalance" useGeneratedKeys="true" keyProperty="id">
        insert into shop_user_balance
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="balance != null">balance,</if>
            <if test="allBalance != null">all_balance,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="balance != null">#{balance},</if>
            <if test="allBalance != null">#{allBalance},</if>
         </trim>
    </insert>

    <update id="updateShopUserBalance" parameterType="cn.iocoder.yudao.module.shop.entity.UserBalance">
        update shop_user_balance
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="balance != null">balance = #{balance},</if>
            <if test="allBalance != null">all_balance = #{allBalance},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteShopUserBalanceById" parameterType="Long">
        delete from shop_user_balance where id = #{id}
    </delete>

    <delete id="deleteShopUserBalanceByIds" parameterType="String">
        delete from shop_user_balance where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectShopUserBalanceByUserId" parameterType="Long" resultType="cn.iocoder.yudao.module.shop.entity.UserBalance">
        <include refid="selectShopUserBalanceVo"/>
        where user_id = #{user_id}
    </select>
    <update id="frozenBalance" >
        UPDATE
        shop_user_balance
        SET balance = balance - #{frozenBalance},frozen_balance = frozen_balance + #{frozenBalance}
        where user_id = #{userId}

    </update>
    <update id="thawBalance" >
        UPDATE
        shop_user_balance
        SET balance = balance + #{frozenBalance},frozen_balance = frozen_balance - #{frozenBalance}
        where user_id = #{userId}
    </update>
    <update id="subtractFrozenBalance" >
        UPDATE
        shop_user_balance
        SET frozen_balance = frozen_balance - #{frozenBalance}
        where user_id = #{userId}
    </update>
    <update id="subtractBalance" >
        UPDATE
        shop_user_balance
        SET balance = balance - #{balance}
        where user_id = #{userId}
    </update>

    <update id="addBalance" >
        UPDATE
        shop_user_balance
        SET balance = balance + #{balance},all_balance = all_balance + #{balance}
        where user_id = #{userId}
    </update>
    <update id="rewardBalance" >
        UPDATE
        shop_user_balance
        SET balance = balance + #{balance}
        where user_id = #{userId}
    </update>
    <update id="refundBalance" >
        UPDATE
        shop_user_balance
        SET balance = balance + #{balance}
        where user_id = #{userId}
    </update>
</mapper>
