<?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.changeBalance.mapper.ChangeBalanceMapper">

    <resultMap type="ChangeBalanceVO" id="RlChangeBalanceResult" autoMapping="true"/>

    <sql id="selectRlChangeBalanceVo">
        select cb.change_id, cb.order_no, cb.out_trade_no, cb.type, cb.bus_type, cb.before_balance, cb.after_balance,cb.create_by,
               cb.amount, cb.owner_name, cb.owner_id, cb.owner_phone, cb.owner_type, cb.create_time, cb.reason from ss_change_balance cb
		left join ss_user u on u.user_id = cb.owner_id
    </sql>

    <sql id="searchCondition">
        <if test="orderNo != null  and orderNo != ''"> and cb.order_no like concat('%', #{orderNo}, '%')</if>
        <if test="outTradeNo != null  and outTradeNo != ''"> and cb.out_trade_no = #{outTradeNo}</if>
        <if test="type != null  and type != ''"> and cb.type = #{type}</if>
        <if test="busType != null  and busType != ''"> and cb.bus_type = #{busType}</if>
        <if test="beforeBalance != null "> and cb.before_balance = #{beforeBalance}</if>
        <if test="afterBalance != null "> and cb.after_balance = #{afterBalance}</if>
        <if test="amount != null "> and cb.amount = #{amount}</if>
        <if test="ownerName != null  and ownerName != ''"> and cb.owner_name like concat('%', #{ownerName}, '%')</if>
        <if test="ownerId != null "> and cb.owner_id = #{ownerId}</if>
        <if test="ownerPhone != null  and ownerPhone != ''"> and cb.owner_phone like concat('%', #{ownerPhone}, '%')</if>
        <if test="ownerType != null  and ownerType != ''"> and cb.owner_type = #{ownerType}</if>
        <if test="reason != null  and reason != ''"> and cb.reason = #{reason}</if>
        <if test="createTime != null "> and date(cb.create_time) = #{createTime}</if>
    </sql>

    <select id="selectRlChangeBalanceList" parameterType="ChangeBalance" resultMap="RlChangeBalanceResult">
        <include refid="selectRlChangeBalanceVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        ${params.dataScope}
        order by create_time desc
    </select>

    <select id="selectRlChangeBalanceByChangeId" parameterType="Long" resultMap="RlChangeBalanceResult">
        <include refid="selectRlChangeBalanceVo"/>
        where change_id = #{changeId}
    </select>

    <select id="lastChangeBalance" resultMap="RlChangeBalanceResult">
        <include refid="selectRlChangeBalanceVo"/>
        WHERE owner_id = #{ownerId}
        ORDER BY create_time DESC
            LIMIT 1
    </select>

    <select id="selectFee" resultType="java.math.BigDecimal">
        select COALESCE(SUM(amount), 0) amount from ss_change_balance cb
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <insert id="insertRlChangeBalance" parameterType="ChangeBalance" useGeneratedKeys="true" keyProperty="changeId">
        insert into ss_change_balance
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">order_no,</if>
            <if test="outTradeNo != null">out_trade_no,</if>
            <if test="type != null">type,</if>
            <if test="busType != null">bus_type,</if>
            <if test="beforeBalance != null">before_balance,</if>
            <if test="afterBalance != null">after_balance,</if>
            <if test="amount != null">amount,</if>
            <if test="ownerName != null">owner_name,</if>
            <if test="ownerId != null">owner_id,</if>
            <if test="ownerPhone != null">owner_phone,</if>
            <if test="ownerType != null">owner_type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="reason != null">reason,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">#{orderNo},</if>
            <if test="outTradeNo != null">#{outTradeNo},</if>
            <if test="type != null">#{type},</if>
            <if test="busType != null">#{busType},</if>
            <if test="beforeBalance != null">#{beforeBalance},</if>
            <if test="afterBalance != null">#{afterBalance},</if>
            <if test="amount != null">#{amount},</if>
            <if test="ownerName != null">#{ownerName},</if>
            <if test="ownerId != null">#{ownerId},</if>
            <if test="ownerPhone != null">#{ownerPhone},</if>
            <if test="ownerType != null">#{ownerType},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="reason != null">#{reason},</if>
         </trim>
    </insert>

    <update id="updateRlChangeBalance" parameterType="ChangeBalance">
        update ss_change_balance
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="outTradeNo != null">out_trade_no = #{outTradeNo},</if>
            <if test="type != null">type = #{type},</if>
            <if test="busType != null">bus_type = #{busType},</if>
            <if test="beforeBalance != null">before_balance = #{beforeBalance},</if>
            <if test="afterBalance != null">after_balance = #{afterBalance},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="ownerName != null">owner_name = #{ownerName},</if>
            <if test="ownerId != null">owner_id = #{ownerId},</if>
            <if test="ownerPhone != null">owner_phone = #{ownerPhone},</if>
            <if test="ownerType != null">owner_type = #{ownerType},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="reason != null">reason = #{reason},</if>
        </trim>
        where change_id = #{changeId}
    </update>

    <delete id="deleteRlChangeBalanceByChangeId" parameterType="Long">
        delete from ss_change_balance where change_id = #{changeId}
    </delete>

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