<?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.com.countrygarden.bi.main.dao.twerp.hm.TbSysUserMapper">
    <sql id="selectAll">
    <![CDATA[
        SELECT u.UserCode, u.UserName, u.LoginCode, u.PassWord, u.CommID, u.DepCode, u.Memo, u.OrganId
        FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
        ]]>
    </sql>

    <select id="findByLogin" parameterType="Map" resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser">
        <include refid="selectAll"/>
        <![CDATA[
          WHERE u.IsDelete = 0 AND u.LoginCode = #{loginCode} AND u.PassWord = #{passWord} and u.rentid=1853
        ]]>
    </select>

    <select id="findByLoginCode" parameterType="Map" resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser">
        <include refid="selectAll"/>
        <![CDATA[
          WHERE u.IsDelete = 0 AND u.LoginCode = #{loginCode} and u.rentid=1853
        ]]>
    </select>

    <!--绩效权限搜索框按照用户名、登陆账号查询-->
    <select id="searchUser" parameterType="Map" resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser">
        select UserCode, UserName, LoginCode, PassWord, CommID, DepCode, Memo, OrganId FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User]
        where
        <if test="userName!=null">
            UserName like CONCAT('%',#{userName},'%')
            and
        </if>
        IsDelete=0
        and rentid=1853
        and UserCode in
        <foreach item="item" index="index" collection="userCodes" open="("     separator="," close=")">
            #{item}
        </foreach>
        order by UserCode
        offset #{pageCurrent} ROWS
        FETCH next #{pageSize} rows ONLY
    </select>

    <!--计算searchUser个数-->
    <select id="countSearchUser" parameterType="Map" resultType="java.lang.Long">
        select count(*) FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User]
        where
        <if test="userName!=null">
            UserName like CONCAT('%',#{userName},'%')
            and
        </if>
        IsDelete=0
        and rentid=1853
        and UserCode in
        <foreach item="item" index="index" collection="userCodes" open="("     separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="findByUserCode" parameterType="string" resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser">
        <include refid="selectAll"/>
        <![CDATA[
          WHERE u.IsDelete = 0 AND u.UserCode = #{userCode} and u.rentid=1853
        ]]>
    </select>

    <select id="findByUserCodeNew" parameterType="string" resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser">
        <![CDATA[
        SELECT DISTINCT u.UserCode, u.UserName, u.LoginCode, u.PassWord, u.CommID, u.DepCode, u.Memo, u.OrganId,u.MobileTel
        FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
        ]]>
        <![CDATA[
          WHERE  u.UserCode = #{userCode} and rentid=1853
        ]]>
    </select>

    <select id="findByDepartmentOrKey" resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser">
        <![CDATA[
          SELECT u.UserCode, u.UserName, u.LoginCode,u.DepCode,d.DepName
          FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
          INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] as d on d.DepCode = u.DepCode and (d.IsDelete is null OR d.IsDelete!='1') AND u.rentid=d.rentid
          WHERE u.IsDelete = 0 and u.rentid=1853
        ]]>
        <if test="searchKey!=null and searchKey !='' ">
            <![CDATA[
              AND (u.UserName LIKE '%'+#{searchKey}+'%' OR u.LoginCode LIKE '%'+#{searchKey}+'%')
            ]]>
        </if>
        <if test="depCode!=null and depCode!=''">
            <![CDATA[
              AND u.DepCode = #{DepCode}
            ]]>
        </if>
    </select>

    <select id="findByUserCodes" resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser">
        <![CDATA[
          SELECT u.UserCode, u.UserName, u.LoginCode,u.DepCode,d.DepName
          FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
          INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] as d on d.DepCode = u.DepCode and (d.IsDelete is null OR d.IsDelete!='1') and u.rentid=d.rentid
          WHERE u.IsDelete = 0 AND u.rentid=1853
        ]]>
        <if test="userCodes!=null ">
            <![CDATA[
              AND u.UserCode IN (
            ]]>
            ${userCodes} )
        </if>
        <![CDATA[
          ORDER BY u.UserCode ASC
        ]]>
    </select>

    <!-- 查询项目总名字 -->
    <select id="queryRegionAvatarInfoName" parameterType="java.lang.String" resultType="java.lang.String">
				SELECT i.Names
FROM [HM_wygl_new_1853].[dbo].[Tb_HSPR_RegionAvatarInfo] as i where i.CommID=#{commId} and i.rentid=1853
	</select>
    <!--
	获取已存在的用户的erp的id集合
	-->
    <select id="selectIdList" resultType="java.lang.String">
		SELECT erp_user_id FROM `sys_user`
	</select>

    <select id="findByUserCodesOfEnable" resultType="java.lang.String">
        SELECT t.UserCode
        FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS t
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] as d on d.DepCode = t.DepCode and (d.IsDelete is
        null OR d.IsDelete!='1')
        WHERE t.IsDelete = 0 AND t.rentid=1853 and d.rentid=1853
        <if test="userCodes!=null ">
            AND t.UserCode IN
            (${userCodes})
        </if>
    </select>

    <select id="findByNameAndCode" resultType="cn.com.countrygarden.bi.main.dto.tbsysuser.SysUserDto">
        <![CDATA[
          SELECT u.UserCode code, u.UserName name, u.LoginCode,u.DepCode depCode,d.DepName depName,u.MobileTel phone
          FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
          INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] as d on d.DepCode = u.DepCode and (d.IsDelete is null OR d.IsDelete!='1')
          WHERE u.IsDelete = 0 AND u.rentid=1853 and d.rentid=1853
        ]]>

        <if test="userQuery.employeeNames!=null  and userQuery.employeeNames!=''">
            <![CDATA[
              AND u.UserName LIKE concat('%',#{userQuery.employeeNames},'%')
            ]]>
        </if>
        <if test="userQuery.depName!=null and userQuery.depName!='' ">
            <![CDATA[
              AND d.DepName LIKE concat('%',#{userQuery.depName},'%')
            ]]>
        </if>
        <if test="userQuery.employeeCodes!=null ">
            and u.UserCode in
            <foreach collection="userQuery.employeeCodes" item="employeeCodes" open="(" close=")" separator="," index="index">
                #{employeeCodes}
            </foreach>
        </if>
        <if test="userQuery.phoneOrEmails!=null ">
            and u.MobileTel in
            <foreach collection="userQuery.phoneOrEmails" item="phoneOrEmails" open="(" close=")" separator="," index="index">
                #{phoneOrEmails}
            </foreach>
        </if>
        <![CDATA[
          ORDER BY u.UserCode ASC
        ]]>
    </select>

    <select id="tbSysUsersList" resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser">
        <![CDATA[
          SELECT top 20 u.UserCode userCode, u.UserName userName, u.LoginCode loginCode,u.DepCode depCode
          FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
          WHERE u.IsDelete = 0 AND u.rentid=1853
        ]]>
        <if test="search!=null and search!=''">
            <![CDATA[
            and (u.UserCode like concat('%', #{search}, '%') or u.UserName like concat('%', #{search}, '%') or u.LoginCode like concat('%', #{search}, '%'))
             ]]>
        </if>
    </select>
    <select id="getErpOrgList" resultType="cn.com.countrygarden.bi.main.model.twerp.ErpCommunity">
        SELECT DISTINCT d2.DepName organName
        FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
        JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] as d on d.DepCode = u.DepCode  and (d.IsDelete is null OR d.IsDelete!='1') and u.rentid=d.rentid
        JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] d2 on d2.SortDepCode = LEFT(d.SortDepCode, 8) and u.rentid=d2.rentid
        WHERE u.IsDelete = 0 and u.rentid=1853
    </select>

    <select id="getUserName" resultType="java.lang.String" parameterType="list">
        SELECT  u.UserName,  u.OrganId
        FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
        WHERE u.UserCode IN
        <foreach collection="userCodeList" index="index" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
    </select>

    <select id="findByNameAndCodeInsuranceOrder"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser">
        <![CDATA[
          SELECT u.UserCode, u.UserName, u.LoginCode,u.DepCode,d.DepName,u.MobileTel
          FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
          INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] as d on d.DepCode = u.DepCode and (d.IsDelete is null OR d.IsDelete!='1')
          WHERE u.IsDelete = 0 AND  u.rentid=1853 and d.rentid=1853
        ]]>
        <if test="userQuery.excludeIdsSql!=null and userQuery.excludeIdsSql !='' ">
            AND u.UserCode NOT IN (#{userQuery.excludeIdsSql})
        </if>
        <if test="userQuery.loginCode!=null ">
            <![CDATA[
              AND u.LoginCode LIKE concat('%',#{userQuery.loginCode},'%')
            ]]>
        </if>
        <if test="userQuery.userName!=null ">
            <![CDATA[
              AND u.UserName LIKE concat('%',#{userQuery.userName},'%')
            ]]>
        </if>
        <if test="userQuery.deptName!=null ">
            <![CDATA[
              AND d.DepName LIKE concat('%',#{userQuery.deptName},'%')
            ]]>
        </if>
        <![CDATA[
          ORDER BY u.UserCode ASC
        ]]>
    </select>

    <select id="erpjiashuju"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser">
SELECT DISTINCT u.UserCode
		FROM [HM_wygl_new_1853].[dbo].Tb_Sys_User u
		where u.rentid=1853

    </select>
</mapper>