<?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.sunxung.factoring.mapper.sys.SessionMapper">

	<resultMap type="Session" id="sessionResult">
		<result property="id" column="id" />
		<result property="loginName" column="loginName" />
		<result property="userName" column="userName" />
		<result property="userId" column="userId" />
		<result property="url" column="url" />
		<result property="loginTime" column="loginTime" />
		<result property="laterTime" column="laterTime" />
		<result property="accessIp" column="accessIp" />
		<result property="remember" column="remember" />
		<result property="gmtCreate" column="gmt_create"/>
		<result property="gmtModified" column="gmt_modified"/>
	</resultMap>

	<insert id="add" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="Session">
		insert into t_session
		(loginName, userName, userId, url, loginTime, laterTime, accessIp,remember)
		values
		(#{loginName}, #{userName}, #{userId}, #{url}, #{loginTime}, #{laterTime}, #{accessIp},#{remember})
	</insert>

	<update id="update" parameterType="Session">
		update t_session 
		<set>
			id=#{id}
			<if test="url!=null and url!=''">
				, url=#{url}
			</if>
			<if test="laterTime!=null">
				, laterTime=#{laterTime}
			</if>
			<if test="accessIp!=null and accessIp!=''">
				, accessIp=#{accessIp}
			</if>
		</set>
		 where id=#{id}
	</update>

	<select id="findByUserId" parameterType="java.lang.Long"
		resultMap="sessionResult">
		select * from t_session where userId = #{value}
	</select>

	<select id="get" parameterType="java.lang.Long" resultMap="sessionResult">
		select * from t_session where id = #{value}
	</select>

	<delete id="delete" parameterType="java.lang.Long">
		delete from t_session where id = #{value}
	</delete>
	
	<delete id="deleteByUserId" parameterType="java.lang.Long">
		delete from t_session where userId = #{value}
	</delete>

	<select id="countBySearch" parameterType="BaseSearch" resultType="java.lang.Integer">
		select count(*) from t_session
	</select>

	<!-- 获取全部session -->
	<select id="findBySearch" parameterType="BaseSearch" resultMap="sessionResult">
		select * from t_session order by laterTime desc,loginTime desc
	</select>

</mapper>