<?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.yifu.cloud.plus.v1.permission.mapper.SysUserPermissionMapper">

	<resultMap id="sysUserPermissionMap" type="com.yifu.cloud.plus.v1.yifu.permission.entity.SysUserPermission">
    <id property="id" column="ID"/>
    <result property="empName" column="EMP_NAME"/>
    <result property="empLoginName" column="EMP_LOGIN_NAME"/>
    <result property="empDept" column="EMP_DEPT"/>
    <result property="permissionType" column="PERMISSION_TYPE"/>
    <result property="name" column="NAME"/>
    <result property="code" column="CODE"/>
    <result property="isContains" column="IS_CONTAINS"/>
    <result property="permissionStatus" column="PERMISSION_STATUS"/>
    <result property="deleteFlag" column="DELETE_FLAG"/>
    <result property="createBy" column="CREATE_BY"/>
    <result property="updateBy" column="UPDATE_BY"/>
    <result property="createName" column="CREATE_NAME"/>
	<result property="createTime" column="CREATE_TIME"/>
    <result property="updateTime" column="UPDATE_TIME"/>
    <result property="sourceType" column="SOURCE_TYPE"/>
  </resultMap>

	<resultMap id="sysUserPermissionExportMap" type="com.yifu.cloud.plus.v1.yifu.permission.vo.SysUserPermissionExportVo">
		<result property="empName" column="EMP_NAME"/>
		<result property="empLoginName" column="EMP_LOGIN_NAME"/>
		<result property="empDept" column="EMP_DEPT"/>
		<result property="permissionType" column="PERMISSION_TYPE"/>
		<result property="name" column="NAME"/>
		<result property="code" column="CODE"/>
		<result property="isContains" column="IS_CONTAINS"/>
		<result property="permissionStatus" column="PERMISSION_STATUS"/>
	</resultMap>

	<resultMap id="nameAndCodeMap" type="com.yifu.cloud.plus.v1.yifu.permission.vo.SysUserDeptVo">
		<result property="name" column="name"/>
		<result property="code" column="code"/>
		<result property="domainId" column="domainId"/>
	</resultMap>

	<resultMap id="dictMap" type="com.yifu.cloud.plus.v1.yifu.permission.vo.SysDictVo">
		<result property="id" column="id"/>
		<result property="name" column="name"/>
	</resultMap>

    <sql id="Base_Column_List">
                a.ID,
                a.EMP_NAME,
                a.EMP_LOGIN_NAME,
                b.fd_dept_name as EMP_DEPT,
                a.PERMISSION_TYPE,
                a.NAME,
                a.CODE,
                a.IS_CONTAINS,
                a.PERMISSION_STATUS,
                a.DELETE_FLAG,
                a.CREATE_TIME
    </sql>
    <sql id="sysUserPermission_where">
        <if test="sysUserPermission != null">
			<if test="sysUserPermission.idList != null">
				AND a.id in
				<foreach item="idStr" index="index" collection="sysUserPermission.idList" open="(" separator="," close=")">
					#{idStr}
				</foreach>
			</if>
			<if test="sysUserPermission.idList == null">
                    <if test="sysUserPermission.empName != null and sysUserPermission.empName.trim() != ''">
                        AND a.EMP_NAME like concat('%',#{sysUserPermission.empName},'%')
                    </if>
                    <if test="sysUserPermission.empLoginName != null and sysUserPermission.empLoginName.trim() != ''">
                        AND a.EMP_LOGIN_NAME = #{sysUserPermission.empLoginName}
                    </if>
                    <if test="sysUserPermission.empDept != null and sysUserPermission.empDept.trim() != ''">
                        AND b.fd_dept_name like concat('%',#{sysUserPermission.empDept},'%')
                    </if>
                    <if test="sysUserPermission.permissionType != null and sysUserPermission.permissionType.trim() != ''">
                        AND a.PERMISSION_TYPE = #{sysUserPermission.permissionType}
                    </if>
                    <if test="sysUserPermission.name != null and sysUserPermission.name.trim() != ''">
                        AND a.NAME like concat('%',#{sysUserPermission.name},'%')
                    </if>
                    <if test="sysUserPermission.code != null and sysUserPermission.code.trim() != ''">
                        AND a.CODE = #{sysUserPermission.code}
                    </if>
                    <if test="sysUserPermission.permissionStatus != null and sysUserPermission.permissionStatus.trim() != ''">
                        AND a.PERMISSION_STATUS = #{sysUserPermission.permissionStatus}
                    </if>
			</if>
        </if>
    </sql>
    <!--sysUserPermission简单分页查询-->
    <select id="getSysUserPermissionPage" resultMap="sysUserPermissionMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys_user_permission a left join sys_user_info b on a.user_id = b.fd_id
        <where>
            a.DELETE_FLAG = '1'
            <include refid="sysUserPermission_where"/>
        </where>
		order by a.CREATE_TIME desc
    </select>

	<select id="userSelectCount" resultType="java.lang.Long">
		SELECT
			count(1)
		FROM sys_user_permission a left join sys_user_info b on a.user_id = b.fd_id
		<where>
			a.DELETE_FLAG = '1'
			<include refid="sysUserPermission_where"/>
		</where>
	</select>

	<select id="userSelectList" resultMap="sysUserPermissionExportMap">
		SELECT
		a.EMP_NAME,
		a.EMP_LOGIN_NAME,
		b.fd_dept_name as EMP_DEPT,
		c.name AS PERMISSION_TYPE,
		a.NAME,
		a.CODE,
		CASE WHEN a.IS_CONTAINS=0 THEN "是"
		WHEN a.IS_CONTAINS =1 THEN "否"
		ELSE a.IS_CONTAINS END as IS_CONTAINS,
		CASE WHEN a.PERMISSION_STATUS=0 THEN "启用"
		WHEN a.PERMISSION_STATUS =1 THEN "禁用"
		ELSE a.PERMISSION_STATUS END as PERMISSION_STATUS
		FROM sys_user_permission a
		LEFT JOIN permission_type c on a.PERMISSION_TYPE = c.dict_id and c.dict_type='permission'
		LEFT JOIN sys_user_info b on a.user_id = b.fd_id
		<where>
			a.DELETE_FLAG = '1'
			<include refid="sysUserPermission_where"/>
		</where>
		limit #{sysUserPermission.limitStart},#{sysUserPermission.limitEnd}
	</select>

	<select id="selectUnitVO" resultMap="nameAndCodeMap">
		SELECT
			a.fd_3a9d1d4d438bfc code,a.fd_3a9d1d4caeeeb8 name
			from ekp_05b7485fd444f95a6177 a
		where 1=1
			<if test="name != null and name.trim() != ''">
				AND (a.fd_3a9d1d4d438bfc like concat('%',#{name},'%') or a.fd_3a9d1d4caeeeb8 like concat('%',#{name},'%'))
			</if>
	</select>

	<select id="selectContractVO" resultMap="nameAndCodeMap">
		SELECT
		a.fd_3ade33763baa0e code,a.fd_3ade334bac0016 name
		from ekp_0103c453b2848c3bb31c a
		where 1=1
		<if test="name != null and name.trim() != ''">
			AND (a.fd_3ade33763baa0e like concat('%',#{name},'%') or a.fd_3ade334bac0016 like concat('%',#{name},'%'))
		</if>
	</select>

	<select id="selectSettleVO" resultMap="nameAndCodeMap">
		SELECT
		a.fd_3a37fe508071fe code,a.fd_3a2b38c61ef9aa name
		from ekp_24a8e6a7fc143bb8c48a a
		where 1=1
		<if test="name != null and name.trim() != ''">
			AND (a.fd_3a37fe508071fe like concat('%',#{name},'%') or a.fd_3a2b38c61ef9aa like concat('%',#{name},'%'))
		</if>
	</select>

	<select id="selectContractSimpleVO" resultMap="nameAndCodeMap">
		SELECT
		a.NAME,
		a.CODE
		from sys_user_permission a
		where a.EMP_LOGIN_NAME = #{loginName} and a.PERMISSION_TYPE = #{type} and a.PERMISSION_STATUS = '0'
		<if test="name != null and name.trim() != ''">
			AND (a.NAME like concat('%',#{name},'%') or a.CODE like concat('%',#{name},'%'))
		</if>
	</select>

	<select id="selectBusinessVO" resultMap="nameAndCodeMap">
		SELECT
		a.fd_3b107f1084fd96 code,a.fd_3b04b95268f19c name
		from ekp_2f8e08660c542d8a169f a
		where 1=1
		<if test="name != null and name.trim() != ''">
			AND (a.fd_3b107f1084fd96 like concat('%',#{name},'%') or a.fd_3b04b95268f19c like concat('%',#{name},'%'))
		</if>
	</select>

	<select id="selectQuotationVO" resultMap="nameAndCodeMap">
		SELECT
		a.fd_3b04b9dc64a9f4 code,a.fd_3b04b9d8688cfa name
		from ekp_fd1449d265845e087bf2 a
		where 1=1
		<if test="name != null and name.trim() != ''">
			AND (a.fd_3b04b9dc64a9f4 like concat('%',#{name},'%') or a.fd_3b04b9d8688cfa like concat('%',#{name},'%'))
		</if>
	</select>

	<select id="selectDeptVO" resultMap="nameAndCodeMap">
		SELECT
		a.fd_no code,a.fd_name name,a.fd_id domainId
		from sys_org_element a
		where a.fd_is_available = '1' and a.fd_org_type = 2
		<if test="name != null and name.trim() != ''">
			AND (a.fd_no like concat('%',#{name},'%') or a.fd_name like concat('%',#{name},'%'))
		</if>
	</select>

	<select id="selectTwoGsVO" resultMap="nameAndCodeMap">
		SELECT
		a.fd_3b1235d8f35c6c name
		from ekp_793f0c590104b7ebd460 a
		where 1 = 1
		<if test="name != null and name.trim() != ''">
			AND a.fd_3b1235d8f35c6c like concat('%',#{name},'%')
		</if>
	</select>

	<!-- 查找税务主体分页 -->
	<select id="selectTaxVO" resultMap="nameAndCodeMap">
		select a.fd_3afab4c6755286 AS name
		from ekp_7e091e5e36848fb97451 a
		where a.fd_3afab4c6755286 is not null
		<if test="name != null and name.trim() != ''">
			AND a.fd_3afab4c6755286 like concat('%',#{name},'%')
		</if>
		GROUP BY a.fd_3afab4c6755286 ORDER BY a.fd_3afab4c6755286 desc
	</select>

	<!--根据部门编码查询id-->
	<select id="selectSettleByCode" resultType="java.lang.String">
		SELECT
		a.fd_id
		from sys_org_element a
		where a.fd_is_available = '1' and a.fd_org_type = 2 and a.fd_no = #{code}
		limit 1
	</select>

	<select id="selectDictByType" resultMap="dictMap">
		SELECT
		a.dict_id as id,a.name
		from permission_type a
		where a.dict_type = #{type}
	</select>

	<select id="selectDictByTypeOne" resultMap="dictMap">
		SELECT
			a.dict_id as id,a.name
		from permission_type a
		where a.dict_type = #{type} and a.user_id = #{userId}
	</select>

	<!--针对BU归属和条线类型时的存在判断-->
	<select id="selectDictByName" resultType="java.lang.Integer">
		SELECT
			count(1)
		from permission_type a
		where a.dict_type = #{type} and a.name = #{name}
	</select>

	<!--判断对应维度的名称或者编码是否存在-->
	<select id="selectUnitCount" resultType="java.lang.Integer">
		SELECT
		count(1)
		from ekp_05b7485fd444f95a6177 a
		where a.fd_3a9d1d4d438bfc = #{name}
	</select>

	<select id="selectContractCount" resultType="java.lang.Integer">
		SELECT
		count(1)
		from ekp_0103c453b2848c3bb31c a
		where a.fd_3ade33763baa0e = #{name}
	</select>

	<select id="selectSettleCount" resultType="java.lang.Integer">
		SELECT
		count(1)
		from ekp_24a8e6a7fc143bb8c48a a
		where a.fd_3a37fe508071fe = #{name}
	</select>

	<select id="selectBusinessCount" resultType="java.lang.Integer">
		SELECT
		count(1)
		from ekp_2f8e08660c542d8a169f a
		where a.fd_3b107f1084fd96 = #{name}
	</select>

	<select id="selectQuotationCount" resultType="java.lang.Integer">
		SELECT
		count(1)
		from ekp_fd1449d265845e087bf2 a
		where a.fd_3b04b9dc64a9f4 = #{name}
	</select>

	<select id="selectDeptCount" resultType="java.lang.Integer">
		SELECT
		count(1)
		from sys_org_element a
		where a.fd_is_available = '1' and fd_org_type = 2 and a.fd_no = #{name}
	</select>

	<select id="selectTwoGsCount" resultType="java.lang.Integer">
		SELECT
			count(1)
		from ekp_793f0c590104b7ebd460 a
		where a.fd_3b1235d8f35c6c = #{name}
	</select>

	<select id="selectTaxCount" resultType="java.lang.Integer">
		SELECT
			count(1)
		from ekp_7e091e5e36848fb97451 a
		where a.fd_3afab4c6755286 = #{name}
	</select>

	<!-- 查找当前用户的所有权限 -->
	<select id="getUserPermissionByUserId" resultMap="sysUserPermissionMap">
		select
			p.PERMISSION_TYPE,p.NAME,p.CODE,ifnull(p.IS_CONTAINS,'1') IS_CONTAINS
		from
			sys_user_permission p
		where p.DELETE_FLAG='1' and p.PERMISSION_STATUS='0'
		  and p.USER_ID = #{userId}
		ORDER BY p.PERMISSION_TYPE
	</select>

	<!-- 查找权限,用来新建 - 客户 -->
	<select id="getUserPermissionByAutoCreateKH" resultMap="sysUserPermissionMap">
		select
			e.fd_name EMP_NAME ,p.fd_login_name EMP_LOGIN_NAME ,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
			 ,'1' PERMISSION_TYPE ,d.fd_3a9d1d4caeeeb8 NAME ,d.fd_3a9d1d4d438bfc CODE,0 SOURCE_TYPE
			 ,'1' IS_CONTAINS ,'0' PERMISSION_STATUS ,'1' DELETE_FLAG ,e.fd_id USER_ID ,d.fd_id DOMAIN_ID
		from ekp_fa3e25a1b6c472683e96 d
				 LEFT JOIN ekp_c472683e96_8b4d79b2c8 f on f.fd_parent_id = d.fd_id
				 LEFT JOIN sys_org_element e on e.fd_id = f.fd_3b118b5e5d1024
				 JOIN sys_org_person p on e.fd_id=p.fd_id
		where d.fd_id = #{fdId} GROUP BY e.fd_id
	</select>
	<!-- 查找权限,用来新建 - 商机 -->
	<select id="getUserPermissionByAutoCreateSJ" resultMap="sysUserPermissionMap">
		select
			e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
			 ,'2' PERMISSION_TYPE,d.fd_3b04b95268f19c NAME,d.fd_3b107f1084fd96 CODE,0 SOURCE_TYPE
			 ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
		from ekp_24385b8cc934eb8a9231 d
				 LEFT JOIN ekp_34eb8a9231_8c4fb81d42 f on f.fd_parent_id = d.fd_id
				 LEFT JOIN sys_org_element e on e.fd_id = f.fd_3b0e8c7f169ed2
				 JOIN sys_org_person p on e.fd_id=p.fd_id
		where d.fd_id = #{fdId} GROUP BY e.fd_id
	</select>
	<!-- 查找权限,用来新建 - 报价单 -->
	<select id="getUserPermissionByAutoCreateBJD" resultMap="sysUserPermissionMap">
		select
			e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
			 ,'3' PERMISSION_TYPE,d.fd_3b04b9d8688cfa NAME,d.fd_3b04b9dc64a9f4 CODE,0 SOURCE_TYPE
			 ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
		from ekp_fd06fd1251d4bac8cc91 d
				 LEFT JOIN ekp_d4bac8cc91_8aad2804b4 f1 on f1.fd_parent_id = d.fd_id
				 LEFT JOIN ekporg_18308def06b6c534b70e f on f.fd_parent_id = f1.fd_id
				 LEFT JOIN sys_org_element e on e.fd_id = f.sys_org_person_id JOIN sys_org_person p on e.fd_id=p.fd_id
		where d.fd_id = #{fdId} GROUP BY e.fd_id
	</select>
	<!-- 查找权限,用来新建 - 合同 -->
	<select id="getUserPermissionByAutoCreateHT" resultMap="sysUserPermissionMap">
		select a.EMP_NAME,a.EMP_LOGIN_NAME,a.EMP_DEPT,a.PERMISSION_TYPE,a.NAME,a.CODE,a.SOURCE_TYPE,a.IS_CONTAINS,a.PERMISSION_STATUS,a.DELETE_FLAG,a.USER_ID,a.DOMAIN_ID
		from (
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'4' PERMISSION_TYPE,d.fd_3ad97ed761c75c NAME,d.fd_3ade319226ed52 CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_4834ce2ea964f1a8cfdb d
						  LEFT JOIN ekporg_183823dc7ae199aa37e9 f on f.fd_parent_id = d.fd_id
						  LEFT JOIN sys_org_element e on e.fd_id = f.sys_org_person_id JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
				 union ALL
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'4' PERMISSION_TYPE,d.fd_3ad97ed761c75c NAME,d.fd_3ade319226ed52 CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_4834ce2ea964f1a8cfdb d
						  LEFT JOIN sys_org_element e on e.fd_id = d.fd_3ade318b64b54c
						  JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
			 ) a GROUP BY a.USER_ID
	</select>
	<!-- 查找权限,用来新建 - 项目 -->
	<select id="getUserPermissionByAutoCreateXM" resultMap="sysUserPermissionMap">
		select a.EMP_NAME,a.EMP_LOGIN_NAME,a.EMP_DEPT,a.PERMISSION_TYPE,a.NAME,a.CODE,a.SOURCE_TYPE,a.IS_CONTAINS,a.PERMISSION_STATUS,a.DELETE_FLAG,a.USER_ID,a.DOMAIN_ID
		from (
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'5' PERMISSION_TYPE,d.fd_3a829ae9bab760 NAME,d.fd_3a829aea0b84a4 CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_fb3b8d0c8f148639b8e2 d
						  LEFT JOIN ekporg_1832fea7d722404c1e92 f on f.fd_parent_id = d.fd_id
						  LEFT JOIN sys_org_element e on e.fd_id = f.sys_org_person_id JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
				 union ALL
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'5' PERMISSION_TYPE,d.fd_3a829ae9bab760 NAME,d.fd_3a829aea0b84a4 CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_fb3b8d0c8f148639b8e2 d
						  LEFT JOIN sys_org_element e on e.fd_id = d.fd_dept_manager
						  JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}

			 ) a GROUP BY a.USER_ID
	</select>

	<!-- 查找权限,用来新建 - 合同变更 -->
	<select id="getUserPermissionByAutoCreateHTBG" resultMap="sysUserPermissionMap">
		select a.EMP_NAME,a.EMP_LOGIN_NAME,a.EMP_DEPT,a.PERMISSION_TYPE,a.NAME,a.CODE,a.SOURCE_TYPE,a.IS_CONTAINS,a.PERMISSION_STATUS,a.DELETE_FLAG,a.USER_ID,a.DOMAIN_ID
		from (
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'4' PERMISSION_TYPE,d.fd_3b8797876dc13c_text NAME,d.fd_3b879793d37afe CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_224c54af6ae4418ae0a2 d
						  LEFT JOIN ekporg_18de3386b4709d6f33b7 f on f.fd_parent_id = d.fd_id
						  LEFT JOIN sys_org_element e on e.fd_id = f.sys_org_person_id JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
				 union all
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'4' PERMISSION_TYPE,d.fd_3b8797876dc13c_text NAME,d.fd_3b879793d37afe CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_224c54af6ae4418ae0a2 d
						  left join ekp_0103c453b2848c3bb31c h on d.fd_3b879793d37afe=h.fd_3ade33763baa0e
						  LEFT JOIN sys_org_element e on e.fd_id = h.fd_3ade318b64b54c JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
			 ) a
		GROUP BY a.USER_ID
	</select>

	<!-- 查找权限,用来新建 - 项目变更 -->
	<select id="getUserPermissionByAutoCreateXMBG" resultMap="sysUserPermissionMap">
		select a.EMP_NAME,a.EMP_LOGIN_NAME,a.EMP_DEPT,a.PERMISSION_TYPE,a.NAME,a.CODE,a.SOURCE_TYPE,a.IS_CONTAINS,a.PERMISSION_STATUS,a.DELETE_FLAG,a.USER_ID,a.DOMAIN_ID
		from (
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'5' PERMISSION_TYPE,d.fd_3b8797876dc13c_text NAME,d.fd_3b879793d37afe CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_d701e3e72794ff390ca8 d
						  LEFT JOIN ekporg_19643802f278857760c1 f on f.fd_parent_id = d.fd_id
						  LEFT JOIN sys_org_element e on e.fd_id = f.sys_org_person_id JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
				 union ALL
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'5' PERMISSION_TYPE,d.fd_3b8797876dc13c_text NAME,d.fd_3b879793d37afe CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_d701e3e72794ff390ca8 d
						  LEFT JOIN sys_org_element e on e.fd_id = d.fd_changeafter
						  JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
			 ) a GROUP BY a.USER_ID
	</select>

	<!-- 查找fdId对应的code -->
	<select id="selectCodeByFdId" resultType="java.lang.String">
		select d.fd_3a9d1d4d438bfc CODE from ekp_fa3e25a1b6c472683e96 d where d.fd_id = #{fdId}
		union ALL
		select d.fd_3b107f1084fd96 CODE from ekp_24385b8cc934eb8a9231 d where d.fd_id = #{fdId}
		union ALL
		select d.fd_3b04b9dc64a9f4 CODE from ekp_fd06fd1251d4bac8cc91 d where d.fd_id = #{fdId}
		union ALL
		select d.fd_3ade319226ed52 CODE from ekp_4834ce2ea964f1a8cfdb d where d.fd_id = #{fdId}
		union ALL
		select d.fd_3a829aea0b84a4 CODE from ekp_fb3b8d0c8f148639b8e2 d where d.fd_id = #{fdId}
		union ALL
		select d.fd_3b879793d37afe CODE from ekp_d701e3e72794ff390ca8 d where d.fd_id = #{fdId}
		union ALL
		select d.fd_3b879793d37afe CODE from ekp_224c54af6ae4418ae0a2 d where d.fd_id = #{fdId}
		limit 1
	</select>
	<!-- 删除EKP新建的 -->
	<delete id="deleteUserPermissionByIdAndType">
		delete from sys_user_permission p
		where SOURCE_TYPE = 0 and CODE = #{code} and PERMISSION_TYPE = #{permissionType}
	</delete>
	<select id="selectDivitionCount" resultType="java.lang.Integer">
		SELECT
			count(1)
		from ekp_shenhepeizhi_table a
		where a.fd_shiyebu = #{name}
	</select>
	<select id="selectDivitionLineCount" resultType="java.lang.Integer">
		SELECT
			count(1)
		from ekp_shenhepeizhi_table a
		where a.fd_tiaoxian = #{name}
	</select>

	<!-- 变更后项目团队 -->
	<select id="getUserPermissionByAutoCreateXMBG3" resultMap="sysUserPermissionMap">
		select a.EMP_NAME,a.EMP_LOGIN_NAME,a.EMP_DEPT,a.PERMISSION_TYPE,a.NAME,a.CODE,a.SOURCE_TYPE,a.IS_CONTAINS,a.PERMISSION_STATUS,a.DELETE_FLAG,a.USER_ID,a.DOMAIN_ID
		from (
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'5' PERMISSION_TYPE,d.fd_3b8797876dc13c_text NAME,d.fd_3b879793d37afe CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_d701e3e72794ff390ca8 d
						  LEFT JOIN ekporg_19643802f278857760c1 f on f.fd_parent_id = d.fd_id
						  LEFT JOIN sys_org_element e on e.fd_id = f.sys_org_person_id JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
			 ) a GROUP BY a.USER_ID
	</select>
	<!-- 变更后项目负责人 -->
	<select id="getUserPermissionByAutoCreateXMBG1" resultMap="sysUserPermissionMap">
		select a.EMP_NAME,a.EMP_LOGIN_NAME,a.EMP_DEPT,a.PERMISSION_TYPE,a.NAME,a.CODE,a.SOURCE_TYPE,a.IS_CONTAINS,a.PERMISSION_STATUS,a.DELETE_FLAG,a.USER_ID,a.DOMAIN_ID
		from (
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'5' PERMISSION_TYPE,d.fd_3b8797876dc13c_text NAME,d.fd_3b879793d37afe CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_d701e3e72794ff390ca8 d
						  LEFT JOIN sys_org_element e on e.fd_id = d.fd_changeafter
						  JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
			 ) a GROUP BY a.USER_ID
	</select>

	<!-- 变更前项目团队 -->
	<select id="getUserPermissionByAutoCreateXMBG4" resultMap="sysUserPermissionMap">
		select a.EMP_NAME,a.EMP_LOGIN_NAME,a.EMP_DEPT,a.PERMISSION_TYPE,a.NAME,a.CODE,a.SOURCE_TYPE,a.IS_CONTAINS,a.PERMISSION_STATUS,a.DELETE_FLAG,a.USER_ID,a.DOMAIN_ID
		from (
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'5' PERMISSION_TYPE,d.fd_3b8797876dc13c_text NAME,d.fd_3b879793d37afe CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_d701e3e72794ff390ca8 d
						  LEFT JOIN ekporg_18622320f73771d520db f on f.fd_parent_id = d.fd_id
						  LEFT JOIN sys_org_element e on e.fd_id = f.sys_org_person_id JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
			 ) a GROUP BY a.USER_ID
	</select>
	<!-- 变更前项目负责人 -->
	<select id="getUserPermissionByAutoCreateXMBG2" resultMap="sysUserPermissionMap">
		select a.EMP_NAME,a.EMP_LOGIN_NAME,a.EMP_DEPT,a.PERMISSION_TYPE,a.NAME,a.CODE,a.SOURCE_TYPE,a.IS_CONTAINS,a.PERMISSION_STATUS,a.DELETE_FLAG,a.USER_ID,a.DOMAIN_ID
		from (
				 select
					 e.fd_name EMP_NAME,p.fd_login_name EMP_LOGIN_NAME,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) EMP_DEPT
					  ,'5' PERMISSION_TYPE,d.fd_3b8797876dc13c_text NAME,d.fd_3b879793d37afe CODE,0 SOURCE_TYPE
					  ,'1' IS_CONTAINS,'0' PERMISSION_STATUS,'1' DELETE_FLAG,e.fd_id USER_ID,d.fd_id DOMAIN_ID
				 from ekp_d701e3e72794ff390ca8 d
						  LEFT JOIN sys_org_element e on e.fd_id = d.fd_changebefore

						  JOIN sys_org_person p on e.fd_id=p.fd_id
				 where d.fd_id = #{fdId}
			 ) a GROUP BY a.USER_ID
	</select>
</mapper>
