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.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
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.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("red","white", "ivory", "lightyellow", "yellow", "snow", "floralwhite","blue","orange","wheat","lightgreen");
    @Override
    public List<Map<String, Object>> getShipRouteData() {
        List<Map<String, Object>> combinedData = new ArrayList<>();
        // 获取近一小时的经纬度信息
        List<CruiseShipMapping> recentLocations = cruiseShipMappingMapper.getShipRouteData(null);
        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("id", recentLocations.get(i).getId());
            data.put("polylinePath",data1);
            data.put("color", color.get(RandomUtils.nextInt(0, 9)));
            data.put("shipName", StringUtils.isBlank(recentLocations.get(i).getShipName()) ? "" : recentLocations.get(i).getShipName());
            data.put("shipType", StringUtils.isBlank(recentLocations.get(i).getShipType()) ? "" : recentLocations.get(i).getShipType());
            data.put("lengthOverall", recentLocations.get(i).getLengthOverall());
            data.put("registrationNumber", recentLocations.get(i).getRegistrationNumber());
            data.put("quantity", recentLocations.get(i).getQuantity());
            data.put("propulsion", StringUtils.isBlank(recentLocations.get(i).getPropulsion()) ? "" : recentLocations.get(i).getPropulsion());
            data.put("passengerCapacity", recentLocations.get(i).getPassengerCapacity());
            data.put("safetyEquipment", recentLocations.get(i).getSafetyEquipment());
            data.put("did", recentLocations.get(i).getDid());
            combinedData.add(data);
        }
        return combinedData;
    }

    @Override
    public int saveCruiseShipMapping(List objectList) {
        return cruiseShipMappingMapper.saveCruiseShipMapping(objectList);
    }

    @Override
    public List<Map<String, Object>> getGpsByDid(Long did) {
        List<Map<String, Object>> combinedData = new ArrayList<>();
        // 获取近一小时的经纬度信息
        List<CruiseShipMapping> recentLocations = cruiseShipMappingMapper.getShipRouteData(did);
        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("id", recentLocations.get(i).getId());
            data.put("polylinePath",data1);
            data.put("color", color.get(RandomUtils.nextInt(0, 9)));
            data.put("shipName", StringUtils.isBlank(recentLocations.get(i).getShipName()) ? "" : recentLocations.get(i).getShipName());
            data.put("shipType", StringUtils.isBlank(recentLocations.get(i).getShipType()) ? "" : recentLocations.get(i).getShipType());
            data.put("lengthOverall", recentLocations.get(i).getLengthOverall());
            data.put("registrationNumber", recentLocations.get(i).getRegistrationNumber());
            data.put("quantity", recentLocations.get(i).getQuantity());
            data.put("propulsion", StringUtils.isBlank(recentLocations.get(i).getPropulsion()) ? "" : recentLocations.get(i).getPropulsion());
            data.put("passengerCapacity", recentLocations.get(i).getPassengerCapacity());
            data.put("safetyEquipment", recentLocations.get(i).getSafetyEquipment());
            data.put("did", recentLocations.get(i).getDid());
            combinedData.add(data);
        }
        return combinedData;
    }

    @Override
    public void saveEquipmentHistoryLocation(List objectList) {
        cruiseShipMappingMapper.saveEquipmentHistoryLocation(objectList);
    }
}
