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

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.modules.statis.service.UpkeepStatisService;
import com.gci.boot.modules.statis.service.ViolationStatisService;
import com.gci.boot.modules.statis.vo.ColumnItemVO;
import com.gci.boot.modules.statis.vo.StatisParamVO;
import com.gci.boot.modules.statis.vo.UpkeepStatisVO;
import com.gci.boot.modules.statis.vo.ViolationRecordStatisVO;
import com.gci.boot.modules.system.service.ISysDictService;
import com.gci.boot.modules.system.vo.DictModel;
import com.gci.boot.modules.vehicles.entity.TbUpkeepOffer;
import com.gci.boot.modules.vehicles.mapper.TbUpkeepBillMapper;
import com.gci.boot.modules.vehicles.mapper.TbViolationRecordMapper;
import com.gci.boot.modules.vehicles.service.ITbUpkeepBillService;
import com.gci.boot.modules.vehicles.service.ITbUpkeepOfferService;
import com.gci.boot.modules.vehicles.vo.RepairDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ViolationStatisServiceImpl implements ViolationStatisService {

    @Autowired
    private TbViolationRecordMapper violationRecordMapper;
    @Autowired
    private ISysDictService sysDictService;

    @Override
    public Page getViolationStatisPage(Page 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");
        }

        List<ViolationRecordStatisVO> list = new ArrayList<>();
        list =  violationRecordMapper.findStatisViolationByDayMonthYear(page,vo);

        //补充动态列---违章分类
        List<DictModel> modelList =  sysDictService.queryDictItemsByCode("violation_behavior");
        modelList.sort(Comparator.comparing(DictModel::getValue));
        for(ViolationRecordStatisVO violationRecordStatisVO : list){
            List<String> violationActs = Arrays.asList(violationRecordStatisVO.getViolationActIds().split(","));
            List<ColumnItemVO> colList = new ArrayList<>();
            for(DictModel dictModel : modelList){
                ColumnItemVO itemVO = new ColumnItemVO();
                itemVO.setColName(dictModel.getText());
                long count = violationActs.stream().filter(item->dictModel.getValue().equals(item)).count();
                itemVO.setCount((int) count);
                colList.add(itemVO);
            }
            violationRecordStatisVO.setColList(colList);
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public List<List<Object>> getAllViolationStatis(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<ViolationRecordStatisVO> list = new ArrayList<>();
        list =  violationRecordMapper.findStatisViolationAll(vo);

        List<List<Object>> datas = new ArrayList<>();
        //车辆种类字典
        List<DictModel> vehicleTypeList =  sysDictService.queryDictItemsByCode("vehicle_type");
        Map<String, String> vehicleTypeMap = vehicleTypeList.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText));
        //补充动态列---报修分类
        List<DictModel> modelList =  sysDictService.queryDictItemsByCode("violation_behavior");
        modelList.sort(Comparator.comparing(DictModel::getValue));
        for(ViolationRecordStatisVO violationRecordStatisVO : list){
            List<Object> data = new ArrayList<>();
            data.add(violationRecordStatisVO.getProjectName());
            data.add(violationRecordStatisVO.getStatisTime());
            data.add(vehicleTypeMap.getOrDefault(violationRecordStatisVO.getVehicleType(),violationRecordStatisVO.getVehicleType() + " 无匹配字典"));
            data.add(violationRecordStatisVO.getViolationTimes());
            data.add(violationRecordStatisVO.getViolationAmount());
            data.add(violationRecordStatisVO.getAvgAmount());
            //补充动态列---违章分类
            List<String> violationActs = Arrays.asList(violationRecordStatisVO.getViolationActIds().split(","));
            for(DictModel dictModel : modelList){
                long count = violationActs.stream().filter(item->dictModel.getValue().equals(item)).count();
                data.add(count);
            }
            datas.add(data);
        }
        return datas;
    }
}
