<?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.miaxis.user.mapper.UserInfoMapper">

    <resultMap type="UserInfo" id="UserInfoResult">
        <result property="id"    column="id"    />
        <result property="phone"    column="phone"    />
        <result property="wechar"    column="wechar"    />
        <result property="headImage"    column="head_image"    />
        <result property="nickName"    column="nick_name"    />
        <result property="openid"    column="openid"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="status"    column="status"    />
        <result property="isVip"    column="is_vip"    />
        <result property="unionId"    column="union_id"    />
    </resultMap>

    <sql id="selectUserInfoVo">
        select * from user_info
    </sql>

    <select id="selectUserInfoList" parameterType="UserInfo" resultMap="UserInfoResult">
        <include refid="selectUserInfoVo"/>
        <where>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="wechar != null  and wechar != ''"> and wechar = #{wechar}</if>
            <if test="headImage != null  and headImage != ''"> and head_image = #{headImage}</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="openid != null  and openid != ''"> and openid = #{openid}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="isVip != null "> and is_vip = #{isVip}</if>
            <if test="unionId != null  and unionId != ''"> and union_id = #{unionId}</if>
        </where>
    </select>

    <select id="selectUserByParentOpenid" resultType="com.miaxis.common.core.domain.entity.UserInfo">
        SELECT
            u.openid,
            u.nick_name,
            u.head_image,
            u.create_time
        FROM
            user_info u
            LEFT JOIN wx_spread_relation sr ON sr.openid = u.openid
        WHERE sr.parent_openid = #{openid}
        ORDER BY u.create_time desc

    </select>

    <select id="selectUserByList" resultType="com.miaxis.common.core.domain.entity.UserInfo">
        SELECT
            u.openid,
            u.nick_name,
            u.head_image,
            u.create_time
        FROM
            user_info u
        LEFT JOIN wx_spread_relation sr ON sr.openid = u.openid
        WHERE sr.parent_openid in
        <foreach collection="list" item="openid" open="(" separator="," close=")">
            #{openid}
        </foreach>
        ORDER BY u.create_time desc

    </select>
    <select id="getUpUserInfo"  resultType="com.miaxis.common.core.domain.entity.UserInfo">
        select * from user_info
        where openid = (select parent_openid from wx_spread_relation where openid = #{fromUserName})
    </select>
    <select id="getDownLevelPoints"  resultType="com.miaxis.extension.vo.ExtensionIncomeVo">
        select id,openid,IFNULL(nick_name,'未知') as nick_name,IFNULL(head_image,'未知') as head_image,achievement,achievement_settled,
        (select count(1) from wx_spread_relation where parent_openid =t1.openid) as extension_count from user_info t1
        where openid in (select openid from wx_spread_relation where parent_openid = #{openid}) order by achievement desc
    </select>

    <select id="getPcDownLevelPoints"  resultType="com.miaxis.extension.vo.ExtensionIncomeVo">
        select id,openid,IFNULL(nick_name,'未知') as nick_name,IFNULL(head_image,'未知') as head_image,achievement+achievement_settled as achievement_total,achievement,achievement_settled,
        (select count(1) from wx_spread_relation where parent_openid =t1.openid) as extension_count ,
        ( SELECT count( 1 ) FROM wx_spread_relation WHERE parent_openid = t1.openid ) AS extension_count ,
        IFNULL(( SELECT sum(payer_total) FROM wx_order WHERE openid = t1.openid and trade_state = 'SUCCESS'  ),0) as cost_count
        from user_info t1
        where openid in (select openid from wx_spread_relation where parent_openid = #{openid})
        <if test="sortByParmName != null  and sortByParmName != ''"> order by ${sortByParmName} desc</if>
    </select>

</mapper>
