<?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.dgtly.system.mapper.SysUserMapper">

	<resultMap  id="SysUserResult" type="SysUser">
		<id     property="userId"       column="user_id"      />
		<result property="companyId"       column="company_id"      />
		<result property="deptId"       column="dept_id"      />
		<result property="loginName"    column="login_name"   />
		<result property="userName"     column="user_name"    />
		<result property="email"        column="email"        />
		<result property="phonenumber"  column="phonenumber"  />
		<result property="sex"          column="sex"          />
		<result property="avatar"       column="avatar"       />
		<result property="password"     column="password"     />
		<result property="salt"         column="salt"         />
		<result property="idType"         column="id_type"         />
		<result property="idNumber"         column="id_number"         />
		<result property="accountId"         column="account_id"         />
		<result property="isAuthentication"         column="is_authentication"         />
		<result property="personAttestationTime"         column="person_attestation_time"         />
		<result property="receiveUrlMobileNo"         column="receive_url_mobile_no"         />
		<result property="status"       column="status"       />
		<result property="delFlag"      column="del_flag"     />
		<result property="loginIp"      column="login_ip"     />
		<result property="loginDate"    column="login_date"   />
		<result property="createBy"     column="create_by"    />
		<result property="createTime"   column="create_time"  />
		<result property="updateBy"     column="update_by"    />
		<result property="updateTime"   column="update_time"  />
		<result property="remark"       column="remark"       />
		<result property="isConsignee"       column="is_consignee"       />
		<result property="isIndependently"  column="is_independently" />
		<result property="isSelfDelivery" column="is_self_delivery" />
		<result property="isSync"       column="is_sync"       />
		<result property="quit"       column="quit"       />
		<result property="isAuthentication"       column="is_authentication"       />
		<result property="personAttestationTime"       column="person_attestation_time"       />
		<result property="isOrgAuthentication"       column="is_org_authentication"       />
		<result property="orgAttestationTime"       column="org_attestation_time"       />
        <!--<result property="salesLevel"   column="sales_level"  />-->
        <!--<result property="orgName"      column="org_name"     />-->
        <!--<result property="orgCode"      column="org_code"     />-->
		<association property="dept"    column="dept_id" javaType="SysDept" resultMap="deptResult" />
		<association property="company"    column="company_id" javaType="SysCompany" resultMap="companyResult" />
        <association property="sysUserExt"    column="user_id" javaType="SysUserExt" resultMap="SysUserExtResult" />
        <collection  property="roles"   javaType="java.util.List"        resultMap="RoleResult" />

	</resultMap>

	<resultMap id="deptResult" type="SysDept">
		<id     property="deptId"   column="dept_id"     />
		<result property="parentId" column="parent_id"   />
		<result property="deptName" column="dept_name"   />
		<result property="orderNum" column="order_num"   />
		<result property="leader"   column="leader"      />
		<result property="status"   column="dept_status" />
	</resultMap>

	<resultMap id="companyResult" type="SysCompany">
		<id     property="id"   column="id"     />
		<result property="companyName" column="company_name"   />
	</resultMap>

	<resultMap id="RoleResult" type="SysRole">
		<id     property="roleId"       column="role_id"        />
		<result property="roleName"     column="role_name"      />
		<result property="roleKey"      column="role_key"       />
		<result property="roleSort"     column="role_sort"      />
		<result property="dataScope"     column="data_scope"    />
		<result property="status"       column="role_status"    />
	</resultMap>

    <resultMap id="SysUserExtResult" type="SysUserExt" >
        <result property="userId"    column="user_id"    />
        <result property="orgCode"    column="org_code"    />
        <result property="salesLevel"    column="sales_level"    />
        <result property="orgName"    column="org_name"    />
        <result property="sapEmployeeId"    column="sap_employee_id"    />
        <result property="bossEmployeeId"    column="boss_employee_id"    />
        <result property="bossName"    column="boss_name"    />
        <result property="customerCode"    column="customer_code"    />
        <result property="isCustomerManager"    column="is_customer_manager"    />
		<result property="postName"    column="post_name"    />
		<result property="postCode"    column="post_code"    />
		<result property="identity"       column="identity"       />
    </resultMap>

	<sql id="selectUserVo">
        select  u.user_id,u.company_id, u.dept_id, u.login_name, u.user_name, u.email, u.avatar, u.phonenumber, u.sex, u.password, u.salt,u.id_type,u.id_number,u.account_id,u.is_authentication,u.receive_url_mobile_no, u.status,
        		u.del_flag, u.login_ip, u.login_date, u.create_time, u.remark,u.is_consignee,u.person_attestation_time,u.is_independently,u.is_self_delivery,is_sync,
        		c.company_name,u.quit,
       		    d.dept_id, d.parent_id, d.dept_name, d.order_num, d.leader, d.status as dept_status,
       		    r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status,
       		    suse.sales_level,suse.org_name,suse.org_code,suse.sap_employee_id,suse.boss_employee_id,suse.boss_name,suse.customer_code,suse.is_customer_manager,
       		    suse.post_name,suse.post_code,suse.identity
		from sys_user u
			 left join sys_dept d on u.dept_id = d.dept_id
			 left join sys_company c on u.company_id = c.id
			 left join sys_user_role ur on u.user_id = ur.user_id
			 left join sys_role r on r.role_id = ur.role_id
			 left join sys_user_ext suse on suse.user_id = u.user_id
    </sql>

	<select id="selectUserList" parameterType="SysUser" resultMap="SysUserResult">
		select u.user_id,u.company_id, u.dept_id, u.login_name, u.user_name,u.id_type,u.id_number,u.account_id,u.is_authentication,u.receive_url_mobile_no, u.email, u.avatar,
          u.phonenumber, u.password, u.sex, u.salt, u.status, u.del_flag,u.quit,u.is_independently,u.is_self_delivery,
          u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,
          d.dept_name, d.leader,
		suse.identity,suse.sales_level,suse.org_name,suse.org_code,suse.post_name,suse.is_customer_manager,ce.is_authentication is_org_authentication,ce.org_attestation_time
		from sys_user u
		left join sys_dept d on u.dept_id = d.dept_id
		left join sys_company c on u.company_id = c.id
        left join sys_user_ext suse on suse.user_id = u.user_id
		LEFT JOIN customers_ext ce ON suse.org_code = ce.chains_code
		where u.del_flag = '0'
		<if test="loginName != null and loginName != ''">
			AND u.login_name like concat('%', #{loginName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND u.status = #{status}
		</if>
        <if test="sysUserExt != null and sysUserExt.salesLevel !=null
        and sysUserExt.salesLevel != ''">
            AND suse.sales_level = #{sysUserExt.salesLevel}
        </if>
		<if test="sysUserExt != null and sysUserExt.orgCode !=null
        and sysUserExt.orgCode != ''">
			AND suse.org_code = #{sysUserExt.orgCode}
		</if>
		<if test="sysUserExt != null and sysUserExt.orgName !=null
        and sysUserExt.orgName != ''">
			AND suse.org_name like concat('%', #{sysUserExt.orgName}, '%')
		</if>
		<if test="sysUserExt != null and sysUserExt.postName !=null
        and sysUserExt.postName != ''">
			AND suse.post_name = #{sysUserExt.postName}
		</if>
		<if test="sysUserExt != null and sysUserExt.identity !=null
        and sysUserExt.identity != ''">
			AND suse.identity = #{sysUserExt.identity}
		</if>
		<if test="userName != null and userName != ''">
			AND u.user_name like concat('%', #{userName}, '%')
		</if>
		<if test="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<if test="deptId != null and deptId != 0">
			AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE FIND_IN_SET (#{deptId},ancestors) ))
		</if>
		<if test="companyId != null and companyId != 0">
			AND u.company_id = #{companyId}
		</if>
		<if test="isAuthentication != null and isAuthentication != ''">
			AND u.is_authentication = #{isAuthentication}
		</if>
		<if test="isOrgAuthentication != null and isOrgAuthentication != '' and isOrgAuthentication =='1'.toString()">
			AND ce.is_authentication = '1'
		</if>
		<if test="isOrgAuthentication != null and isOrgAuthentication != '' and isOrgAuthentication =='0'.toString()">
			AND (ce.is_authentication = '0' or ce.is_authentication is null)
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectAllocatedList" parameterType="SysUser" resultMap="SysUserResult">
	    select distinct u.user_id,u.company_id, u.dept_id,u.id_type,u.id_number,u.account_id,u.is_authentication,u.receive_url_mobile_no, u.login_name, u.user_name, u.email, u.avatar, u.phonenumber, u.status, u.create_time
	    from sys_user u
			 left join sys_dept d on u.dept_id = d.dept_id
			 left join sys_company c on u.company_id = c.id
			 left join sys_user_role ur on u.user_id = ur.user_id
			 left join sys_role r on r.role_id = ur.role_id
	    where u.del_flag = '0' and r.role_id = #{roleId}
	    <if test="loginName != null and loginName != ''">
			AND u.login_name like concat('%', #{loginName}, '%')
		</if>
		<if test="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<if test="companyId != null and companyId != ''">
			AND u.company_id like #{companyId}
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectUnallocatedList" parameterType="SysUser" resultMap="SysUserResult">
	    select distinct u.user_id,u.company_id,u.id_type,u.id_number,u.account_id,u.is_authentication,u.receive_url_mobile_no, u.dept_id, u.login_name, u.user_name, u.email, u.avatar, u.phonenumber, u.status, u.create_time
	    from sys_user u
			 left join sys_dept d on u.dept_id = d.dept_id
			 left join sys_company c on u.company_id = c.id
			 left join sys_user_role ur on u.user_id = ur.user_id
			 left join sys_role r on r.role_id = ur.role_id
	    where u.del_flag = '0' and (r.role_id != #{roleId} or r.role_id IS NULL)
	    and u.user_id not in (select u.user_id from sys_user u inner join sys_user_role ur on u.user_id = ur.user_id and ur.role_id = #{roleId})
	    <if test="loginName != null and loginName != ''">
			AND u.login_name like concat('%', #{loginName}, '%')
		</if>
		<if test="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
		<if test="companyId != null and companyId != ''">
			AND u.company_id like #{companyId}
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectUserByLoginName" parameterType="String" resultMap="SysUserResult">
	    <include refid="selectUserVo"/>
		where u.login_name = #{userName} and u.quit is null and u.del_flag ='0' GROUP BY login_name
	</select>

	<select id="selectUserByIsManager"  resultType="java.lang.String">
		select  u.login_name
		from sys_user u
		left join sys_user_ext suse on suse.user_id = u.user_id
		where 1=1
		<if test="isCustomerManager !=null ">
			and suse.is_customer_manager  like concat('%',#{isCustomerManager}, '%')
		</if>
		 and u.quit is null
		  and u.login_name is not null
	</select>

	<select id="selectAllUser" parameterType="String" resultMap="SysUserResult">
		SELECT
			u.user_id,
			u.company_id,
			u.dept_id,
			u.login_name,
			u.user_name,
			u.email,
			u.avatar,
			u.phonenumber,
			u.sex,
			u.PASSWORD,
			u.salt,
			u.id_type,
			u.id_number,
			u.account_id,
			u.is_authentication,
		    u.is_independently,
			u.is_self_delivery,
			u.receive_url_mobile_no,
			u.STATUS,
			u.del_flag,
			u.login_ip,
			u.login_date,
			u.create_time,
			u.remark,
			u.is_consignee,
			u.person_attestation_time,
			is_sync,
			u.quit,
			suse.sales_level,
			suse.org_name,
			suse.org_code,
			suse.sap_employee_id,
			suse.boss_employee_id,
			suse.boss_name,
			suse.customer_code,
			suse.is_customer_manager,
			suse.post_name,
			suse.post_code,
			suse.identity
		FROM
			sys_user u
				LEFT JOIN sys_user_ext suse ON suse.user_id = u.user_id
		where suse.is_customer_manager is not null and  suse.is_customer_manager !=''
	</select>

	<select id="selectUserByPhoneNumber" parameterType="String" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.phonenumber = #{phonenumber}
	</select>

	<select id="selectUserByEmail" parameterType="String" resultMap="SysUserResult">
	    <include refid="selectUserVo"/>
		where u.email = #{email}  and del_flag='0'  and quit is null
	</select>

	<select id="checkLoginNameUnique"  resultType="int">
		select count(1) from sys_user where login_name=#{loginName} and del_flag='0'  and quit is null
	</select>

	<select id="checkPhoneUnique"  resultMap="SysUserResult">
		select user_id, phonenumber from sys_user where phonenumber=#{phonenumber}  and del_flag='0'  and quit is null
	</select>

	<select id="checkEmailUnique"  resultMap="SysUserResult">
		select user_id, email from sys_user where email=#{email}  and del_flag='0'  and quit is null
	</select>

	<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where u.user_id = #{userId}  and u.del_flag='0'  and u.quit is null
	</select>

	<delete id="deleteUserById" parameterType="Long">
 		delete from sys_user where user_id = #{userId}
 	</delete>

	<delete id="deleteUserByLoginName" parameterType="Long">
		delete from sys_user where login_name = #{loginName}
	</delete>

 	<delete id="deleteUserByIds" parameterType="Long">
 		update sys_user set del_flag = '2' where user_id in
 		<foreach collection="array" item="userId" open="(" separator="," close=")">
 			#{userId}
        </foreach>
 	</delete>

 	<update id="updateUser" parameterType="SysUser">
 		update sys_user
 		<set>
 			<if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>
 			<if test="loginName != null and loginName != ''">login_name = #{loginName},</if>
 			<if test="userName != null and userName != ''">user_name = #{userName},</if>
 			<if test="email != null and email != ''">email = #{email},</if>
 			<if test="phonenumber != null and phonenumber != ''">phonenumber = #{phonenumber},</if>
 			<if test="sex != null and sex != ''">sex = #{sex},</if>
 			<if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
 			<if test="password != null and password != ''">password = #{password},</if>
 			<if test="salt != null and salt != ''">salt = #{salt},</if>
 			<if test="idType != null and idType != ''">id_type = #{idType},</if>
 			<if test="idNumber != null and idNumber != ''">id_number = #{idNumber},</if>
 			<if test="accountId != null and accountId != ''">account_id = #{accountId},</if>
 			<if test="isAuthentication != null and isAuthentication != ''">is_authentication = #{isAuthentication},</if>
			<if test="isIndependently != null ">is_independently = #{isIndependently},</if>
			<if test="isSelfDelivery !=null">is_self_delivery =#{isSelfDelivery},</if>
 			<if test="personAttestationTime != null">person_attestation_time = #{personAttestationTime},</if>
 			<if test="receiveUrlMobileNo != null and receiveUrlMobileNo != ''">receive_url_mobile_no = #{receiveUrlMobileNo},</if>
 			<if test="status != null and status != ''">status = #{status},</if>
 			<if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},</if>
 			<if test="loginDate != null">login_date = #{loginDate},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
 			<if test="remark != null">remark = #{remark},</if>
 			<if test="isConsignee != null">is_consignee = #{isConsignee},</if>
 			<if test="isSync != null">is_sync = #{isSync},</if>
			<if test="quit != null">quit = #{quit},</if>
 			update_time = sysdate()
 		</set>
 		where user_id = #{userId}
	</update>

	<update id="updateUserByLoginName" parameterType="SysUser">
		update sys_user
		<set>
			<if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>
			<if test="userName != null and userName != ''">user_name = #{userName},</if>
			<if test="email != null and email != ''">email = #{email},</if>
			<if test="phonenumber != null and phonenumber != ''">phonenumber = #{phonenumber},</if>
			<if test="sex != null and sex != ''">sex = #{sex},</if>
			<if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
			<if test="password != null and password != ''">password = #{password},</if>
			<if test="salt != null and salt != ''">salt = #{salt},</if>
			<if test="idType != null and idType != ''">id_type = #{idType},</if>
			<if test="idNumber != null and idNumber != ''">id_number = #{idNumber},</if>
			<if test="accountId != null and accountId != ''">account_id = #{accountId},</if>
			<if test="isAuthentication != null and isAuthentication != ''">is_authentication = #{isAuthentication},</if>
			<if test="personAttestationTime != null">person_attestation_time = #{personAttestationTime},</if>
			<if test="receiveUrlMobileNo != null and receiveUrlMobileNo != ''">receive_url_mobile_no = #{receiveUrlMobileNo},</if>
			<if test="status != null and status != ''">status = #{status},</if>
			<if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},</if>
			<if test="loginDate != null">login_date = #{loginDate},</if>
			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
			<if test="remark != null">remark = #{remark},</if>
			<if test="isConsignee != null">is_consignee = #{isConsignee},</if>
			<if test="isSync != null">is_sync = #{isSync},</if>
			<if test="isIndependently != null ">is_independently = #{isIndependently},</if>
			<if test="isSelfDelivery !=null">is_self_delivery =#{isSelfDelivery},</if>
			update_time = sysdate()
		</set>
		where login_name = #{loginName} and quit is null  and del_flag='0'
	</update>

	<update id="updateIsAuthentication" parameterType="String">
		update sys_user set is_authentication = #{isAuthentication} ,person_attestation_time = now()
		where account_id = #{accountId}
	</update>


	<update id="changeStatus">
		update sys_user set del_flag = #{status}
		where company_id = #{companyId}
		<if test="status == 0 ">and del_flag = 3 </if>
		<if test="status == 3 ">and del_flag = 0 </if>
	</update>

 	<insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
		<selectKey keyProperty="userId" order="AFTER" resultType="java.lang.Long">
			select LAST_INSERT_ID();
		</selectKey>
 		insert into sys_user(
 			<if test="userId != null and userId != 0">user_id,</if>
 			<if test="deptId != null and deptId != 0">dept_id,</if>
			<if test="companyId != null and companyId != 0">company_id,</if>
 			<if test="loginName != null and loginName != ''">login_name,</if>
 			<if test="userName != null and userName != ''">user_name,</if>
 			<if test="email != null and email != ''">email,</if>
 			<if test="avatar != null and avatar != ''">avatar,</if>
 			<if test="phonenumber != null and phonenumber != ''">phonenumber,</if>
 			<if test="sex != null and sex != ''">sex,</if>
 			<if test="password != null and password != ''">password,</if>
 			<if test="salt != null and salt != ''">salt,</if>
 			<if test="status != null and status != ''">status,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			<if test="remark != null and remark != ''">remark,</if>
 			create_time
 		)values(
 			<if test="userId != null and userId != ''">#{userId},</if>
 			<if test="deptId != null and deptId != ''">#{deptId},</if>
			<if test="companyId != null and companyId != 0">#{companyId},</if>
 			<if test="loginName != null and loginName != ''">#{loginName},</if>
 			<if test="userName != null and userName != ''">#{userName},</if>
 			<if test="email != null and email != ''">#{email},</if>
 			<if test="avatar != null and avatar != ''">#{avatar},</if>
 			<if test="phonenumber != null and phonenumber != ''">#{phonenumber},</if>
 			<if test="sex != null and sex != ''">#{sex},</if>
 			<if test="password != null and password != ''">#{password},</if>
 			<if test="salt != null and salt != ''">#{salt},</if>
 			<if test="status != null and status != ''">#{status},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			<if test="remark != null and remark != ''">#{remark},</if>
 			sysdate()
 		)
	</insert>




    <select id="selectAllUserLoginName"  resultType="java.lang.String">
        select u.login_name
        from sys_user u  where u.quit is null
	</select>

	<select id="selectDiyUserLoginName"  resultType="java.lang.String">
        select distinct(u.login_name)
        from sys_user u left join sys_user_ext b on u.user_id = b.user_id where b.post_name='DIY经销商' and   u.quit is null
	</select>

    <insert id="insertUserNoKey" parameterType="SysUser" >
        insert into sys_user(
        <if test="userId != null and userId != 0">user_id,</if>
        <if test="deptId != null and deptId != 0">dept_id,</if>
        <if test="loginName != null and loginName != ''">login_name,</if>
        <if test="userName != null and userName != ''">user_name,</if>
        <if test="email != null and email != ''">email,</if>
        <if test="phonenumber != null and phonenumber != ''">phonenumber,</if>
        <if test="sex != null and sex != ''">sex,</if>
        <if test="password != null and password != ''">password,</if>
        <if test="salt != null and salt != ''">salt,</if>
        <if test="status != null and status != ''">status,</if>
        create_time
        )values(
        <if test="userId != null and userId != ''">#{userId},</if>
        <if test="deptId != null and deptId != ''">#{deptId},</if>
        <if test="loginName != null and loginName != ''">#{loginName},</if>
        <if test="userName != null and userName != ''">#{userName},</if>
        <if test="email != null and email != ''">#{email},</if>
        <if test="phonenumber != null and phonenumber != ''">#{phonenumber},</if>
        <if test="sex != null and sex != ''">#{sex},</if>
        <if test="password != null and password != ''">#{password},</if>
        <if test="salt != null and salt != ''">#{salt},</if>
        <if test="status != null and status != ''">#{status},</if>
        sysdate()
        )
    </insert>


	<select id="selectUserByEmplyeeId" parameterType="String" resultMap="SysUserResult">
		<include refid="selectUserVo"/>
		where suse.sap_employee_id = #{emplyeeId}
	</select>


	<select id="selectLoginNamesByCostumerCode" resultType="java.util.Map" parameterType="String">
		select  sue.org_code customer_code,GROUP_CONCAT(su.login_name SEPARATOR '|') touser from sys_user  su
		left join sys_user_ext sue on sue.user_id = su.user_id
		where sue.org_code in
		<foreach collection="array" item="customerCode" open="(" separator="," close=")">
			#{customerCode}
		</foreach>
		and sue.is_customer_manager != 0
		GROUP BY sue.org_code
	</select>

	<select id="selectLoginNamesByOrgCodeSelf" resultType="java.util.Map" parameterType="String">
		select  sue.org_code customer_code,GROUP_CONCAT(su.login_name SEPARATOR '|') touser from sys_user  su
		left join sys_user_ext sue on sue.user_id = su.user_id
		where 1=1
		and sue.org_code =#{customerCode}
		and sue.is_customer_manager in ('1','2','3')
		and  sue.is_customer_manager != 0
		and su.quit is null
		and su.del_flag = 0
		GROUP BY sue.org_code
	</select>

	<select id="selectLoginNameByCustomerCode" resultType="java.lang.String">
		SELECT
			u.login_name
		FROM
			order_sales_sum_tuc_customer c
				LEFT JOIN sys_user_ext e ON e.sap_employee_id = c.emp_id
				LEFT JOIN sys_user u ON u.user_id = e.user_id
		WHERE
			c.customer_id =#{customerId}
		 	and u.quit is null
		    and u.del_flag = 0
		GROUP BY
			u.user_id limit 1
	</select>

	<select id="selectLoginNamesByCustomerModelVO" resultType="java.util.Map" parameterType="String">
		select DISTINCT sue.org_code customer_code,GROUP_CONCAT(su.login_name SEPARATOR '|') touser from sys_user  su
		left join sys_user_ext sue on sue.user_id = su.user_id
		where 1=1
		<if test="customerCode !=null and customerCode !='' ">
			and sue.org_code =#{customerCode}
		</if>
		<if test="customerType !=null and customerType !='' and customerType =='D9' ">
			and sue.identity in ('1')
		</if>
		<if test="customerType !=null and customerType !='' and customerType =='Y9' ">
			and sue.identity in ('2')
		</if>
		and sue.is_customer_manager != 0
		GROUP BY sue.org_code
	</select>

	<select id="selectLoginNamesByCostumerCode1" resultType="java.util.Map" parameterType="String">
		select DISTINCT sue.org_code customer_code,GROUP_CONCAT(su.login_name SEPARATOR '|') touser from sys_user  su
		left join sys_user_ext sue on sue.user_id = su.user_id
		where sue.org_code in
		<foreach collection="array" item="customerCode" open="(" separator="," close=")">
			#{customerCode}
		</foreach>
		GROUP BY sue.org_code
	</select>

	<select id="selectLoginNamesByEmployeeCode" resultType="Map" parameterType="String">
		select DISTINCT sue.sap_employee_id employee_code,GROUP_CONCAT(su.login_name SEPARATOR '|') touser ,sue.org_code from sys_user  su
		left join sys_user_ext sue on sue.user_id = su.user_id
		where sue.sap_employee_id in
		<foreach collection="array" item="employeeCode" open="(" separator="," close=")">
			#{employeeCode}
		</foreach>
		GROUP BY sue.sap_employee_id
	</select>
	<select id="selectUserByOrgCode" resultMap="SysUserResult" >
		SELECT
			u.user_id,
			u.user_name,
			a.is_consignee,
			a.is_independently,
			a.is_self_delivery,
			v.sap_employee_id,
		    v.is_customer_manager,
			v.org_code
		FROM
			sys_user u
		LEFT JOIN sys_user_ext v ON u.user_id = v.user_id
		left join sys_user_customer_authorization a on a.user_id = u.user_id
		WHERE
			v.org_code =#{orgCode} and u.quit is null
			and v.is_customer_manager !='0' and v.is_customer_manager !='8' GROUP BY 	u.user_id
	</select>

	<select id="getCustomNameByCode" resultType="java.lang.String" >
		select customer_name  from order_sales_sum_tuc_customer where customer_id=#{code} limit 1
	</select>

	<select id="selectSyncDateUser" resultType="java.lang.String" >
		select GROUP_CONCAT(su.login_name SEPARATOR '|') touser from sys_user su
		left join sys_user_sync_data sus on sus.user_id = su.user_id  where sus.type = #{type}
	</select>

	<insert id="insertUserRole">
		insert into sys_user_role (user_id,role_id) select ${userId},${roleId} from dual where not exists ( select 1 from sys_user_role where user_id = #{userId} and role_id = #{roleId} )
	</insert>
	<delete id="deleteByLoginName" parameterType="int">
		delete from sys_user_author_rel where user_id = (
		    select user_id from sys_user where 1=1 and del_flag='0' and  quit is null
		    <if test="loginName !=null and loginName !='' ">
				and login_name=#{loginName}
			</if>

			)
	</delete>

	<select id="selectUser" parameterType="SysUser" resultMap="SysUserResult">
		select u.user_id,u.company_id, u.dept_id, u.login_name, u.user_name,u.id_type,u.id_number,u.account_id,u.is_authentication,u.receive_url_mobile_no, u.email, u.avatar,
			   u.phonenumber, u.password, u.sex, u.salt, u.status, u.del_flag,
			   u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,u.quit
		from sys_user u where is_sync ='0' and  quit is null
	</select>

	<select id="sendmailUnionUser" resultType="com.dgtly.system.domain.UserVO"  >
		select  u.user_name as userName,u.phonenumber,u.login_name as loginName
		from sys_user u LEFT JOIN sys_user_ext e on e.user_id =u.user_id WHERE
				( u.phonenumber, u.user_name ) IN (
				SELECT
					phonenumber,
					user_name
				FROM
					sys_user where del_flag=0 and quit is null
				GROUP BY
					phonenumber,
					user_name
				HAVING
					count(*) > 1) and u.del_flag=0 and u.quit is null
				and e.is_customer_manager is null order by user_name
	</select>

	<select id="selectAllUserByUserId" parameterType="Long" resultMap="SysUserResult">
		SELECT *
		FROM
			sys_user u
				LEFT JOIN sys_user_ext suse ON suse.user_id = u.user_id
		where u.user_id = #{userId}
	</select>
</mapper>
