<?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.channel.mapper.KuaishouChannelRelMapper">

    <select id="queryByAccountChannelCode"
            resultType="cn.com.ctop.kuaishou.modules.channel.entity.KuaishouChannelRel">
        SELECT t1.*
        FROM ctop_kuaishou_channel_rel t1
                 LEFT JOIN ctop_kuaishou_channel_app_info t2 ON t1.app_id = t2.app_id
        WHERE t1.account_id =#{accountId}
          AND t2.app_name = #{appName}
          AND channel_code =  #{channelCode} limit 1
    </select>

    <select id="queryUsePlanCount" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(1), 0)
        FROM (
                 SELECT 1
                 FROM ctop_kuaishou_channel_rel
                 WHERE account_id = #{accountId}
                   AND channel_code = #{channelCode}
                   AND app_id is not null
                   AND campaign_id is not null
                 GROUP BY campaign_id
             ) t
    </select>

    <select id="queryUseGroupCount" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(1), 0)
        FROM (SELECT 1
              FROM ctop_kuaishou_channel_rel
              WHERE account_id = #{accountId}
                AND channel_code = #{channelCode}
                AND app_id IS NOT NULL
                AND unit_id IS NOT NULL
              GROUP BY unit_id) t
    </select>
</mapper>