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

import jdk.internal.dynalink.linker.LinkerServices;
import me.zhengjie.ykt.domain.DmRoomDevice;
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;

/**
* @website https://el-admin.vip
* @author zhirong
* @date 2021-11-19
**/
public interface DmRoomDeviceRepository extends JpaRepository<DmRoomDevice, Long>, JpaSpecificationExecutor<DmRoomDevice> {
    /**
     *
     * @param code
     * @param buildAddress
     * @return
     */
    @Query(value = "SELECT  * FROM dm_room_device r " +
            "WHERE r.id  IN (SELECT device_id FROM dm_users_devices WHERE user_id = :code) AND if(:buildAddress != '',r.build_address = :buildAddress,1=1) AND if(:deviceType IS NOT NULL,r.device_type = :deviceType,1=1)",nativeQuery = true)
    List<DmRoomDevice> getDmRoomDeviceByUser(@Param("code") String code,@Param("buildAddress") String buildAddress,@Param("deviceType") Integer deviceType);

    /**
     *
     * @param code
     * @param doorId
     * @return
     */
    @Query(value = "SELECT r.* FROM dm_room_device r WHERE code=:code AND if(:doorId IS NOT NULL ,door_id = :doorId,1=1)",nativeQuery = true)
    List<DmRoomDevice> findByCode(@Param("code") String code,@Param("doorId") String doorId);

    /**
     *
     * @param code
     * @return
     */
    @Query(value = "SELECT r.* FROM dm_room_device r WHERE code=:code",nativeQuery = true)
    List<DmRoomDevice> findByCode(@Param("code") String code);

    /**
     *
     * @param device
     * @return
     */
    @Query(value = "SELECT r.* FROM dm_room_device r WHERE id=:device ",nativeQuery = true)
    DmRoomDevice findDeviceById(@Param("device")Long device);
    /**
     * 根据用户id和设备查询  查询 in (list) 的数据
     * @param userId  用户id
     * @return
     */
    @Query(value = "SELECT d.* FROM dm_room_device d inner join dm_users_devices u on d.code = u.room_device_id where u.user_id = :userId AND if(:deviceType IS NOT NULL,d.device_type = :deviceType,1=1 ) ",nativeQuery = true)
    List<DmRoomDevice> findUserDevice(@Param("userId") String userId,@Param("deviceType") Integer deviceType);
}