package com.aidex.common.gps.server.impl;

import com.aidex.common.gps.common.GeneratorGspApi;
import com.aidex.common.gps.common.GpsApi;
import com.aidex.common.gps.common.GpsRequest;
import com.aidex.common.gps.domain.LocationEntity;
import com.aidex.common.gps.server.IGpsService;
import com.aidex.system.domain.SysShip;
import com.aidex.system.service.SysShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GpsService implements IGpsService {

    @Autowired
    private GeneratorGspApi generatorGspApi;
    @Autowired
    private GpsRequest<LocationEntity> gpsRequest;
    @Autowired
    private SysShipService sysShipService;


    public String getLocationUrl(String shipId){
        SysShip sysShip = sysShipService.get(shipId);
        return generatorGspApi.GenerationMapUrl(GeneratorGspApi.RquestMethod.follow,sysShip.getGpsDeviceNum(), GeneratorGspApi.GpsMapType.QQ);
    }

    public LocationEntity getLocation(String shipId){
        SysShip sysShip = sysShipService.get(shipId);
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.findLocationByBatch);
        params.put("macid",sysShip.getGpsDeviceNum());
        params.put("mapType", GeneratorGspApi.GpsMapType.QQ);
        LocationEntity locationEntitie = gpsRequest.post(params);
        return locationEntitie;
    }

    public List<LocationEntity> getLocationBatch(){
        List<SysShip> sysShips = sysShipService.findList(new SysShip());
        String macIds = sysShips.stream().map(SysShip::getGpsDeviceNum).collect(Collectors.joining(","));
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.findLocationByBatch);
        params.put("macids",macIds);
        params.put("mapType", GeneratorGspApi.GpsMapType.QQ);
        List<LocationEntity> locationEntities = gpsRequest.postArray(params);
         return locationEntities;
    }
}
