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


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.util.Date8Util;
import com.tofly.es.CarGpsUpload;
import com.tofly.gpsboot.common.Constant;
import com.tofly.es.dto.EsCarGpsUploadQueryDto;
import com.tofly.es.dto.EsQueryDto;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.mapper.es.CarGpsUploadRepository;
import com.tofly.gpsboot.service.es.ICarGpsUploadService;
import com.tofly.gpsboot.util.EsQueryUtil;
import com.tofly.gpsboot.util.EsSaveInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

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

/**
 * 车辆Gps信息服务接口
 *
 * @author BZ-DJJ
 */
@Service
@Slf4j
public class CarGpsUploadServiceImpl implements ICarGpsUploadService {
    @Autowired
    private EsSaveInfoUtil esSaveInfoUtil;
    @Autowired
    private CarGpsUploadRepository carGpsUploadRepository;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public Boolean carGpsSave(CarGpsUpload esCarGpsUpload) {
        esSaveInfoUtil.checkInsertEsInfo(carGpsUploadRepository.getIndex());
        if (esCarGpsUpload.getUploadTime() == null) {
            esCarGpsUpload.setUploadTime(Date8Util.getCurrentDate());
        }

        if (esCarGpsUpload.getGpsTime() == null) {
            esCarGpsUpload.setGpsTime(Date8Util.getCurrentDate());
        }
        Long carId = esSaveInfoUtil.getCarId(esCarGpsUpload.getGpsId());
        esCarGpsUpload.setCarId(carId);
        carGpsUploadRepository.save(esCarGpsUpload);
        return true;
    }

    @Override
    public Boolean updateGpsCarInfo(CarGpsUpload carGpsUpload) {
        return carGpsUploadRepository.update(carGpsUpload);
    }

    @Override
    public CarGpsUpload getByIdGpsCarInfo(String id) {
        return carGpsUploadRepository.findById(id).get();
    }

    @Override
    public Boolean removeByIdGpsCarInfo(String id) {
        carGpsUploadRepository.deleteById(id);
        return true;
    }

    @Override
    public Page<List<CarGpsUpload>> getPageGpsCarInfo(Page page, EsQueryDto dto) {
        CarGpsUpload gpsUpload = new CarGpsUpload();
        QueryBuilder[] queryBuilders = EsQueryUtil.putQUery(page, dto);
        return carGpsUploadRepository.getPage(gpsUpload, page, queryBuilders);
    }

    @Override
    public Page<List<CarGpsUpload>> getAllPage(Page page, EsQueryDto dto) {
        CarGpsUpload gpsUpload = new CarGpsUpload();
        QueryBuilder[] queryBuilders = EsQueryUtil.putQUery(page, dto);
        return carGpsUploadRepository.getAllPage(gpsUpload, page,
                esSaveInfoUtil.DyMonth(carGpsUploadRepository.getIndex()), elasticsearchTemplate, queryBuilders);
    }

    @Override
    public List<CarGpsUpload> getAll(EsQueryDto dto) {
        CarGpsUpload gpsUpload = new CarGpsUpload();
        Page page = new Page();
        QueryBuilder[] queryBuilders = EsQueryUtil.putQUery(page, dto);
        return carGpsUploadRepository.getAll(gpsUpload, page,
                esSaveInfoUtil.DyMonth(carGpsUploadRepository.getIndex()), elasticsearchTemplate, queryBuilders);
    }

    @Override
    public Map<String, Object> getCarLocation(EsCarGpsUploadQueryDto dto) {
        CarGpsUpload gpsUpload = new CarGpsUpload();
        if (null == dto.getCarId()) {
            throw new BusinessException(Constant.IS_CAR_ID_NULL_CODE, Constant.IS_CAR_ID_NULL);
        }
        Page page = new Page();
        page.setSize(new Long(10000));
        QueryBuilder[] queryBuilders = EsQueryUtil.CarGpsUploadQuery(page, dto);
        List<CarGpsUpload> result = carGpsUploadRepository.getAll(gpsUpload, page,
                esSaveInfoUtil.DyMonth(carGpsUploadRepository.getIndex()), elasticsearchTemplate, queryBuilders);
        Map<String, Object> resultMap = new HashMap<>(12);
        int end = result.size() - 1;
        resultMap.put("lat", result.size() == 0 ? null : result.get(0).getLocationLatitude());
        resultMap.put("lng", result.size() == 0 ? null : result.get(0).getLocationLongitude());
        resultMap.put("onTime", result.size() == 0 ? null : result.get(end).getGpsTime());
        resultMap.put("offTime", result.size() == 0 ? null : result.get(0).getGpsTime());
        return resultMap;
    }

    @Override
    public List<CarGpsUpload> getGpsCarList(EsCarGpsUploadQueryDto dto) {
        CarGpsUpload gpsUpload = new CarGpsUpload();
        Page page = new Page();
        page.setSize(new Long(10000));
        QueryBuilder[] queryBuilders = EsQueryUtil.CarGpsUploadQuery(page, dto);
        return carGpsUploadRepository.getAll(gpsUpload, page,
                esSaveInfoUtil.DyMonth(carGpsUploadRepository.getIndex()), elasticsearchTemplate, queryBuilders);
    }

    @Override
    public Page<List<CarGpsUpload>> getGpsCarListPage(Page page, EsCarGpsUploadQueryDto dto) {
        CarGpsUpload gpsUpload = new CarGpsUpload();
        QueryBuilder[] queryBuilders = EsQueryUtil.CarGpsUploadQuery(page, dto);
        return carGpsUploadRepository.getAllPage(gpsUpload, page,
                esSaveInfoUtil.DyMonth(carGpsUploadRepository.getIndex()), elasticsearchTemplate, queryBuilders);
    }

}
