<?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.agent.AgentPointsTxMapper">
    <resultMap id="AgentPointsTxMap" type="com.gameplatform.server.model.entity.agent.AgentPointsTx">
        <id property="id" column="id" />
        <result property="accountId" column="account_id" />
        <result property="type" column="type" />
        <result property="beforePoints" column="before_points" />
        <result property="deltaPoints" column="delta_points" />
        <result property="afterPoints" column="after_points" />
        <result property="reason" column="reason" />
        <result property="refId" column="ref_id" />
        <result property="operatorId" column="operator_id" />
        <result property="createdAt" column="created_at" />
    </resultMap>

    <select id="findById" parameterType="long" resultMap="AgentPointsTxMap">
        SELECT id, account_id, type, before_points, delta_points, after_points, reason, ref_id, operator_id, created_at
        FROM agent_points_tx
        WHERE id = #{id}
        LIMIT 1
    </select>

    <insert id="insert" parameterType="com.gameplatform.server.model.entity.agent.AgentPointsTx" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO agent_points_tx (account_id, type, before_points, delta_points, after_points, reason, ref_id, operator_id)
        VALUES (#{accountId}, #{type}, #{beforePoints}, #{deltaPoints}, #{afterPoints}, #{reason}, #{refId}, #{operatorId})
    </insert>

    <select id="findByAccountId" resultMap="AgentPointsTxMap">
        SELECT id, account_id, type, before_points, delta_points, after_points, reason, ref_id, operator_id, created_at
        FROM agent_points_tx
        WHERE account_id = #{accountId}
        ORDER BY created_at DESC
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="countByAccountId" resultType="long">
        SELECT COUNT(1) FROM agent_points_tx WHERE account_id = #{accountId}
    </select>

    <select id="findByAccountIdAndType" resultMap="AgentPointsTxMap">
        SELECT id, account_id, type, before_points, delta_points, after_points, reason, ref_id, operator_id, created_at
        FROM agent_points_tx
        WHERE account_id = #{accountId} AND type = #{type}
        ORDER BY created_at DESC
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="countByAccountIdAndType" resultType="long">
        SELECT COUNT(1) FROM agent_points_tx WHERE account_id = #{accountId} AND type = #{type}
    </select>
</mapper>
