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

    <resultMap type="TuyaSitesInfo" id="TuyaSitesInfoResult">
        <result property="siteId"    column="site_id"    />
        <result property="siteNameCn"    column="site_name_cn"    />
        <result property="siteNameEn"    column="site_name_en"    />
        <result property="domainName"    column="domain_name"    />
        <result property="siteLogo1"    column="site_logo1"    />
        <result property="siteLogo2"    column="site_logo2"    />
        <result property="siteLogo3"    column="site_logo3"    />
        <result property="homeTitle"    column="home_title"    />
        <result property="siteProvider"    column="site_provider"    />
        <result property="siteCopyright"    column="site_copyright"    />
        <result property="siteCompany"    column="site_company"    />
        <result property="postalAddress"    column="postal_address"    />
        <result property="siteTel"    column="site_tel"    />
        <result property="siteFax"    column="site_fax"    />
        <result property="siteEmail"    column="site_email"    />
        <result property="seoTitle"    column="seo_title"    />
        <result property="seoKeyword"    column="seo_keyword"    />
        <result property="seoDescription"    column="seo_description"    />
        <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="powerUnit"    column="power_unit"    />
        <result property="percentageFee"    column="percentage_fee"    />
        <result property="percentageBrokerage"    column="percentage_brokerage"    />
    </resultMap>

    <sql id="selectTuyaSitesInfoVo">
        select site_id, site_name_cn, site_name_en, domain_name, site_logo1, site_logo2, site_logo3,
               home_title, site_provider, site_copyright, site_company, postal_address, site_tel, site_fax,
               site_email, seo_title, seo_keyword, seo_description, sort_id, is_lock, create_time, create_by,
               update_time, update_by, flag, remark, power_unit, percentage_fee, percentage_brokerage from tuya_sites_info
    </sql>

    <select id="checkSiteNameUnique" parameterType="String" resultMap="TuyaSitesInfoResult">
        select TOP 1 site_id, site_name_cn, site_name_en, domain_name, site_logo1, site_logo2, site_logo3, home_title,
               site_provider, site_copyright, site_company, postal_address, site_tel, site_fax, site_email, seo_title,
               seo_keyword, seo_description, sort_id, is_lock, create_time, create_by, update_time, update_by, flag,
               remark, power_unit, percentage_fee, percentage_brokerage
        from tuya_sites_info
        where site_name_cn=#{siteNameCn}
    </select>

    <select id="checkSiteNameEnUnique" parameterType="String" resultMap="TuyaSitesInfoResult">
        select TOP 1 site_id, site_name_cn, site_name_en, domain_name, site_logo1, site_logo2, site_logo3, home_title,
               site_provider, site_copyright, site_company, postal_address, site_tel, site_fax, site_email, seo_title,
               seo_keyword, seo_description, sort_id, is_lock, create_time, create_by, update_time, update_by, flag,
               remark, power_unit, percentage_fee, percentage_brokerage
        from tuya_sites_info
        where site_name_en=#{siteNameEn}
    </select>

    <select id="selectTuyaSitesInfoList" parameterType="TuyaSitesInfo" resultMap="TuyaSitesInfoResult">
        <include refid="selectTuyaSitesInfoVo"/>
        <where>
            <if test="siteNameCn != null  and siteNameCn != ''"> and site_name_cn = #{siteNameCn}</if>
            <if test="siteNameEn != null  and siteNameEn != ''"> and site_name_en = #{siteNameEn}</if>
            <if test="domainName != null  and domainName != ''"> and domain_name like concat('%', #{domainName}, '%')</if>
            <if test="siteLogo1 != null  and siteLogo1 != ''"> and site_logo1 = #{siteLogo1}</if>
            <if test="siteLogo2 != null  and siteLogo2 != ''"> and site_logo2 = #{siteLogo2}</if>
            <if test="siteLogo3 != null  and siteLogo3 != ''"> and site_logo3 = #{siteLogo3}</if>
            <if test="homeTitle != null  and homeTitle != ''"> and home_title = #{homeTitle}</if>
            <if test="siteProvider != null  and siteProvider != ''"> and site_provider = #{siteProvider}</if>
            <if test="siteCopyright != null  and siteCopyright != ''"> and site_copyright = #{siteCopyright}</if>
            <if test="siteCompany != null  and siteCompany != ''"> and site_company = #{siteCompany}</if>
            <if test="postalAddress != null  and postalAddress != ''"> and postal_address = #{postalAddress}</if>
            <if test="siteTel != null  and siteTel != ''"> and site_tel = #{siteTel}</if>
            <if test="siteFax != null  and siteFax != ''"> and site_fax = #{siteFax}</if>
            <if test="siteEmail != null  and siteEmail != ''"> and site_email = #{siteEmail}</if>
            <if test="seoTitle != null  and seoTitle != ''"> and seo_title = #{seoTitle}</if>
            <if test="seoKeyword != null  and seoKeyword != ''"> and seo_keyword = #{seoKeyword}</if>
            <if test="seoDescription != null  and seoDescription != ''"> and seo_description = #{seoDescription}</if>
            <if test="sortId != null "> and sort_id = #{sortId}</if>
            and flag = 0
        </where>
    </select>

    <select id="sitesInfoSelect" parameterType="TuyaSitesInfo" resultMap="TuyaSitesInfoResult">
        <include refid="selectTuyaSitesInfoVo"/>
        <where>
            <if test="siteNameCn != null  and siteNameCn != ''"> and site_name_cn = #{siteNameCn}</if>
            <if test="siteNameEn != null  and siteNameEn != ''"> and site_name_en = #{siteNameEn}</if>
            <if test="domainName != null  and domainName != ''"> and domain_name like concat('%', #{domainName}, '%')</if>
            <if test="siteLogo1 != null  and siteLogo1 != ''"> and site_logo1 = #{siteLogo1}</if>
            <if test="siteLogo2 != null  and siteLogo2 != ''"> and site_logo2 = #{siteLogo2}</if>
            <if test="siteLogo3 != null  and siteLogo3 != ''"> and site_logo3 = #{siteLogo3}</if>
            <if test="homeTitle != null  and homeTitle != ''"> and home_title = #{homeTitle}</if>
            <if test="siteProvider != null  and siteProvider != ''"> and site_provider = #{siteProvider}</if>
            <if test="siteCopyright != null  and siteCopyright != ''"> and site_copyright = #{siteCopyright}</if>
            <if test="siteCompany != null  and siteCompany != ''"> and site_company = #{siteCompany}</if>
            <if test="postalAddress != null  and postalAddress != ''"> and postal_address = #{postalAddress}</if>
            <if test="siteTel != null  and siteTel != ''"> and site_tel = #{siteTel}</if>
            <if test="siteFax != null  and siteFax != ''"> and site_fax = #{siteFax}</if>
            <if test="siteEmail != null  and siteEmail != ''"> and site_email = #{siteEmail}</if>
            <if test="seoTitle != null  and seoTitle != ''"> and seo_title = #{seoTitle}</if>
            <if test="seoKeyword != null  and seoKeyword != ''"> and seo_keyword = #{seoKeyword}</if>
            <if test="seoDescription != null  and seoDescription != ''"> and seo_description = #{seoDescription}</if>
            <if test="sortId != null "> and sort_id = #{sortId}</if>
             and is_lock = 0
            and flag = 0
        </where>
    </select>

    <select id="selectTuyaSitesInfoBySiteId" parameterType="Long" resultMap="TuyaSitesInfoResult">
        <include refid="selectTuyaSitesInfoVo"/>
        where site_id = #{siteId}
    </select>

    <insert id="insertTuyaSitesInfo" parameterType="TuyaSitesInfo">
        insert into tuya_sites_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="siteId != null">site_id,</if>
            <if test="siteNameCn != null">site_name_cn,</if>
            <if test="siteNameEn != null">site_name_en,</if>
            <if test="domainName != null">domain_name,</if>
            <if test="siteLogo1 != null">site_logo1,</if>
            <if test="siteLogo2 != null">site_logo2,</if>
            <if test="siteLogo3 != null">site_logo3,</if>
            <if test="homeTitle != null">home_title,</if>
            <if test="siteProvider != null">site_provider,</if>
            <if test="siteCopyright != null">site_copyright,</if>
            <if test="siteCompany != null">site_company,</if>
            <if test="postalAddress != null">postal_address,</if>
            <if test="siteTel != null">site_tel,</if>
            <if test="siteFax != null">site_fax,</if>
            <if test="siteEmail != null">site_email,</if>
            <if test="seoTitle != null">seo_title,</if>
            <if test="seoKeyword != null">seo_keyword,</if>
            <if test="seoDescription != null">seo_description,</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>
            <if test="powerUnit != null">power_unit,</if>
            <if test="percentageFee != null">percentage_fee,</if>
            <if test="percentageBrokerage != null">percentage_brokerage,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="siteId != null">#{siteId},</if>
            <if test="siteNameCn != null">#{siteNameCn},</if>
            <if test="siteNameEn != null">#{siteNameEn},</if>
            <if test="domainName != null">#{domainName},</if>
            <if test="siteLogo1 != null">#{siteLogo1},</if>
            <if test="siteLogo2 != null">#{siteLogo2},</if>
            <if test="siteLogo3 != null">#{siteLogo3},</if>
            <if test="homeTitle != null">#{homeTitle},</if>
            <if test="siteProvider != null">#{siteProvider},</if>
            <if test="siteCopyright != null">#{siteCopyright},</if>
            <if test="siteCompany != null">#{siteCompany},</if>
            <if test="postalAddress != null">#{postalAddress},</if>
            <if test="siteTel != null">#{siteTel},</if>
            <if test="siteFax != null">#{siteFax},</if>
            <if test="siteEmail != null">#{siteEmail},</if>
            <if test="seoTitle != null">#{seoTitle},</if>
            <if test="seoKeyword != null">#{seoKeyword},</if>
            <if test="seoDescription != null">#{seoDescription},</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>
            <if test="powerUnit != null">#{powerUnit},</if>
            <if test="percentageFee != null">#{percentageFee},</if>
            <if test="percentageBrokerage != null">#{percentageBrokerage},</if>
        </trim>
    </insert>

    <update id="updateTuyaSitesInfo" parameterType="TuyaSitesInfo">
        update tuya_sites_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="siteNameCn != null">site_name_cn = #{siteNameCn},</if>
            <if test="siteNameEn != null">site_name_en = #{siteNameEn},</if>
            <if test="domainName != null">domain_name = #{domainName},</if>
            <if test="siteLogo1 != null">site_logo1 = #{siteLogo1},</if>
            <if test="siteLogo2 != null">site_logo2 = #{siteLogo2},</if>
            <if test="siteLogo3 != null">site_logo3 = #{siteLogo3},</if>
            <if test="homeTitle != null">home_title = #{homeTitle},</if>
            <if test="siteProvider != null">site_provider = #{siteProvider},</if>
            <if test="siteCopyright != null">site_copyright = #{siteCopyright},</if>
            <if test="siteCompany != null">site_company = #{siteCompany},</if>
            <if test="postalAddress != null">postal_address = #{postalAddress},</if>
            <if test="siteTel != null">site_tel = #{siteTel},</if>
            <if test="siteFax != null">site_fax = #{siteFax},</if>
            <if test="siteEmail != null">site_email = #{siteEmail},</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="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>
            <if test="powerUnit != null">power_unit = #{powerUnit},</if>
            <if test="percentageFee != null">percentage_fee = #{percentageFee},</if>
            <if test="percentageBrokerage != null">percentage_brokerage = #{percentageBrokerage},</if>
        </trim>
        where site_id = #{siteId}
    </update>

    <delete id="deleteTuyaSitesInfoBySiteId" parameterType="Long">
        delete from tuya_sites_info where site_id = #{siteId}
    </delete>

    <delete id="deleteTuyaSitesInfoBySiteIds" parameterType="String">
        delete from tuya_sites_info where site_id in
        <foreach item="siteId" collection="array" open="(" separator="," close=")">
            #{siteId}
        </foreach>
    </delete>
    <select id="hasChildBySiteId" parameterType="Long" resultType="int">
        select count(1) from tuya_channel_info
        where  channel_site_id = #{siteId}
    </select>
</mapper>
