<?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.TuyaCategoryInfoMapper">

    <resultMap type="TuyaCategoryInfo" id="TuyaCategoryInfoResult">
        <result property="categoryId"    column="category_id"    />
        <result property="categoryName"    column="category_name"    />
        <result property="siteId"    column="site_id"    />
        <result property="siteName"    column="site_name"    />
        <result property="channelId"    column="channel_id"    />
        <result property="channelName"    column="channel_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="parentName"    column="parent_name"    />
        <result property="categoryTitle"    column="category_title"    />
        <result property="categoryTitle2"    column="category_title2"    />
        <result property="categoryLinkUrl"    column="category_link_url"    />
        <result property="categoryImgUrl1"    column="category_img_url1"    />
        <result property="categoryImgUrl2"    column="category_img_url2"    />
        <result property="categoryImgUrl3"    column="category_img_url3"    />
        <result property="seoTitle"    column="seo_title"    />
        <result property="seoKeyword"    column="seo_keyword"    />
        <result property="seoDescription"    column="seo_description"    />
        <result property="categoryContent"    column="category_content"    />
        <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"    />
    </resultMap>

    <sql id="selectTuyaCategoryInfoVo">
        select a.category_id, a.category_name, c.site_name_cn site_name, a.channel_id, b.channel_name,
               a.parent_id,aa.category_name parent_name,a.category_title,a.category_title2, a.category_link_url,
               a.category_img_url1,a.category_img_url2, a.category_img_url3,a.seo_title, a.seo_keyword,
               a.seo_description, a.category_content,a.sort_id, a.is_lock, a.create_time,a.create_by,
               a.update_time, a.update_by, a.flag, a.remark,c.site_id
        from tuya_category_info a
            inner  join tuya_channel_info b on a.channel_id = b.channel_id
            inner join tuya_sites_info c on b.channel_site_id = c.site_id
            left  join tuya_category_info aa on a.parent_id = aa.category_id
    </sql>

    <select id="checkCategoryNameUnique" parameterType="TuyaCategoryInfo" resultMap="TuyaCategoryInfoResult">
        select TOP 1 a.category_id, a.category_name, c.site_name_cn site_name, a.channel_id, b.channel_name,
               a.parent_id,aa.category_name parent_name,a.category_title,a.category_title2, a.category_link_url,
               a.category_img_url1,a.category_img_url2, a.category_img_url3,a.seo_title, a.seo_keyword,
               a.seo_description, a.category_content,a.sort_id, a.is_lock, a.create_time,a.create_by,
               a.update_time, a.update_by, a.flag, a.remark,c.site_id
        from tuya_category_info a
                 inner  join tuya_channel_info b on a.channel_id = b.channel_id
                 inner join tuya_sites_info c on b.channel_site_id = c.site_id
                 left  join tuya_category_info aa on a.parent_id = aa.category_id
        where c.site_id=#{siteId} and b.channel_id = #{channelId} and a.parent_id = #{parentId} and a.category_name = #{categoryName}
    </select>

    <select id="selectTuyaCategoryInfoList" parameterType="TuyaCategoryInfo" resultMap="TuyaCategoryInfoResult">
        <include refid="selectTuyaCategoryInfoVo"/>
        <where>
            <if test="categoryName != null  and categoryName != ''"> and a.category_name like concat('%', #{categoryName}, '%')</if>
            <if test="siteName != null  and siteName != ''"> and c.site_name_cn like concat('%', #{siteName}, '%')</if>
            <if test="channelId != null  and channelId != ''"> and a.channel_id = #{channelId}</if>
            <if test="channelName != null  and channelName != ''"> and b.channel_name like concat('%', #{channelName}, '%')</if>
            <if test="parentId != null "> and a.parent_id = #{parentId}</if>
            <if test="categoryTitle != null  and categoryTitle != ''"> and a.category_title like concat('%', #{categoryTitle}, '%')</if>
            <if test="categoryTitle2 != null  and categoryTitle2 != ''"> and a.category_title2 = #{categoryTitle2}</if>
            <if test="seoTitle != null  and seoTitle != ''"> and a.seo_title = #{seoTitle}</if>
            <if test="seoKeyword != null  and seoKeyword != ''"> and a.seo_keyword = #{seoKeyword}</if>
            <if test="seoDescription != null  and seoDescription != ''"> and a.seo_description = #{seoDescription}</if>
            <if test="categoryContent != null  and categoryContent != ''"> and a.category_content = #{categoryContent}</if>
            <if test="isLock != null "> and a.is_lock = #{isLock}</if>
            <if test="flag != null "> and a.flag = #{flag}</if>
            <if test="siteId != null "> and b.channel_site_id = #{siteId}</if>
        </where>
    </select>

    <select id="selectTuyaCategoryInfoByCategoryId" parameterType="Long" resultMap="TuyaCategoryInfoResult">
        <include refid="selectTuyaCategoryInfoVo"/>
        where a.category_id = #{categoryId}
    </select>

    <insert id="insertTuyaCategoryInfo" parameterType="TuyaCategoryInfo">
        insert into tuya_category_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="categoryId != null">category_id,</if>
            <if test="categoryName != null">category_name,</if>
            <if test="siteId != null">site_id,</if>
            <if test="siteName != null">site_name,</if>
            <if test="channelId != null">channel_id,</if>
            <if test="channelName != null">channel_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="parentName != null">parent_name,</if>
            <if test="categoryTitle != null">category_title,</if>
            <if test="categoryTitle2 != null">category_title2,</if>
            <if test="categoryLinkUrl != null">category_link_url,</if>
            <if test="categoryImgUrl1 != null">category_img_url1,</if>
            <if test="categoryImgUrl2 != null">category_img_url2,</if>
            <if test="categoryImgUrl3 != null">category_img_url3,</if>
            <if test="seoTitle != null">seo_title,</if>
            <if test="seoKeyword != null">seo_keyword,</if>
            <if test="seoDescription != null">seo_description,</if>
            <if test="categoryContent != null">category_content,</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="categoryId != null">#{categoryId},</if>
            <if test="categoryName != null">#{categoryName},</if>
            <if test="siteId != null">#{siteId},</if>
            <if test="siteName != null">#{siteName},</if>
            <if test="channelId != null">#{channelId},</if>
            <if test="channelName != null">#{channelName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="parentName != null">#{parentName},</if>
            <if test="categoryTitle != null">#{categoryTitle},</if>
            <if test="categoryTitle2 != null">#{categoryTitle2},</if>
            <if test="categoryLinkUrl != null">#{categoryLinkUrl},</if>
            <if test="categoryImgUrl1 != null">#{categoryImgUrl1},</if>
            <if test="categoryImgUrl2 != null">#{categoryImgUrl2},</if>
            <if test="categoryImgUrl3 != null">#{categoryImgUrl3},</if>
            <if test="seoTitle != null">#{seoTitle},</if>
            <if test="seoKeyword != null">#{seoKeyword},</if>
            <if test="seoDescription != null">#{seoDescription},</if>
            <if test="categoryContent != null">#{categoryContent},</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="updateTuyaCategoryInfo" parameterType="TuyaCategoryInfo">
        update tuya_category_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="categoryName != null">category_name = #{categoryName},</if>
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="siteName != null">site_name = #{siteName},</if>
            <if test="channelId != null">channel_id = #{channelId},</if>
            <if test="channelName != null">channel_name = #{channelName},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="parentName != null">parent_name = #{parentName},</if>
            <if test="categoryTitle != null">category_title = #{categoryTitle},</if>
            <if test="categoryTitle2 != null">category_title2 = #{categoryTitle2},</if>
            <if test="categoryLinkUrl != null">category_link_url = #{categoryLinkUrl},</if>
            <if test="categoryImgUrl1 != null">category_img_url1 = #{categoryImgUrl1},</if>
            <if test="categoryImgUrl2 != null">category_img_url2 = #{categoryImgUrl2},</if>
            <if test="categoryImgUrl3 != null">category_img_url3 = #{categoryImgUrl3},</if>
            <if test="seoTitle != null">seo_title = #{seoTitle},</if>
            <if test="seoKeyword != null">seo_keyword = #{seoKeyword},</if>
            <if test="seoDescription != null">seo_description = #{seoDescription},</if>
            <if test="categoryContent != null">category_content = #{categoryContent},</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 category_id = #{categoryId}
    </update>

    <delete id="deleteTuyaCategoryInfoByCategoryId" parameterType="Long">
        delete from tuya_category_info where category_id = #{categoryId}
    </delete>

    <delete id="deleteTuyaCategoryInfoByCategoryIds" parameterType="String">
        delete from tuya_category_info where category_id in
        <foreach item="categoryId" collection="array" open="(" separator="," close=")">
            #{categoryId}
        </foreach>
    </delete>

    <select id="hasChildByCategoryId" parameterType="Long" resultType="int">
        select count(1) from tuya_category_info
        where  parent_id = #{categoryId}
    </select>
</mapper>
