<?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.ShopUserBalanceChangesMapper">

    <resultMap type="cn.iocoder.yudao.module.shop.entity.ShopUserBalanceChanges" id="ShopUserBalanceChangesResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="changeAmount"    column="change_amount"    />
        <result property="changeType"    column="change_type"    />
        <result property="changeReason"    column="change_reason"    />
        <result property="changeTime"    column="change_time"    />
        <result property="realName"    column="real_name"    />
    </resultMap>

    <sql id="selectShopUserBalanceChangesVo">
        select id, shop_user_balance_changes.user_id, change_amount, change_type, change_reason, change_time,
               sys_user.real_name  from
            shop_user_balance_changes left join sys_user on
            shop_user_balance_changes.user_id = sys_user.user_id
    </sql>

    <select id="selectShopUserBalanceChangesList" parameterType="cn.iocoder.yudao.module.shop.entity.ShopUserBalanceChanges" resultMap="ShopUserBalanceChangesResult">
        <include refid="selectShopUserBalanceChangesVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="realName != null "> and sys_user.real_name like concat('%', #{realName}, '%')</if>
            <if test="changeType != null  and changeType != ''"> and change_type = #{changeType}</if>
            <if test="changeReason != null  and changeReason != ''"> and change_reason = #{changeReason}</if>
            <if test="params.beginChangeTime != null and params.beginChangeTime != '' and params.endChangeTime != null and params.endChangeTime != ''"> and change_time between #{params.beginChangeTime} and #{params.endChangeTime}</if>
        </where>
        order by change_time desc
    </select>

    <select id="selectShopUserBalanceChangesById" parameterType="Long" resultMap="ShopUserBalanceChangesResult">
        <include refid="selectShopUserBalanceChangesVo"/>
        where id = #{id}
    </select>

    <insert id="insertShopUserBalanceChanges" parameterType="cn.iocoder.yudao.module.shop.entity.ShopUserBalanceChanges">
        insert into shop_user_balance_changes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="userId != null">user_id,</if>
            <if test="changeAmount != null">change_amount,</if>
            <if test="changeType != null">change_type,</if>
            <if test="changeReason != null">change_reason,</if>
            <if test="changeTime != null">change_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="userId != null">#{userId},</if>
            <if test="changeAmount != null">#{changeAmount},</if>
            <if test="changeType != null">#{changeType},</if>
            <if test="changeReason != null">#{changeReason},</if>
            <if test="changeTime != null">#{changeTime},</if>
         </trim>
    </insert>

    <update id="updateShopUserBalanceChanges" parameterType="cn.iocoder.yudao.module.shop.entity.ShopUserBalanceChanges">
        update shop_user_balance_changes
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="changeAmount != null">change_amount = #{changeAmount},</if>
            <if test="changeType != null">change_type = #{changeType},</if>
            <if test="changeReason != null">change_reason = #{changeReason},</if>
            <if test="changeTime != null">change_time = #{changeTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteShopUserBalanceChangesById" parameterType="Long">
        delete from shop_user_balance_changes where id = #{id}
    </delete>

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