<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~
  ~      Copyright (c) 2018-2025, lengleng All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the yifu4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: lengleng (wangiegie@gmail.com)
  ~
  -->

<!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.SysUserInfoMapper">

  <resultMap id="sysUserInfoMap" type="com.yifu.cloud.plus.v1.yifu.permission.entity.SysUserInfo">
    <id property="fdId" column="fd_id"/>
    <result property="fdName" column="fd_name"/>
    <result property="fdLoginName" column="fd_login_name"/>
    <result property="fdLdapDn" column="fd_ldap_dn"/>
    <result property="fdDeptName" column="fd_dept_name"/>
    <result property="fdMobileNo" column="fd_mobile_no"/>
    <result property="fdEmail" column="fd_email"/>
    <result property="status" column="status"/>
    <result property="roleNames" column="role_names"/>
    <result property="createBy" column="CREATE_BY"/>
    <result property="createName" column="CREATE_NAME"/>
    <result property="createTime" column="CREATE_TIME"/>
    <result property="updateBy" column="UPDATE_BY"/>
    <result property="updateTime" column="UPDATE_TIME"/>
  </resultMap>
    <sql id="Base_Column_List">
                a.fd_id,
                a.fd_name,
                a.fd_login_name,
                a.fd_ldap_dn,
                a.fd_dept_name,
                a.fd_mobile_no,
                a.fd_email,
                a.status,
                GROUP_CONCAT(r.role_name ORDER BY r.create_time asc) as role_names,
                a.CREATE_BY,
                a.CREATE_NAME,
                a.CREATE_TIME,
                a.UPDATE_BY,
                a.UPDATE_TIME
    </sql>
    <sql id="sysUserInfo_where">
        <if test="sysUserInfo != null">
                    <if test="sysUserInfo.fdId != null and sysUserInfo.fdId.trim() != ''">
                        AND a.fd_id = #{sysUserInfo.fdId}
                    </if>
                    <if test="sysUserInfo.fdName != null and sysUserInfo.fdName.trim() != ''">
                        AND a.fd_name like concat('%',#{sysUserInfo.fdName},'%')
                    </if>
                    <if test="sysUserInfo.fdLoginName != null and sysUserInfo.fdLoginName.trim() != ''">
                        AND a.fd_login_name = #{sysUserInfo.fdLoginName}
                    </if>
                    <if test="sysUserInfo.fdLdapDn != null and sysUserInfo.fdLdapDn.trim() != ''">
                        AND a.fd_ldap_dn = #{sysUserInfo.fdLdapDn}
                    </if>
                    <if test="sysUserInfo.fdDeptName != null and sysUserInfo.fdDeptName.trim() != ''">
                        AND a.fd_dept_name like concat('%',#{sysUserInfo.fdDeptName},'%')
                    </if>
                    <if test="sysUserInfo.fdMobileNo != null and sysUserInfo.fdMobileNo.trim() != ''">
                        AND a.fd_mobile_no = #{sysUserInfo.fdMobileNo}
                    </if>
                    <if test="sysUserInfo.fdEmail != null and sysUserInfo.fdEmail.trim() != ''">
                        AND a.fd_email = #{sysUserInfo.fdEmail}
                    </if>
                    <if test="sysUserInfo.status != null">
                        AND a.status = #{sysUserInfo.status}
                    </if>
                    <if test="sysUserInfo.createBy != null and sysUserInfo.createBy.trim() != ''">
                        AND a.CREATE_BY = #{sysUserInfo.createBy}
                    </if>
                    <if test="sysUserInfo.createName != null and sysUserInfo.createName.trim() != ''">
                        AND a.CREATE_NAME = #{sysUserInfo.createName}
                    </if>
                    <if test="sysUserInfo.createTime != null">
                        AND a.CREATE_TIME = #{sysUserInfo.createTime}
                    </if>
                    <if test="sysUserInfo.updateBy != null and sysUserInfo.updateBy.trim() != ''">
                        AND a.UPDATE_BY = #{sysUserInfo.updateBy}
                    </if>
                    <if test="sysUserInfo.updateTime != null">
                        AND a.UPDATE_TIME = #{sysUserInfo.updateTime}
                    </if>
        </if>
    </sql>
    <!--sysUserInfo简单分页查询-->
    <select id="getSysUserInfoPage" resultMap="sysUserInfoMap">
		SELECT
			a.fd_id,
			a.fd_name,
			a.fd_login_name,
			a.fd_ldap_dn,
			a.fd_dept_name,
			a.fd_mobile_no,
			a.fd_email,
			a.status,
			a.role_names,
			a.CREATE_BY,
			a.CREATE_NAME,
			a.CREATE_TIME,
			a.UPDATE_BY,
			a.UPDATE_TIME
		from (
			SELECT
			<include refid="Base_Column_List"/>
			FROM sys_user_info a
			LEFT JOIN sys_user_role_res res on a.fd_login_name=res.login_name
			LEFT JOIN sys_role_info r on res.role_id=r.fd_id and r.delete_flag = 0 and r.status = 0
			<where>
				1=1
				<include refid="sysUserInfo_where"/>
			</where>
			GROUP BY a.fd_id
		) a
		<if test="sysUserInfo != null">
			<if test="sysUserInfo.roleNames != null and sysUserInfo.roleNames.trim() != ''">
				where a.role_names like concat('%',#{sysUserInfo.roleNames},'%')
			</if>
		</if>
		order by a.CREATE_TIME desc
    </select>

	<!--list-->
    <select id="getEkpUser" resultMap="sysUserInfoMap">
		select e.fd_id,e.fd_name,p.fd_login_name,e.fd_ldap_dn
			 ,SUBSTRING_INDEX(SUBSTRING_INDEX(e.fd_ldap_dn,',ou', 2),',ou=', -1) AS fd_dept_name
			 ,p.fd_mobile_no,p.fd_email
		from sys_org_element e JOIN sys_org_person p on e.fd_id=p.fd_id
		where e.fd_is_available = '1'
    </select>

	<!--list-->
	<select id="getResAndRoleNameUserId" resultType="com.yifu.cloud.plus.v1.yifu.permission.entity.SysUserRoleRes">
		SELECT
			a.fd_id as fdId
			,a.user_id as userId
			,a.role_id as roleId
			,r.role_name as roleName
		FROM sys_user_role_res a
		LEFT JOIN sys_role_info r on a.role_id=r.fd_id and r.delete_flag = 0 and r.status = 0
		where a.login_name=#{loginName}
	</select>

	<!--给EKP用的，获取列表是否全量的权限-->
	<!--#全量0；部分全量1；数据权限2；客户a；商机b；报价单c；合同d；项目e；BUf；条线g；收入归属h；）-->
	<select id="getAuthByUserIdAndModelId" resultType="java.lang.Integer">
		select
			case
				when a.data_auth is null then 0
				when a.data_auth like "%0%" then 1
				when a.data_auth like "%a%" and a.CUSTOMER_ID is not null then 1
				when a.data_auth like "%b%" and a.BUSSINESS_ID is not null then 1
				when a.data_auth like "%c%" and a.QUOTATION_ID is not null then 1
				when a.data_auth like "%d%" and a.CONTRACT_ID is not null then 1
				when a.data_auth like "%e%" and a.DOMAIN_ID is not null then 1
				when a.data_auth like "%f%" and a.BU_ID is not null then 1
				when a.data_auth like "%g%" and a.LINE_ID is not null then 1
				when a.data_auth like "%h%" and a.INCOME_ID is not null then 1
				else 2 end
		from (
				 SELECT
					 GROUP_CONCAT(DISTINCT a.data_auth) as data_auth
					  ,b.INCOME_ID
					  ,b.CUSTOMER_ID
					  ,b.CONTRACT_ID
					  ,b.BUSSINESS_ID
					  ,b.QUOTATION_ID
					  ,b.DOMAIN_ID
					  ,b.BU_ID
					  ,b.LINE_ID
				 FROM
					 sys_menu b
						 join sys_role_menu_res m on b.id=m.menu_id
						 join sys_role_info a on m.role_id = a.fd_id
						 join sys_user_role_res r on r.role_id = a.fd_id
				 where a.delete_flag = 0 and a.status = 0 and b.DELETE_STATUS='1' and b.PERMISSION_STATUS='0'
				   and r.user_id = #{userId} and b.MODEL_ID=#{modelId}
			 ) a
	</select>

	<select id="getUserByCodeOrLoginName" resultMap="sysUserInfoMap">
		SELECT
		a.fd_id,
		a.fd_name,
		a.fd_login_name,
		a.fd_dept_name
		FROM sys_user_info a
		<where>
			1=1 and a.status = 0
			<if test="code != null and code != ''">
				AND (a.fd_login_name like concat('%',#{code},'%') or a.fd_name like concat('%',#{code},'%'))
			</if>
		</where>
		order by a.CREATE_TIME desc
	</select>
</mapper>
