<?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.gameplatform.server.mapper.account.UserAccountMapper">
    <resultMap id="UserAccountMap" type="com.gameplatform.server.model.entity.account.UserAccount">
        <id property="id" column="id" />
        <result property="userType" column="user_type" />
        <result property="username" column="username" />
        <result property="passwordHash" column="password_hash" />
        <result property="status" column="status" />
        <result property="pointsBalance" column="points_balance" />
        <result property="createdAt" column="created_at" />
        <result property="updatedAt" column="updated_at" />
    </resultMap>

    <select id="findByUsernameAndType" resultMap="UserAccountMap">
        SELECT id, user_type, username, password_hash, status, points_balance, created_at, updated_at
        FROM user_account
        WHERE username = #{username}
          AND user_type = #{userType}
        LIMIT 1
    </select>

    <select id="findByUsername" resultMap="UserAccountMap">
        SELECT id, user_type, username, password_hash, status, points_balance, created_at, updated_at
        FROM user_account
        WHERE username = #{username}
        LIMIT 1
    </select>



    <update id="setStatus">
        UPDATE user_account SET status = #{status} WHERE id = #{id}
    </update>

    <update id="updatePassword">
        UPDATE user_account SET password_hash = #{passwordHash} WHERE id = #{id}
    </update>

    <select id="countByFilter" resultType="long">
        SELECT COUNT(1) FROM user_account
        <where>
            <if test="userType != null and userType != ''">AND user_type = #{userType}</if>
            <if test="status != null and status != ''">AND status = #{status}</if>
            <if test="keyword != null and keyword != ''">
                AND username LIKE CONCAT('%', #{keyword}, '%')
            </if>
        </where>
    </select>

    <select id="listByFilter" resultMap="UserAccountMap">
        SELECT id, user_type, username, password_hash, status, points_balance, created_at, updated_at
        FROM user_account
        <where>
            <if test="userType != null and userType != ''">AND user_type = #{userType}</if>
            <if test="status != null and status != ''">AND status = #{status}</if>
            <if test="keyword != null and keyword != ''">
                AND username LIKE CONCAT('%', #{keyword}, '%')
            </if>
        </where>
        ORDER BY id DESC
        LIMIT #{size} OFFSET #{offset}
    </select>
</mapper>
