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

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

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.modules.basedata.entity.TbVehicle;
import com.gci.boot.modules.basedata.service.ITbVehicleService;
import com.gci.boot.modules.projects.entity.ProjectVehicleBindLog;
import com.gci.boot.modules.projects.service.IProjectVehicleBindLogService;
import com.gci.boot.modules.projects.service.IProjectsService;
import com.gci.boot.modules.statis.service.AnnualInspectionInfoStatisService;
import com.gci.boot.modules.statis.vo.AnnualInspectionInfoStatisVo;
import com.gci.boot.modules.statis.vo.StatisParamVO;
import com.gci.boot.modules.system.service.ISysDictService;
import com.gci.boot.modules.vehicles.mapper.TbAnnualInspectionInfoMapper;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class AnnualInspectionInfoStatisServiceImpl implements AnnualInspectionInfoStatisService {
    @Autowired
    private TbAnnualInspectionInfoMapper annualInspectionInfoMapper;
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private IProjectsService projectsService;
    @Autowired
    private ITbVehicleService vehicleService;
    @Autowired
    private IProjectVehicleBindLogService bindLogService;

    @Override
    public IPage<AnnualInspectionInfoStatisVo> page(Page<AnnualInspectionInfoStatisVo> page, StatisParamVO vo) {
        if("1".equals(vo.getStatisType())){
            vo.setStatisTime("YYYY-MM-DD");
        }else if("2".equals(vo.getStatisType())){
            vo.setStatisTime("YYYY-MM");
        }else if("3".equals(vo.getStatisType())){
            vo.setStatisTime("YYYY");
        }else{
            vo.setStatisTime("YYYY-MM-DD");
        }

        IPage<AnnualInspectionInfoStatisVo> pageList = annualInspectionInfoMapper.findStatisAnnualInspectionInfoByDayMonthYear(page,vo);
        extracted(vo, pageList.getRecords());
        return pageList;
    }

    private void extracted(StatisParamVO vo, List<AnnualInspectionInfoStatisVo> records) {
        if (records.isEmpty()) {
            return;
        }
        List<TbVehicle> list = vehicleService.list();
        Map<String, Map<String, List<TbVehicle>>> stringMapMap = CollStreamUtil.groupBy2Key(list, TbVehicle::getProjectId, TbVehicle::getVehicleType);
        List<ProjectVehicleBindLog> bindLogList = bindLogService.list(new LambdaQueryWrapper<ProjectVehicleBindLog>().isNotNull(ProjectVehicleBindLog::getProjectId));
        for (AnnualInspectionInfoStatisVo record : records) {
            //年检期间车数 不是年检次数 没有做一年的限制可能会有重复的数据
            int countPeriod = 0;
            //项目下种类车数 当前车辆数量 减去统计期间绑定车辆 加上统计期间解绑车辆
            int count = 0;
            if (StrUtil.isNotBlank(record.getPlateNumbers())) {
                countPeriod = StrUtil.split(record.getPlateNumbers(),StrUtil.COMMA).size();
            }

            List<TbVehicle> theTypeVehicleList = new ArrayList<>();

            if (ObjectUtil.isEmpty(stringMapMap.get(record.getProjectId()))) {
                count = 0;
            } else {
                Map<String, List<TbVehicle>> stringListMap = stringMapMap.get(record.getProjectId());
                if (ObjectUtil.isEmpty(stringListMap.get(record.getVehicleType()))) {
                    count = 0;
                } else {
                    theTypeVehicleList = stringListMap.get(record.getVehicleType());
                    count = theTypeVehicleList.size();
                }
            }

            List<ProjectVehicleBindLog> collect = bindLogList.stream().filter(i ->
                    DateUtil.format(i.getCreateTime(), vo.getStatisTime()).equals(record.getStatisTime())
                            && i.getProjectId().equals(record.getProjectId())).collect(Collectors.toList());
            for (ProjectVehicleBindLog projectVehicleBindLog : collect) {
                TbVehicle vehicle = theTypeVehicleList.stream().filter(t -> t.getPlateNumber().equals(projectVehicleBindLog.getPlateNumber())).findFirst().orElse(null);
                if (vehicle == null) {
                    continue;
                }
                if (projectVehicleBindLog.getBindType() == 1) {
                    count = count - 1;
                } else if (projectVehicleBindLog.getBindType() == 2){
                    count = count + 1;
                }
            }

            //车辆总数为0年检车数大于0 完成率为100%，车辆总数为0年检车数为0 完成率为0%
            if (count <= 0 && countPeriod >0) {
                record.setCompletionRate("100%");
            } else if (count <= 0) {
                record.setCompletionRate("0%");
            } else {
                double div = NumberUtil.div(countPeriod, count);
                if (div > 1) {
                    div = 1;
                }
                record.setCompletionRate(NumberUtil.formatPercent(div,2));
            }
        }
    }

    @Override
    public List<AnnualInspectionInfoStatisVo> getAll(StatisParamVO vo) {
        if ("1".equals(vo.getStatisType())) {
            vo.setStatisTime("YYYY-MM-DD");
        } else if ("2".equals(vo.getStatisType())) {
            vo.setStatisTime("YYYY-MM");
        } else if ("3".equals(vo.getStatisType())) {
            vo.setStatisTime("YYYY");
        } else {
            vo.setStatisTime("YYYY-MM-DD");
        }

        List<AnnualInspectionInfoStatisVo> all =
            annualInspectionInfoMapper.findStatisAnnualInspectionInfoAll(vo, new ArrayList<>());
        extracted(vo, all);
        return all;
    }
}
