package com.cpxt.biz.service.impl;

import java.util.*;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cpxt.biz.domain.StatDriver;
import com.cpxt.biz.mapper.StatDriverMapper;
import com.cpxt.biz.service.IStatDriverService;
import com.cpxt.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 司机统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-31
 */
@Service
public class StatDriverServiceImpl implements IStatDriverService
{
    @Autowired
    private StatDriverMapper statDriverMapper;

    /**
     * 查询司机统计
     * 
     * @param id 司机统计主键
     * @return 司机统计
     */
    @Override
    public StatDriver selectStatDriverById(Long id)
    {
        return statDriverMapper.selectById(id);
    }

    /**
     * 查询司机统计列表
     * 
     * @param statDriver 司机统计
     * @return 司机统计
     */
    @Override
    public List<StatDriver> selectStatDriverList(StatDriver statDriver)
    {
        LambdaQueryWrapper<StatDriver> queryWrapper = new LambdaQueryWrapper<>();
        return statDriverMapper.selectList(queryWrapper);
    }

    /**
     * 新增司机统计
     * 
     * @param statDriver 司机统计
     * @return 结果
     */
    @Override
    public int insertStatDriver(StatDriver statDriver)
    {
        return statDriverMapper.insert(statDriver);
    }

    /**
     * 修改司机统计
     * 
     * @param statDriver 司机统计
     * @return 结果
     */
    @Override
    public int updateStatDriver(StatDriver statDriver)
    {
        return statDriverMapper.updateById(statDriver);
    }

    /**
     * 批量删除司机统计
     * 
     * @param ids 需要删除的司机统计主键
     * @return 结果
     */
    @Override
    public int deleteStatDriverByIds(Long[] ids)
    {
        return statDriverMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除司机统计信息
     * 
     * @param id 司机统计主键
     * @return 结果
     */
    @Override
    public int deleteStatDriverById(Long id)
    {
        return statDriverMapper.deleteById(id);
    }

    @Override
    public Map<String,Map<String,Object>> statics(StatDriver statDriver) {
        String statDate = DateUtil.format(new Date(), DateUtils.YYYY_MM_DD);
        LambdaQueryWrapper<StatDriver> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StatDriver::getDriverId,statDriver.getDriverId());
        queryWrapper.eq(StatDriver::getStatDate,statDate);
        StatDriver driver = statDriverMapper.selectOne(queryWrapper);
        Map<String,Object> today = new HashMap<>();
        today.put("driverName",driver.getDriverName());
        today.put("odometer",driver.getOdometer());
        today.put("orderCount",driver.getOrderCount());
        today.put("onlineTime",driver.getOnlineTime());
        Map<String,Map<String,Object>> result = new HashMap<>();
        Map map = statDriverMapper.selectSum(statDriver.getDriverId());
        Map<String,Object> sum = new HashMap<>();
        today.put("driverName",map.get("driver_name"));
        today.put("odometer",map.get("odometer"));
        today.put("orderCount",map.get("order_count"));
        today.put("onlineTime",map.get("online_time"));
        result.put("today",today);
        result.put("sum",sum);
        return result;
    }
}
