<?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.bst.balanceLog.mapper.BalanceLogMapper">

    <resultMap type="BalanceLogVO" id="BalanceLogResult" autoMapping="true"/>

    <sql id="selectBalanceLogVo">
        select
            bal.id,
            bal.user_id,
            bal.amount,
            bal.before_balance,
            bal.after_balance,
            bal.bst_type,
            bal.bst_id,
            bal.reason,
            bal.create_time,
            su.nick_name as user_name
        from bst_balance_log bal
            left join sys_user su on su.user_id = bal.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and bal.id = #{query.id}</if>
        <if test="query.userId != null "> and bal.user_id = #{query.userId}</if>
        <if test="query.bstType != null  and query.bstType != ''"> and bal.bst_type = #{query.bstType}</if>
        <if test="query.bstId != null "> and bal.bst_id = #{query.bstId}</if>
        <if test="query.reason != null  and query.reason != ''"> and bal.reason like concat('%', #{query.reason}, '%')</if>
        <if test="query.userName != null  and query.userName != ''"> and su.nick_name like concat('%', #{query.userName}, '%')</if>
        ${@com.ruoyi.framework.util.DataScopeUtil@dataScope(
            null,
            "bal.user_id",
            null,
            null,
            query.scope
        )}
        ${query.params.dataScope}
    </sql>

    <select id="selectBalanceLogList" parameterType="BalanceLogQuery" resultMap="BalanceLogResult">
        <include refid="selectBalanceLogVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectBalanceLogById" parameterType="Long" resultMap="BalanceLogResult">
        <include refid="selectBalanceLogVo"/>
        where id = #{id}
    </select>

    <insert id="insertBalanceLog" parameterType="BalanceLog" useGeneratedKeys="true" keyProperty="id">
        insert into bst_balance_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="amount != null">amount,</if>
            <if test="beforeBalance != null">before_balance,</if>
            <if test="afterBalance != null">after_balance,</if>
            <if test="bstType != null">bst_type,</if>
            <if test="bstId != null">bst_id,</if>
            <if test="reason != null">reason,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="beforeBalance != null">#{beforeBalance},</if>
            <if test="afterBalance != null">#{afterBalance},</if>
            <if test="bstType != null">#{bstType},</if>
            <if test="bstId != null">#{bstId},</if>
            <if test="reason != null">#{reason},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateBalanceLog" parameterType="BalanceLog">
        update bst_balance_log
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.amount != null">amount = #{data.amount},</if>
        <if test="data.beforeBalance != null">before_balance = #{data.beforeBalance},</if>
        <if test="data.afterBalance != null">after_balance = #{data.afterBalance},</if>
        <if test="data.bstType != null">bst_type = #{data.bstType},</if>
        <if test="data.bstId != null">bst_id = #{data.bstId},</if>
        <if test="data.reason != null">reason = #{data.reason},</if> 
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
    </sql>

    <delete id="deleteBalanceLogById" parameterType="Long">
        delete from bst_balance_log where id = #{id}
    </delete>

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