package com.ningdatech.carapi.scheduler.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.util.NdDateUtils;
import com.ningdatech.basic.util.StrPool;
import com.ningdatech.carapi.scheduler.entity.VehicleOnlineStatusHour;
import com.ningdatech.carapi.scheduler.mapper.VehicleOnlineStatusHourMapper;
import com.ningdatech.carapi.scheduler.service.IVehicleOnlineStatusHourService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 统计车辆上线状态 按小时 服务实现类
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-11-29
 */
@Service
@Slf4j
@AllArgsConstructor
public class VehicleOnlineStatusHourServiceImpl extends ServiceImpl<VehicleOnlineStatusHourMapper, VehicleOnlineStatusHour> implements IVehicleOnlineStatusHourService {

    private final VehicleOnlineStatusHourMapper mapper;

    @Override
    public VehicleOnlineStatusHour getOneByReqInMonthTable(VehicleOnlineStatusHour statusHour) {
        String tableName = "nd_vehicle_online_status_hour_" + NdDateUtils.format(new Date(),"yyyy_MM");
        if(mapper.countTable(tableName) == 0){
            mapper.createTable(tableName);
        }
        return mapper.selectOneByTable(tableName,statusHour);
    }

    @Override
    public List<VehicleOnlineStatusHour> listByReqInMonthTable(VehicleOnlineStatusHour statusHour) {
        String tableName = "nd_vehicle_online_status_hour_" + NdDateUtils.format(new Date(),"yyyy_MM");
        if(mapper.countTable(tableName) == 0){
            mapper.createTable(tableName);
        }
        return mapper.listByTable(tableName,statusHour);
    }

    @Override
    public Boolean saveByReqInMonthTable(VehicleOnlineStatusHour statusHour) {
        String tableName = "nd_vehicle_online_status_hour_" + NdDateUtils.format(new Date(),"yyyy_MM");
        if(mapper.countTable(tableName) == 0){
            mapper.createTable(tableName);
        }
        return mapper.saveByReqInMonthTable(tableName,statusHour);
    }

    @Override
    public Boolean updateByReqInMonthTable(VehicleOnlineStatusHour oldValue) {
        String tableName = "nd_vehicle_online_status_hour_" + NdDateUtils.format(new Date(),"yyyy_MM");
        if(mapper.countTable(tableName) == 0){
            mapper.createTable(tableName);
        }
        return mapper.updateByReqInMonthTable(tableName,oldValue);
    }

    @Override
    public Boolean batchSave(List<VehicleOnlineStatusHour> batchOnlineStatusHourToAdd) {
        String tableName = "nd_vehicle_online_status_hour_" + NdDateUtils.format(new Date(),"yyyy_MM");
        if(mapper.countTable(tableName) == 0){
            mapper.createTable(tableName);
        }
        return mapper.batchSave(tableName,batchOnlineStatusHourToAdd);
    }
}
