<?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.yonge.cooleshow.auth.dal.dao.SysUserDeviceDao">

	<resultMap type="com.yonge.cooleshow.auth.api.entity.SysUserDevice" id="SysUserDevice">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="device_num_" property="deviceNum" />
		<result column="bind_time_" property="bindTime" />
		<result column="device_type_" property="deviceType" />
		<result column="client_id_" property="clientId" />
		<result column="del_flag_" property="delFlag" />
		<result column="update_time_" property="updateTime" />
		<result column="phone_" property="user.phone" />
		<result column="real_name_" property="user.realName" />
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="SysUserDevice">
		SELECT * FROM
		sys_user_device WHERE id_ = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="SysUserDevice">
		SELECT * FROM sys_user_device
		ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.yonge.cooleshow.auth.api.entity.SysUserDevice"
		useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!-- <selectKey resultClass="int" keyProperty="id" > SELECT SEQ_WSDEFINITION_ID.nextval 
			AS ID FROM DUAL </selectKey> -->
		INSERT INTO sys_user_device
		(id_,user_id_,device_num_,bind_time_,device_type_,client_id_,del_flag_)
		VALUES(#{id},#{userId},#{deviceNum},#{bindTime},#{deviceType},#{clientId},0)
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.yonge.cooleshow.auth.api.entity.SysUserDevice">
		UPDATE sys_user_device
		<set>
			<if test="deviceType != null">
				device_type_ = #{deviceType},
			</if>
			<if test="userId != null">
				user_id_ = #{userId},
			</if>
			<if test="deviceNum != null">
				device_num_ = #{deviceNum},
			</if>
			<if test="id != null">
				id_ = #{id},
			</if>
			<if test="bindTime != null">
				bind_time_ = #{bindTime},
			</if>
			<if test="clientId != null">
				client_id_ = #{clientId},
			</if>
			<if test="delFlag != null">
				del_flag_ = #{delFlag},
			</if>
			update_time_ = now()
		</set>
		WHERE id_ = #{id}
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete">
		DELETE FROM sys_user_device WHERE id_ = #{id}
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="SysUserDevice" parameterType="map">
		SELECT ud.*,u.phone_,u.real_name_ FROM sys_user_device ud left join sys_user u on ud.user_id_ = u.id_ 
		where ud.del_flag_ = 0
		<if test="search != null and search != ''">
			and (u.real_name_ LIKE CONCAT('%',#{search},'%') OR u.phone_ LIKE CONCAT('%',#{search},'%') OR u.id_ like CONCAT('%',#{search},'%'))
		</if>
		<if test="deviceNum != null">
			and device_num_ = #{deviceNum}
		</if>
		<if test="bindStartTime != null">
			and date(bind_time_) &gt;= #{bindStartTime}
		</if>
		<if test="bindEndTime != null">
			and date(bind_time_) &lt;= #{bindEndTime}
		</if>
		ORDER BY id_
		<include refid="global.limit" />
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(ud.user_id_) FROM sys_user_device ud left join sys_user u on ud.user_id_ = u.id_
		where ud.del_flag_ = 0
		<if test="search != null and search != ''">
			and (u.real_name_ LIKE CONCAT('%',#{search},'%') OR u.phone_ LIKE CONCAT('%',#{search},'%') OR u.id_ like CONCAT('%',#{search},'%'))
		</if>
		<if test="deviceNum != null">
			and device_num_ = #{deviceNum}
		</if>
		<if test="bindStartTime != null">
			and date(bind_time_) &gt;= #{bindStartTime}
		</if>
		<if test="bindEndTime != null">
			and date(bind_time_) &lt;= #{bindEndTime}
		</if>
	</select>
	
	<select id="queryByUserId" resultMap="SysUserDevice" parameterType="map">
		SELECT * FROM sys_user_device WHERE user_id_ = #{userId} and del_flag_ = 0
		<if test="clientId != null">
			and client_id_ = #{clientId}
		</if>
	</select>
	
	<select id="queryByDeviceNum" resultMap="SysUserDevice" parameterType="map">
		SELECT * FROM sys_user_device WHERE device_num_ = #{deviceNum} and del_flag_ = 0
		<if test="clientId != null">
			and client_id_ = #{clientId}
		</if>
	</select>
	
</mapper>
