<?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.channelWithdraw.mapper.ChannelWithdrawMapper">

    <resultMap type="ChannelWithdrawVO" id="ChannelWithdrawResult" autoMapping="true">
        <result property="serviceRate" column="service_rate" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullDecimalTypeHandler"/>
        <result property="costRate" column="cost_rate" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullDecimalTypeHandler"/>
    </resultMap>

    <sql id="selectChannelWithdrawVo">
        select
            scw.channel_id,
            scw.name,
            scw.account_type,
            scw.service_type,
            scw.service_rate,
            scw.enabled,
            scw.cost_rate,
            scw.picture,
            scw.min_amount,
            scw.max_amount
        from ss_channel_withdraw scw
    </sql>

    <sql id="searchCondition">
        <if test="query.channelId != null "> and scw.channel_id = #{query.channelId}</if>
        <if test="query.name != null  and query.name != ''"> and scw.name like concat('%', #{query.name}, '%')</if>
        <if test="query.accountType != null  and query.accountType != ''"> and scw.account_type = #{query.accountType}</if>
        <if test="query.serviceType != null  and query.serviceType != ''"> and scw.service_type = #{query.serviceType}</if>
        <if test="query.enabled != null "> and scw.enabled = #{query.enabled}</if>
        <if test="query.accountTypes != null and query.accountTypes.size() > 0">
            and scw.account_type in
            <foreach item="item" collection="query.accountTypes" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectChannelWithdrawList" parameterType="ChannelWithdrawQuery" resultMap="ChannelWithdrawResult">
        <include refid="selectChannelWithdrawVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectChannelWithdrawByChannelId" parameterType="Long" resultMap="ChannelWithdrawResult">
        <include refid="selectChannelWithdrawVo"/>
        where channel_id = #{channelId}
    </select>

    <insert id="insertChannelWithdraw" parameterType="ChannelWithdraw" useGeneratedKeys="true" keyProperty="channelId">
        insert into ss_channel_withdraw
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="accountType != null and accountType != ''">account_type,</if>
            <if test="serviceType != null and serviceType != ''">service_type,</if>
            <if test="serviceRate != null">service_rate,</if>
            <if test="enabled != null">enabled,</if>
            <if test="costRate != null">cost_rate,</if>
            <if test="picture != null">picture,</if>
            <if test="minAmount != null">min_amount,</if>
            <if test="maxAmount != null">max_amount,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="accountType != null and accountType != ''">#{accountType},</if>
            <if test="serviceType != null and serviceType != ''">#{serviceType},</if>
            <if test="serviceRate != null">#{serviceRate},</if>
            <if test="enabled != null">#{enabled},</if>
            <if test="costRate != null">#{costRate},</if>
            <if test="picture != null">#{picture},</if>
            <if test="minAmount != null">#{minAmount},</if>
            <if test="maxAmount != null">#{maxAmount},</if>
         </trim>
    </insert>

    <update id="updateChannelWithdraw" parameterType="ChannelWithdraw">
        update ss_channel_withdraw
        <trim prefix="SET" suffixOverrides=",">
            <if test="data.name != null and data.name != ''">name = #{data.name},</if>
            <if test="data.accountType != null and data.accountType != ''">account_type = #{data.accountType},</if>
            <if test="data.serviceType != null and data.serviceType != ''">service_type = #{data.serviceType},</if>
            <if test="data.serviceRate != null">service_rate = #{data.serviceRate},</if>
            <if test="data.enabled != null">enabled = #{data.enabled},</if>
            <if test="data.costRate != null">cost_rate = #{data.costRate},</if>
            <if test="data.picture != null">picture = #{data.picture},</if>
            <if test="data.minAmount != null">min_amount = #{data.minAmount},</if>
            <if test="data.maxAmount != null">max_amount = #{data.maxAmount},</if>
        </trim>
        where channel_id = #{data.channelId}
    </update>

    <delete id="deleteChannelWithdrawByChannelId" parameterType="Long">
        delete from ss_channel_withdraw where channel_id = #{channelId}
    </delete>

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