package com.tofly.gpsboot.custom.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.gpsboot.custom.mapper.CustomCarMochaitomMapper;
import com.tofly.gpsboot.custom.service.CustomCarMochaitomService;
import com.tofly.gpsboot.service.car.CarMochaitomService;
import com.tofly.pojo.car.CarMochaitom;
import com.tofly.pojo.car.cusom.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
@Slf4j
public class CustomCarMochaitomServiceImpl extends ServiceImpl<CustomCarMochaitomMapper, CustomCarMochaitom> implements CustomCarMochaitomService {

    private CustomCarMochaitomMapper customCarMochaitomMapper;
    private CarMochaitomService carMochaitomService;

    @Override
    public Page<Map> pageByCarNumber(Page page, String number) {
        return page.setRecords(baseMapper.pageByCarNumber(page, number));
    }

    @Override
    public List<CarMochaitomCount> pageCount(CarMochaitomSearch carMochaitomSearch) throws ParseException {
        List<Long> list = new ArrayList<>();
        CarMochaitomSql carMochaitomSql = new CarMochaitomSql();
        if (StringUtil.isNotEmpty(carMochaitomSearch.getCarList())) {
            String[] carId = carMochaitomSearch.getCarList().split(",");
            for (String c : carId) {
                list.add(Long.parseLong(c));
            }
            carMochaitomSql.setCarIds(list);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        carMochaitomSql.setBeginTime(simpleDateFormat.parse(carMochaitomSearch.getBeginTime()));
        carMochaitomSql.setEndTime(simpleDateFormat.parse(carMochaitomSearch.getEndTime()));
        /*List<CarMochaitom> mochaitoms = carMochaitomService.list(Wrappers.<CarMochaitom>lambdaQuery()
                .in(list.size() > 0, CarMochaitom::getCarId, list).le(StringUtil.isNotEmpty(carMochaitomSearch.getEndTime()), CarMochaitom::getUseTime, carMochaitomSearch.getEndTime()));*/
        List<CarMochaitom> mochaitoms = customCarMochaitomMapper.getMaxMonth(carMochaitomSql);
        List<CarMochaitomCount> pageCount = customCarMochaitomMapper.pageCount(carMochaitomSql);
        pageCount.forEach(k -> {
            mochaitoms.forEach(v -> {
                if (k.getCarId().equals(v.getCarId())) {
                    k.setMileageTotal(v.getCarMileage());
                }
            });
            if (k.getOilUse() != null && k.getMileage() != null) {
                k.setFuelConsumption(k.getOilUse() * 100 / k.getMileage());
            }
            Field[] fields = k.getClass().getDeclaredFields();
            for (Field f : fields) {
                String name = f.getName();
                if (f.getGenericType().toString().equals("class java.lang.Double")) {
                    try {
                        Method d = k.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
                        Method s = k.getClass().getMethod("set" + name.substring(0, 1).toUpperCase() + name.substring(1), f.getType());
                        if (d.invoke(k) != null) {
                            Double b = Double.parseDouble(d.invoke(k).toString());
                            s.invoke(k, new BigDecimal(b).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        }
                    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return pageCount;
    }

    @Override
    public List<CarMochaitom> getList(CarMochaitomList carMochaitomList) throws ParseException {
        /*String beginTime = "";
        String endTime = "";
        if (StringUtil.isNotEmpty(date)) {
            beginTime = date + "-1";
            endTime = date + "-12";
        }*/
        /*CarMochaitomSearch carMochaitomSearch = new CarMochaitomSearch();
        carMochaitomSearch.setBeginTime(beginTime);
        carMochaitomSearch.setEndTime(endTime);
        carMochaitomSearch.setCid(carId);*/
        /*List<CarMochaitom> list = carMochaitomService.list(Wrappers.<CarMochaitom>lambdaQuery()
                .eq(CarMochaitom::getCarId, carId).like(StringUtil.isNotEmpty(date),CarMochaitom::getUseTime,date));
        Map<Integer,CarMochaitom> map = new HashMap<>();
        list.forEach(k->{
            Integer i = Integer.parseInt(k.getUseTime().substring(5));
            map.put(i,k);
        });
        List<CarMochaitom> list1 = new ArrayList<>();
        map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(k->list1.add(k.getValue()));
        return list1;*/
        CarMochaitomSql carMochaitomSql = new CarMochaitomSql();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        if (StringUtil.isNotEmpty(carMochaitomList.getBeginTime())) {
            carMochaitomSql.setBeginTime(simpleDateFormat.parse(carMochaitomList.getBeginTime()));
        }
        if (StringUtil.isNotEmpty(carMochaitomList.getEndTime())) {
            carMochaitomSql.setEndTime(simpleDateFormat.parse(carMochaitomList.getEndTime()));
        }
        carMochaitomSql.setCarId(carMochaitomList.getCarId());
        return customCarMochaitomMapper.getList(carMochaitomSql);
    }
}
