<?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.sqx.modules.app.dao.UserMoneyDetailsDao">
    <select id="monthIncome" resultType="Double">
        select sum(money)
        from user_money_details
        where user_id = #{userId}
          and classify = 4
          and type = 2
          and date_format(create_time, '%Y-%m') = date_format(#{date}, '%Y-%m')
    </select>
    <select id="selectMyProfit" resultType="Double">
        select sum(money)
        from user_money_details
        where user_id = #{userId}
          and classify!=3
    </select>

    <select id="selectMoneyDetails" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        select *
        from user_money_details
        where user_id = #{userId}
          and type = 2
    </select>
    <select id="getProfitList" resultType="java.util.HashMap">
        SELECT u.user_id,
               u.avatar,
               u.nick_name,
               SUM(money) AS money
        FROM user_money_details d,
             tb_user u
        WHERE d.relation_id = u.user_id
          AND d.user_id = #{userId}
          AND classify = 2
          AND d.user_level = #{type}
        GROUP BY d.relation_id DESC
        ORDER BY money DESC
    </select>
    <select id="getAgentRanking" resultType="java.util.HashMap">
        SELECT u.user_id,
               u.avatar,
               u.nick_name,
               IF(u.divide_scale = 0 || u.divide_two_scale = 0, 0, 1)                             AS isAgent,
               SUM(money - IFNULL((SELECT SUM(money)
                                   FROM user_money_details md
                                   WHERE md.relation_id = d.relation_id AND md.classify = 5), 0)) AS money
        FROM user_money_details d,
             tb_user u
        WHERE u.user_id = d.user_id
          AND classify = 2
        GROUP BY user_id
        ORDER BY money DESC
    </select>
    <select id="findMoneyDetails" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        select * from user_money_details u where 1=1

        <if test="userMoneyDetails.userId!=null">
            and user_id = #{userMoneyDetails.userId}
        </if>
        <if test="userMoneyDetails.classify==null">
            and classify is null
        </if>
        order by create_time desc
    </select>

    <select id="deductMoneySum" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0) from user_money_details u where 1 = 1 and u.classify= 1 and type = 2
        <if test="dateType==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType==2">
            and date_format(create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType==3">
            and date_format(create_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="cashDepositMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(money), 0) from pay_details where 1 = 1
        <if test="dateType==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType==2">
            and date_format(create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType==3">
            and date_format(create_time,'%Y')=date_format(#{date},'%Y')
        </if>
        and type = 4 and state = 1
    </select>

    <select id="selectCashDeposit" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        select umd.*, tu.phone as phone, tu.nick_name as nickName
        from user_money_details umd
        left join tb_user tu on umd.user_id = tu.user_id
        where 1 = 1
        <if test="phone!=null and phone!=''">
            and tu.phone = #{phone}
        </if>
        <if test="type!=null">
            and umd.type = #{type}
        </if>
        <if test="userId!=null">
            and umd.user_id = #{userId}
        </if>
        and classify = 1
        order by umd.create_time desc
    </select>

</mapper>