<?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="cn.superdesk.uniorder.dao.mapper.sys.SysUserMapperEx">

	<resultMap id="SysUserViewResultMap" type="cn.superdesk.uniorder.dto.vo.sys.SysUserView">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="user_name" property="userName" jdbcType="VARCHAR"/>
		<result column="fullname" property="fullname" jdbcType="VARCHAR"/>
		<result column="phone" property="phone" jdbcType="VARCHAR"/>

		<result column="role_id" property="roleId" jdbcType="BIGINT"/>
		<result column="role_name" property="roleName" jdbcType="VARCHAR"/>

<!--		su.sys_role_ext_data_id,-->
<!--		sred.name as sys_role_ext_data_name,-->
		<result column="sys_role_ext_data_id" property="sysRoleExtDataId" jdbcType="BIGINT"/>
		<result column="sys_role_ext_data_name" property="sysRoleExtDataName" jdbcType="VARCHAR"/>

<!--		su.source_from,-->
		<result column="source_from" property="sourceFrom" jdbcType="VARCHAR"/>

<!--		su.personalized_logo,-->
<!--		su.personalized_subhead,-->
		<result column="personalized_logo" property="personalizedLogo" jdbcType="VARCHAR"/>
		<result column="personalized_subhead" property="personalizedSubhead" jdbcType="VARCHAR"/>


		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="create_user_id" property="createUserId" jdbcType="BIGINT"/>
		<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
		<result column="update_user_id" property="updateUserId" jdbcType="BIGINT"/>
		<collection property="sourceTypeList" ofType="cn.superdesk.uniorder.dto.vo.order.OrderSourceTypeUser"
		            javaType="java.util.List" column="id" select="GetSourceTypeByUserId">
		</collection>
	</resultMap>

	<resultMap id="SourceTypeResultMap" type="cn.superdesk.uniorder.dto.vo.order.OrderSourceTypeUser">
		<id column="source_id" property="sourceId" jdbcType="BIGINT"/>
		<result column="source_name" property="sourceName" jdbcType="VARCHAR"/>
	</resultMap>

	<select id="findSysUserView" resultMap="SysUserViewResultMap">
		SELECT
			su.id,
			su.user_name,
			su.fullname,
			su.phone,

			su.role_id,
			sr.role_name,

			su.sys_role_ext_data_id,
			sred.name as sys_role_ext_data_name,

			su.source_from,

			su.personalized_logo,
			su.personalized_subhead,

			su.create_time,
			su.create_user_id,
			su.update_time,
			su.update_user_id
		FROM
			sys_user su
			LEFT JOIN sys_role sr ON su.role_id = sr.id
			left join sys_role_ext_data sred on su.sys_role_ext_data_id = sred.id
		<where>

			su.user_name !='admin'
		<!--  and enabled != '' -->
		<choose>
			<when test="enabled != null">
				and su.enabled = #{enabled,jdbcType=TINYINT}
			</when>
			<otherwise>
				and su.enabled = 1
			</otherwise>
		</choose>
		<!--  and deleted != '' -->
		<choose>
			<when test="deleted != null">
				and su.deleted = #{deleted,jdbcType=TINYINT}
			</when>
			<otherwise>
				and su.deleted = 0
			</otherwise>
		</choose>

		<if test="sourceFromsSelected != null and sourceFromsSelected != '' and sourceFromsSelected.size > 0">
			and su.source_from in
			<foreach collection="sourceFromsSelected" index="index" item="source_from" open="(" close=")" separator=",">
				#{source_from}
			</foreach>
		</if>

		<if test="roleId != null ">
			and su.role_id = #{roleId,jdbcType=BIGINT}
		</if>
		<if test="searchParam != null and searchParam != '' ">
			and (su.user_name like CONCAT('%',#{searchParam,jdbcType=VARCHAR},'%')
			or su.fullname like CONCAT('%',#{searchParam,jdbcType=VARCHAR},'%')
			or su.phone like CONCAT('%',#{searchParam,jdbcType=VARCHAR},'%')
			)
		</if>
		</where>

		ORDER BY su.create_time DESC
	</select>

	<select id="GetSourceTypeByUserId" resultMap="SourceTypeResultMap">
	select
		a.sourcetype_id as source_id ,
		b.source_name
	from
		sys_user_order_sourcetype a
		inner join order_source_type b on a.sourcetype_id=b.id
	where
		a.user_id = #{_parameter,jdbcType=BIGINT}
	</select>

	<select id="findAll" resultMap="cn.superdesk.uniorder.dao.mapper.sys.SysUserMapper.BaseResultMap">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.sys.SysUserMapper.Base_Column_List"/>
		from
			sys_user su
		<where>
			<!--  and enabled != '' -->
			<choose>
				<when test="enabled != null">
					and su.enabled = #{enabled,jdbcType=TINYINT}
				</when>
				<otherwise>
					and su.enabled = 1
				</otherwise>
			</choose>
			<!--  and deleted != '' -->
			<choose>
				<when test="deleted != null">
					and su.deleted = #{deleted,jdbcType=TINYINT}
				</when>
				<otherwise>
					and su.deleted = 0
				</otherwise>
			</choose>
			<if test="username != null and username != '' ">
				and su.user_name = #{username,jdbcType=VARCHAR}
			</if>
			<if test="password != null and password != '' ">
				and su.user_password = #{password,jdbcType=VARCHAR}
			</if>
			<if test="ids != null and ids != ''">
				and su.id in
				<foreach collection="ids" index="index" item="id" open="(" close=")" separator=",">
					#{id}
				</foreach>
			</if>
			<if test="fullname != null and fullname != '' ">
				and su.fullname = #{fullname}
			</if>
		</where>
	</select>

</mapper>
