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

import com.ningdatech.basic.util.NdDateUtils;
import com.ningdatech.carapi.scheduler.entity.VehicleOnlineStatusDay;
import com.ningdatech.carapi.scheduler.entity.VehicleOnlineStatusHour;
import com.ningdatech.carapi.scheduler.mapper.VehicleOnlineStatusDayMapper;
import com.ningdatech.carapi.scheduler.service.IVehicleOnlineStatusDayService;
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;
import java.util.Set;

/**
 * <p>
 * 统计车辆上线状态 按天 服务实现类
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-12-18
 */
@Service
@AllArgsConstructor
@Slf4j
public class VehicleOnlineStatusDayServiceImpl extends ServiceImpl<VehicleOnlineStatusDayMapper, VehicleOnlineStatusDay> implements IVehicleOnlineStatusDayService {

    private final VehicleOnlineStatusDayMapper mapper;

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

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

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

    @Override
    public Set<String> getOnlineCarPlatesInMonthTables() {
        String tableName = "nd_vehicle_online_status_day_" + NdDateUtils.format(new Date(),"yyyy_MM");
        String date = NdDateUtils.format(LocalDateTime.now(),NdDateUtils.DEFAULT_DATE_TIME_START_FORMAT);
        return mapper.selectOnlineCarPlatesInMonthTable(tableName,date);
    }

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

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