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

import me.zhengjie.modules.dmAccessTraffic.domain.DmAccessTraffic;
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;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author zhirong
* @date 2021-11-22
**/
public interface DmAccessTrafficRepository extends JpaRepository<DmAccessTraffic, Long>, JpaSpecificationExecutor<DmAccessTraffic> {

    //一卡通 通行总数
    @Query(value = " SELECT COUNT(1) FROM ( SELECT COUNT(1) FROM dm_access_traffic WHERE DATE_FORMAT(access_time,'%Y-%m') =  DATE_FORMAT(CURDATE(),'%Y-%m')  GROUP BY user_number) as tmp  ",nativeQuery = true)
    Integer getCountAccess();

    //一卡通 入园
    @Query(value = " SELECT COUNT(1) FROM dm_access_traffic WHERE come_go_out = '00' ",nativeQuery = true)
    Integer getAccessGo();

    //一卡通 出园
    @Query(value = " SELECT COUNT(1) FROM dm_access_traffic WHERE come_go_out = '01' ",nativeQuery = true)
    Integer getAccessOut();

    //一卡通 人脸识别
    @Query(value = " SELECT COUNT(1) FROM dm_access_traffic WHERE card_number = 0 ",nativeQuery = true)
    Integer getAccessRl();

    //一卡通 园区卡
    @Query(value = " SELECT COUNT(1) FROM dm_access_traffic WHERE card_number != 0 ",nativeQuery = true)
    Integer getAccessCt();

    //一卡通 其他
    @Query(value = " SELECT COUNT(1) FROM dm_access_traffic WHERE passage_way = '其他' ",nativeQuery = true)
    Integer getAccessQt();

    /**
     * 运营今日人流量统计
     * @return
     */
    @Query(value = "SELECT COUNT(1) as card_number,DATE_FORMAT(access_time,'%H') as access_time " +
            "FROM dm_access_traffic WHERE TO_DAYS(access_time) = TO_DAYS(NOW()) " +
            "GROUP BY DATE_FORMAT(access_time,'%H') ",nativeQuery = true)
    List<Map<String,String>> YunUserRequest();

    @Query(value = "SELECT id, user_number, card_number, room_device, come_go_out, access_time, access_photo " +
            "FROM dm_access_traffic WHERE TO_DAYS(NOW()) - TO_DAYS(access_time) <= 1 ",nativeQuery = true)
    List<Map<String,Object>> accessTrf();

    /**
     * 根据通行时间查询数据
     * @return
     */
    @Query(value = "select u.user_number, d.code, t.access_time from dm_access_traffic as t left join dm_user as u on u.id = t.user_number left join dm_room_device as d on d.code = t.room_device where DATE_FORMAT(t.access_time,'%Y-%m-%d %H:%i:%s') between :start and :end and t.user_number is not null order by t.access_time desc",nativeQuery = true)
    List<Map<String,String>> queryByAccessTime(@Param("start") String start, @Param("end") String end);

    /**
     * 根据通行时间查询数据
     * @return
     */
    @Query(value = "select * from dm_access_traffic where user_number=:userNumber and room_device =:roomDevice and access_time = :accessTime",nativeQuery = true)
    List<Map<String,String>> queryByRoomDevice(@Param("userNumber") String userNumber, @Param("roomDevice") String roomDevice, @Param("accessTime") String accessTime);
}