package com.gci.boot.modules.dataview.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.gci.boot.common.util.DateUtils;
import com.gci.boot.modules.dataview.mapper.VehiclesCenterMapper;
import com.gci.boot.modules.dataview.service.VehiclesCenterService;
import com.gci.boot.modules.dataview.vo.VehicleCenterVo;
import com.gci.boot.modules.system.service.ISysDictService;
import com.gci.boot.modules.system.vo.DictModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lyz
 * @date 2023/9/27 11:48
 */
@Slf4j
@Service
public class VehiclesCenterServiceImpl implements VehiclesCenterService {


    private static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    @Autowired
    private VehiclesCenterMapper vehiclesCenterMapper;

    @Autowired
    private ISysDictService sysDictService;

    @Override
    public Map violationStatistics(String startTimeStr, String endTimeStr, Integer groupType) throws ParseException {
        HashMap resultMap = new HashMap(3);
        LocalDateTime end = LocalDateTime.now();
        LocalDateTime start = end.minusMonths(5);
        Date endTime = DateUtils.convertLocalDateTimeToDate(end);
        Date startTime = DateUtils.convertLocalDateTimeToDate(start);
        if(ObjectUtil.isNotEmpty(startTimeStr)){
            startTime = DateUtils.parseDate(startTimeStr,TIME_FORMAT);
        }
        if(ObjectUtil.isNotEmpty(endTimeStr)){
            endTime = DateUtils.parseDate(endTimeStr,TIME_FORMAT);
        }
        List<VehicleCenterVo>  byMonthList =  vehiclesCenterMapper.violationStatisticsByMonth(DateUtils.formatTime(startTime),DateUtils.formatTime(endTime));
        List<VehicleCenterVo>  byProjectList =  vehiclesCenterMapper.violationStatisticsByProject(startTime,endTime);
        List<VehicleCenterVo>  byTypeDbList =  vehiclesCenterMapper.violationStatisticsByVehicleType(startTime,endTime);
        List<VehicleCenterVo>  byTypeList = coverVehicleType(byTypeDbList);
        resultMap.put("byMonth",byMonthList);
        resultMap.put("byProject",byProjectList);
        resultMap.put("byType",byTypeList);
        return resultMap;
    }

    @Override
    public Map insuranceStatistics(String startTimeStr, String endTimeStr, Integer groupType) throws ParseException {
        HashMap resultMap = new HashMap(3);
        LocalDateTime end = LocalDateTime.now();
        LocalDateTime start = end.minusMonths(5);
        Date endTime = DateUtils.convertLocalDateTimeToDate(end);
        Date startTime = DateUtils.convertLocalDateTimeToDate(start);
        if(ObjectUtil.isNotEmpty(startTimeStr)){
            startTime = DateUtils.parseDate(startTimeStr,TIME_FORMAT);
        }
        if(ObjectUtil.isNotEmpty(endTimeStr)){
            endTime = DateUtils.parseDate(endTimeStr,TIME_FORMAT);
        }
        List<VehicleCenterVo>  byMonthList =  vehiclesCenterMapper.insuranceStatisticsByMonth(DateUtils.formatTime(startTime),DateUtils.formatTime(endTime));
        List<VehicleCenterVo>  byProjectList =  vehiclesCenterMapper.insuranceStatisticsByProject(startTime,endTime);
        List<VehicleCenterVo>  byTypeDbList =  vehiclesCenterMapper.insuranceStatisticsByVehicleType(startTime,endTime);
        List<VehicleCenterVo>  byTypeList = coverVehicleType(byTypeDbList);
        resultMap.put("byMonth",byMonthList);
        resultMap.put("byProject",byProjectList);
        resultMap.put("byType",byTypeList);
        return resultMap;
    }

    @Override
    public Map accidentStatistics(String startTimeStr, String endTimeStr, Integer groupType) throws ParseException {
        HashMap resultMap = new HashMap(3);
        LocalDateTime end = LocalDateTime.now();
        LocalDateTime start = end.minusMonths(5);
        Date endTime = DateUtils.convertLocalDateTimeToDate(end);
        Date startTime = DateUtils.convertLocalDateTimeToDate(start);
        if(ObjectUtil.isNotEmpty(startTimeStr)){
            startTime = DateUtils.parseDate(startTimeStr,TIME_FORMAT);
        }
        if(ObjectUtil.isNotEmpty(endTimeStr)){
            endTime = DateUtils.parseDate(endTimeStr,TIME_FORMAT);
        }
        List<VehicleCenterVo>  byMonthList =  vehiclesCenterMapper.accidentStatisticsByMonth(DateUtils.formatTime(startTime),DateUtils.formatTime(endTime));
        List<VehicleCenterVo>  byProjectList =  vehiclesCenterMapper.accidentStatisticsByProject(startTime,endTime);
        List<VehicleCenterVo>  byTypeDbList =  vehiclesCenterMapper.accidentStatisticsByVehicleType(startTime,endTime);
        List<VehicleCenterVo>  byTypeList = coverVehicleType(byTypeDbList);
        resultMap.put("byMonth",byMonthList);
        resultMap.put("byProject",byProjectList);
        resultMap.put("byType",byTypeList);
        return resultMap;
    }

    @Override
    public Map inspectionStatistics(String startTimeStr, String endTimeStr, Integer groupType) throws ParseException {
        HashMap resultMap = new HashMap(3);
        LocalDateTime end = LocalDateTime.now();
        LocalDateTime start = end.minusMonths(5);
        Date endTime = DateUtils.convertLocalDateTimeToDate(end);
        Date startTime = DateUtils.convertLocalDateTimeToDate(start);
        if(ObjectUtil.isNotEmpty(startTimeStr)){
            startTime = DateUtils.parseDate(startTimeStr,TIME_FORMAT);
        }
        if(ObjectUtil.isNotEmpty(endTimeStr)){
            endTime = DateUtils.parseDate(endTimeStr,TIME_FORMAT);
        }
        List<VehicleCenterVo>  byMonthList =  vehiclesCenterMapper.accidentStatisticsByMonth(DateUtils.formatTime(startTime),DateUtils.formatTime(endTime));
        List<VehicleCenterVo>  byProjectList =  vehiclesCenterMapper.accidentStatisticsByProject(startTime,endTime);
        List<VehicleCenterVo>  byTypeDbList =  vehiclesCenterMapper.accidentStatisticsByVehicleType(startTime,endTime);
        List<VehicleCenterVo>  byTypeList = coverVehicleType(byTypeDbList);
        resultMap.put("byMonth",byMonthList);
        resultMap.put("byProject",byProjectList);
        resultMap.put("byType",byTypeList);
        return resultMap;
    }

    @Override
    public Map repairStatistics(String startTimeStr, String endTimeStr) throws ParseException {
        HashMap resultMap = new HashMap(3);
        LocalDateTime end = LocalDateTime.now();
        LocalDateTime start = end.minusMonths(5);
        Date endTime = DateUtils.convertLocalDateTimeToDate(end);
        Date startTime = DateUtils.convertLocalDateTimeToDate(start);
        if(ObjectUtil.isNotEmpty(startTimeStr)){
            startTime = DateUtils.parseDate(startTimeStr,"yyyy-MM-dd HH:mm:ss");
        }
        if(ObjectUtil.isNotEmpty(endTimeStr)){
            endTime = DateUtils.parseDate(endTimeStr,"yyyy-MM-dd HH:mm:ss");
        }
        List<VehicleCenterVo>  byMonthList =  vehiclesCenterMapper.repairStatisticsByMonth(DateUtils.formatTime(startTime),DateUtils.formatTime(endTime));
        List<VehicleCenterVo>  byProjectList =  vehiclesCenterMapper.repairStatisticsByProject(startTime,endTime);
        List<VehicleCenterVo>  byTypeDbList =  vehiclesCenterMapper.repairStatisticsByVehicleType(startTime,endTime);
        List<VehicleCenterVo>  byTypeList = coverVehicleType(byTypeDbList);
        resultMap.put("byMonth",byMonthList);
        resultMap.put("byProject",byProjectList);
        resultMap.put("byType",byTypeList);
        return resultMap;
    }

    @Override
    public Map upkeepStatistics(String startTimeStr, String endTimeStr) throws ParseException {
        HashMap resultMap = new HashMap(3);
        LocalDateTime end = LocalDateTime.now();
        LocalDateTime start = end.minusMonths(5);
        Date endTime = DateUtils.convertLocalDateTimeToDate(end);
        Date startTime = DateUtils.convertLocalDateTimeToDate(start);
        if(ObjectUtil.isNotEmpty(startTimeStr)){
            startTime = DateUtils.parseDate(startTimeStr,"yyyy-MM-dd HH:mm:ss");
        }
        if(ObjectUtil.isNotEmpty(endTimeStr)){
            endTime = DateUtils.parseDate(endTimeStr,"yyyy-MM-dd HH:mm:ss");
        }
        List<VehicleCenterVo>  byMonthList =  vehiclesCenterMapper.upkeepStatisticsByMonth(DateUtils.formatTime(startTime),DateUtils.formatTime(endTime));
        List<VehicleCenterVo>  byProjectList =  vehiclesCenterMapper.upkeepStatisticsByProject(startTime,endTime);
        List<VehicleCenterVo>  byTypeDbList =  vehiclesCenterMapper.upkeepStatisticsByVehicleType(startTime,endTime);
        List<VehicleCenterVo>  byTypeList = coverVehicleType(byTypeDbList);
        resultMap.put("byMonth",byMonthList);
        resultMap.put("byProject",byProjectList);
        resultMap.put("byType",byTypeList);
        return resultMap;
    }


    private List<VehicleCenterVo> coverVehicleType(List<VehicleCenterVo>  byTypeList){
        List<DictModel> modelList = sysDictService.queryDictItemsByCode("vehicle_type");
        List<VehicleCenterVo> resultList = modelList.stream().map(a -> {
            VehicleCenterVo vo = new VehicleCenterVo();
            vo.setDimension(a.getText());
            Long getCount = byTypeList.stream().filter(db -> {
                return db.getDimension().equals(a.getValue());
            }).map(db -> {
                return db.getCount();
            }).findFirst().orElse(0L);
            vo.setCount(getCount);
            return vo;
        }).collect(Collectors.toList());
        return resultList;
    }
}
