<?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.backendsys.mapper.System.SysUserPointsHistoryMapper">

    <sql id="includeUserPointsHistory">
        uh.id id,
        uh.target_user_id target_user_id,
        u.username target_username,
        uh.point_adjustment point_adjustment,
        uh.point_balance point_balance,
        uh.activity_type activity_type,
        uh.activity_type_description activity_type_description,
        COALESCE(uh.activity_detail, '') activity_detail,
        uh.operator_type operator_type,
        COALESCE(uh.operator_user_id, '') operator_user_id,
        COALESCE(u2.username, '') operator_username,
        uh.create_time create_time,
        uh.update_time update_time
    </sql>

    <resultMap id="resultMapUserPointsHistoryList" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="target_user_id" column="target_user_id" javaType="java.lang.Long" />
        <result property="target_username" column="target_username" />
        <result property="point_adjustment" column="point_adjustment" javaType="java.lang.Float" />
        <result property="point_balance" column="point_balance" javaType="java.lang.Float" />
        <result property="activity_type" column="activity_type" />
        <result property="activity_type_description" column="activity_type_description" />
        <result property="activity_detail" column="activity_detail" />
        <result property="operator_type" column="operator_type" />
        <result property="operator_user_id" column="operator_user_id" javaType="java.lang.Long" />
        <result property="operator_username" column="operator_username" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <!-- 查询 列表 -->
    <select id="queryUserPointsHistoryList" resultMap="resultMapUserPointsHistoryList">
        SELECT <include refid="includeUserPointsHistory" />
        FROM sys_user_points_history uh
            LEFT JOIN sys_user u ON uh.target_user_id = u.id
            LEFT JOIN sys_user u2 ON uh.operator_user_id = u2.id
        <where>
            <if test="target_user_id != null and target_user_id != ''">
                AND uh.target_user_id = #{target_user_id}
            </if>
            <if test="activity_type != null and activity_type != ''">
                AND uh.activity_type = #{activity_type}
            </if>
            <if test="activity_detail != null and activity_detail != ''">
                AND uh.activity_detail LIKE CONCAT('%', #{activity_detail}, '%')
            </if>
            <if test="operator_type != null and operator_type != ''">
                AND uh.operator_type = #{operator_type}
            </if>
            <if test="operator_user_id != null and operator_user_id != ''">
                AND uh.operator_user_id = #{operator_user_id}
            </if>
            <if test="create_time_begin != null and create_time_begin != '' and create_time_end != null and create_time_end != ''">
                AND uh.create_time BETWEEN #{create_time_begin} AND #{create_time_end}
            </if>
        </where>
        ORDER BY uh.create_time DESC
    </select>

    <!-- 创建 -->
    <insert id="insertUserPointsHistory" parameterType="com.backendsys.entity.System.SysUserPointsDTO"
        useGeneratedKeys="true" keyProperty="target_user_id">
        INSERT INTO sys_user_points_history (
            target_user_id, point_adjustment, point_balance,
            activity_type, activity_type_description,
            operator_type
            <if test="activity_detail != null and activity_detail != ''">, activity_detail</if>
            <if test="operator_user_id != null and operator_user_id != ''">, operator_user_id</if>
        ) VALUES (
            #{target_user_id}, #{point_adjustment}, #{point_balance},
            #{activity_type}, #{activity_type_description},
            #{operator_type}
            <if test="activity_detail != null and activity_detail != ''">, #{activity_detail}</if>
            <if test="operator_user_id != null and operator_user_id != ''">, #{operator_user_id}</if>
        )
    </insert>

</mapper>