<?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.PointsRecordMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.usercenter.TbPointsRecord">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="points" property="points" />
        <result column="type" property="type" />
        <result column="source_type" property="sourceType" />
        <result column="source_id" property="sourceId" />
        <result column="description" property="description" />
        <result column="create_time" property="createTime" />
    </resultMap>

    <!-- 字段列表 -->
    <sql id="Base_Column_List">
        id, user_id, points, type, source_type, source_id, description, create_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.points != null">
                    AND points = #{filter.points}
                </if>
                <if test="filter.type != null">
                    AND type = #{filter.type}
                </if>
                <if test="filter.sourceType != null">
                    AND source_type = #{filter.sourceType}
                </if>
                <if test="filter.sourceId != null and filter.sourceId != ''">
                    AND source_id = #{filter.sourceId}
                </if>
                <if test="filter.description != null and filter.description != ''">
                    AND description LIKE CONCAT('%', #{filter.description}, '%')
                </if>
                <if test="filter.createTime != null">
                    AND create_time = #{filter.createTime}
                </if>
            </if>
        </where>
    </sql>

    <!-- 根据ID查询单条记录 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_points_record
        WHERE id = #{id}
    </select>

    <!-- 根据条件查询单条记录 -->
    <select id="selectOne" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_points_record
        <include refid="Base_Where_Clause" />
        LIMIT 1
    </select>

    <!-- 根据条件查询列表 -->
    <select id="selectList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_points_record
        <include refid="Base_Where_Clause" />
    </select>

    <!-- 根据条件分页查询 -->
    <select id="selectPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_points_record
        <include refid="Base_Where_Clause" />
    </select>

    <!-- 根据条件查询记录（支持排序） -->
    <select id="selectListWithOrder" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_points_record
        <include refid="Base_Where_Clause" />
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
            <if test="orderDirection != null and orderDirection != ''">
                ${orderDirection}
            </if>
        </if>
    </select>

    <!-- 根据条件分页查询（支持排序） -->
    <select id="selectPageWithOrder" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_points_record
        <include refid="Base_Where_Clause" />
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
            <if test="orderDirection != null and orderDirection != ''">
                ${orderDirection}
            </if>
        </if>
    </select>

    <!-- 插入单条记录 -->
    <insert id="insertOne" parameterType="org.xyzh.common.dto.usercenter.TbPointsRecord">
        INSERT INTO tb_points_record (
            id, user_id, points, type, source_type, source_id, description, create_time
        ) VALUES (
            #{entity.id}, #{entity.userId}, #{entity.points}, #{entity.type}, 
            #{entity.sourceType}, #{entity.sourceId}, #{entity.description}, #{entity.createTime}
        )
    </insert>

    <!-- 批量插入记录 -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO tb_points_record (
            id, user_id, points, type, source_type, source_id, description, create_time
        ) VALUES
        <foreach collection="entities" item="entity" separator=",">
            (
                #{entity.id}, #{entity.userId}, #{entity.points}, #{entity.type}, 
                #{entity.sourceType}, #{entity.sourceId}, #{entity.description}, #{entity.createTime}
            )
        </foreach>
    </insert>

    <!-- 根据ID更新记录 -->
    <update id="updateById" parameterType="org.xyzh.common.dto.usercenter.TbPointsRecord">
        UPDATE tb_points_record
        <set>
            <if test="entity.userId != null and entity.userId != ''">
                user_id = #{entity.userId},
            </if>
            <if test="entity.points != null">
                points = #{entity.points},
            </if>
            <if test="entity.type != null">
                type = #{entity.type},
            </if>
            <if test="entity.sourceType != null">
                source_type = #{entity.sourceType},
            </if>
            <if test="entity.sourceId != null and entity.sourceId != ''">
                source_id = #{entity.sourceId},
            </if>
            <if test="entity.description != null and entity.description != ''">
                description = #{entity.description},
            </if>
        </set>
        WHERE id = #{entity.id}
    </update>

    <!-- 根据条件更新记录 -->
    <update id="updateByFilter" parameterType="org.xyzh.common.dto.usercenter.TbPointsRecord">
        UPDATE tb_points_record
        <set>
            <if test="entity.userId != null and entity.userId != ''">
                user_id = #{entity.userId},
            </if>
            <if test="entity.points != null">
                points = #{entity.points},
            </if>
            <if test="entity.type != null">
                type = #{entity.type},
            </if>
            <if test="entity.sourceType != null">
                source_type = #{entity.sourceType},
            </if>
            <if test="entity.sourceId != null and entity.sourceId != ''">
                source_id = #{entity.sourceId},
            </if>
            <if test="entity.description != null and entity.description != ''">
                description = #{entity.description},
            </if>
        </set>
        <include refid="Base_Where_Clause" />
    </update>

    <!-- 删除方法使用MyBatis-Plus的BaseMapper提供的方法 -->
    <!-- deleteById(Serializable id) - 根据ID删除 -->
    <!-- deleteBatchIds(Collection<? extends Serializable> idList) - 批量删除 -->
    <!-- deleteByMap(Map<String, Object> columnMap) - 根据Map删除 -->
    <!-- delete(Wrapper<T> queryWrapper) - 根据条件删除 -->

    <!-- 根据条件统计记录数 -->
    <select id="countByFilter" resultType="long">
        SELECT COUNT(1)
        FROM tb_points_record
        <include refid="Base_Where_Clause" />
    </select>

    <!-- 检查记录是否存在 -->
    <select id="existsByFilter" resultType="boolean">
        SELECT COUNT(1) > 0
        FROM tb_points_record
        <include refid="Base_Where_Clause" />
    </select>

    <!-- 查询积分记录列表 -->
    <select id="selectPointsRecords" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_points_record
        <include refid="Base_Where_Clause" />
    </select>

    <!-- 根据用户ID查询积分记录 -->
    <select id="selectByUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_points_record
        WHERE user_id = #{userId}
        ORDER BY create_time DESC
    </select>

    <!-- 根据用户ID和类型查询积分记录 -->
    <select id="selectByUserIdAndType" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_points_record
        WHERE user_id = #{userId} AND type = #{type}
        ORDER BY create_time DESC
    </select>

    <!-- 根据用户ID查询积分记录（包含用户基本信息） -->
    <select id="selectPointsRecordsWithUser" resultMap="BaseResultMap">
        SELECT
        pr.id, pr.user_id, pr.points, pr.type, pr.source_type, pr.source_id, 
        pr.description, pr.create_time
        FROM tb_points_record pr
        LEFT JOIN tb_sys_user u ON pr.user_id = u.id
        WHERE pr.user_id = #{userId}
        ORDER BY pr.create_time DESC
    </select>

    <!-- 查询用户积分统计 -->
    <select id="selectPointsStatistics" resultMap="BaseResultMap">
        SELECT
        user_id,
        SUM(points) as points,
        COUNT(*) as type,
        MAX(create_time) as create_time
        FROM tb_points_record
        WHERE user_id = #{userId}
        GROUP BY user_id
    </select>

    <!-- 插入积分记录 -->
    <insert id="insertPointsRecord" parameterType="org.xyzh.common.dto.usercenter.TbPointsRecord">
        INSERT INTO tb_points_record (
            id, user_id, points, type, source_type, source_id, description, create_time
        ) VALUES (
            #{id}, #{userId}, #{points}, #{type}, #{sourceType}, #{sourceId}, 
            #{description}, #{createTime}
        )
    </insert>

    <!-- 更新积分记录 -->
    <update id="updatePointsRecord" parameterType="org.xyzh.common.dto.usercenter.TbPointsRecord">
        UPDATE tb_points_record
        <set>
            <if test="userId != null and userId != ''">
                user_id = #{userId},
            </if>
            <if test="points != null">
                points = #{points},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="sourceType != null">
                source_type = #{sourceType},
            </if>
            <if test="sourceId != null and sourceId != ''">
                source_id = #{sourceId},
            </if>
            <if test="description != null and description != ''">
                description = #{description},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除积分记录 -->
    <delete id="deletePointsRecord" parameterType="org.xyzh.common.dto.usercenter.TbPointsRecord">
        DELETE FROM tb_points_record
        WHERE id = #{id}
    </delete>

    <!-- 批量插入积分记录 -->
    <insert id="batchInsertPointsRecords" parameterType="java.util.List">
        INSERT INTO tb_points_record (
            id, user_id, points, type, source_type, source_id, description, create_time
        ) VALUES
        <foreach collection="pointsRecordList" item="item" separator=",">
            (
                #{item.id}, #{item.userId}, #{item.points}, #{item.type}, 
                #{item.sourceType}, #{item.sourceId}, #{item.description}, #{item.createTime}
            )
        </foreach>
    </insert>

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

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

    <!-- 统计积分记录总数 -->
    <select id="countPointsRecords" resultType="long">
        SELECT COUNT(1)
        FROM tb_points_record
        <include refid="Base_Where_Clause" />
    </select>

</mapper>