<?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="com.game.business.mapper.AppUserAgentMapper">
    
    <resultMap type="com.game.business.domain.AppUserAgent" id="AppUserAgentResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="pid"    column="pid"    />
        <result property="topId"    column="top_id"    />
        <result property="subset"    column="subset"    />
        <result property="liveRate"    column="live_rate"    />
        <result property="dividendGuaranteeRate"    column="dividend_guarantee_rate"    />
        <result property="auditStatus"    column="audit_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectAppUserAgentVo">
        select id, user_id, pid, top_id,subset, live_rate, dividend_guarantee_rate, audit_status, create_time, update_time from app_user_agent
    </sql>

    <select id="selectAppUserAgentList" parameterType="com.game.business.domain.AppUserAgent" resultMap="AppUserAgentResult">
        <include refid="selectAppUserAgentVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="pid != null "> and pid = #{pid}</if>
            <if test="topId != null "> and top_id = #{topId}</if>
            <if test="subset != null "> and subset = #{subset}</if>
            <if test="liveRate != null "> and live_rate = #{liveRate}</if>
            <if test="dividendGuaranteeRate != null "> and dividend_guarantee_rate = #{dividendGuaranteeRate}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and audit_status = #{auditStatus}</if>
        </where>
    </select>
    
    <select id="selectAppUserAgentById" parameterType="Long" resultMap="AppUserAgentResult">
        <include refid="selectAppUserAgentVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertAppUserAgent" parameterType="com.game.business.domain.AppUserAgent" useGeneratedKeys="true" keyProperty="id">
        insert into app_user_agent
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="userId != null">user_id,</if>
            <if test="pid != null">pid,</if>
            <if test="topId != null">top_id,</if>
            <if test="subset != null and subset != ''">subset,</if>
            <if test="liveRate != null">live_rate,</if>
            <if test="dividendGuaranteeRate != null">dividend_guarantee_rate,</if>
            <if test="auditStatus != null">audit_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="userId != null">#{userId},</if>
            <if test="pid != null">#{pid},</if>
            <if test="topId != null">#{topId},</if>
            <if test="subset != null and subset != ''">#{subset},</if>
            <if test="liveRate != null">#{liveRate},</if>
            <if test="dividendGuaranteeRate != null">#{dividendGuaranteeRate},</if>
            <if test="auditStatus != null">#{auditStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateAppUserAgent" parameterType="com.game.business.domain.AppUserAgent">
        update app_user_agent
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="pid != null">pid = #{pid},</if>
            <if test="topId != null">top_id = #{topId},</if>
            <if test="subset != null and subset != ''">subset = #{subset},</if>
            <if test="liveRate != null">live_rate = #{liveRate},</if>
            <if test="dividendGuaranteeRate != null">dividend_guarantee_rate = #{dividendGuaranteeRate},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppUserAgentById" parameterType="Long">
        delete from app_user_agent where id = #{id}
    </delete>

    <delete id="deleteAppUserAgentByIds" parameterType="String">
        delete from app_user_agent where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="sumBetting" resultType="java.lang.Double">
        select IFNULL(sum(betting_amount),0) as amount
        from app_user_agent as a left join app_game_betting as b on a.user_id = b.user_id
        where b.create_time between #{beginTime} and #{endTime}
        and a.pid = #{pid}
    </select>

    <select id="teamList" resultType="com.game.business.vo.AppAgentTeamVo">
        select
            a.user_id as userId,
            c.username as nickName,
            c.coin as coin,
            c.diamond_coin as diamondCoin,
            a.pid as pid,
            (
                select IFNULL(sum(betting_amount),0) from app_game_betting as b where b.user_id = a.user_id
                <if test="teamDto.beginTime != null and teamDto.beginTime != ''">
                    and <![CDATA[ b.create_time >= concat(#{teamDto.beginTime},' 00:00:00') ]]>
                </if>
                <if test="teamDto.endTime != null and teamDto.endTime != ''">
                    and <![CDATA[ b.create_time <= concat(#{teamDto.endTime},' 23:59:59') ]]>
                </if>
             ) as bettingAmount,
            (
                select IFNULL(sum(d.game_commission),0) from app_user_count as d where d.user_id = a.user_id
                <if test="teamDto.beginTime != null and teamDto.beginTime != ''">
                    and <![CDATA[ d.str_date >= #{teamDto.beginTime} ]]>
                </if>
                <if test="teamDto.endTime != null and teamDto.endTime != ''">
                    and <![CDATA[ d.str_date <= #{teamDto.endTime} ]]>
                </if>
            ) as gameCommission,
            (
                select IFNULL(sum(f.live_commission),0) from app_user_count as f where f.user_id = a.user_id
                    <if test="teamDto.beginTime != null and teamDto.beginTime != ''">
                        and <![CDATA[ f.str_date >= #{teamDto.beginTime} ]]>
                    </if>
                    <if test="teamDto.endTime != null and teamDto.endTime != ''">
                        and <![CDATA[ f.str_date <= #{teamDto.endTime} ]]>
                    </if>
            ) as liveCommission,
            (
                select IFNULL(sum(e.game_win_amount - e.game_lose_amount),0) from app_user_count as e where e.user_id = a.user_id
                <if test="teamDto.beginTime != null and teamDto.beginTime != ''">
                    and <![CDATA[ e.str_date >= #{teamDto.beginTime} ]]>
                </if>
                <if test="teamDto.endTime != null and teamDto.endTime != ''">
                    and <![CDATA[ e.str_date <= #{teamDto.endTime} ]]>
                </if>
            ) as gameAmount,
            (
            select IFNULL(sum(g.recharge_amount),0) from app_user_count as g where g.user_id = a.user_id
            <if test="teamDto.beginTime != null and teamDto.beginTime != ''">
                and <![CDATA[ g.str_date >= #{teamDto.beginTime} ]]>
            </if>
            <if test="teamDto.endTime != null and teamDto.endTime != ''">
                and <![CDATA[ g.str_date <= #{teamDto.endTime} ]]>
            </if>
            ) as rechargeAmount,
            (
            select IFNULL(sum(g.recharge_commission),0) from app_user_count as h where h.user_id = a.user_id
            <if test="teamDto.beginTime != null and teamDto.beginTime != ''">
                and <![CDATA[ h.str_date >= #{teamDto.beginTime} ]]>
            </if>
            <if test="teamDto.endTime != null and teamDto.endTime != ''">
                and <![CDATA[ h.str_date <= #{teamDto.endTime} ]]>
            </if>
            ) as rechargeCommission,
            (
            select IFNULL(sum(g.recharge_give),0) from app_user_count as i where i.user_id = a.user_id
            <if test="teamDto.beginTime != null and teamDto.beginTime != ''">
                and <![CDATA[ i.str_date >= #{teamDto.beginTime} ]]>
            </if>
            <if test="teamDto.endTime != null and teamDto.endTime != ''">
                and <![CDATA[ i.str_date <= #{teamDto.endTime} ]]>
            </if>
            ) as rechargeGive,
            a.live_rate as liveRate,
            a.dividend_guarantee_rate as dividendGuaranteeRate
        from app_user_agent as a left join mugozbg_live.app_user as c on a.user_id = c.userid where (a.pid = #{teamDto.pid} or a.user_id = #{teamDto.pid})
        <if test="teamDto.userId != null ">
             and a.user_id = #{teamDto.userId}
        </if>
        <if test="teamDto.nickName != null and teamDto.nickName != ''">
             and c.username = #{teamDto.nickName}
        </if>
    </select>
    <select id="teamListNew" resultType="com.game.business.vo.AppAgentTeamVo">
        select
            a.user_id as userId,
            c.username as nickName,
            c.coin as coin,
            c.diamond_coin as diamondCoin,
            a.pid as pid,
            IFNULL((LENGTH(a.subset) - LENGTH(REPLACE(a.subset, ',', ''))) + 1,0) as teamNum,
            IFNULL(sum(b.game_betting),0) as bettingAmount,
            IFNULL(sum(b.game_commission - b.live_betting_commission + b.recharge_commission + b.withdrawal_commission),0) as gameCommission,
            IFNULL(sum(b.live_betting_commission),0) as liveBettingCommission,
            IFNULL(sum(b.live_commission),0) as liveCommission,
            IFNULL(sum((b.game_win_amount - (b.game_betting - b.game_lose_amount)) - b.game_lose_amount + b.game_commission + b.recharge_give + b.recharge_commission +  b.withdrawal_commission),0) as gameAmount,
            IFNULL(sum(b.recharge_amount),0) as rechargeAmount,
            IFNULL(sum(b.recharge_commission),0) as rechargeCommission,
            IFNULL(sum(b.withdrawal_commission),0) as withdrawalCommission,
            IFNULL(sum(b.recharge_give),0) as rechargeGive,
            a.live_rate as liveRate,
            a.dividend_guarantee_rate as dividendGuaranteeRate
        from app_user_agent as a left join app_user_count as b on a.user_id = b.user_id left join mugozbg_live.app_user as c on a.user_id = c.userid
        where (a.pid = #{teamDto.pid} or a.user_id = #{teamDto.pid})
        <if test="teamDto.userId != null ">
             and a.user_id = #{teamDto.userId}
        </if>
        <if test="teamDto.nickName != null and teamDto.nickName != ''">
             and c.username = #{teamDto.nickName}
        </if>
        <if test="teamDto.beginTime != null and teamDto.beginTime != ''">
            and <![CDATA[ b.str_date >= #{teamDto.beginTime} ]]>
        </if>
        <if test="teamDto.endTime != null and teamDto.endTime != ''">
            and <![CDATA[ b.str_date <= #{teamDto.endTime} ]]>
        </if>
        group by a.user_id order by a.user_id
    </select>

    <select id="selectAllAgentList" resultMap="AppUserAgentResult">
        WITH RECURSIVE useragent AS
       (
           SELECT *
           FROM app_user_agent
           WHERE pid = #{pid}
          <if test="beginTime != null and beginTime != ''">
             and <![CDATA[create_time >= #{beginTime} ]]>
          </if>
          <if test="endTime != null and endTime != ''">
                and <![CDATA[create_time <= #{endTime} ]]>
          </if>
           UNION ALL

           SELECT o.*
           FROM app_user_agent o
                    JOIN useragent s ON o.pid = s.user_id
            <if test="beginTime != null and beginTime != ''">
                and <![CDATA[o.create_time >= #{beginTime} ]]>
            </if>
            <if test="endTime != null and endTime != ''">
                and <![CDATA[o.create_time <= #{endTime} ]]>
            </if>
       )
        SELECT * FROM useragent where 1=1
        <if test="userId != null">
            and user_id = #{userId}
        </if>
        union all
        select * from app_user_agent where user_id = #{pid}
    </select>

    <select id="selectSuperList"  resultMap="AppUserAgentResult">
        WITH RECURSIVE useragent AS
           (
               SELECT *
               FROM app_user_agent
               WHERE user_id = #{userId}
               UNION ALL

               SELECT o.*
               FROM app_user_agent o
                        JOIN useragent s ON o.user_id = s.pid
           )
        SELECT * FROM useragent where 1=1
    </select>
</mapper>