package com.cico.nest.lcdp.boot.core.qxsd.application.service.impl;

import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.ReportCarTypeFlowVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.TableStatsReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.StatisticalReportsService;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.result.XlwResultData;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.TunnelDetailMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TunnelDetailDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwTableStatsDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.feign.XlwClientFeign;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.VehicleType.*;
import static com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.VehicleType.other_car;

/**
 * @author zws
 * @date 2024/10/28
 */
@Service
public class StatisticalReportsServiceImpl implements StatisticalReportsService {

    @Resource
    private XlwClientFeign xlwClientFeign;
    @Resource
    private TunnelDetailMapper tunnelDetailMapper;

    @Override
    public List<ReportCarTypeFlowVO> carTypeFlow(TableStatsReqVO tableStatsReqVO) {
        XlwTableStatsDTO dto = new XlwTableStatsDTO();
        BeanUtils.copyProperties(tableStatsReqVO,dto);
        if(StringUtils.isEmpty(tableStatsReqVO.getXlwTunnelId())){
            TunnelDetailDO tunnelDetailDO = tunnelDetailMapper.selectById(tableStatsReqVO.getTunnelId());
            dto.setTunnelId(tunnelDetailDO.getXlwTunnelId());
        }else{
            dto.setTunnelId(tableStatsReqVO.getXlwTunnelId());
        }
        XlwResultData<List<Map<String,Object>>> resultData = xlwClientFeign.carFlowPageDay(dto);
        List<Map<String,Object>> data = resultData.getData();
        //危化品车辆数量
        int dangerousCount = 0;
        //轿车数量
        int sedanCount = 0;
        //客车数量
        int passengerCount = 0;
        //货车数量
        int truckCount = 0;
        //未知
        int otherCount = 0;
        if (data!=null) {
            for (Map<String, Object> map : data) {
                Map<String, Integer> dataMap = (Map<String, Integer>) map.get("dataMap");
                for (String key : dataMap.keySet()) {
                    Integer value = dataMap.get(key);
                    switch (key) {
                        case "轿车", "SUV/MPV", "小型轿车", "微型轿车","面包车" -> sedanCount = sedanCount + value;
                        case "货车", "小货车", "皮卡车" -> truckCount = truckCount + value;
                        case "大型客车", "中型客车" -> passengerCount = passengerCount + value;
                        default -> otherCount = otherCount + value;
                    }
                }
            }
        }
        int allCount = dangerousCount + sedanCount + passengerCount + truckCount + otherCount;
        return List.of(
                new ReportCarTypeFlowVO(hazardous_chemicals.desc,dangerousCount,allCount == 0 ? 0D : dangerousCount*1.0/allCount),
                new ReportCarTypeFlowVO(passenger.desc,passengerCount,allCount == 0 ? 0D : passengerCount*1.0/allCount),
                new ReportCarTypeFlowVO(sedan.desc,sedanCount,allCount == 0 ? 0D : sedanCount*1.0/allCount),
                new ReportCarTypeFlowVO(truck.desc,truckCount,allCount == 0 ? 0D : truckCount*1.0/allCount),
                new ReportCarTypeFlowVO(other_car.desc,otherCount,allCount == 0 ? 0D : otherCount*1.0/allCount),
                new ReportCarTypeFlowVO("全部",allCount,100.00)
        );
    }

    @Override
    public List<Map<String,Object>> carTypeFlowDay(TableStatsReqVO tableStatsReqVO) {
        XlwTableStatsDTO dto = new XlwTableStatsDTO();
        BeanUtils.copyProperties(tableStatsReqVO,dto);
        TunnelDetailDO tunnelDetailDO = tunnelDetailMapper.selectById(tableStatsReqVO.getTunnelId());
        dto.setTunnelId(tunnelDetailDO.getXlwTunnelId());
        XlwResultData<List<Map<String,Object>>> resultData = xlwClientFeign.carFlowPageDay(dto);
        List<Map<String,Object>> data = resultData.getData();
        if (data==null){
            return data;
        }
        for(Map<String,Object> map:data){
            Map<String,Integer> dataMap = (Map<String, Integer>) map.get("dataMap");
            //危化品车辆数量
            int dangerousCount = 0;
            //轿车数量
            int sedanCount = 0;
            //客车数量
            int passengerCount = 0;
            //货车数量
            int truckCount = 0;
            //未知
            int otherCount = 0;
            for(String key:dataMap.keySet()){
                Integer value = dataMap.get(key);
                switch (key){
                    case "轿车","SUV/MPV","SUV/MPV车型","小型轿车","微型轿车" -> sedanCount = sedanCount + value;
                    case "皮卡","微卡","重中型货车","货车","小货车","皮卡车" -> truckCount = truckCount + value;
                    case "面包车","大中型客车","大型客车","中型客车" -> passengerCount = passengerCount + value;
                    default -> otherCount = otherCount + value;
                }
            }
            int allCount = dangerousCount + sedanCount + passengerCount + truckCount + otherCount;
            map.put("dataMap",
                    List.of(
                            new ReportCarTypeFlowVO(hazardous_chemicals.desc,dangerousCount,allCount == 0 ? 0D : dangerousCount*1.0/allCount),
                            new ReportCarTypeFlowVO(passenger.desc,passengerCount,allCount == 0 ? 0D : passengerCount*1.0/allCount),
                            new ReportCarTypeFlowVO(sedan.desc,sedanCount,allCount == 0 ? 0D : sedanCount*1.0/allCount),
                            new ReportCarTypeFlowVO(truck.desc,truckCount,allCount == 0 ? 0D : truckCount*1.0/allCount),
                            new ReportCarTypeFlowVO(other_car.desc,otherCount,allCount == 0 ? 0D : otherCount*1.0/allCount),
                            new ReportCarTypeFlowVO("全部",allCount,100.00)
                    )
            );
        }
        return data;
    }

    @Override
    public List<Map<String, Object>> carTypeFlowHour(TableStatsReqVO tableStatsReqVO) {
        XlwTableStatsDTO dto = new XlwTableStatsDTO();
        BeanUtils.copyProperties(tableStatsReqVO,dto);
        TunnelDetailDO tunnelDetailDO = tunnelDetailMapper.selectById(tableStatsReqVO.getTunnelId());
        dto.setTunnelId(tunnelDetailDO.getXlwTunnelId());
        XlwResultData<List<Map<String,Object>>> resultData = xlwClientFeign.carFlowPageHour(dto);
        List<Map<String,Object>> data = resultData.getData();
        if (data==null){
            return new ArrayList<>();
        }
        for(Map<String,Object> map:data){
            Map<String,Integer> dataMap = (Map<String, Integer>) map.get("dataMap");
            //危化品车辆数量
            int dangerousCount = 0;
            //轿车数量
            int sedanCount = 0;
            //客车数量
            int passengerCount = 0;
            //货车数量
            int truckCount = 0;
            //未知
            int otherCount = 0;
            for(String key:dataMap.keySet()){
                Integer value = dataMap.get(key);
                switch (key){
                    case "轿车","SUV/MPV","SUV/MPV车型","小型轿车","微型轿车" -> sedanCount = sedanCount + value;
                    case "皮卡","微卡","重中型货车","货车","小货车","皮卡车" -> truckCount = truckCount + value;
                    case "面包车","大中型客车","大型客车","中型客车" -> passengerCount = passengerCount + value;
                    default -> otherCount = otherCount + value;
                }
            }
            int allCount = dangerousCount + sedanCount + passengerCount + truckCount + otherCount;
            map.put("dataMap",
                    List.of(
                            new ReportCarTypeFlowVO(hazardous_chemicals.desc,dangerousCount,allCount == 0 ? 0D : dangerousCount*1.0/allCount),
                            new ReportCarTypeFlowVO(passenger.desc,passengerCount,allCount == 0 ? 0D : passengerCount*1.0/allCount),
                            new ReportCarTypeFlowVO(sedan.desc,sedanCount,allCount == 0 ? 0D : sedanCount*1.0/allCount),
                            new ReportCarTypeFlowVO(truck.desc,truckCount,allCount == 0 ? 0D : truckCount*1.0/allCount),
                            new ReportCarTypeFlowVO(other_car.desc,otherCount,allCount == 0 ? 0D : otherCount*1.0/allCount),
                            new ReportCarTypeFlowVO("全部",allCount,100.00)
                    )
            );
        }
        return data;
    }

    @Override
    public List<Map<String, Object>> carTypeSpeedTrend(TableStatsReqVO tableStatsReqVO) {
        XlwTableStatsDTO dto = new XlwTableStatsDTO();
        BeanUtils.copyProperties(tableStatsReqVO,dto);
        TunnelDetailDO tunnelDetailDO = tunnelDetailMapper.selectById(tableStatsReqVO.getTunnelId());
        dto.setTunnelId(tunnelDetailDO.getXlwTunnelId());
        XlwResultData<List<Map<String,Object>>> resultData = xlwClientFeign.speedTrend(dto);
        return resultData.getData();
    }
}
