<?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="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.*,
        (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>
            <if test="mobile!=''">
                AND mobile LIKE concat('%', #{mobile}, '%')
            </if>
            <if test="status!='-999'">
                AND status = #{status} OR status is null
            </if>
            <if test="uid!='-999'">
                AND au.uid = #{uid}
            </if>
        </where>
        ) 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>
