<?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.achievement.mapper.UserAchievementProgressMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.usercenter.TbUserAchievementProgress">
        <id column="id" property="ID" />
        <result column="user_id" property="userID" />
        <result column="achievement_id" property="achievementID" />
        <result column="current_value" property="currentValue" />
        <result column="target_value" property="targetValue" />
        <result column="progress_percentage" property="progressPercentage" />
        <result column="completed" property="completed" />
        <result column="last_update_time" property="lastUpdateTime" />
        <result column="create_time" property="createTime" />
    </resultMap>

    <!-- 字段列表 -->
    <sql id="Base_Column_List">
        id, user_id, achievement_id, current_value, target_value, progress_percentage, 
        completed, last_update_time, create_time
    </sql>

    <!-- 根据用户ID和成就ID查询进度 -->
    <select id="selectByUserIdAndAchievementId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_achievement_progress
        WHERE user_id = #{userId} AND achievement_id = #{achievementId}
        LIMIT 1
    </select>

    <!-- 根据用户ID查询所有进度 -->
    <select id="selectByUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_achievement_progress
        WHERE user_id = #{userId}
        ORDER BY last_update_time DESC
    </select>

    <!-- 根据用户ID和条件类型查询进度 -->
    <select id="selectByUserIdAndConditionType" resultMap="BaseResultMap">
        SELECT
        p.id, p.user_id, p.achievement_id, p.current_value, p.target_value, 
        p.progress_percentage, p.completed, p.last_update_time, p.create_time
        FROM tb_user_achievement_progress p
        INNER JOIN tb_achievement a ON p.achievement_id = a.achievement_id
        WHERE p.user_id = #{userId} AND a.condition_type = #{conditionType} AND a.deleted = 0
        ORDER BY p.last_update_time DESC
    </select>

    <!-- 查询未完成的进度 -->
    <select id="selectIncompletedByUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_user_achievement_progress
        WHERE user_id = #{userId} AND completed = 0
        ORDER BY last_update_time DESC
    </select>

    <!-- 插入进度记录 -->
    <insert id="insertProgress" parameterType="org.xyzh.common.dto.usercenter.TbUserAchievementProgress">
        INSERT INTO tb_user_achievement_progress (
            id, user_id, achievement_id, current_value, target_value, 
            progress_percentage, completed, last_update_time, create_time
        ) VALUES (
            #{ID}, #{userID}, #{achievementID}, #{currentValue}, #{targetValue},
            #{progressPercentage}, #{completed}, #{lastUpdateTime}, #{createTime}
        )
    </insert>

    <!-- 更新进度记录 -->
    <update id="updateProgress" parameterType="org.xyzh.common.dto.usercenter.TbUserAchievementProgress">
        UPDATE tb_user_achievement_progress
        <set>
            <if test="currentValue != null">
                current_value = #{currentValue},
            </if>
            <if test="targetValue != null">
                target_value = #{targetValue},
            </if>
            <if test="progressPercentage != null">
                progress_percentage = #{progressPercentage},
            </if>
            <if test="completed != null">
                completed = #{completed},
            </if>
            <if test="lastUpdateTime != null">
                last_update_time = #{lastUpdateTime},
            </if>
        </set>
        WHERE id = #{ID}
    </update>

    <!-- 增加进度值 -->
    <update id="incrementProgress">
        UPDATE tb_user_achievement_progress
        SET 
            current_value = current_value + #{incrementValue},
            progress_percentage = LEAST(100, ROUND(((current_value + #{incrementValue}) * 100.0 / target_value), 0)),
            completed = IF((current_value + #{incrementValue}) >= target_value, 1, 0),
            last_update_time = NOW()
        WHERE user_id = #{userId} AND achievement_id = #{achievementId}
    </update>

    <!-- 批量插入进度记录 -->
    <insert id="batchInsertProgress" parameterType="java.util.List">
        INSERT INTO tb_user_achievement_progress (
            id, user_id, achievement_id, current_value, target_value, 
            progress_percentage, completed, last_update_time, create_time
        ) VALUES
        <foreach collection="progressList" item="item" separator=",">
            (
                #{item.ID}, #{item.userID}, #{item.achievementID}, #{item.currentValue}, 
                #{item.targetValue}, #{item.progressPercentage}, #{item.completed}, 
                #{item.lastUpdateTime}, #{item.createTime}
            )
        </foreach>
    </insert>

    <!-- 删除进度记录 -->
    <delete id="deleteProgress">
        DELETE FROM tb_user_achievement_progress
        WHERE user_id = #{userId} AND achievement_id = #{achievementId}
    </delete>

</mapper>
