package com.project.zcustom.service.car.Impl;

import com.project.zcustom.domain.addional.*;
import com.project.zcustom.mapper.car.PlatCarMapper;
import com.project.zcustom.service.car.ILargePlatCarService;
import com.project.zcustom.service.logistics.ILargeHousingLandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class LargePlatCarServiceImpl implements ILargePlatCarService {

    @Autowired
    private PlatCarMapper platCarMapper;

    public LargeCar getResource(String appOrg){
        LargeCar largeCar = platCarMapper.getResourceNumber(appOrg);
        if (largeCar == null){
            LargeCar car = new LargeCar();
            car.setTotalCarNum(0L);
            car.setCarNumOne(0L);
            car.setCarNumTwo(0L);
            car.setCarNumThree(0L);
            car.setCarNumFour(0L);
            car.setCarNumFive(0L);
            car.setEnterpriseCarNum(0L);
            car.setHeadCarNum(0L);
            car.setNewPowerCarNum(0L);
            car.setOilCarNum(0L);
            car.setEstablishmentsCarNum(0L);
            car.setYearAddNum(0L);
            car.setYearReduceNum(0L);
            return car;
        }
        return largeCar;
    }

    public Map<String, Object> getCarDetail(String appOrg, Long flag, Long pageSize, Long page){
        Map<String, Object> map = new HashMap<>();
        map.put("list", platCarMapper.getCarDetail(appOrg, flag, pageSize, page * pageSize));
        map.put("total", platCarMapper.getCarDetailNum(appOrg, flag));
        return map;
    }

    public LargeCar getMonitor(String appOrg){
        LargeCar largeCar = platCarMapper.getMonitorNumber(appOrg);
        if (largeCar == null){
            LargeCar car = new LargeCar();
            car.setTerminalInstall(0L);
            car.setTerminalTotal(0L);
            car.setTerminalOnline(0L);
            car.setNoPidNum(0L);
            car.setNotArchivedNum(0L);
            car.setOverSpeedNum(0L);
            car.setHolidayNum(0L);
            return car;
        }
        return largeCar;
    }

    public Map<String, Object> getWarnDetail(String appOrg, String alertType, Long pageSize, Long page){
        Map<String, Object> map = new HashMap<>();
        map.put("list", platCarMapper.getWarnDetail(appOrg, alertType, pageSize, page * pageSize));
        map.put("total", platCarMapper.getWarnDetailNum(appOrg, alertType));
        return map;
    }

    public LargeCar getOperationManageTopHalf(String appOrg){
        LargeCar largeCar = platCarMapper.getOperationManageTopHalf(appOrg);
        if (largeCar == null){
            LargeCar car = new LargeCar();
            car.setPcdNum(0L);
            car.setAvgPcdNum(0L);
            car.setAvgMile(BigDecimal.valueOf(0));
            car.setMileCount(BigDecimal.valueOf(0));
            return car;
        }
        return largeCar;
    }

    public Map<String, Object> getOperationManageLastHalf(){
        Map<String, Object> map = new HashMap<>();
        List<LargeUnitRank> list1 = platCarMapper.getUnitRankTop();
        map.put("x1", list1.stream().map(LargeUnitRank::getTotalCarNum).collect(Collectors.toList()));
        map.put("y1", list1.stream().map(LargeUnitRank::getPcdNum).collect(Collectors.toList()));
        map.put("z1", list1.stream().map(LargeUnitRank::getAppName).collect(Collectors.toList()));
        List<LargeUnitRank> list2 = platCarMapper.getUnitRankLast();
        map.put("x2", list2.stream().map(LargeUnitRank::getTotalCarNum).collect(Collectors.toList()));
        map.put("y2", list2.stream().map(LargeUnitRank::getPcdNum).collect(Collectors.toList()));
        map.put("z2", list2.stream().map(LargeUnitRank::getAppName).collect(Collectors.toList()));
        map.put("list", platCarMapper.getCarRank());
        return map;
    }

    public List<Long> getSelfCarNum(){
        List<String> appOrg = new ArrayList<>(Arrays.asList("1A003", "1B001", "1C001", "1D001", "1E001", "1F001", "1K001", "1L001", "1H001", "1M001", "1J001"));
        List<LargeCar> carList = platCarMapper.getSelfCarNum(appOrg);
        List<Long> list = new ArrayList<>();
        for (String it : appOrg){
            for (LargeCar car : carList){
                if(car.getAppOrg().equals(it)){
                    if (car.getTotalCarNum() == null){
                        car.setTotalCarNum(0L);
                    }
                    list.add(car.getTotalCarNum());
                    break;
                }
            }
        }
        return list;
    }
}
