/*
*  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.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
* @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);

    /**
     * 根据用户id删除用户系统表数据
     * @param userId
     */
    @Modifying
    @Transactional
    @Query(value = "delete from dm_user_system where user_id = :userId", nativeQuery = true)
    void deleteUserSystem(@Param("userId") String userId);

    /**
     * 新增用户系统表数据
     * @param userId
     * @param systemId
     */
    @Modifying
    @Transactional
    @Query(value = "insert into dm_user_system(user_id,system_id) values(:userId, :systemId)", nativeQuery = true)
    void createUserSystem(@Param("userId") String userId, @Param("systemId") Integer systemId);

    /**
     * 根据用户id删除用户系统表数据
     * @param userId
     */
    @Modifying
    @Transactional
    @Query(value = "delete from dm_user_menu where user_id = :userId", nativeQuery = true)
    void deleteUserAppMenu(@Param("userId") String userId);

    /**
     * 新增用户系统表数据
     * @param userId
     * @param appMenuId
     */
    @Modifying
    @Transactional
    @Query(value = "insert into dm_user_menu(user_id,app_menu_id) values(:userId, :appMenuId)", nativeQuery = true)
    void createUserAppMenu(@Param("userId") String userId, @Param("appMenuId") Integer appMenuId);

    /**
     * 查询所有数据不分页
     *
     * @return
     */
    @Query(value = "SELECT u.user_code as userCode, u.name as name,u.dept_id as deptId,u.sex as sex,u.card_id as cardId  FROM dm_user u", nativeQuery = true)
    List<Map<String,String>> queryAll();
}