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

import me.zhengjie.modules.dm.dmVisitorsApply.domain.DmVisitorsApply;
import me.zhengjie.modules.dm.dmVisitorsApply.service.dto.DmVisXq;
import me.zhengjie.modules.dm.dmVisitorsApply.service.dto.DmVisitorsDto;
import org.springframework.data.domain.Page;
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.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;

/**
* @website https://el-admin.vip
* @author juan
* @date 2021-11-19
**/
public interface DmVisitorsApplyRepository extends JpaRepository<DmVisitorsApply, Long>, JpaSpecificationExecutor<DmVisitorsApply> {
    @Query(value = " SELECT a.id as id,u.`name` as user_number,d.`name` as dept_id,administration,start_time," +
            "end_time,count(b.apply_id) as for_visiting,ui.`name` as request_user,a.create_time " +
            "FROM dm_visitors_apply a " +
            "LEFT JOIN dm_visitors_user b ON a.id = b.apply_id " +
            "LEFT JOIN dm_user u ON a.user_number = u.id " +
            "LEFT JOIN dm_user ui ON a.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 " +
            "WHERE if(:requestUser != '',ui.`name` LIKE %:requestUser%,1=1) OR if(:startTime != '',start_time >= :startTime,1=1) OR if(:endTime != '',end_time <= :endTime,1=1) " +
            "GROUP BY id ",nativeQuery = true)
    List<DmVisitorsApply> selectVisApply(@Param("requestUser") String requestUser,@Param("startTime") String startTime,@Param("endTime") String endTime);

    /**
     * 访客记录 app
     * @param
     * @return
     */
    @Query(nativeQuery =true,value = "SELECT `id`, `user_number`, a.`dept_id`, `start_time`, `end_time`, `administration`, `vis_number`, `for_visiting`, " +
            "`visitor_name`,qr_code, `visitor_phone`, `plate_number` FROM dm_visitors_apply a " +
            "LEFT JOIN dm_visitors_user b ON b.apply_id = a.id WHERE id = :id ")
    List<DmVisXq> getVisitors (@Param("id") Long id);

    @Query(value = "select * FROM (SELECT id,start_time,for_visiting,approve_state,user_number FROM dm_visitors_apply WHERE type = '00' AND user_number = ?1 OR request_user = ?1 ORDER BY start_time DESC) a ",
            countQuery = "SELECT count(*) FROM (SELECT id,start_time,for_visiting,approve_state FROM dm_visitors_apply WHERE type = '00' AND user_number = ?1 OR request_user = ?1) a ",
            nativeQuery = true)
    List<Map<String,String>> getVisApp(String userNumber,Pageable pageable);

    /**
     * 查询访客信息
     * @param userNumber
     * @param pageable
     * @return
     */
    @Query(value = "select * FROM (SELECT apply.id,apply.start_time,apply.for_visiting,apply.approve_state,user.visitor_name FROM dm_visitors_apply as apply left join dm_visitors_user user on apply.id = user.apply_id  WHERE type = '00' AND user_number = ?1 OR request_user = ?1 ORDER BY apply.id DESC) a ",
            countQuery = "SELECT count(*) FROM (SELECT pply.id,apply.start_time,apply.for_visiting,apply.approve_state FROM dm_visitors_apply as apply left join dm_visitors_user user on apply.id = user.apply_id WHERE type = '00' AND user_number = ?1 OR request_user = ?1) a ",
            nativeQuery = true)
    List<Map<String, String>> getVisitantApp(String userNumber, Pageable pageable);

    @Query(value = "select * FROM (SELECT va.id,va.start_time,va.for_visiting,va.approve_state FROM dm_visitors_apply as va LEFT JOIN dm_visitors_user vu ON va.id = vu.apply_id WHERE va.type = '00' AND vu.visitor_phone = ?1 ORDER BY va.start_time DESC) a",
            countQuery = "SELECT count(*) FROM (SELECT va.id,va.start_time,va.for_visiting,va.approve_state FROM dm_visitors_apply as va LEFT JOIN dm_visitors_user vu ON va.id = vu.apply_id WHERE va.type = '00' AND vu.visitor_phone = ?1) a",
            nativeQuery = true)
    List<Map<String,String>> getVisitorVisitList(String phone,Pageable pageable);

    @Transactional
    @Modifying
    @Query(value = "update dm_visitors_apply set  apply_status = :status where id in (:list)",nativeQuery = true)
    int updateDmVisitorsApplyStatus(@Param("list") List<String> list,@Param("status") Integer status);

    @Query(value = "select * from dm_visitors_apply where task_id = ?1",nativeQuery = true)
    DmVisitorsApply findDmVisitorsApplyByTaskId(String taskId);
}