<?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.channel.mapper.ChannelMapper">

    <resultMap type="ChannelVO" id="SmChannelResult" 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"/>
        <result property="channelConfig" column="channel_config" typeHandler="com.ruoyi.ss.channel.mapper.typehandler.ChannelConfigJsonTypeHandler"/>
        <result property="appIds" column="app_ids" typeHandler="com.ruoyi.system.mapper.typehandler.LongSplitListTypeHandler"/>
    </resultMap>

    <sql id="selectSmChannelVo">
        select
            sc.channel_id,
            sc.name,
            sc.enabled,
            sc.service_rate,
            sc.cost_rate,
            sc.service_type,
            sc.picture,
            sc.platform,
            sc.api_type,
            sc.type,
            sc.channel_config,
            sc.deleted,
            sc.create_time,
            sc.app_ids
        from sm_channel sc
    </sql>

    <sql id="searchCondition">
        <if test="query.name != null  and query.name != ''"> and sc.name like concat('%', #{query.name}, '%')</if>
        <if test="query.enabled != null "> and sc.enabled = #{query.enabled}</if>
        <if test="query.serviceType != null  and query.serviceType != ''"> and sc.service_type = #{query.serviceType}</if>
        <if test="query.platform != null  and query.platform != ''"> and sc.platform = #{query.platform}</if>
        <if test="query.apiType != null  and query.apiType != ''"> and sc.api_type = #{query.apiType}</if>
        <if test="query.type != null  and query.type != ''"> and sc.type = #{query.type}</if>
        <if test="query.deleted == null">and sc.deleted = false</if>
        <if test="query.deleted != null">and sc.deleted = #{query.deleted}</if>
        <if test="query.appId != null">
            and find_in_set(#{query.appId}, app_ids)
        </if>
        <if test="query.channelIds != null and query.channelIds.size() > 0 ">
            and sc.channel_id in
            <foreach item="item" collection="query.channelIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectSmChannelList" parameterType="ChannelQuery" resultMap="SmChannelResult">
        <include refid="selectSmChannelVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectSmChannelByChannelId" parameterType="Long" resultMap="SmChannelResult">
        <include refid="selectSmChannelVo"/>
        where sc.channel_id = #{channelId}
    </select>

    <insert id="insertSmChannel" parameterType="Channel" useGeneratedKeys="true" keyProperty="channelId">
        insert into sm_channel
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">`name`,</if>
            <if test="enabled != null">enabled,</if>
            <if test="serviceRate != null">service_rate,</if>
            <if test="costRate != null">cost_rate,</if>
            <if test="serviceType != null">service_type,</if>
            <if test="picture != null">picture,</if>
            <if test="platform != null">platform,</if>
            <if test="apiType != null">api_type,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="channelConfig != null">channel_config,</if>
            <if test="createTime != null">create_time,</if>
            <if test="appIds != null">app_ids,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="enabled != null">#{enabled},</if>
            <if test="serviceRate != null">#{serviceRate},</if>
            <if test="costRate != null">#{costRate},</if>
            <if test="serviceType != null">#{serviceType},</if>
            <if test="picture != null">#{picture},</if>
            <if test="platform != null">#{platform},</if>
            <if test="apiType != null">#{apiType},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="channelConfig != null">#{channelConfig,typeHandler=com.ruoyi.ss.channel.mapper.typehandler.ChannelConfigJsonTypeHandler},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="appIds != null">#{appIds,typeHandler=com.ruoyi.system.mapper.typehandler.LongSplitListTypeHandler},</if>
         </trim>
    </insert>

    <update id="updateSmChannel" parameterType="Channel">
        update sm_channel
        <trim prefix="SET" suffixOverrides=",">
            <if test="data.name != null">name = #{data.name},</if>
            <if test="data.enabled != null">enabled = #{data.enabled},</if>
            <if test="data.serviceRate != null">service_rate = #{data.serviceRate},</if>
            <if test="data.costRate != null">cost_rate = #{data.costRate},</if>
            <if test="data.serviceType != null">service_type = #{data.serviceType},</if>
            <if test="data.picture != null">picture = #{data.picture},</if>
            <if test="data.platform != null">platform = #{data.platform},</if>
            <if test="data.apiType != null">api_type = #{data.apiType},</if>
            <if test="data.type != null and data.type != ''">type = #{data.type},</if>
            <if test="data.channelConfig != null">channel_config = #{data.channelConfig,typeHandler=com.ruoyi.ss.channel.mapper.typehandler.ChannelConfigJsonTypeHandler},</if>
            <if test="data.createTime != null">create_time = #{data.createTime},</if>
            <if test="data.appIds != null">app_ids = #{data.appIds,typeHandler=com.ruoyi.system.mapper.typehandler.LongSplitListTypeHandler},</if>
        </trim>
        where channel_id = #{data.channelId}
    </update>

    <update id="logicDel">
        update sm_channel
        set deleted = true
        where channel_id in
        <foreach item="channelId" collection="ids" open="(" separator="," close=")">
            #{channelId}
        </foreach>
        and deleted = false
    </update>

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

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