/*
*  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 com.usky.meeting.repository;

import com.usky.meeting.domain.MeetingRoom;
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;
import java.util.Set;

/**
* @website https://el-admin.vip
* @author shiguangji
* @date 2021-03-12
**/
public interface MeetingRoomRepository extends JpaRepository<MeetingRoom, Long>, JpaSpecificationExecutor<MeetingRoom> {

    /**
     * 会议室使用情况
     * @return
     */
//    @Query(value = "select" +
//            " count(a.room_id) as total," +
//            " sum(if(a.roomStatus=0,1,0)) as idle," +
//            " sum(if(a.roomStatus=1,1,0)) as in_use " +
//            " from ( " +
//                " select r.room_id,case when ifnull(t.total,0) = 0 then 0 else 1 end as roomStatus" +
//                " from dm_meeting_room as r " +
//                " left join (" +
//                    " select room_id,count(*) as total " +
//                    " from dm_meeting " +
//                    " where 1=1 " +
//                    " and end_date >= NOW() and is_cancel = 0 " +
//                    " group by room_id" +
//                " ) as t on t.room_id = r.room_id " +
//            " ) as a",nativeQuery = true)
    @Query(value = "select" +
            " count(a.room_id) as total," +
            " sum(if(a.room_status=0,1,0)) as idle," +
            " sum(if(a.room_status=1,1,0)) as in_use " +
            " from meeting_room as a where a.tenant_id=:tenantId" ,nativeQuery = true)
    Map<String,Object> roomSummary(@Param("tenantId") Integer tenantId);

//    //查询所有参会人和签到人数
//    @Query(value = "select count(m.user_id) as attendeeTotal,sum(if(m.is_sign=1,1,0)) as attendeeInUse from dm_meeting_attendee as m" ,nativeQuery = true)
//    Map<String,Object> attendeeSummary();
//
//    /**
//     * 添加会议审批人
//     * @param meetingRoomId
//     * @param userId
//     */
//
//
//    @Modifying
//    @Query(value = "delete from dm_meeting_approve_user where meeting_id = :meetingRoomId",nativeQuery = true)
//    void delRoomApproveUser(@Param("meetingRoomId") Long meetingRoomId);
//
///*    @Query(value = "select count(1) as meetSum from dm_meeting_approve_user as d  where meeting_id = :meetingRoomId",nativeQuery = true)
//    Map<String,Object> findRoom(@Param("meetingRoomId") Long meetingRoomId);*/
//    @Query(value = "select r.room_id as roomId," +
//            "r.room_name as roomName," +
//            "r.floor_id as floorId," +
//            "r.description as description," +
//            "r.storage_id as storageId," +
//            "r.img_path as imgPath," +
//            "r.capacity as capacity," +
//            "r.`position` as `position`," +
//            "r.maintainer as maintainer," +
//            "r.contacts as contacts," +
//            "r.remark as remark," +
//            "r.status as status," +
//            "r.room_status as roomStatus," +
//            "r.reserve_status as reserveStatus," +
//            "r.environmental as environmental," +
//            "r.last_use_time as lastUseTime," +
//            "r.is_approve as isApprove " +
//            "from dm_meeting_room as r where if(:floorId IS NOT NULL,r.floor_id = :floorId,1=1)" ,nativeQuery = true)
//    List<Map<String,String>> getAllMeetingRoom(@Param("floorId") Long floorId);

    /**
     * 根据会议室id更改会议室状态为 1-使用中
     */
    @Transactional
    @Modifying
    @Query(value = "update meeting_room set room_status = 1 where room_id in ?1  ",nativeQuery = true)
    void updateMeetingRoomStatusOnUse(Set<Long> ids);

    /**
     * 根据会议室id更改会议室状态为 0-未使用
     */
    @Transactional
    @Modifying
    @Query(value = "update meeting_room set room_status = 0 where room_id in ?1  ",nativeQuery = true)
    void updateMeetingRoomStatusFreeUse(Set<Long> ids);

    @Query(value = "select * from meeting_room where floor_id = :floorId",nativeQuery = true)
    List<MeetingRoom> getMeetingRoomList(@Param("floorId") Long floorId);

    @Query(value = "select id from sys_tenant where domain = :domain",nativeQuery = true)
    Integer getTenantIdByDomain(@Param("domain") String domain);
//
//    /**
//     * 会议室列表(运营)
//     */
//    @Query(value = "select m.room_id as roomId,m.room_name as roomName,if(m.room_status=0,'空闲','在用') as roomStatus,if(m.room_status=0,0,(select count(ma.id) as should_number from dm_meeting_attendee as ma left join dm_meeting as m on ma.meeting_id = m.meeting_id left join dm_meeting_room as mr on m.room_id = mr.room_id where (m.start_date <=curtime() and m.end_date >= curtime()) and mr.room_id = roomId)) as shouldNumber,if(m.room_status=0,0,(select sum(if(ma.is_sign=1,1,0)) as should_number from dm_meeting_attendee as ma left join dm_meeting as m on ma.meeting_id = m.meeting_id left join dm_meeting_room as mr on m.room_id = mr.room_id where (m.start_date <=curtime() and m.end_date >= curtime()) and mr.room_id = roomId)) as realNumber from dm_meeting_room as m" ,nativeQuery = true)
//    List<Map<String,Object>> getMeetingRoomListYY();
//
//    /**
//     * 会议室列表(运营)
//     */
//    @Query(value = "select m.room_id as roomId,m.room_name as roomName,if(m.room_status=0,'空闲','在用') as roomStatus,if(m.room_status=0,'',(select m.meeting_name as meetingName from  dm_meeting as m left join dm_meeting_room as mr on m.room_id = mr.room_id where (m.start_date <=curtime() and m.end_date >= curtime()) and mr.room_id = roomId)) as meeting,if(m.room_status=0,'',(select m.start_date as startDate from  dm_meeting as m left join dm_meeting_room as mr on m.room_id = mr.room_id where (m.start_date <=curtime() and m.end_date >= curtime()) and mr.room_id = roomId)) as startDate,if(m.room_status=0,'',(select m.end_date as endDate from  dm_meeting as m left join dm_meeting_room as mr on m.room_id = mr.room_id where (m.start_date <=curtime() and m.end_date >= curtime()) and mr.room_id = roomId)) as endDate,if(m.room_status=0,'',(select u.`name` from dm_user as u left join dm_meeting as m on u.id = m.initiator_id left join dm_meeting_room as mr on m.room_id = mr.room_id where (m.start_date <=curtime() and m.end_date >= curtime()) and mr.room_id = roomId)) as initiator,if(m.room_status=0,0,(select count(ma.id) as should_number from dm_meeting_attendee as ma left join dm_meeting as m on ma.meeting_id = m.meeting_id left join dm_meeting_room as mr on m.room_id = mr.room_id where (m.start_date <=curtime() and m.end_date >= curtime()) and mr.room_id = roomId)) as shouldNumber,if(m.room_status=0,0,(select sum(if(ma.is_sign=1,1,0)) as should_number from dm_meeting_attendee as ma left join dm_meeting as m on ma.meeting_id = m.meeting_id left join dm_meeting_room as mr on m.room_id = mr.room_id where (m.start_date <=curtime() and m.end_date >= curtime()) and mr.room_id = roomId)) as realNumber from dm_meeting_room as m where m.room_id = :roomId" ,nativeQuery = true)
//    Map<String,Object> getMeetingRoomListDetailYY(@Param("roomId")Long roomId);
}