<?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="org.xyzh.usercenter.mapper.UserPointsMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.usercenter.TbUserPoints">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="total_points" property="totalPoints" />
        <result column="current_points" property="currentPoints" />
        <result column="level" property="level" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 字段列表 -->
    <sql id="Base_Column_List">
        id, user_id, total_points, current_points, level, create_time, update_time
    </sql>

    <!-- 查询条件 -->
    <sql id="Base_Where_Clause">
        <where>
            <if test="filter != null">
                <if test="filter.id != null and filter.id != ''">
                    AND id = #{filter.id}
                </if>
                <if test="filter.userId != null and filter.userId != ''">
                    AND user_id = #{filter.userId}
                </if>
                <if test="filter.totalPoints != null">
                    AND total_points = #{filter.totalPoints}
                </if>
                <if test="filter.currentPoints != null">
                    AND current_points = #{filter.currentPoints}
                </if>
                <if test="filter.level != null">
                    AND level = #{filter.level}
                </if>
            </if>
        </where>
    </sql>

    <!-- 查询用户积分列表 -->
    <select id="selectUserPoints" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_points
        <include refid="Base_Where_Clause" />
    </select>

    <!-- 根据用户ID查询积分信息 -->
    <select id="selectByUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_points
        WHERE user_id = #{userId}
    </select>

    <!-- 检查用户积分是否存在 -->
    <select id="countByUserId" resultType="int">
        SELECT COUNT(1)
        FROM tb_user_points
        WHERE user_id = #{userId}
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 根据用户ID查询积分信息（包含用户基本信息） -->
    <select id="selectUserPointsWithUser" resultMap="BaseResultMap">
        SELECT
        up.id, up.user_id, up.total_points, up.current_points, up.level, 
        up.create_time, up.update_time
        FROM tb_user_points up
        LEFT JOIN tb_sys_user u ON up.user_id = u.id
        WHERE up.user_id = #{userId}
    </select>

    <!-- 查询积分排行榜 -->
    <select id="selectPointsRanking" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_points
        ORDER BY current_points DESC, total_points DESC
        <if test="limit != null and limit > 0">
            LIMIT #{limit}
        </if>
    </select>

    <!-- 插入用户积分 -->
    <insert id="insertUserPoints" parameterType="org.xyzh.common.dto.usercenter.TbUserPoints">
        INSERT INTO tb_user_points (
            id, user_id, total_points, current_points, level, create_time, update_time
        ) VALUES (
            #{id}, #{userId}, #{totalPoints}, #{currentPoints}, 
            #{level}, #{createTime}, #{updateTime}
        )
    </insert>

    <!-- 更新用户积分 -->
    <update id="updateUserPoints" parameterType="org.xyzh.common.dto.usercenter.TbUserPoints">
        UPDATE tb_user_points
        <set>
            <if test="userId != null and userId != ''">
                user_id = #{userId},
            </if>
            <if test="totalPoints != null">
                total_points = #{totalPoints},
            </if>
            <if test="currentPoints != null">
                current_points = #{currentPoints},
            </if>
            <if test="level != null">
                level = #{level},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除用户积分 -->
    <delete id="deleteUserPoints" parameterType="org.xyzh.common.dto.usercenter.TbUserPoints">
        DELETE FROM tb_user_points
        WHERE id = #{id}
    </delete>

    <!-- 批量插入用户积分 -->
    <insert id="batchInsertUserPoints" parameterType="java.util.List">
        INSERT INTO tb_user_points (
            id, user_id, total_points, current_points, level, create_time, update_time
        ) VALUES
        <foreach collection="userPointsList" item="item" separator=",">
            (
                #{item.id}, #{item.userId}, #{item.totalPoints}, #{item.currentPoints}, 
                #{item.level}, #{item.createTime}, #{item.updateTime}
            )
        </foreach>
    </insert>

    <!-- 批量删除用户积分 -->
    <delete id="batchDeleteUserPoints">
        DELETE FROM tb_user_points
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 分页查询用户积分 -->
    <select id="selectUserPointsPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_points
        <include refid="Base_Where_Clause" />
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计用户积分总数 -->
    <select id="countUserPoints" resultType="long">
        SELECT COUNT(1)
        FROM tb_user_points
        <include refid="Base_Where_Clause" />
    </select>

</mapper>