package com.xinxincaravan.caravan.vehicle.biz;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.wxiaoqi.security.common.exception.BaseException;
import com.xinxincaravan.caravan.vehicle.constant.ResCode.ResCode;
import com.xinxincaravan.caravan.vehicle.entity.Vehicle;
import com.xinxincaravan.caravan.vehicle.entity.VehicleDepartureLog;
import com.xinxincaravan.caravan.vehicle.mapper.VehicleDepartureLogMapper;
import com.xinxincaravan.caravan.vehicle.mapper.VehicleMapper;
import com.xinxincaravan.caravan.vehicle.vo.VehicleDepartureLogVo;
import com.xinxincaravan.caravan.vehicle.vo.VehicleDepartureStatisticDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class VehicleDepartureService {

    @Autowired
    VehicleDepartureLogMapper vehicleDepartureLogMapper;

    @Autowired
    VehicleMapper vehicleMapper;

    public PageInfo<VehicleDepartureLogVo> page(String numberPlate,String time, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return new PageInfo<>(vehicleDepartureLogMapper.selectVoAll(numberPlate,time));
    }

    public PageInfo<VehicleDepartureLogVo> pageNotAllData(String numberPlate,String time, Integer page, Integer limit, List<Integer> companyList) {
        PageHelper.startPage(page, limit);
        if (companyList == null || companyList.size() == 0) {
            companyList = Arrays.asList(-1);
        }
        return new PageInfo<>(vehicleDepartureLogMapper.selectVoAllNotAllData(numberPlate,time, companyList));
    }

    public PageInfo<VehicleDepartureLogVo> findByVehicle(String vehicleId, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return new PageInfo<>(vehicleDepartureLogMapper.selectByVehicleId(vehicleId));
    }

    public VehicleDepartureStatisticDataVo statistic(String numberPlate) {
        Vehicle vehicle = vehicleMapper.selectByNumberPlate(numberPlate);
        if (vehicle == null) {
            throw new BaseException(ResCode.VEHICLE_DEPARTURE_VEHICLE_UNEXIST.getDesc(),
                    ResCode.VEHICLE_DEPARTURE_VEHICLE_UNEXIST.getCode());
        }

        VehicleDepartureStatisticDataVo statisticData = new VehicleDepartureStatisticDataVo();
        // 出行次数
        VehicleDepartureLog departureLog = new VehicleDepartureLog();
        departureLog.setVehicleId(vehicle.getId());
        statisticData.setDepartureCount(vehicleDepartureLogMapper.selectCount(departureLog));
        // 出行天数
        statisticData.setDepartureDay(vehicleDepartureLogMapper.selectDayByVehicleId(vehicle.getId()));
        // 出行公里数
        statisticData.setDepartureMileage(vehicleDepartureLogMapper.selectMileageByVehicleId(vehicle.getId()));
        return statisticData;
    }
}
