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

    <insert id="replaceBatch">
        replace into ctop_kuaishou_app_package_rel(
        id,
        apptem_id,
        project_id,
        account_id,
        app_id,
        app_version,
        data_status,
        return_url,
        message,
        return_update_time
        )
        values
        <foreach collection="relList" item="rel" separator=",">
            (
            #{rel.id},
            #{rel.apptemId},
            #{rel.projectId},
            #{rel.accountId},
            #{rel.appId},
            #{rel.appVersion},
            #{rel.dataStatus},
            #{rel.returnUrl},
            #{rel.message},
            #{rel.returnUpdateTime}
            )
        </foreach>
    </insert>

    <select id="queryEntityByAccountId" resultType="cn.com.ctop.kuaishou.modules.ai.entity.KuaishouAppPackageRel">
        SELECT apptem_id, app_id, project_id
        FROM ctop_kuaishou_app_package_rel
        where account_id = #{accountId}
          and app_version = #{appVersion}
          and data_status = 1
    </select>

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

    <select id="getRelatedAccounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT apptem_id as 'id',
               IFNULL(t2.auth_name, '') as 'authName',
               IFNULL(t2.account_id, '') as 'accountId',
               (SELECT COUNT(1)
                FROM ctop_kuaishou_app_list
                where account_id = t1.account_id) as 'appCount'
        FROM ctop_kuaishou_app_package_rel t1
                 LEFT JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
        where t1.apptem_id =#{id}
          AND t1.data_status = 1
        order by t1.create_time desc
    </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 account_id,data_status FROM ctop_kuaishou_app_package_rel
            WHERE  project_id = #{projectId}
              AND apptem_id = #{id}
        ) t2 ON t1.account_id = t2.account_id
        where t1.account_status = 0
          AND t1.project_id = #{projectId}
    </select>

    <select id="queryAppId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT app_id,impression_url,actionbar_click_url,ad_photo_played_t3s_url,t2.track_url FROM  ctop_kuaishou_app_package_rel t1
        LEFT JOIN ctop_kuaishou_app_package t2 ON t2.id = t1.apptem_id
        where t1.data_status = 1
        AND t1.account_id = #{accountId}
        AND t1.apptem_id = #{appTemId}
    </select>

    <select id="getAppsDetail" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            apptem_id as 'apptemId',
            t2.id,
            t1.project_id as 'projectId',
            t1.app_name as 'appName',
            IFNULL(t1.track_url, '') as 'tracUrl',
            t1.platform ,
            t2.app_version as 'appVersion',
            IFNULL(t2.impression_url, '') as 'impressionUrl',
            IFNULL(t2.actionbar_click_url, '') as 'actionbarClickUrl',
            IFNULL(t2.ad_photo_played_t3s_url, '') as 'adPhotoPlayedT3sUrl',
            t2.app_id as 'appId',
            (SELECT COUNT(1) FROM ctop_kuaishou_app_package_rel WHERE apptem_id = t2.apptem_id AND data_status = 1) as 'count'
        FROM ctop_kuaishou_app_package t1
                LEFT JOIN ctop_kuaishou_app_package_rel t2 ON t1.id = t2.apptem_id
        WHERE t2.data_status = 1
          AND apptem_id in
        <foreach collection="ids" item="ids" separator="," open="(" close=")">
            #{ids}
        </foreach>
    </select>

</mapper>