<?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.ai.mapper.KuaishouDirectionalPckageRelMapper">

    <insert id="replace">
        replace
        into ctop_kuaishou_directional_package_rel(
        id ,
        dir_id ,
        project_id ,
        template_id ,
        template_name ,
        data_status,
        rel_id ,
        rel_type ,
        exclude_population ,
        paid_audience ,
        population ,
        message
        )
        values
            (
        #{id} ,
        #{dirId} ,
        #{projectId} ,
        #{templateId} ,
        #{templateName} ,
        #{dataStatus} ,
        #{relId} ,
        #{relType} ,
        #{excludePopulation} ,
        #{paidAudience} ,
        #{population} ,
        #{message}
        )

    </insert>

    <select id="queryPushResults" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        dir_id 'id',
        IFNULL(t2.auth_name,'') as 'authName',
        IFNULL(t2.account_id,'') as 'accountId',
        IFNULL(t1.template_id,'') as 'templateId',
        IFNULL(t1.message,'-') as 'message',
        CASE WHEN t1.data_status =1 THEN '推送成功' ELSE '推送失败' end as 'result'
        FROM ctop_kuaishou_directional_package_rel t1
        LEFT JOIN ctop_user_allocation t2 ON t1.rel_id = t2.account_id
        where dir_id = #{id}
        AND rel_type = 'account'
        <if test=" status!=null">
            AND t1.data_status = #{status}
        </if>
        <if test="accountIds!=null">
            AND rel_id IN
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getAccountByProjectId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t1.account_id as 'accountId', CONCAT(t1.auth_name, '(', t1.account_id, ')') as 'authName', IFNULL(t2.data_status, 0) as 'state'
        FROM ctop_user_allocation t1
                 LEFT JOIN (
            SELECT rel_id, data_status
            FROM ctop_kuaishou_directional_package_rel
            WHERE project_id = #{projectId}
              AND rel_type = 'account'
              AND dir_id = #{id}
        ) t2 ON t1.account_id = t2.rel_id
        where t1.account_status = 0
          AND t1.project_id = #{projectId}
    </select>

    <select id="queryRelId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT rel_id, template_id
        FROM ctop_kuaishou_directional_package_rel
        WHERE project_id = #{projectId}
          AND rel_type = #{type}
          AND dir_id = #{id}
    </select>

    <select id="queryUnitIdByAccountId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT rel_id, template_id
        FROM (
                 SELECT DISTINCT unit_id
                 FROM ctop_kuaishou_group
                 where account_id = #{accountId}) t1
                 INNER JOIN ctop_kuaishou_directional_package_rel t2 ON t1.unit_id = t2.rel_id
        WHERE t2.rel_type = 'unit'
          AND t2.dir_id = #{id}
    </select>

    <select id="selectTemplateId" resultType="java.lang.Long">
        SELECT template_id
        FROM ctop_kuaishou_directional_package_rel
        WHERE rel_type = 'account'
        AND dir_id = #{dirId}
        AND rel_id =#{accountId}
        and data_status = 1
    </select>

    <update id="updateTemName">
        update
            ctop_kuaishou_directional_package_rel
        set template_name = #{templateName}
        where dir_id = #{id}
    </update>
</mapper>