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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.*;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.constant.CommonConstant;
import com.gci.boot.modules.basedata.entity.TbVehicle;
import com.gci.boot.modules.basedata.service.ITbVehicleService;
import com.gci.boot.modules.gis.entity.TbGisGpsStatistics;
import com.gci.boot.modules.gis.mapper.TbGisGpsStatisticsMapper;
import com.gci.boot.modules.gis.service.ITbGisGpsStatisticsService;
import com.gci.boot.modules.projects.service.IProjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 车辆gps相关数据统计 服务实现类
 * </p>
 *
 * @author 2464
 * @since 2023-10-31
 */
@Service
public class TbGisGpsStatisticsServiceImpl extends ServiceImpl<TbGisGpsStatisticsMapper, TbGisGpsStatistics> implements ITbGisGpsStatisticsService {

    @Autowired
    private ITbVehicleService vehicleService;

    @Autowired
    private IProjectsService projectsService;
    @Autowired
    private TbGisGpsStatisticsMapper gisGpsStatisticsMapper;

    @Override
    public void init(String time) {
        DateTime dateTime = new DateTime(time);
        DateTime beginOfMonth = DateUtil.beginOfMonth(dateTime);
        DateTime endOfMonth = DateUtil.endOfMonth(dateTime);
        DateTime toDay = new DateTime();
        if (endOfMonth.after(toDay)) {
            endOfMonth = DateUtil.endOfDay(DateUtil.offset(toDay,DateField.DAY_OF_MONTH,-1));
        }

        List<TbVehicle> list = vehicleService.list();
        List<TbGisGpsStatistics> gisGpsStatisticsList = gisGpsStatisticsMapper.initStatisticsCount(beginOfMonth,endOfMonth);
        for (TbVehicle tbVehicle : list) {
            List<TbGisGpsStatistics> collect = gisGpsStatisticsList.stream().filter(g -> g.getPlateNumber().equals(tbVehicle.getPlateNumber())).collect(Collectors.toList());
            if (ObjectUtil.isEmpty(collect)) {
                DateRange range = DateUtil.range(beginOfMonth, endOfMonth, DateField.DAY_OF_MONTH);
                while (range.hasNext()) {
                    DateTime next = range.next();
                    TbGisGpsStatistics tbGisGpsStatistics = collect.parallelStream().filter(g -> g.getStatisTime().equals(next.toString("yyyy-MM-dd"))).findAny().orElse(new TbGisGpsStatistics());
                    if (BeanUtil.isEmpty(tbGisGpsStatistics)) {
                        if (StrUtil.isNotBlank(tbVehicle.getProjectId())) {
                            tbGisGpsStatistics.setProjectId(tbVehicle.getProjectId());
                        }
                        tbGisGpsStatistics.setSupportEic(StrUtil.isBlank(tbVehicle.getVinCode()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportJt(StrUtil.isBlank(tbVehicle.getLocationTerminalId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportSafety(StrUtil.isBlank(tbVehicle.getActiveSafetyDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportVideo(StrUtil.isBlank(tbVehicle.getVideoDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportTerminal(CommonConstant.NO);
                        tbGisGpsStatistics.setVehicleType(tbVehicle.getVehicleType());
                        tbGisGpsStatistics.setDealer(tbVehicle.getDealer());
                        tbGisGpsStatistics.setPlateNumber(tbVehicle.getPlateNumber());
                        tbGisGpsStatistics.setStatisTime(next.toString("yyyy-MM-dd"));
                        tbGisGpsStatistics.setJtCount(0L);
                        tbGisGpsStatistics.setJtValidCount(0L);
                        tbGisGpsStatistics.setJtNotValidCount(0L);
                        tbGisGpsStatistics.setEicCount(0L);
                        tbGisGpsStatistics.setEicValidCount(0L);
                        tbGisGpsStatistics.setEicNotValidCount(0L);
                        tbGisGpsStatistics.setWorkCount(0L);
                        tbGisGpsStatistics.setWorkNotValidCount(0L);
                        tbGisGpsStatistics.setWorkValidCount(0L);
                        gisGpsStatisticsList.add(tbGisGpsStatistics);
                    } else {
                        if (StrUtil.isNotBlank(tbVehicle.getProjectId())) {
                            tbGisGpsStatistics.setProjectId(tbVehicle.getProjectId());
                        }
                        tbGisGpsStatistics.setSupportEic(StrUtil.isBlank(tbVehicle.getVinCode()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportJt(StrUtil.isBlank(tbVehicle.getLocationTerminalId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportSafety(StrUtil.isBlank(tbVehicle.getActiveSafetyDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportVideo(StrUtil.isBlank(tbVehicle.getVideoDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setVehicleType(tbVehicle.getVehicleType());
                        tbGisGpsStatistics.setDealer(tbVehicle.getDealer());
                    }
                }
            } else {
                DateRange range = DateUtil.range(beginOfMonth, endOfMonth, DateField.DAY_OF_MONTH);
                while (range.hasNext()) {
                    DateTime next = range.next();
                    TbGisGpsStatistics tbGisGpsStatistics = collect.parallelStream().filter(g -> g.getStatisTime().equals(next.toString("yyyy-MM-dd"))).findAny().orElse(new TbGisGpsStatistics());
                    if (BeanUtil.isEmpty(tbGisGpsStatistics)) {
                        if (StrUtil.isNotBlank(tbVehicle.getProjectId())) {
                            tbGisGpsStatistics.setProjectId(tbVehicle.getProjectId());
                        }
                        tbGisGpsStatistics.setSupportEic(StrUtil.isBlank(tbVehicle.getVinCode()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportJt(StrUtil.isBlank(tbVehicle.getLocationTerminalId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportSafety(StrUtil.isBlank(tbVehicle.getActiveSafetyDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportVideo(StrUtil.isBlank(tbVehicle.getVideoDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportTerminal(CommonConstant.NO);
                        tbGisGpsStatistics.setVehicleType(tbVehicle.getVehicleType());
                        tbGisGpsStatistics.setDealer(tbVehicle.getDealer());
                        tbGisGpsStatistics.setPlateNumber(tbVehicle.getPlateNumber());
                        tbGisGpsStatistics.setStatisTime(next.toString("yyyy-MM-dd"));
                        tbGisGpsStatistics.setJtCount(0L);
                        tbGisGpsStatistics.setJtValidCount(0L);
                        tbGisGpsStatistics.setJtNotValidCount(0L);
                        tbGisGpsStatistics.setEicCount(0L);
                        tbGisGpsStatistics.setEicValidCount(0L);
                        tbGisGpsStatistics.setEicNotValidCount(0L);
                        tbGisGpsStatistics.setWorkCount(0L);
                        tbGisGpsStatistics.setWorkNotValidCount(0L);
                        tbGisGpsStatistics.setWorkValidCount(0L);
                        gisGpsStatisticsList.add(tbGisGpsStatistics);
                    } else {
                        if (StrUtil.isNotBlank(tbVehicle.getProjectId())) {
                            tbGisGpsStatistics.setProjectId(tbVehicle.getProjectId());
                        }
                        tbGisGpsStatistics.setSupportEic(StrUtil.isBlank(tbVehicle.getVinCode()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportJt(StrUtil.isBlank(tbVehicle.getLocationTerminalId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportSafety(StrUtil.isBlank(tbVehicle.getActiveSafetyDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setSupportVideo(StrUtil.isBlank(tbVehicle.getVideoDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                        tbGisGpsStatistics.setVehicleType(tbVehicle.getVehicleType());
                        tbGisGpsStatistics.setDealer(tbVehicle.getDealer());
                    }
                }
            }
        }
        remove(new QueryWrapper<TbGisGpsStatistics>().between("statis_time",beginOfMonth.toString("yyyy-MM-dd"),endOfMonth.toString("yyyy-MM-dd")));
        saveBatch(gisGpsStatisticsList);

    }

    @Override
    public void gen() {
        DateTime dateTime = new DateTime();
        String time = dateTime.offset(DateField.DAY_OF_YEAR, -1).toString(DatePattern.NORM_DATE_PATTERN);
        insertData(time);
    }


    private void insertData(String time) {
        List<TbVehicle> list = vehicleService.list();
        List<TbGisGpsStatistics> gisGpsStatisticsList = gisGpsStatisticsMapper.StatisticsCount(time);
        for (TbVehicle tbVehicle : list) {
            TbGisGpsStatistics tbGisGpsStatistics = gisGpsStatisticsList.parallelStream().filter(g -> g.getPlateNumber().equals(tbVehicle.getPlateNumber())).findAny().orElse(new TbGisGpsStatistics());
            if (BeanUtil.isEmpty(tbGisGpsStatistics)) {
                if (StrUtil.isNotBlank(tbVehicle.getProjectId())) {
                    tbGisGpsStatistics.setProjectId(tbVehicle.getProjectId());
                }
                tbGisGpsStatistics.setSupportEic(StrUtil.isBlank(tbVehicle.getVinCode()) ? CommonConstant.NO : CommonConstant.YES);
                tbGisGpsStatistics.setSupportJt(StrUtil.isBlank(tbVehicle.getLocationTerminalId()) ? CommonConstant.NO : CommonConstant.YES);
                tbGisGpsStatistics.setSupportSafety(StrUtil.isBlank(tbVehicle.getActiveSafetyDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                tbGisGpsStatistics.setSupportVideo(StrUtil.isBlank(tbVehicle.getVideoDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                tbGisGpsStatistics.setSupportTerminal(CommonConstant.NO);
                tbGisGpsStatistics.setVehicleType(tbVehicle.getVehicleType());
                tbGisGpsStatistics.setDealer(tbVehicle.getDealer());
                tbGisGpsStatistics.setPlateNumber(tbVehicle.getPlateNumber());
                tbGisGpsStatistics.setStatisTime(time);
                tbGisGpsStatistics.setJtCount(0L);
                tbGisGpsStatistics.setJtValidCount(0L);
                tbGisGpsStatistics.setJtNotValidCount(0L);
                tbGisGpsStatistics.setEicCount(0L);
                tbGisGpsStatistics.setEicValidCount(0L);
                tbGisGpsStatistics.setEicNotValidCount(0L);
                tbGisGpsStatistics.setWorkCount(0L);
                tbGisGpsStatistics.setWorkNotValidCount(0L);
                tbGisGpsStatistics.setWorkValidCount(0L);
                gisGpsStatisticsList.add(tbGisGpsStatistics);
            } else {
                if (StrUtil.isNotBlank(tbVehicle.getProjectId())) {
                    tbGisGpsStatistics.setProjectId(tbVehicle.getProjectId());
                }
                tbGisGpsStatistics.setSupportEic(StrUtil.isBlank(tbVehicle.getVinCode()) ? CommonConstant.NO : CommonConstant.YES);
                tbGisGpsStatistics.setSupportJt(StrUtil.isBlank(tbVehicle.getLocationTerminalId()) ? CommonConstant.NO : CommonConstant.YES);
                tbGisGpsStatistics.setSupportSafety(StrUtil.isBlank(tbVehicle.getActiveSafetyDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                tbGisGpsStatistics.setSupportVideo(StrUtil.isBlank(tbVehicle.getVideoDeviceId()) ? CommonConstant.NO : CommonConstant.YES);
                tbGisGpsStatistics.setVehicleType(tbVehicle.getVehicleType());
                tbGisGpsStatistics.setDealer(tbVehicle.getDealer());
            }
        }
        remove(new QueryWrapper<TbGisGpsStatistics>().apply("statis_time = {0}", time));
        saveBatch(gisGpsStatisticsList);
    }
}
