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

import com.aidex.common.app.domain.vo.SysWharfVO;
import com.aidex.common.app.server.IAppService;
import com.aidex.common.exception.SysException;
import com.aidex.common.gps.common.GeneratorGspApi;
import com.aidex.common.gps.domain.LocationEntity;
import com.aidex.common.gps.server.IGpsService;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.dist.CalcDist;
import com.aidex.system.domain.SysNotice;
import com.aidex.system.domain.SysShip;
import com.aidex.system.domain.SysWharf;
import com.aidex.system.service.SysNoticeService;
import com.aidex.system.service.SysShipService;
import com.aidex.system.service.SysWharfService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class AppService implements IAppService {

    @Autowired
    private SysNoticeService sysNoticeService;
    @Autowired
    private SysWharfService sysWharfService;
    @Autowired
    private IGpsService iGpsService;


    public List<SysNotice> getCarouselList(){

        List<SysNotice> carouselList = sysNoticeService.findList(new SysNotice(){{setCarousel("1");}});
        return carouselList;
    }

    public List<SysNotice> getNoticeList(){
        List<SysNotice> noticeList = sysNoticeService.findList(new SysNotice(){{setCarousel("0");setNoticeType("1");}});
        return noticeList;
    }

    public SysNotice findCarouselAndNoticeDetails(String id){
        return sysNoticeService.get(id);
    }

    public List<SysWharf> findSysWharfList(){
        return sysWharfService.findList(new SysWharf());
    }

    public List<LocationEntity> findAllLocation(){
        return iGpsService.getLocationBatch();
    }

    public List<SysWharfVO> findNearestStation(String lat, String lon){
        if(StringUtils.isEmpty(lat) || StringUtils.isEmpty(lon))
            throw new SysException(4441,"请告诉我你在哪");
        List<SysWharf> listWharf = sysWharfService.findList(new SysWharf());
        List<SysWharfVO> vos = new ArrayList<>();
        for (SysWharf sysWharf : listWharf) {
            SysWharfVO wharfVO = new SysWharfVO();
            BeanUtils.copyProperties(sysWharf,wharfVO);
            wharfVO.setDistance(CalcDist.calculateDistance(Double.valueOf(lat),Double.valueOf(lon),
                    Double.valueOf(sysWharf.getLat()),Double.valueOf(sysWharf.getLng())));
            vos.add(wharfVO);
        }
        return  vos.stream().sorted(Comparator.comparing(SysWharfVO::getDistance))
                .collect(Collectors.toList());
    }


}
