<?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.PointsTransactionMapper">

    <resultMap type="com.kexue.skills.entity.PointsTransaction" id="PointsTransactionMap">
        <result property="transactionId" column="transaction_id" jdbcType="BIGINT"/>
        <result property="userId" column="user_id" jdbcType="BIGINT"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="transactionType" column="transaction_type" jdbcType="INTEGER"/>
        <result property="points" column="points" jdbcType="INTEGER"/>
        <result property="beforePoints" column="before_points" jdbcType="INTEGER"/>
        <result property="afterPoints" column="after_points" jdbcType="INTEGER"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="transactionNo" column="transaction_no" jdbcType="VARCHAR"/>
        <result property="payType" column="pay_type" jdbcType="INTEGER"/>
        <result property="businessId" column="business_id" jdbcType="BIGINT"/>
        <result property="businessType" column="business_type" jdbcType="VARCHAR"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <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="PointsTransactionMap">
        select 
          transaction_id, user_id, user_name, transaction_type, points, before_points, after_points, status, 
          transaction_no, pay_type, business_id, business_type, remark, create_time, update_time, create_by, update_by, delete_flag
        from points_transaction
        where transaction_id = #{transactionId}
    </select>

    <!--分页查询-->    
    <select id="getPageList" resultMap="PointsTransactionMap">
        select 
          transaction_id, user_id, user_name, transaction_type, points, before_points, after_points, status, 
          transaction_no, pay_type, business_id, business_type, remark, create_time, update_time, create_by, update_by, delete_flag
        from points_transaction
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="transactionType != null">
                and transaction_type = #{transactionType}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="transactionNo != null and transactionNo != ''">
                and transaction_no = #{transactionNo}
            </if>
            <if test="businessType != null and businessType != ''">
                and business_type = #{businessType}
            </if>
            <if test="createTimeStart != null">
                and create_time <![CDATA[ >= ]]> #{createTimeStart}
            </if>
            <if test="createTimeEnd != null">
                and create_time <![CDATA[ <= ]]> #{createTimeEnd}
            </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="PointsTransactionMap">
        select 
          transaction_id, user_id, user_name, transaction_type, points, before_points, after_points, status, 
          transaction_no, pay_type, business_id, business_type, remark, create_time, update_time, create_by, update_by, delete_flag
        from points_transaction
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="transactionType != null">
                and transaction_type = #{transactionType}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="transactionNo != null and transactionNo != ''">
                and transaction_no = #{transactionNo}
            </if>
            <if test="businessType != null and businessType != ''">
                and business_type = #{businessType}
            </if>
            <if test="createTimeStart != null">
                and create_time <![CDATA[ >= ]]> #{createTimeStart}
            </if>
            <if test="createTimeEnd != null">
                and create_time <![CDATA[ <= ]]> #{createTimeEnd}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
    </select>

    <!--新增数据-->    
    <insert id="insert" useGeneratedKeys="true" keyProperty="transactionId">
        insert into points_transaction
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="transactionType != null">transaction_type,</if>
            <if test="points != null">points,</if>
            <if test="beforePoints != null">before_points,</if>
            <if test="afterPoints != null">after_points,</if>
            <if test="status != null">status,</if>
            <if test="transactionNo != null">transaction_no,</if>
            <if test="payType != null">pay_type,</if>
            <if test="businessId != null">business_id,</if>
            <if test="businessType != null">business_type,</if>
            <if test="remark != null">remark,</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="transactionType != null">#{transactionType},</if>
            <if test="points != null">#{points},</if>
            <if test="beforePoints != null">#{beforePoints},</if>
            <if test="afterPoints != null">#{afterPoints},</if>
            <if test="status != null">#{status},</if>
            <if test="transactionNo != null">#{transactionNo},</if>
            <if test="payType != null">#{payType},</if>
            <if test="businessId != null">#{businessId},</if>
            <if test="businessType != null">#{businessType},</if>
            <if test="remark != null">#{remark},</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_transaction
        <set>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="transactionType != null">transaction_type = #{transactionType},</if>
            <if test="points != null">points = #{points},</if>
            <if test="beforePoints != null">before_points = #{beforePoints},</if>
            <if test="afterPoints != null">after_points = #{afterPoints},</if>
            <if test="status != null">status = #{status},</if>
            <if test="transactionNo != null">transaction_no = #{transactionNo},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="businessType != null">business_type = #{businessType},</if>
            <if test="remark != null">remark = #{remark},</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 transaction_id = #{transactionId}
    </update>

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

    <!--物理删除-->    
    <delete id="deleteById">
        delete from points_transaction
        where transaction_id = #{transactionId}
    </delete>
</mapper>