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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.modules.statis.service.AccidentInfoStatisService;
import com.gci.boot.modules.statis.vo.AccidentInfoStatisVo;
import com.gci.boot.modules.statis.vo.AnnualInspectionInfoStatisVo;
import com.gci.boot.modules.statis.vo.ColumnItemVO;
import com.gci.boot.modules.statis.vo.StatisParamVO;
import com.gci.boot.modules.system.service.ISysDictService;
import com.gci.boot.modules.system.vo.DictModel;
import com.gci.boot.modules.vehicles.entity.TbAccidentInfo;
import com.gci.boot.modules.vehicles.mapper.TbAccidentInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class AccidentInfoStatisServiceImpl implements AccidentInfoStatisService {

    @Autowired
    private TbAccidentInfoMapper accidentInfoMapper;
    @Autowired
    private ISysDictService sysDictService;

    @Override
    public IPage<AccidentInfoStatisVo> page(Page<AccidentInfoStatisVo> 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<AccidentInfoStatisVo> list = accidentInfoMapper.findStatisAccidentInfoByDayMonthYear(page,vo);
        if (!list.getRecords().isEmpty()) {
            List<DictModel> accidentForm = sysDictService.queryDictItemsByCode("accident_form");
            for (AccidentInfoStatisVo record : list.getRecords()) {
                List<ColumnItemVO> columnItemVOS = new ArrayList<>();
                List<TbAccidentInfo> tbAccidentInfos = accidentInfoMapper.selectBatchIds(StrUtil.split(record.getIds(),","));
                for (DictModel dictModel : accidentForm) {
                    ColumnItemVO columnItemVO = new ColumnItemVO();
                    columnItemVO.setColName(dictModel.getText());
                    long count = tbAccidentInfos.stream().filter(item -> dictModel.getValue().equals(item.getAccidentForm())).count();
                    columnItemVO.setCount(Convert.toInt(count));
                    columnItemVOS.add(columnItemVO);
                }
                record.setColList(columnItemVOS);
            }
        }
        return list;
    }

    @Override
    public List<List<Object>> 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<List<Object>> datas = new ArrayList<>();
        List<AccidentInfoStatisVo> list = accidentInfoMapper.findStatisAccidentInfoAll(vo);

        //车辆种类字典
        List<DictModel> vehicleTypeList =  sysDictService.queryDictItemsByCode("vehicle_type");
        Map<String, String> vehicleTypeMap = vehicleTypeList.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText));
        List<DictModel> accidentForm = sysDictService.queryDictItemsByCode("accident_form");
        for (AccidentInfoStatisVo record : list) {
            List<Object> data = new ArrayList<>();
            data.add(record.getProjectName());
            data.add(record.getStatisTime());
            data.add(vehicleTypeMap.getOrDefault(record.getVehicleType(),record.getVehicleType()+"无匹配字典"));
            data.add(record.getAccidentTimes());

            List<TbAccidentInfo> tbAccidentInfos = accidentInfoMapper.selectBatchIds(StrUtil.split(record.getIds(),","));
            for (DictModel dictModel : accidentForm) {
                long count = tbAccidentInfos.stream().filter(item -> dictModel.getValue().equals(item.getAccidentForm())).count();
                data.add(count);
            }
            datas.add(data);
        }
        return datas;
    }
}
