<?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.cftech.base.org.dao.TerminalUserMapper">

	<resultMap id="resultMap" type="com.cftech.base.org.model.TerminalUser">
		<id column="terminal_id" property="terminalId"/>
		<result column="user_id" property="userId"/>
	</resultMap>

	<sql id="sqlWhere">
		<if test="conds!=null">
			<trim prefix="WHERE" prefixOverrides="AND|OR">
				<foreach collection="conds.conds" index="index" item="cond">
					${cond.linkType}
					<if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
					<if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
					<if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
					<if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
					<if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
					<if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
					<if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND #{cond.endValue}</if>
					<if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
					<if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
					<if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
					<if test="cond.condType == 'IN'">${cond.param} IN
						<foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
							#{item}
						</foreach>
					</if>
				</foreach>
			</trim>
		</if>
	</sql>

	<sql id="sqlColumns">
		terminal_id, user_id
	</sql>

	<insert id="save" parameterType="com.cftech.base.org.model.TerminalUser" keyProperty="id">
		insert into wx_qy_terminal_user
		(
		<include refid="sqlColumns"/>
		)
		values
		(
		#{terminalId, jdbcType=BIGINT},  #{userId, jdbcType=BIGINT}
		)
	</insert>

	<select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
		SELECT <include refid="sqlColumns" /> FROM wx_qy_terminal_user t
		WHERE t.id=#{id}
	</select>

	<select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT COUNT(1)  FROM wx_qy_terminal_user<include refid="sqlWhere" />
	</select>

	<select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
		SELECT <include refid="sqlColumns" />, terminal.name terminalName,qyuser.wxuser_id wxUserId
		FROM wx_qy_terminal_user t
		left join wx_qy_terminal terminal on terminal.id = t.terminal_id
		left join t_qyuser qyuser on qyuser.id = t.user_id
		<include refid="sqlWhere" />
		<if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
		<if test="limit>0">limit #{offset},#{limit}</if>
	</select>

	<update id="update" parameterType="com.cftech.base.org.model.TerminalUser">
		update wx_qy_terminal_user
		<set>
			<if test="terminalId != null">
				terminal_id = #{terminalId, jdbcType=BIGINT},
			</if>
			<if test="userId != null">
				user_id = #{userId, jdbcType=BIGINT},
			</if>
		</set>
		where id=#{id,jdbcType=BIGINT}
	</update>

	<update id="delete" parameterType="java.lang.Long">
		update wx_qy_terminal_user set delflag=1 where id=#{id,jdbcType=BIGINT}
	</update>
	
	<select id="getTerminalPro" parameterType="java.lang.Long" resultType="java.util.Map">
		select Group_concat(terminal_id) id,Group_concat(name) name from wx_qy_terminal_user t
		left join wx_qy_terminal ter on ter.id = t.terminal_id
		where t.user_id=#{id} and ter.delflag=0
	</select>

	
	<!-- 删除此人员下面的所有终端 -->
	<delete id="removeTerminal" parameterType="java.lang.Long">
	delete from wx_qy_terminal_user  where user_id = #{id}
	</delete>
	
	<!-- 批量为某终端绑定商品 -->
	<insert id="saveBatchTerminalUser" parameterType="java.util.Map">
		insert into wx_qy_terminal_user (terminal_id,user_id)
		values  
		<foreach item="item" index="index" collection="terminalsids"  separator="," >
			(#{item},#{userId})		
		</foreach> 
	</insert>

</mapper>