<?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="cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouTemplateTargetMapper">

    <insert id="templateTargetList">
        replace into ctop_kuaishou_template_target(
        id,
        template_id,
        region,
        district_ids,
        user_type,
        ages_range,
        min,
        max,
        gender,
        disable_installed_app_switch,
        platform_os,
        android_osv,
        ios_osv,
        network,
        filter_converted_level,
        device_brand,
        device_price,
        business_interest_type,
        business_interest,
        fans_star,
        interest_video,
        app_interest,
        app_ids,
        population,
        paid_audience,
        exclude_population,
        is_open,
        no_age_break,
        no_gender_break,
        no_area_break,
        keyword,
        label,
        interest_label,
        time_type,
        strength_type,
        scene_type,
        create_time,
        update_time)
        values
        <foreach collection="templateTargetList" item="templateTarget" separator=",">
            (
            #{templateTarget.id},
            #{templateTarget.templateId},
            #{templateTarget.region},
            #{templateTarget.districtIds},
            #{templateTarget.userType},
            #{templateTarget.agesRange},
            #{templateTarget.min},
            #{templateTarget.max},
            #{templateTarget.gender},
            #{templateTarget.disableInstalledAppSwitch},
            #{templateTarget.platformOs},
            #{templateTarget.androidOsv},
            #{templateTarget.iosOsv},
            #{templateTarget.network},
            #{templateTarget.filterConvertedLevel},
            #{templateTarget.deviceBrand},
            #{templateTarget.devicePrice},
            #{templateTarget.businessInterestType},
            #{templateTarget.businessInterest},
            #{templateTarget.fansStar},
            #{templateTarget.interestVideo},
            #{templateTarget.appInterest},
            #{templateTarget.appIds},
            #{templateTarget.population},
            #{templateTarget.paidAudience},
            #{templateTarget.excludePopulation},
            #{templateTarget.isOpen},
            #{templateTarget.noAgeBreak},
            #{templateTarget.noGenderBreak},
            #{templateTarget.noAreaBreak},
            #{templateTarget.keyword},
            #{templateTarget.label},
            #{templateTarget.interestLabel},
            #{templateTarget.timeType},
            #{templateTarget.strengthType},
            #{templateTarget.sceneType},
            #{templateTarget.createTime},
            #{templateTarget.updateTime}
            )
        </foreach>
    </insert>

    <select id="queryTemplateTarget" resultType="cn.com.ctop.kuaishou.modules.batch.entity.KuaishouTemplateTarget">
        SELECT t1.template_id,
               population,
               paid_audience,
               exclude_population
        FROM ctop_kuaishou_template_target t1
                 LEFT JOIN ctop_kuaishou_template t2 ON t1.template_id = t2.template_id
        WHERE t1.template_id = #{templateId}
          AND t2.account_id = #{accountId}
    </select>

    <select id="queryTemplateTargetByIds" resultType="cn.com.ctop.kuaishou.modules.batch.entity.KuaishouTemplateTarget">
        SELECT *
        FROM ctop_kuaishou_template_target t1
        WHERE t1.template_id in
        <foreach collection="templateIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>

    </select>

    <delete id="deleteOldTemplateTarget">
        delete from ctop_kuaishou_template_target where template_id in
        <foreach collection="list" item="item" separator=","
                 open="(" close=")">
            #{item.templateId}
        </foreach>
    </delete>

</mapper>