<?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.m2pool.system.mapper.SysUserLevelMapper">

    <select id="getUserLevelInfo" resultType="com.m2pool.system.api.entity.SysUserLeveDate">
        SELECT
	        l.id,l.user_id userId,u.user_name userName,u.email email,l.level_type levelType,l.create_time startTime,l.expire_time expireTime,r.role_id roleId
	    FROM
            sys_user_level_date l
            LEFT JOIN sys_user u ON l.user_id = u.user_id
            LEFT JOIN sys_user_role ur on ur.user_id = u.user_id
            LEFT JOIN sys_role r on r.role_id = ur.role_id
        where l.user_id = #{userId} and l.status = 0 order by l.create_time desc limit 1
    </select>

    <select id="getExpireUserIds" resultType="java.lang.Long">
            select id from sys_user_level_date where expire_time &lt;= sysdate() and status = 0
    </select>

    <insert id="createUserLevelInfo">
        insert into sys_user_level_date(user_id,create_time,expire_time,level_type,status) values
        (#{vo.userId},#{vo.startTime},#{vo.expireTime},#{vo.levelType},0)
    </insert>

    <update id="deleteExpireUserLevelInfo">
        update sys_user_level_date set status = 1 where  user_id in
        <foreach collection="ids" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </update>

    <delete id="deleteUserLevelInfoByUserId">
        delete from sys_user_level_date where user_id = #{userId}
    </delete>


</mapper>

