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

    <resultMap type="ChannelVO" id="SmChannelResult" autoMapping="true">
        <result property="costRate" column="cost_rate" typeHandler="com.ruoyi.common.mybatis.typehandler.NonNullDecimalTypeHandler"/>
        <result property="channelConfig" column="channel_config" typeHandler="com.ruoyi.bst.channel.mapper.typehandler.ChannelConfigJsonTypeHandler"/>
        <result property="appIds" column="app_ids" typeHandler="com.ruoyi.common.mybatis.typehandler.LongSplitListTypeHandler"/>
    </resultMap>

    <sql id="selectSmChannelVo">
        select
            bc.channel_id,
            bc.name,
            bc.enabled,
            bc.cost_rate,
            bc.picture,
            bc.api_type,
            bc.type,
            bc.channel_config,
            bc.deleted,
            bc.create_time,
            bc.app_ids
        from bst_channel bc
    </sql>

    <sql id="searchCondition">
        <if test="query.name != null  and query.name != ''"> and bc.name like concat('%', #{query.name}, '%')</if>
        <if test="query.enabled != null "> and bc.enabled = #{query.enabled}</if>
        <if test="query.apiType != null  and query.apiType != ''"> and bc.api_type = #{query.apiType}</if>
        <if test="query.type != null  and query.type != ''"> and bc.type = #{query.type}</if>
        <if test="query.deleted == null">and bc.deleted = false</if>
        <if test="query.deleted != null">and bc.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 bc.channel_id in
            <foreach item="item" collection="query.channelIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>

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

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

    <insert id="insertChannel" parameterType="Channel" useGeneratedKeys="true" keyProperty="channelId">
        insert into bst_channel
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">`name`,</if>
            <if test="enabled != null">enabled,</if>
            <if test="costRate != null">cost_rate,</if>
            <if test="picture != null">picture,</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="costRate != null">#{costRate},</if>
            <if test="picture != null">#{picture},</if>
            <if test="apiType != null">#{apiType},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="channelConfig != null">#{channelConfig,typeHandler=com.ruoyi.bst.channel.mapper.typehandler.ChannelConfigJsonTypeHandler},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="appIds != null">#{appIds,typeHandler=com.ruoyi.common.mybatis.typehandler.LongSplitListTypeHandler},</if>
         </trim>
    </insert>

    <update id="updateChannel" parameterType="Channel">
        update bst_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.costRate != null">cost_rate = #{data.costRate},</if>
            <if test="data.picture != null">picture = #{data.picture},</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.bst.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.common.mybatis.typehandler.LongSplitListTypeHandler},</if>
        </trim>
        where channel_id = #{data.channelId}
    </update>

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

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

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

    <!-- selectNameList -->

    <select id="selectNameList">
        select
            bc.channel_id,
            bc.name
        from bst_channel bc
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

</mapper>
