<?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.kym.mapper.miniapp.UserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.kym.entity.miniapp.User">
        <id column="id" property="id"/>
        <result column="openid" property="openid"/>
        <result column="unionid" property="unionid"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="gender" property="gender"/>
        <result column="nickname" property="nickname"/>
        <result column="mobile_phone" property="mobilePhone"/>
        <result column="avatar" property="avatar"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="last_login_time" property="lastLoginTime"/>
    </resultMap>

    <resultMap id="CustomUserMap" type="com.kym.entity.admin.vo.CustomUserVo">
        <id column="user_id" property="userId"/>
        <id column="user_name" property="userName"/>
        <result column="mobile_phone" property="mobilePhone"/>
        <result column="status" property="status"/>
        <result column="register_time" property="registerTime"/>
        <result column="last_login_time" property="lastLoginTime"/>
        <result column="recharge_times" property="rechargeTimes"/>
        <result column="recharge_amount" property="rechargeAmount"/>
        <result column="charge_times" property="chargeTimes"/>
        <result column="total_power" property="totalPower"/>
        <result column="total_money" property="totalMoney"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, openid, unionid, username, password, gender, nickname, mobile_phone, avatar, status, create_time, update_time, last_login_time
    </sql>


    <select id="listUser" resultMap="CustomUserMap" parameterType="java.lang.Long">
        <if test="userId!=null">
            SELECT
            t1.`id` user_id,
            t1.`username` user_name,
            t1.`mobile_phone`,
            t1.`status`,
            t1.`create_time` register_time,
            t1.`last_login_time`,
            t3.recharge_times,
            t3.recharge_amount,
            t2.charge_times,
            t2.total_power,
            t2.total_money
            FROM
            t_user t1
            LEFT JOIN
            (SELECT
            user_id,
            COUNT(IFNULL(1, 0)) charge_times,
            SUM(IFNULL(total_power, 0)) total_power,
            SUM(IFNULL(total_money, 0)) total_money
            FROM
            t_charge_order
            WHERE user_id = ${userId}
            AND order_status = 1) t2
            ON t1.`id` = t2.`user_id`
            LEFT JOIN
            (SELECT
            user_id,
            COUNT(IFNULL(1, 0)) recharge_times,
            SUM(IFNULL(amount, 0)) recharge_amount
            FROM
            t_wallet_detail
            WHERE user_id = ${userId}
            AND TYPE = 1) t3
            ON t1.`id` = t3.`user_id`
            WHERE t1.id = ${userId}
        </if>
        <if test="userId==null">

            SELECT
            user.`id` user_id,
            user.`username` user_name,
            user.`mobile_phone`,
            user.`status`,
            user.`create_time` register_time,
            user.`last_login_time`,
            t.recharge_times,
            t.recharge_amount,
            t1.charge_times,
            t1.total_power,
            t1.total_money
            FROM t_user `user`
            LEFT JOIN
            (SELECT user_id,
            COUNT(
            CASE
            WHEN wd.`type` = 1
            THEN 1
            ELSE NULL
            END
            ) recharge_times,
            SUM(
            CASE
            WHEN wd.`type` = 1
            THEN wd.`amount`
            ELSE 0
            END
            ) recharge_amount
            FROM t_wallet_detail wd
            GROUP BY wd.user_id) t
            ON t.user_id = user.id
            LEFT JOIN
            (SELECT user_id,
            COUNT(
            CASE
            WHEN o.`order_status` = 1
            THEN 1
            ELSE NULL
            END
            ) charge_times,
            SUM(
            CASE
            WHEN o.`total_power` > 0
            THEN o.`total_power`
            ELSE 0
            END
            ) total_power,
            SUM(
            CASE
            WHEN o.`total_money` > 0
            THEN o.`total_money`
            ELSE 0
            END
            ) total_money
            FROM t_charge_order o
            GROUP BY o.user_id) t1
            ON t1.user_id = user.id
            ORDER BY register_time DESC
        </if>
    </select>

</mapper>
