<?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.mmc.iuav.user.dao.userpoints.UserPointsDao">

    <resultMap id="pointList" type="com.mmc.iuav.user.entity.userpoints.UserPointsDO">
        <result property="id" column="id"/>
        <result property="userAccountId" column="user_account_id"/>
        <result property="totalPoints" column="total_points"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="nickName" column="nick_name"/>
        <result property="phoneNum" column="phone_num"/>
    </resultMap>

    <select id="selectUserPoints" resultType="com.mmc.iuav.user.entity.userpoints.UserPointsDO">
        select id, user_account_id, total_points
        from user_points
        where user_account_id = #{id}
    </select>
    <update id="updatePoints" parameterType="com.mmc.iuav.user.entity.userpoints.UserPointsDO">
        UPDATE user_points
        <set>
            total_points = #{totalPoints},
            update_time=NOW()
        </set>
        where user_account_id = #{userAccountId}
    </update>
    <insert id="insertPoints" parameterType="com.mmc.iuav.user.entity.userpoints.UserPointsDO">
        insert into user_points(total_points, user_account_id, create_time,
                                update_time)
        values (#{totalPoints}, #{userAccountId}, NOW(), NOW())
    </insert>

    <select id="userPointList" resultMap="pointList" parameterType="com.mmc.iuav.user.model.qo.userpoints.UserPointQO">
        SELECT up.id,
        up.user_account_id,
        up.total_points,
        up.create_time,
        up.update_time,
        ua.nick_name,
        ua.phone_num
        FROM user_points up
        INNER JOIN user_account ua ON up.user_account_id = ua.id
        WHERE ua.`disable` = 0
        <if test=" phoneNum != null and phoneNum != '' ">
            and ua.phone_num like CONCAT("%",#{phoneNum},"%")
        </if>
        <if test=" nickName != null and nickName != '' ">
            and ua.nick_name like CONCAT("%",#{nickName},"%")
        </if>
        order by up.create_time desc
        limit #{pageNo}, #{pageSize}
    </select>

    <select id="count" resultType="java.lang.Integer">
        SELECT count(*)
        FROM user_points up
        INNER JOIN user_account ua ON up.user_account_id = ua.id
        WHERE ua.`disable` = 0
        <if test=" phoneNum != null and phoneNum != '' ">
            and ua.phone_num like CONCAT("%",#{phoneNum},"%")
        </if>
        <if test=" nickName != null and nickName != '' ">
            and ua.nick_name like CONCAT("%",#{nickName},"%")
        </if>
    </select>


</mapper>
