<?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.TbSysUserRoleMapper">
    <sql id="selectAll">
    <![CDATA[
        SELECT DISTINCT r.RoleCode, r.RoleName, r.RoleDescribe, r.IsSysRole, r.CommID, r.SysRoleCode
        FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_Role] AS r
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_UserRole] AS ur ON (r.RoleCode = ur.RoleCode)
        ]]>
    </sql>

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

    <select id="findUserOrganCodeByUserCode" parameterType="string" resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUserRole">
        <![CDATA[
SELECT TOP
	1 u.UserCode,
	ur.RoleCode,
	SUBSTRING ( d.OrganCode, 1, 4 ) AS OrganCode,
	d.CommID,
	o.OrganName
FROM
	[HM_wygl_new_1853].[dbo].[Tb_Sys_User] u
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_UserRole] ur ON ur.UserCode = u.UserCode
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] d ON d.RoleCode = ur.RoleCode
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o ON ( SUBSTRING ( d.OrganCode, 1, 4 ) = o.OrganCode AND o.isDelete = 0 )
WHERE
	u.UserCode = #{userCode} and u.rentid=1853 and ur.rentid=1853 and d.rentid=1853
	and o.rentid=1853
        ]]>
    </select>


</mapper>