/*
*  Copyright 2019-2020 Zheng Jie
*
*  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.
*/
package me.zhengjie.modules.dm.user.repository;

import me.zhengjie.modules.dm.user.domain.DmUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
* @website https://el-admin.vip
* @author shiguangji
* @date 2021-03-09
**/
public interface DmUserRepository extends JpaRepository<DmUser, String>, JpaSpecificationExecutor<DmUser> {

    /**
     * 查询未绑定账户的用户
     * @param userId
     * @return
     */
    @Query(value = "SELECT u.* FROM dm_user u WHERE " +
            "u.id not in (select dm_user_id from sys_user where dm_user_id is not null and if(:userId != '',user_id != :userId,1=1))",nativeQuery = true)
    List<DmUser> findNotBindUser(@Param("userId") String userId);

    /**
     * 根据邮箱查询
     * @param email 邮箱
     * @return /
     */
    DmUser findByEmail(String email);

    /**
     * 查询
     * @return
     */
    @Query(value = "SELECT COUNT(1) FROM dm_user WHERE dept_id = ?1",nativeQuery = true)
    Integer selecIsNotDept(String deptId);

    @Query(value = "SELECT DISTINCT name FROM sys_dept WHERE dept_id in (?1)",nativeQuery = true)
    List selectDeptName(String deptId);

    @Query(value = "SELECT u.* FROM dm_user as u left join sys_dept d on d.dept_id = u.dept_id where u.card_id = :cardId ",nativeQuery = true)
    DmUser findUserByCardId(@Param("cardId") String cardId);
}