<?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.liquidnet.service.stone.mapper.StoneScoreUserMapper">

    <resultMap id="StoneScoreListDtoResult" type="com.liquidnet.service.stone.dto.StoneScoreListDto">
        <result column="nickname" property="nickName"/>
        <result column="avatar" property="avatar"/>
        <result column="mobile" property="mobile"/>
        <result column="uid" property="uid"/>
        <result column="status" property="status"/>
        <result column="score_all" property="scoreAll"/>
        <result column="score_use" property="scoreUse"/>
        <result column="score_rest" property="scoreRest"/>
    </resultMap>

    <select id="list" parameterType="java.util.Map" resultMap="StoneScoreListDtoResult">
        SELECT hu.*,
        SUM(CASE WHEN score <![CDATA[>]]> 0 THEN score ELSE 0 END) AS 'score_all',
        ABS(SUM(CASE WHEN score <![CDATA[<]]> 0 THEN score ELSE 0 END)) AS 'score_use',
        ('score_all' - 'score_use') as 'score_rest',
        avatar
        FROM (SELECT nickname,
        mobile,
        avatar,
        au.uid,
        IFNULL(ssu.status,1) as 'status'
        from adam_user as au
        inner join adam_user_info aui on au.uid = aui.uid
        left join stone_score_user ssu on ssu.uid = aui.uid
        <where>
            <if test="mobile!=''">
                AND mobile LIKE concat('%', #{mobile}, '%')
            </if>
            <if test="status==1">
                AND status = 1 OR status is null
            </if>
            <if test="status==2">
                AND status = 2
            </if>
            <if test="uid!='-999'">
                AND au.uid = #{uid}
            </if>
        </where>
        limit 50
        ) as hu
        left join stone_score_logs sslog on sslog.uid = hu.uid
        group by hu.uid;
    </select>

    <select id="detail" parameterType="java.util.Map" resultMap="StoneScoreListDtoResult">
        select hu.*,
        avatar,
        SUM(CASE WHEN score <![CDATA[>]]> 0 THEN score ELSE 0 END) AS 'score_all',
        ABS(SUM(CASE WHEN score <![CDATA[<]]> 0 THEN score ELSE 0 END)) AS 'score_use',
        ('score_all' - 'score_use') as 'score_rest'
        from (select nickname,
        mobile,
        avatar,
        au.uid,
        IFNULL(ssu.status,1) as 'status'
        from adam_user as au
        inner join adam_user_info aui on au.uid = aui.uid
        left join stone_score_user ssu on ssu.uid = aui.uid
        <where>
            au.uid = #{uid}
        </where>
        limit 1
        ) as hu
        left join stone_score_logs sslog on sslog.uid = hu.uid
        group by hu.uid;
    </select>


    <select id="listExport" parameterType="java.util.Map" resultMap="StoneScoreListDtoResult">
        select hu.*,
        (case when score <![CDATA[>]]> 0 then sum(score) else 0 END) as 'score_all',
        (case when score <![CDATA[<]]> 0 then sum(score) else 0 END) as 'score_use',
        ('score_all' - 'score_use') as 'score_rest'
        from (select nickname,
        mobile,
        au.uid,
        IFNULL(ssu.status,1) as 'status'
        from adam_user as au
        inner join adam_user_info aui on au.uid = aui.uid
        left join stone_score_user ssu on ssu.uid = aui.uid
        <where>
            au.uid IN
            <foreach collection="uids" item="uid" index="index" open="(" close=")" separator=",">
                '${uid}'
            </foreach>
        </where>
        ) as hu
        left join stone_score_logs sslog on sslog.uid = hu.uid
        group by hu.uid;
    </select>

</mapper>
