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

import me.zhengjie.modules.dm.dmVisitorsUser.domain.DmVisitorsUser;
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-19
**/
public interface DmVisitorsUserRepository extends JpaRepository<DmVisitorsUser, Long>, JpaSpecificationExecutor<DmVisitorsUser> {
    @Query(value = "SELECT visitor_id as id,visitor_name,visitor_phone,u.`name` as `user`,d.`name` as dept,sj.`name` as job,start_time,end_time," +
            "TIMESTAMPDIFF(HOUR, start_time, end_time) as `hour`,ui.`name` as request_user,b.create_time FROM dm_visitors_user a " +
            "LEFT JOIN dm_visitors_apply b ON a.apply_id=b.id " +
            "LEFT JOIN dm_user u ON b.user_number = u.id " +
            "LEFT JOIN dm_user ui ON b.request_user = ui.id " +
            "LEFT JOIN sys_dept d ON u.dept_id = d.dept_id " +
            "LEFT JOIN dm_users_jobs j ON u.id=j.user_id " +
            "LEFT JOIN sys_job sj ON j.job_id=sj.job_id ",nativeQuery = true)
    List<Map<String,Object>> selectVisitors();

    @Query(value = "SELECT * FROM dm_visitors_user where apply_id = :id ",nativeQuery = true)
    List<DmVisitorsUser> getVisXq(@Param("id") Long id);

    /**
     * 根据车牌号和当天的时间获取访客列表
     */
    @Query(value = "select * from dm_visitors_user where month(start_time) = :month and YEAR(start_time) = :year and day(start_time) = :day and plate_number = :plateNumber",nativeQuery = true)
    List<DmVisitorsUser> getDmVisitorsUserByPlateNumber(@Param("plateNumber") String plateNumber,@Param("year") Integer year, @Param("month") Integer month, @Param("day") Integer day);

    /**
     * 根据车牌号和当天的时间获取访客列表
     */
    @Query(value = "select * from dm_visitors_user where apply_id = :visitorsApplyId",nativeQuery = true)
    List<DmVisitorsUser> findByApplyId(@Param("visitorsApplyId") Long visitorsApplyId);
}