<?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.yami.shop.dao.PointsRechargeMapper">

    <select id="statisticsList" resultType="com.yami.shop.bean.vo.PointsRechargeVO">
        SELECT
        sum(tpr.points) AS totalPoints,
        count(tpr.id) AS totalUserCount,
        tpr.channel_name AS channelName,
        max(tpr.create_time) AS createTime
        FROM tz_points_recharge tpr
        left join tz_user tu on tpr.user_id = tu.user_id
        <where>
              <if test="pointsRecharge.channelId != null"> tpr.channel_id = #{pointsRecharge.channelId}</if>
            <if test="pointsRecharge.channelIdList != null and !pointsRecharge.channelIdList.isEmpty()">
                and tpr.channel_id  in
                <foreach collection="pointsRecharge.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="pointsRecharge.userAttrType != null and pointsRecharge.userAttrType != '' and pointsRecharge.userAttrType != 0">
                AND tu.user_attr_type = #{pointsRecharge.userAttrType}
            </if>
        </where>
        GROUP BY tpr.channel_id, tpr.channel_name
        ORDER BY max(tpr.create_time) DESC
    </select>
    <select id="findPage" resultType="com.yami.shop.bean.model.PointsRecharge">
        select *
        FROM tz_points_recharge tpr
        left join tz_user tu on tpr.user_id = tu.user_id
        <where>
            <if test="pointsRecharge.userPhone != null and pointsRecharge.userPhone != '' "> tpr.user_phone = #{pointsRecharge.userPhone}</if>
            <if test="pointsRecharge.channelIdList != null and !pointsRecharge.channelIdList.isEmpty()">
                and tpr.channel_id  in
                <foreach collection="pointsRecharge.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="pointsRecharge.userAttrType != null and pointsRecharge.userAttrType != '' and pointsRecharge.userAttrType != 0">
                AND tu.user_attr_type = #{pointsRecharge.userAttrType}
            </if>
        </where>
        order by create_time desc
    </select>
    <select id="findList" resultType="com.yami.shop.bean.model.PointsRecharge">
        select *
        FROM tz_points_recharge tpr
        left join tz_user tu on tpr.user_id = tu.user_id
        <where>
            <if test="pointsRecharge.userPhone != null and pointsRecharge.userPhone != '' "> tpr.user_phone = #{pointsRecharge.userPhone}</if>
            <if test="pointsRecharge.channelIdList != null and !pointsRecharge.channelIdList.isEmpty()">
                and tpr.channel_id  in
                <foreach collection="pointsRecharge.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="pointsRecharge.userAttrType != null and pointsRecharge.userAttrType != '' and pointsRecharge.userAttrType != 0">
                AND tu.user_attr_type = #{pointsRecharge.userAttrType}
            </if>
        </where>
        order by create_time desc
    </select>
</mapper>
