<?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.ss.recordBalance.mapper.RecordBalanceMapper">

    <resultMap type="RecordBalanceVO" id="RecordBalanceResult" autoMapping="true"/>

    <sql id="selectRecordBalanceVo">
        select
            srb.id,
            srb.user_id,
            srb.before_balance,
            srb.after_balance,
            srb.reason,
            srb.create_time,
            srb.amount,
            srb.bst_type,
            srb.bst_id,
            if(su.is_real, su.real_name, su.user_name) as user_name
        from ss_record_balance srb
            left join sm_user su on su.user_id = srb.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and srb.id = #{query.id}</if>
        <if test="query.userId != null "> and srb.user_id = #{query.userId}</if>
        <if test="query.reason != null  and query.reason != ''"> and srb.reason like concat('%', #{query.reason}, '%')</if>
        <if test="query.userName != null  and query.userName != ''"> and if(su.is_real, su.real_name, su.user_name) like concat('%', #{query.userName}, '%')</if>
        <if test="query.bstType != null  and query.bstType != ''"> and srb.bst_type = #{query.bstType}</if>
        <if test="query.bstId != null "> and srb.bst_id = #{query.bstId}</if>
    </sql>

    <select id="selectRecordBalanceList" parameterType="RecordBalanceQuery" resultMap="RecordBalanceResult">
        <include refid="selectRecordBalanceVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectRecordBalanceById" parameterType="Long" resultMap="RecordBalanceResult">
        <include refid="selectRecordBalanceVo"/>
        where srb.id = #{id}
    </select>

    <select id="selectOne" resultMap="RecordBalanceResult">
        <include refid="selectRecordBalanceVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        limit 1
    </select>

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

    <update id="updateRecordBalance" parameterType="RecordBalance">
        update ss_record_balance
        <trim prefix="SET" suffixOverrides=",">
            <if test="data.userId != null">user_id = #{data.userId},</if>
            <if test="data.beforeBalance != null">before_balance = #{data.beforeBalance},</if>
            <if test="data.afterBalance != null">after_balance = #{data.afterBalance},</if>
            <if test="data.reason != null and data.reason != ''">reason = #{data.reason},</if>
            <if test="data.createTime != null">create_time = #{data.createTime},</if>
            <if test="data.amount != null">amount = #{data.amount},</if>
            <if test="data.bstType != null">bst_type = #{data.bstType},</if>
            <if test="data.bstId != null">bst_id = #{data.bstId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRecordBalanceById" parameterType="Long">
        delete from ss_record_balance where id = #{id}
    </delete>

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