<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<!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.yifu.admin.mapper.SysUserDeptPermissionMapper">

	<!-- 通用查询映射结果 -->
	<resultMap id="BaseResultMap" type="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUserDeptPermission">
		<id column="id" property="id"/>
		<result column="user_id" property="userId"/>
		<result column="dept_id" property="deptId"/>
		<result column="project_no" property="projectNo"/>
		<result column="project_name" property="projectName"/>
		<result column="client" property="client"/>
		<result column="user_type" property="userType"/>
		<result column="permissions_type" property="permissionsType"/>
		<result column="appoint_dept_scope" property="appointDeptScope"/>
		<result column="project_id" property="projectId"/>
	</resultMap>

	<sql id="Base_Column_List">
        a.id,
        a.user_id,
        a.dept_id,
        a.project_no,
		a.project_name,
        a.client,
        a.user_type,
        a.permissions_type,
        a.appoint_dept_scope,
		a.project_id
    </sql>

	<select id="getUserDeptPermission" resultMap="BaseResultMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM sys_user_dept_permission a
		WHERE a.user_id = #{userId} and a.project_no = #{projectNo}
		limit one
	</select>

	<select id="getUserDeptPermissionList" resultMap="BaseResultMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM sys_user_dept_permission a
		WHERE a.user_id = #{userId}
	</select>

	<select id="selectCasDeptVo" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysCspDeptVo">
		SELECT
			b.name as departName,
			a.dept_id as departId
		FROM sys_user_dept_permission a
		inner join sys_dept b on a.dept_id = b.dept_id
		WHERE a.user_id = #{userId} and a.project_id = #{projectId}
		limit 1
	</select>
</mapper>
