<?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.KuaishouTemplateMapper">
    <insert id="replaceBatch">

        replace into ctop_kuaishou_template(
        id,
        account_id,
        template_id,
        template_name,
        unit_count,
        auto_target,
        create_time,
        update_time)
        values
        <foreach collection="templateList" item="template" separator=",">
            (
            #{template.id},
            #{template.accountId},
            #{template.templateId},
            #{template.templateName},
            #{template.unitCount},
            #{template.autoTarget},
            #{template.createTime},
            #{template.updateTime}
            )
        </foreach>

    </insert>

    <select id="getJsonArrByAccountId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.account_id AS 'accountId',
        t1.template_id AS 'templateId',
        t1.template_name AS 'templateName',
        t1.unit_count AS 'unitCount',
        t2.platform_os AS 'platformOs'
        FROM
        ctop_kuaishou_template t1
        LEFT JOIN ctop_kuaishou_template_target t2 ON t1.template_id = t2.template_id
        WHERE
        t1.account_id = #{accountId}
        <if test="platformOs != null and platformOs!= '' ">
            AND t2.platform_os = #{platformOs}
        </if>
        <if test="autoTarget != null and autoTarget != '' ">
            AND t1.auto_target = #{autoTarget}
        </if>
    </select>


    <select id="queryByTemplateId" resultType="com.alibaba.fastjson.JSONObject">
        select t1.account_id,t1.template_id,t1.template_name,t3.dir_id,t3.project_id
        from ctop_kuaishou_template t1
                 INNER JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
                 left JOIN ctop_kuaishou_directional_package_rel t3 ON t1.template_id = t3.template_id AND t2.project_id = t3.project_id
        where t1.template_id = #{templateId}
        limit 1
    </select>


</mapper>