<?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.fs.ad.mapper.AdSiteMapper">
    
    <resultMap type="AdSite" id="AdSiteResult">
        <result property="id"    column="id"    />
        <result property="templateId"    column="template_id"    />
        <result property="type"    column="type"    />
        <result property="accountId"    column="account_id"    />
        <result property="domainId"    column="domain_id"    />
        <result property="url"    column="url"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectAdSiteVo">
        select * from ad_site
    </sql>

    <select id="selectAdSiteList" parameterType="AdSite" resultType="AdSite">
        select a.*,c.domain,b.name templateName,d.name accountName from ad_site a
        inner join ad_html_template b on a.template_id = b.id
        inner join ad_domain c on a.domain_id = c.id
        left join ad_account d on a.account_id = d.id
        <where>
            <if test="templateId != null "> and a.template_id = #{templateId}</if>
            <if test="type != null "> and a.type = #{type}</if>
            <if test="accountId != null "> and a.account_id = #{accountId}</if>
            <if test="url != null  and url != ''"> and a.url = #{url}</if>
        </where>
    </select>
    
    <select id="selectAdSiteById" parameterType="Long" resultMap="AdSiteResult">
        <include refid="selectAdSiteVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertAdSite" parameterType="AdSite" useGeneratedKeys="true" keyProperty="id">
        insert into ad_site
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="templateId != null">template_id,</if>
            <if test="type != null">type,</if>
            <if test="accountId != null">account_id,</if>
            <if test="url != null">url,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="templateId != null">#{templateId},</if>
            <if test="type != null">#{type},</if>
            <if test="accountId != null">#{accountId},</if>
            <if test="url != null">#{url},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateAdSite" parameterType="AdSite">
        update ad_site
        <trim prefix="SET" suffixOverrides=",">
            <if test="templateId != null">template_id = #{templateId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="accountId != null">account_id = #{accountId},</if>
            <if test="url != null">url = #{url},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAdSiteById" parameterType="Long">
        delete from ad_site where id = #{id}
    </delete>

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