<?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="org.jeecg.modules.adweb.userCountry.mapper.AdwebUserCountryMapper">

    <select id="getSubUserList" resultType="org.jeecg.modules.adweb.userCountry.dto.SubUser">
        SELECT
        t0.sub_id id,
        t4.username,
        t4.realname,
        COUNT(DISTINCT t1.country_id) countryNum,
        enquiryCount enquiryNum
        FROM
        adweb_master_sub_account_relation t0
        LEFT JOIN adweb_user_country t1 ON t0.sub_id = t1.uid AND t1.`status` = 1 AND t1.site_id IN
        <foreach collection="siteIds" open="(" close=")" separator="," item="siteId">
            #{siteId}
        </foreach>
        LEFT JOIN adweb_country t2 ON t1.country_id = t2.geoname_id
        LEFT JOIN
        (
        SELECT
        COUNT(id) enquiryCount,
        principal_uid
        FROM
        adweb_enquiry
        WHERE
        `status` = 1 AND site_id IN
        <foreach collection="siteIds" open="(" close=")" separator="," item="siteId">
            #{siteId}
        </foreach>
        GROUP BY
        principal_uid
        ) t3 ON t0.sub_id = t3.principal_uid
        LEFT JOIN sys_user t4 ON t0.sub_id = t4.id
        WHERE
        t0.`status` = 1
        AND t0.master_id = #{uid}
        GROUP BY
        t0.sub_id,
        t3.enquiryCount
    </select>
    <select id="getSubUserOptions" resultType="org.jeecg.modules.adweb.userCountry.dto.SubUser">
        SELECT t1.id,
               t1.username username,
               t1.realname realname
        FROM adweb_master_sub_account_relation t0,
             sys_user t1
        WHERE t0.sub_id = t1.id
          AND t0.`status` = 1
          AND t1.del_flag = 0
          AND t1.status = 1
          AND t0.master_id = #{uid}
    </select>
</mapper>
