<?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.kexue.skills.mapper.PointsAccountMapper">

    <resultMap type="com.kexue.skills.entity.PointsAccount" id="PointsAccountMap">
        <result property="accountId" column="account_id" jdbcType="BIGINT"/>
        <result property="userId" column="user_id" jdbcType="BIGINT"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="totalPoints" column="total_points" jdbcType="INTEGER"/>
        <result property="availablePoints" column="available_points" jdbcType="INTEGER"/>
        <result property="frozenPoints" column="frozen_points" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="deleteFlag" column="delete_flag" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->    
    <select id="queryById" resultMap="PointsAccountMap">
        select 
          account_id, user_id, user_name, total_points, available_points, frozen_points, create_time, update_time, create_by, update_by, delete_flag
        from points_account
        where account_id = #{accountId}
    </select>

    <!--通过用户ID查询-->    
    <select id="queryByUserId" resultMap="PointsAccountMap">
        select 
          account_id, user_id, user_name, total_points, available_points, frozen_points, create_time, update_time, create_by, update_by, delete_flag
        from points_account
        where user_id = #{userId}
    </select>

    <!--分页查询-->    
    <select id="getPageList" resultMap="PointsAccountMap">
        select 
          account_id, user_id, user_name, total_points, available_points, frozen_points, create_time, update_time, create_by, update_by, delete_flag
        from points_account
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="userName != null and userName != ''">
                and user_name like concat('%', #{userName}, '%')
            </if>
            <if test="totalPoints != null">
                and total_points <![CDATA[ >= ]]> #{totalPoints}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        <if test="sortBy != null and sortBy != ''">
            order by ${sortBy} ${sortDesc ? 'desc' : 'asc'}
        </if>
    </select>

    <!--查询列表-->    
    <select id="getList" resultMap="PointsAccountMap">
        select 
          account_id, user_id, user_name, total_points, available_points, frozen_points, create_time, update_time, create_by, update_by, delete_flag
        from points_account
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="userName != null and userName != ''">
                and user_name like concat('%', #{userName}, '%')
            </if>
            <if test="totalPoints != null">
                and total_points <![CDATA[ >= ]]> #{totalPoints}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
    </select>

    <!--新增数据-->    
    <insert id="insert" useGeneratedKeys="true" keyProperty="accountId">
        insert into points_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="totalPoints != null">total_points,</if>
            <if test="availablePoints != null">available_points,</if>
            <if test="frozenPoints != null">frozen_points,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="deleteFlag != null">delete_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="totalPoints != null">#{totalPoints},</if>
            <if test="availablePoints != null">#{availablePoints},</if>
            <if test="frozenPoints != null">#{frozenPoints},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="deleteFlag != null">#{deleteFlag},</if>
        </trim>
    </insert>

    <!--更新数据-->    
    <update id="update">
        update points_account
        <set>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="totalPoints != null">total_points = #{totalPoints},</if>
            <if test="availablePoints != null">available_points = #{availablePoints},</if>
            <if test="frozenPoints != null">frozen_points = #{frozenPoints},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="deleteFlag != null">delete_flag = #{deleteFlag},</if>
        </set>
        where account_id = #{accountId}
    </update>

    <!--更新积分-->    
    <update id="updatePoints">
        update points_account
        <set>
            <if test="type == 1">
                total_points = total_points + #{points},
                available_points = available_points + #{points}
            </if>
            <if test="type == 2">
                total_points = total_points - #{points},
                available_points = available_points - #{points}
            </if>
            update_time = now()
        </set>
        where user_id = #{userId}
    </update>

    <!--逻辑删除-->    
    <update id="logicDeleteById">
        update points_account
        set delete_flag = 1,
            update_by = #{updateBy},
            update_time = now()
        where account_id = #{accountId}
    </update>

    <!--物理删除-->    
    <delete id="deleteById">
        delete from points_account
        where account_id = #{accountId}
    </delete>
</mapper>