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

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.usercenter.TbUserBrowseRecord">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="browse_type" property="browseType" />
        <result column="browse_id" property="browseId" />
        <result column="browse_time" property="browseTime" />
        <result column="ip_address" property="ipAddress" />
    </resultMap>

    <!-- 字段列表 -->
    <sql id="Base_Column_List">
        id, user_id, browse_type, browse_id, browse_time, ip_address
    </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.browseType != null">
                    AND browse_type = #{filter.browseType}
                </if>
                <if test="filter.browseId != null and filter.browseId != ''">
                    AND browse_id = #{filter.browseId}
                </if>
                <if test="filter.ipAddress != null and filter.ipAddress != ''">
                    AND ip_address = #{filter.ipAddress}
                </if>
                <if test="filter.browseTime != null">
                    AND browse_time = #{filter.browseTime}
                </if>
            </if>
        </where>
    </sql>

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

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

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

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

    <!-- 根据条件查询记录（支持排序） -->
    <select id="selectListWithOrder" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_browse_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_user_browse_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.TbUserBrowseRecord">
        INSERT INTO tb_user_browse_record (
            id, user_id, browse_type, browse_id, browse_time, ip_address
        ) VALUES (
            #{entity.id}, #{entity.userId}, #{entity.browseType}, #{entity.browseId}, 
            #{entity.browseTime}, #{entity.ipAddress}
        )
    </insert>

    <!-- 批量插入记录 -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO tb_user_browse_record (
            id, user_id, browse_type, browse_id, browse_time, ip_address
        ) VALUES
        <foreach collection="entities" item="entity" separator=",">
            (
                #{entity.id}, #{entity.userId}, #{entity.browseType}, #{entity.browseId}, 
                #{entity.browseTime}, #{entity.ipAddress}
            )
        </foreach>
    </insert>

    <!-- 根据ID更新记录 -->
    <update id="updateById" parameterType="org.xyzh.common.dto.usercenter.TbUserBrowseRecord">
        UPDATE tb_user_browse_record
        <set>
            <if test="entity.userId != null and entity.userId != ''">
                user_id = #{entity.userId},
            </if>
            <if test="entity.browseType != null">
                browse_type = #{entity.browseType},
            </if>
            <if test="entity.browseId != null and entity.browseId != ''">
                browse_id = #{entity.browseId},
            </if>
            <if test="entity.browseTime != null">
                browse_time = #{entity.browseTime},
            </if>
            <if test="entity.ipAddress != null and entity.ipAddress != ''">
                ip_address = #{entity.ipAddress},
            </if>
        </set>
        WHERE id = #{entity.id}
    </update>

    <!-- 根据条件更新记录 -->
    <update id="updateByFilter" parameterType="org.xyzh.common.dto.usercenter.TbUserBrowseRecord">
        UPDATE tb_user_browse_record
        <set>
            <if test="entity.userId != null and entity.userId != ''">
                user_id = #{entity.userId},
            </if>
            <if test="entity.browseType != null">
                browse_type = #{entity.browseType},
            </if>
            <if test="entity.browseId != null and entity.browseId != ''">
                browse_id = #{entity.browseId},
            </if>
            <if test="entity.browseTime != null">
                browse_time = #{entity.browseTime},
            </if>
            <if test="entity.ipAddress != null and entity.ipAddress != ''">
                ip_address = #{entity.ipAddress},
            </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_user_browse_record
        <include refid="Base_Where_Clause" />
    </select>

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

    <!-- 查询用户浏览记录列表 -->
    <select id="selectUserBrowseRecords" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_browse_record
        <include refid="Base_Where_Clause" />
    </select>

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

    <!-- 根据用户ID和类型查询浏览记录 -->
    <select id="selectByUserIdAndType" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_browse_record
        WHERE user_id = #{userId} AND browse_type = #{browseType}
        ORDER BY browse_time DESC
    </select>

    <!-- 根据用户ID查询浏览记录（包含用户基本信息） -->
    <select id="selectUserBrowseRecordsWithUser" resultMap="BaseResultMap">
        SELECT
        ubr.id, ubr.user_id, ubr.browse_type, ubr.browse_id, ubr.browse_time, ubr.ip_address
        FROM tb_user_browse_record ubr
        LEFT JOIN tb_sys_user u ON ubr.user_id = u.id
        WHERE ubr.user_id = #{userId}
        ORDER BY ubr.browse_time DESC
    </select>

    <!-- 查询用户浏览统计 -->
    <select id="selectBrowseStatistics" resultMap="BaseResultMap">
        SELECT
        user_id,
        COUNT(*) as browse_type,
        MAX(browse_time) as browse_time
        FROM tb_user_browse_record
        WHERE user_id = #{userId}
        GROUP BY user_id
    </select>

    <!-- 插入用户浏览记录 -->
    <insert id="insertUserBrowseRecord" parameterType="org.xyzh.common.dto.usercenter.TbUserBrowseRecord">
        INSERT INTO tb_user_browse_record (
            id, user_id, browse_type, browse_id, browse_time, ip_address
        ) VALUES (
            #{id}, #{userId}, #{browseType}, #{browseId}, #{browseTime}, #{ipAddress}
        )
    </insert>

    <!-- 更新用户浏览记录 -->
    <update id="updateUserBrowseRecord" parameterType="org.xyzh.common.dto.usercenter.TbUserBrowseRecord">
        UPDATE tb_user_browse_record
        <set>
            <if test="userId != null and userId != ''">
                user_id = #{userId},
            </if>
            <if test="browseType != null">
                browse_type = #{browseType},
            </if>
            <if test="browseId != null and browseId != ''">
                browse_id = #{browseId},
            </if>
            <if test="browseTime != null">
                browse_time = #{browseTime},
            </if>
            <if test="ipAddress != null and ipAddress != ''">
                ip_address = #{ipAddress},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除用户浏览记录 -->
    <delete id="deleteUserBrowseRecord" parameterType="org.xyzh.common.dto.usercenter.TbUserBrowseRecord">
        DELETE FROM tb_user_browse_record
        WHERE id = #{id}
    </delete>

    <!-- 批量插入用户浏览记录 -->
    <insert id="batchInsertUserBrowseRecords" parameterType="java.util.List">
        INSERT INTO tb_user_browse_record (
            id, user_id, browse_type, browse_id, browse_time, ip_address
        ) VALUES
        <foreach collection="userBrowseRecordList" item="item" separator=",">
            (
                #{item.id}, #{item.userId}, #{item.browseType}, #{item.browseId}, 
                #{item.browseTime}, #{item.ipAddress}
            )
        </foreach>
    </insert>

    <!-- 批量删除用户浏览记录 -->
    <delete id="batchDeleteUserBrowseRecords">
        DELETE FROM tb_user_browse_record
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 分页查询用户浏览记录 -->
    <select id="selectUserBrowseRecordsPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_browse_record
        <include refid="Base_Where_Clause" />
        ORDER BY browse_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计用户浏览记录总数 -->
    <select id="countUserBrowseRecords" resultType="long">
        SELECT COUNT(1)
        FROM tb_user_browse_record
        <include refid="Base_Where_Clause" />
    </select>

</mapper>