<?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.sys.dao.UserMapper">

	<resultMap id="resultMap" type="com.cftech.sys.model.User">
		<id column="id" property="id"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
		<result column="roles" property="roles"/>
		<result column="email" property="email"/>
		<result column="state" property="state"/>
		<result column="createtime" property="createtime"/>
		<result column="updatetime" property="updatetime"/>
		<result column="timestamp" property="timestamp"/>
		<result column="description" property="description"/>
		<result column="delflag" property="delflag"/>
		<result column="accountsid" property="accountsid"/>
		<result column="empid" property="empid"/>
		<result column="userid" property="userid"/>
		<result column="default_accid" property="defaultAccId"/>
		<result column="customiz_id" property="customiz_id"/>
	</resultMap>


	<resultMap id="resultAuthMap" type="com.cftech.sys.model.UserAuth">
		<id column="id" property="id"/>
		<result column="username" property="username"/>
		<result column="roles" property="roles"/>
		<result column="email" property="email"/>
		<result column="state" property="state"/>
		<result column="createtime" property="createtime"/>
		<result column="updatetime" property="updatetime"/>
		<result column="timestamp" property="timestamp"/>
		<result column="description" property="description"/>
		<result column="delflag" property="delflag"/>
		<result column="accountsid" property="accountsid"/>
		<result column="empid" property="empid"/>
		<result column="userid" property="userid"/>
		<result column="default_accid" property="defaultAccId"/>
	</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">
		id, username, roles, email, state, createtime, updatetime, timestamp, description, delflag,
		accountsid,empid,userid,default_accid,customiz_id
	</sql>

	<insert id="save" parameterType="com.cftech.sys.model.User" useGeneratedKeys="true" keyProperty="id">
		insert into user
		(
		id, username, password, roles, email, state, createtime, updatetime, timestamp, description, delflag,
		accountsid,empid,userid, default_accid,customiz_id
		)
		values
		(
		#{id, jdbcType=BIGINT},  #{username, jdbcType=VARCHAR},  #{password, jdbcType=CHAR},  #{roles, jdbcType=VARCHAR},  
		#{email, jdbcType=VARCHAR},  #{state, jdbcType=VARCHAR},  now(),  now(),
		#{timestamp, jdbcType=BIGINT}, #{description, jdbcType=VARCHAR}, #{delflag, jdbcType=TINYINT},
		#{accountsid, jdbcType=BIGINT},#{empid,jdbcType=VARCHAR},#{userid,jdbcType=BIGINT},
		#{defaultAccId,jdbcType=BIGINT},#{customiz_id}
		)
	</insert>

	<select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
		SELECT t.id, t.username, t.roles, t.email, t.state, t.createtime, t.updatetime, t.timestamp,
		t.description, t.delflag, t.accountsid,t.empid,t.userid, t.default_accid,
		tq.name wxUserName,t.customiz_id
		FROM user t
		left join t_qyuser tq on t.userid = tq.id
		WHERE t.id=#{id}
	</select>


	<select id="fetchAuthById" parameterType="java.lang.Long" resultMap="resultAuthMap">
		SELECT t.id, t.username, t.roles, t.email, t.state, t.createtime, t.updatetime, t.timestamp,
		t.description, t.delflag, t.accountsid,t.empid,t.userid, t.default_accid,
		tq.name wxUserName
		FROM user t
		left join t_qyuser tq on t.userid = tq.id
		WHERE t.id=#{id}
	</select>

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

	<select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
		SELECT <include refid="sqlColumns" /> 
		FROM user t
		<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.sys.model.User">
		update user
		<set>
			<if test="id != null">
				id = #{id, jdbcType=BIGINT},
			</if>
			<if test="username != null">
				username = #{username, jdbcType=VARCHAR},
			</if>
			<if test="password != null">
				password = #{password, jdbcType=CHAR},
			</if>
			<if test="roles != null">
				roles = #{roles, jdbcType=VARCHAR},
			</if>
			<if test="email != null">
				email = #{email, jdbcType=VARCHAR},
			</if>
			<if test="state != null">
				state = #{state, jdbcType=VARCHAR},
			</if>
			<if test="createtime != null">
				createtime = #{createtime, jdbcType=TIMESTAMP},
			</if>
			<if test="updatetime != null">
				updatetime=now(),
			</if>
			<if test="timestamp != null">
				timestamp = #{timestamp, jdbcType=BIGINT},
			</if>
			<if test="description != null">
				description = #{description, jdbcType=VARCHAR},
			</if>
			<if test="delflag != null">
				delflag = #{delflag, jdbcType=TINYINT},
			</if>
			<if test="accountsid != null">
				accountsid = #{accountsid, jdbcType=BIGINT},
			</if>
			<if test="empid != null">
				empid = #{empid, jdbcType=VARCHAR},
			</if>
			<if test="userid != null">
				userid = #{userid, jdbcType=BIGINT},
			</if>
			<if test="defaultAccId != null">
				default_accid = #{defaultAccId, jdbcType=BIGINT},
			</if>
			<if test="customiz_id != null">
				customiz_id = #{customiz_id},
			</if>
		</set>
		where id=#{id,jdbcType=BIGINT}
	</update>

	<update id="delete" parameterType="java.lang.Long">
		update user set delflag=1 where id=#{id,jdbcType=BIGINT}
	</update>
	
	<!--清空用户所有权限  -->
	<delete id="clearUserRole" parameterType="java.lang.Long" >
		delete from user_role where user_id=#{userId,jdbcType=BIGINT}
	</delete>
	
	<!-- 给用户赋角色 -->
	<insert id="saveBatchRole" parameterType="java.util.Map">
		insert into  user_role (user_id,role_id,accounts_id) values (#{userId,jdbcType=BIGINT},#{roleId,jdbcType=BIGINT},#{accountId,jdbcType=BIGINT})
	</insert>
	<!-- 获取用户分配的门店 -->
<select id="fetchStoreUserById" parameterType="java.lang.Long" resultMap="resultMap">
	SELECT
	users.*,
	(
	SELECT
	GROUP_CONCAT(store.store_id)
	FROM
	t_dc_store_user store
	WHERE
	store.user_id = users.id
	AND store.del_flag = '0'
	) storeids
	FROM
	`user` users
	where users.id=#{id}
</select>


	<select id="fetchByQyuserId" parameterType="java.lang.Long" resultMap="resultMap">
		SELECT t.id, t.username, t.roles, t.email, t.state, t.createtime, t.updatetime, t.timestamp,
		t.description, t.delflag, t.accountsid,t.empid,t.userid, t.default_accid,
		tq.name wxUserName
		FROM user t
		left join t_qyuser tq on t.userid = tq.id
		WHERE tq.id=#{id}
	</select>

	<select id="fetchoruserBycond" parameterType="java.util.Map" resultMap="resultMap">
		SELECT
		id,
		username,
		roles,
		email,
		state,
		createtime,
		updatetime,
		TIMESTAMP,
		description,
		delflag,
		accountsid,
		empid,
		userid,
		default_accid,
		customiz_id
		FROM
		user t
		<include refid="sqlWhere" />OR customiz_id = #{id}
		ORDER BY
		createtime DESC
	</select>
</mapper>