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

    <select id="queryPushableCrowdPackage"
            resultType="cn.com.ctop.kuaishou.modules.ai.entity.KuaishouPopulationPackageRel">
        SELECT t1.*
        FROM ctop_kuaishou_population_package_rel t1
        WHERE `status` = 4
          AND pop_id = #{id}
          AND orientation_id is not null
          AND data_status = 1
          AND account_id IN (
            SELECT account_id
            FROM ctop_kuaishou_advertiser_base_info
            WHERE user_id = (SELECT user_id FROM ctop_kuaishou_advertiser_base_info WHERE account_id = #{accountId})
        )
            limit 1
    </select>

    <select id="queryByAccountId" resultType="cn.com.ctop.kuaishou.modules.ai.entity.KuaishouCrowdPack">
        SELECT *
        FROM ctop_kuaishou_population_package_rel
        where data_status = 1
        <if test="accountId!=null">
            AND account_id = #{accountId}
        </if>
        <if test="orientationId!=null">
            AND orientation_id = #{orientationId}
        </if>
        limit 1
    </select>

    <select id="queryPushResults" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        pop_id as '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_population_package_rel t1
        LEFT JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
        where t1.pop_id = #{id}
        <if test="accountIds!=null">
            AND t1.account_id IN
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getRelatedAccounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT pop_id as 'id',
               IFNULL(t2.auth_name, '') as 'authName',
               IFNULL(t2.account_id, '') as 'accountId',
               t1.`status`,
               CASE t1.`status`
                   WHEN 0 THEN '计算中'
                   WHEN 1 THEN '已生效'
                   WHEN 3 THEN '推送中'
                   WHEN 4 THEN '已推送'
                   WHEN 5 THEN '计算失败'
                   WHEN 6 THEN '推送失败'
                   WHEN 7 THEN '已失效'
                   ELSE ''
                   END as 'statusStr',
               cover_num 'coverNum',
               (SELECT COUNT(1)
                FROM ctop_kuaishou_population_package_rel t
                where t.account_id = t1.account_id
                  AND t.data_status = 1
                  AND (t.`status` = 0 OR t.`status` = 1 or t.`status` = 3 or t.`status` = 4)) as 'populationCount'
        FROM ctop_kuaishou_population_package_rel t1
                 LEFT JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
        where t1.pop_id = #{id}
          AND t1.data_status = 1
    </select>

    <select id="queryEntity" resultType="cn.com.ctop.kuaishou.modules.ai.entity.KuaishouPopulationPackageRel">
        SELECT pop_id,
               account_id,
               orientation_id
        FROM ctop_kuaishou_population_package_rel
        WHERE pop_id = #{id}
          AND data_status = 1
          AND account_id = #{accountId}
    </select>

    <select id="queryByOrientationId" resultType="java.lang.Long">
        SELECT orientation_id
        FROM ctop_kuaishou_population_package_rel
        WHERE data_status = 1
        AND account_id = #{accountId}
        <if test="orientationId!=null">
            AND orientation_id = #{orientationId}
        </if>
    </select>

    <insert id="replaceBatch">
        replace into ctop_kuaishou_population_package_rel(
        id,
        pop_id,
        project_id,
        account_id,
        stat_date,
        orientation_id,
        signature,
        orientation_name,
        population_type,
        `type`,
        status,
        cover_num,
        record_size,
        match_size,
        message,
        data_status
        )
        values
        <foreach collection="packageRels" item="package" separator=",">
            (
            #{package.id},
            #{package.popId},
            #{package.projectId},
            #{package.accountId},
            #{package.statDate},
            #{package.orientationId},
            #{package.signature},
            #{package.orientationName},
            #{package.populationType},
            #{package.type},
            #{package.status},
            #{package.coverNum},
            #{package.recordSize},
            #{package.matchSize},
            #{package.message},
            #{package.dataStatus}
            )
        </foreach>
    </insert>

    <select id="getAccountByProjectId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t1.account_id as 'accountId', CONCAT(t1.auth_name, '(', t1.account_id, ')') as 'authName', CASE
                                                                                                              WHEN
                                                                                                                      t2.status = 0
                                                                                                                      OR
                                                                                                                      t2.status = 1
                                                                                                                      OR
                                                                                                                      t2.status = 3
                                                                                                                      OR
                                                                                                                      t2.status = 4
                                                                                                                  THEN 1
                                                                                                              ELSE 0 END
            as 'state'
        FROM ctop_user_allocation t1
                 LEFT JOIN (
            SELECT account_id, status
            FROM ctop_kuaishou_population_package_rel
            WHERE project_id = #{projectId}
              AND pop_id = #{id}
              AND data_status = 1
        ) t2 ON t1.account_id = t2.account_id
        where t1.account_status = 0
          AND t1.project_id = #{projectId}
    </select>

    <update id="updateStatus">
        update
        ctop_kuaishou_population_package_rel
        <set>
            <if test="rel.status != null">
                status = #{rel.status},
            </if>
            <if test="rel.coverNum != null">
                cover_num = #{rel.coverNum},
            </if>
            <if test="rel.recordSize != null">
                record_size = #{rel.recordSize},
            </if>
            <if test="rel.dataStatus != null">
                data_status = #{rel.dataStatus},
            </if>
            <if test="rel.matchSize != null">
                match_size = #{rel.matchSize},
            </if>
            <if test="rel.message != null">
                message = #{rel.message},
            </if>
        </set>
        where orientation_id = #{rel.orientationId}
        and account_id = #{rel.accountId}
    </update>

</mapper>