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

import me.zhengjie.modules.dm.dmPlateTraffic.domain.DmPlateTraffic;
import org.springframework.data.domain.Pageable;
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 juan
* @date 2021-11-23
**/
public interface DmPlateTrafficRepository extends JpaRepository<DmPlateTraffic, Long>, JpaSpecificationExecutor<DmPlateTraffic> {
    //已停车位
    @Query(value = " SELECT COUNT(1) FROM dm_plate_traffic WHERE come_go_out = '00' AND TO_DAYS(access_time) = TO_DAYS(NOW()) ",nativeQuery = true)
    Integer getYiPlate();

    //剩余车位
    @Query(value = " SELECT 500-(SELECT COUNT(1) FROM dm_plate_traffic WHERE come_go_out = '00' AND TO_DAYS(access_time) = TO_DAYS(NOW())) ",nativeQuery = true)
    Integer getShenPlate();

    //今日出入次数
    @Query(value = " SELECT COUNT(1) FROM dm_plate_traffic ",nativeQuery = true)
    Integer getCountPlate();

    @Query(value = "SELECT id,plate_number,mthtype,intime,outtime,ingatename,outgatename,ysje,sfje,TIMESTAMPDIFF(MINUTE ,intime,outtime) AS countTime FROM dm_plate_traffic plate WHERE plate.plate_number = (SELECT plate_number FROM dm_plate_number WHERE user_number = :userNumber) AND date_sub(curdate(), interval 7 day) <= plate.intime ",nativeQuery = true)
    List<Map<String, Object>> getParkingTrafficByUserNumber(@Param("userNumber") String userNumber, Pageable pageable);
}