<?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.ff.common.mapper.TenantGameQuotaMapper">

    <resultMap type="TenantGameQuota" id="TenantGameQuotaResult">
        <result property="id"    column="id"    />
        <result property="tenantKey"    column="tenant_key"    />
        <result property="balance"    column="balance"    />
        <result property="quotaType"    column="quota_type"    />
        <result property="version"    column="version"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectTenantGameQuotaVo">
        select id, tenant_key, balance, quota_type, version, create_by, create_time, update_by, update_time from ff_tenant_game_quota
    </sql>

    <select id="selectTenantGameQuotaByTenantKey"  resultMap="TenantGameQuotaResult">
        <include refid="selectTenantGameQuotaVo"/>
        where tenant_key = #{tenantKey}  and quota_type = #{quotaType}
    </select>

    <select id="selectTenantGameQuotaList" parameterType="TenantGameQuota" resultMap="TenantGameQuotaResult">
        <include refid="selectTenantGameQuotaVo"/>
        <where>
            <if test="tenantKey != null  and tenantKey != ''"> and tenant_key = #{tenantKey}</if>
            <if test="balance != null "> and balance = #{balance}</if>
            <if test="quotaType != null "> and quota_type = #{quotaType}</if>
            <if test="version != null "> and version = #{version}</if>
        </where>
    </select>

    <select id="selectTenantGameQuotaById" parameterType="Long" resultMap="TenantGameQuotaResult">
        <include refid="selectTenantGameQuotaVo"/>
        where id = #{id}
    </select>

    <update id="changeBalance" parameterType="TenantGameQuota">
        update ff_tenant_game_quota set    balance = #{balance},version =version+1
        where tenant_key = #{tenantKey} and quota_type = #{quotaType}  and  version = #{version}
    </update>

    <insert id="insertTenantGameQuota" parameterType="TenantGameQuota" useGeneratedKeys="true" keyProperty="id">
        insert into ff_tenant_game_quota
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tenantKey != null and tenantKey != ''">tenant_key,</if>
            <if test="balance != null">balance,</if>
            <if test="quotaType != null">quota_type,</if>
            <if test="version != null">version,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tenantKey != null and tenantKey != ''">#{tenantKey},</if>
            <if test="balance != null">#{balance},</if>
            <if test="quotaType != null">#{quotaType},</if>
            <if test="version != null">#{version},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateTenantGameQuota" parameterType="TenantGameQuota">
        update ff_tenant_game_quota
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantKey != null and tenantKey != ''">tenant_key = #{tenantKey},</if>
            <if test="balance != null">balance = #{balance},</if>
            <if test="quotaType != null">quota_type = #{quotaType},</if>
            <if test="version != null">version = #{version},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTenantGameQuotaById" parameterType="Long">
        delete from ff_tenant_game_quota where id = #{id}
    </delete>

    <delete id="deleteTenantGameQuotaByIds" parameterType="String">
        delete from ff_tenant_game_quota where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>