/*
*  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.dmuser.repository;

import me.zhengjie.modules.dmuser.domain.DmUser;
import me.zhengjie.modules.dmuser.domain.Vo.DmUserVo;
import me.zhengjie.modules.dmuser.service.dto.DmUserDto;
import me.zhengjie.ykt.domain.DmRoomDevice;
import me.zhengjie.ykt.service.dto.DmRoomDeviceDto;
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 org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author AndyCheung
* @date 2021-09-30
**/
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 userId
     * @return
     */
    @Query(value = "SELECT u.id as id, u.name as name FROM dm_user u WHERE u.id not in (select user_number from dm_card where user_number is not null and if(:id != '',id != :id,1=1) and status = '00') and u.enabled = '1'", nativeQuery = true)
    List<Map<String,String>> findNotUserCard(@Param("id") String userId);

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

    @Modifying
    @Transactional
    @Query(value = "insert into dm_users_devices (user_id,room_device_id,door_id,device_id) select ?1,?2,?3,?4 from dual where not exists (select user_id,room_device_id,door_id,device_id from dm_users_devices where user_id=?1 AND  room_device_id = ?2 AND door_id = ?3 AND device_id = ?4)",nativeQuery = true)
    int createUserDeviceBySelect(String userId,String roomDeviceId,String doorId,String deviceId);

    @Modifying
    @Transactional
    @Query(value = "insert into dm_users_devices (user_id,room_device_id,door_id,device_id) select ?1,?2,?3,?4 from dual where not exists (select user_id,room_device_id,door_id,device_id from dm_users_devices where user_id=?1 AND  room_device_id = ?2 AND door_id = ?3 AND device_id = ?4)",nativeQuery = true)
    int createUserDevice(String userId,String roomDeviceId,String doorId,String deviceId);

    //根据用户id和设备id查询设备
    @Query(value = "SELECT COUNT(1) FROM dm_users_devices WHERE user_id = :userId AND room_device_id = :roomDeviceId ",nativeQuery = true)
    Integer findUserDeviceCount(@Param("userId") String userId,@Param("roomDeviceId") String roomDeviceId);
    //根据用户id和设备id查询设备数量
    @Query(value = "SELECT user_id,room_device_id,door_id,device_id FROM dm_users_devices WHERE user_id = :userId AND room_device_id = :roomDeviceId AND door_id = :doorId ",nativeQuery = true)
    DmRoomDevice findUserDevice(@Param("userId") String userId,@Param("roomDeviceId") String roomDeviceId,@Param("doorId") String doorId);
    //根据用户id和设备id删除设备
    @Modifying
    @Transactional
    @Query(value = "DELETE  FROM dm_users_devices WHERE user_id = :userId AND if(:roomDeviceId IS NOT NULL , room_device_id = :roomDeviceId,1=1 ) AND if(:doorId IS NOT NULL ,door_id = :doorId, 1=1 )  AND if(:deviceId IS NOT NULL ,device_id = :deviceId, 1=1 )",nativeQuery = true)
    int delUserDevice(@Param("userId") String userId,@Param("roomDeviceId") String roomDeviceId,@Param("doorId") String doorId,@Param("deviceId") String deviceId);

    //人脸识别用户
    @Query(value = "SELECT COUNT(1) FROM dm_user WHERE picture IS NOT NULL ",nativeQuery = true)
    Integer userPicture();

    //园区卡用户
    @Query(value = "SELECT COUNT(1) FROM dm_user u LEFT JOIN dm_card c ON u.id = c.user_number WHERE u.usertype IN('00','01','02') ",nativeQuery = true)
    Integer userCard();

    //临时卡用户
    @Query(value = "SELECT COUNT(1) FROM dm_user u LEFT JOIN dm_card c ON u.id = c.user_number WHERE u.usertype = '03' ",nativeQuery = true)
    Integer userCardLs();

    //本月新增人员数量
    @Query(value = "SELECT COUNT(1) FROM dm_user WHERE DATE_FORMAT(create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ",nativeQuery = true)
    Integer addUser();

    @Query(value = "SELECT u.id,u.`name`,d.dept_id as deptId ,d.`name` as deptName FROM dm_user u LEFT JOIN sys_dept d ON u.dept_id = d.dept_id where u.enabled = 1",nativeQuery = true)
    List<Map<String,Object>> findUser();

    @Query(value = "SELECT u.id,u.`name`,d.dept_id as deptId ,d.`name` as deptName FROM dm_user u LEFT JOIN sys_dept d ON u.dept_id = d.dept_id LEFT JOIN sys_user su on u.id = su.user_id where d.dept_id = :deptId and su.enabled= 1",nativeQuery = true)
    List<Map<String,Object>> findUserByDeptId(@Param("deptId") String deptId);

    @Query(value = "SELECT u.* FROM dm_user u WHERE user_number = :userNumber", nativeQuery = true)
    DmUser findByUserNumber(@Param("userNumber") String userNumber);

    @Query(value = "SELECT u.* FROM dm_user u WHERE user_number = :userNumber", nativeQuery = true)
    DmUser findByUserCardId(@Param("userNumber") String userNumber);

    @Query(value = "SELECT u.* FROM dm_user u WHERE id = :userNumber", nativeQuery = true)
    DmUser findByCardId(@Param("userNumber") String userNumber);

    @Query(value = "SELECT u.* FROM dm_user u WHERE contacts = :phone and name = :name and picture is not null", nativeQuery = true)
    DmUser asd(@Param("phone") String phone,@Param("name") String name);



}