<?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.tuya.mapper.TuyaChannelInfoMapper">

    <resultMap type="TuyaChannelInfo" id="TuyaChannelInfoResult">
        <result property="channelId"    column="channel_id"    />
        <result property="channelName"    column="channel_name"    />
        <result property="channelSiteId"    column="channel_site_id"    />
        <result property="channelLinkUrl"    column="channel_link_url"    />
        <result property="channelLinkImg"    column="channel_link_img"    />
        <result property="sortId"    column="sort_id"    />
        <result property="isLock"    column="is_lock"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="flag"    column="flag"    />
        <result property="remark"    column="remark"    />
        <result property="siteNameCn"    column="site_name_cn"    />
    </resultMap>

    <sql id="selectTuyaChannelInfoVo">
        select channel_id, channel_name, channel_site_id, channel_link_url, channel_link_img,
               tuya_channel_info.sort_id, tuya_channel_info.is_lock, tuya_channel_info.create_time, tuya_channel_info.create_by,
               tuya_channel_info.update_time, tuya_channel_info.update_by, tuya_channel_info.flag, tuya_channel_info.remark,
               site_name_cn
        from tuya_channel_info
            inner join tuya_sites_info on tuya_channel_info.channel_site_id = tuya_sites_info.site_id
    </sql>

    <select id="checkChannelNameUnique" parameterType="TuyaChannelInfo" resultMap="TuyaChannelInfoResult">
        select TOP 1 channel_id, channel_name, channel_site_id, channel_link_url, channel_link_img,
               tuya_channel_info.sort_id, tuya_channel_info.is_lock, tuya_channel_info.create_time, tuya_channel_info.create_by,
               tuya_channel_info.update_time, tuya_channel_info.update_by, tuya_channel_info.flag, tuya_channel_info.remark,
               site_name_cn
        from tuya_channel_info
                 inner join tuya_sites_info on tuya_channel_info.channel_site_id = tuya_sites_info.site_id
        where tuya_sites_info.site_id=#{channelSiteId} and tuya_channel_info.channel_name = #{channelName}
    </select>

    <select id="selectTuyaChannelInfoList" parameterType="TuyaChannelInfo" resultMap="TuyaChannelInfoResult">
        <include refid="selectTuyaChannelInfoVo"/>
        <where>
            <if test="channelName != null  and channelName != ''"> and channel_name like concat('%', #{channelName}, '%')</if>
            <if test="channelSiteId != null "> and channel_site_id = #{channelSiteId}</if>
            <if test="channelLinkUrl != null  and channelLinkUrl != ''"> and channel_link_url = #{channelLinkUrl}</if>
            <if test="channelLinkImg != null  and channelLinkImg != ''"> and channel_link_img = #{channelLinkImg}</if>
            <if test="sortId != null "> and tuya_channel_info.sort_id = #{sortId}</if>
            <if test="isLock != null  and isLock != ''"> and tuya_channel_info.is_lock = #{isLock}</if>
            <if test="flag != null "> and tuya_channel_info.flag = #{flag}</if>
        </where>
    </select>

    <select id="selectTuyaChannelInfoByChannelId" parameterType="Long" resultMap="TuyaChannelInfoResult">
        <include refid="selectTuyaChannelInfoVo"/>
        where channel_id = #{channelId}
    </select>

    <insert id="insertTuyaChannelInfo" parameterType="TuyaChannelInfo">
        insert into tuya_channel_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="channelId != null">channel_id,</if>
            <if test="channelName != null">channel_name,</if>
            <if test="channelSiteId != null">channel_site_id,</if>
            <if test="channelLinkUrl != null">channel_link_url,</if>
            <if test="channelLinkImg != null">channel_link_img,</if>
            <if test="sortId != null">sort_id,</if>
            <if test="isLock != null">is_lock,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="flag != null">flag,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="channelId != null">#{channelId},</if>
            <if test="channelName != null">#{channelName},</if>
            <if test="channelSiteId != null">#{channelSiteId},</if>
            <if test="channelLinkUrl != null">#{channelLinkUrl},</if>
            <if test="channelLinkImg != null">#{channelLinkImg},</if>
            <if test="sortId != null">#{sortId},</if>
            <if test="isLock != null">#{isLock},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="flag != null">#{flag},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTuyaChannelInfo" parameterType="TuyaChannelInfo">
        update tuya_channel_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="channelName != null">channel_name = #{channelName},</if>
            <if test="channelSiteId != null">channel_site_id = #{channelSiteId},</if>
            <if test="channelLinkUrl != null">channel_link_url = #{channelLinkUrl},</if>
            <if test="channelLinkImg != null">channel_link_img = #{channelLinkImg},</if>
            <if test="sortId != null">sort_id = #{sortId},</if>
            <if test="isLock != null">is_lock = #{isLock},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="flag != null">flag = #{flag},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where channel_id = #{channelId}
    </update>

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

    <delete id="deleteTuyaChannelInfoByChannelIds" parameterType="String">
        delete from tuya_channel_info where channel_id in
        <foreach item="channelId" collection="array" open="(" separator="," close=")">
            #{channelId}
        </foreach>
    </delete>
    <select id="hasChildByChannelId" parameterType="Long" resultType="int">
        select count(1) from tuya_category_info
        where  channel_id = #{channelId}
    </select>
</mapper>
