package com.songhua.system.service.impl;

import java.time.LocalDateTime;
import java.util.*;

import com.songhua.common.utils.DateUtils;
import com.songhua.common.utils.uuid.IdUtils;
import com.songhua.system.domain.BasicCruiseShip;
import com.songhua.system.mapper.BasicCruiseShipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.songhua.system.mapper.CruiseShipMappingMapper;
import com.songhua.system.domain.CruiseShipMapping;
import com.songhua.system.service.ICruiseShipMappingService;

/**
 * Service业务层处理
 */
@Service
public class CruiseShipMappingServiceImpl implements ICruiseShipMappingService 
{
    @Autowired
    private CruiseShipMappingMapper cruiseShipMappingMapper;

    @Autowired
    private BasicCruiseShipMapper basicCruiseShipMapper;

    /**
     * 查询
     */
    @Override
    public CruiseShipMapping selectCruiseShipMappingById(String id)
    {
        return cruiseShipMappingMapper.selectCruiseShipMappingById(id);
    }

    /**
     * 查询列表
     */
    @Override
    public List<CruiseShipMapping> selectCruiseShipMappingList(CruiseShipMapping cruiseShipMapping)
    {
        return cruiseShipMappingMapper.selectCruiseShipMappingList(cruiseShipMapping);
    }

    /**
     * 新增
     */
    @Override
    public int insertCruiseShipMapping(CruiseShipMapping cruiseShipMapping)
    {
        cruiseShipMapping.setId(IdUtils.simpleUUID());
        cruiseShipMapping.setShipTime(DateUtils.getNowDate());
        return cruiseShipMappingMapper.insertCruiseShipMapping(cruiseShipMapping);
    }

    /**
     * 修改
     */
    @Override
    public int updateCruiseShipMapping(CruiseShipMapping cruiseShipMapping)
    {
        return cruiseShipMappingMapper.updateCruiseShipMapping(cruiseShipMapping);
    }

    /**
     * 批量删除
     */
    @Override
    public int deleteCruiseShipMappingByIds(String[] ids)
    {
        return cruiseShipMappingMapper.deleteCruiseShipMappingByIds(ids);
    }

    /**
     * 删除信息
     */
    @Override
    public int deleteCruiseShipMappingById(String id)
    {
        return cruiseShipMappingMapper.deleteCruiseShipMappingById(id);
    }

    private List<String> color = Arrays.asList("white", "ivory", "lightyellow", "yellow", "snow", "floralwhite","red","blue","orange","wheat","lightgreen");
    @Override
    public List<Map<String, Object>> getShipRouteData() {
        List<Map<String, Object>> combinedData = new ArrayList<>();
        // 获取近一小时的经纬度信息
        List<CruiseShipMapping> recentLocations = cruiseShipMappingMapper.getShipRouteData();
        for (int i = 0; i <  recentLocations.size(); i++) {
            Map<String, Object> data = new HashMap<>();
            String[] lon = recentLocations.get(i).getLongitude().split(",");
            String[] lat = recentLocations.get(i).getLatitude().split(",");
            List<Map> data1 = new ArrayList<>();
            for (int j = 0; j <  lon.length; j++) {
                Map<String, Object> map = new HashMap<>();
                map.put("lng", lon[j]);
                map.put("lat", lat[j]);
                data1.add(map);
            }
            data.put("polylinePath",data1);
            data.put("color", color.get(i));
            data.put("shipName", recentLocations.get(i).getShipName());
            combinedData.add(data);
        }
        return combinedData;
    }
}
