package com.cpxt.biz.mapper;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cpxt.biz.domain.BizDriverClock;
import com.cpxt.biz.domain.StatCar;
import com.cpxt.biz.domain.StatDriver;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 司机统计Mapper接口
 * 
 * @author ruoyi
 * @date 2024-12-31
 */
@Mapper
public interface StatDriverMapper extends BaseMapper<StatDriver>
{

    @Insert("insert into stat_driver (stat_date, driver_id, driver_name) select #{statDate}, id, name from biz_driver where status = 1 ")
    void initStatDriver(String statDate);

    @Select("select min(odometer) minOdometer, max(odometer) maxOdometer from track_${carId} where car_id = #{carId} and gps_time between #{startTime} and #{endTime} ")
    Map getTodayOdometer(@Param("carId") String carId, @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("select * from stat_driver where driver_id = #{driverId} and stat_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 1 DAY")
    StatDriver selectTodayByDriverId(Long driverId);

    @Select("select count(id) orderCount from biz_order where (driver_id = #{driverId} or copilot_id = #{driverId}) and create_time BETWEEN CURDATE() AND CURDATE() + INTERVAL 1 DAY")
    Map getTodayOrderCount(Long driverId);

    @Select("select * from biz_driver_clock where driver_id = #{driverId} order by on_time desc limit 1 ")
    Map getTodayUsetime(Long driverId);

    @Select("select * from biz_driver_clock where driver_id = #{driverId} and ( on_time BETWEEN CURDATE() - INTERVAL 23 HOUR AND NOW() or off_time between CURDATE() and NOW() ) order by on_time asc ")
    List<BizDriverClock> selectDriverClockList(Long driverId);

    @Select("select t.driver_name from (SELECT driver_name,SUM(odometer) sumo FROM `stat_driver` where stat_date >= #{startTime} and stat_date <= #{endTime} GROUP BY driver_name ORDER BY sumo desc limit 5) t")
    List<String> selectOdometerTop5CarNo(@Param("startTime") String startTime,@Param("endTime") String endTime);

    @Select("select t.sumo from (SELECT driver_name,SUM(odometer) sumo FROM `stat_driver` where stat_date >= #{startTime} and stat_date <= #{endTime} GROUP BY driver_name ORDER BY sumo desc limit 5) t")
    List<Double> selectOdometerTop5Odometer(@Param("startTime") String startTime,@Param("endTime") String endTime);

    @Select("SELECT driver_name,SUM(odometer) as odometer,SUM(order_count) as order_count,SUM(online_time) as online_time FROM stat_driver WHERE driver_id = ${driverId}")
    Map selectSum(Long driverId);
}
