<?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"/>
  </resultMap>

	<resultMap id="sysUserPermissionExportMap" type="com.yifu.cloud.plus.v1.yifu.permission.vo.SysUserPermissionVo">
		<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"/>
	</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,
                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.EMP_DEPT 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.EMP_LOGIN_NAME = b.fd_login_name
        <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.EMP_LOGIN_NAME = b.fd_login_name
		<where>
			a.DELETE_FLAG = '1'
			<include refid="sysUserPermission_where"/>
		</where>
	</select>

	<select id="userSelectList" resultMap="sysUserPermissionExportMap">
		SELECT
		a.EMP_NAME,
		a.EMP_LOGIN_NAME,
		c.fd_dept_name,
		b.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 b on a.PERMISSION_TYPE = b.id
		LEFT JOIN sys_user_info c on a.EMP_LOGIN_NAME = c.fd_login_name
		<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}
		<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_3b04b9daa0c25e 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_3b04b9daa0c25e like concat('%',#{name},'%'))
		</if>
	</select>

	<select id="selectDeptVO" resultMap="nameAndCodeMap">
		SELECT
		a.fd_no code,a.fd_name name
		from sys_org_element a
		where a.fd_is_available = '1' and 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="selectDictByType" resultMap="dictMap">
		SELECT
		a.dict_id as id,a.name
		from permission_type a
		where a.dict_type = #{type}
	</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>

</mapper>
