<?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.sooka.system.mapper.TTempPermissionMapper">

    <resultMap type="TTempPermission" id="TTempPermissionResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="loginName"    column="login_name"    />
        <result property="userName"    column="user_name"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="remainingDays"    column="remaining_days"    />
    </resultMap>

    <sql id="selectTTempPermissionVo">
        SELECT
            *
        FROM
            (
                SELECT
                    id,
                    t.user_id,
                    u.login_name,
                    u.user_name,
                    d.dept_id,
                    d.dept_name,
                    t.create_by,
                    t.create_time,
                    t.update_by,
                    t.update_time,
                    start_time,
                    end_time,
                    TO_DAYS(end_time) - TO_DAYS(NOW()) remaining_days
                FROM
                    t_temp_permission t
                LEFT JOIN sys_user u ON t.user_id = u.user_id
                LEFT JOIN sys_dept d ON u.dept_id = d.dept_id
            ) tb
    </sql>

    <select id="selectTTempPermissionList" parameterType="TTempPermission" resultMap="TTempPermissionResult">
        <include refid="selectTTempPermissionVo"/>
        <where>
            <if test="loginName != null  and loginName != ''"> and login_name like concat('%', #{loginName}, '%')</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="endTime != null "> and remaining_days <![CDATA[<=]]> 7</if>
        </where>
        order by remaining_days asc
    </select>

    <select id="selectTTempPermissionById" parameterType="Long" resultMap="TTempPermissionResult">
        <include refid="selectTTempPermissionVo"/>
        where id = #{id}
    </select>

    <insert id="insertTTempPermission" parameterType="TTempPermission" useGeneratedKeys="true" keyProperty="id">
        insert into t_temp_permission
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
         </trim>
    </insert>

    <update id="updateTTempPermission" parameterType="TTempPermission">
        update t_temp_permission
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTTempPermissionById" parameterType="Long">
        delete from t_temp_permission where id = #{id}
    </delete>

    <delete id="deleteTTempPermissionByIds" parameterType="String">
        delete from t_temp_permission where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
