package com.emergency.avenue.storage.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.avenue.admin.api.vo.InTransitVehicleOrderVO;
import com.emergency.avenue.storage.api.entity.InTransitVehicleInfo;
import com.emergency.avenue.storage.api.request.EyeCommonReq;
import com.emergency.avenue.storage.mapper.InTransitVehicleInfoMapper;
import com.emergency.avenue.storage.service.InTransitVehicleInfoService;
import com.emergency.avenue.storage.util.EyeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class InTransitVehicleInfoServiceImpl extends ServiceImpl<InTransitVehicleInfoMapper, InTransitVehicleInfo> implements InTransitVehicleInfoService {

	@Autowired
	private InTransitVehicleInfoMapper inTransitVehicleInfoMapper;

	@Value("${trajectoryKey.appKey}")
	private String appKey;
	@Value("${trajectoryKey.appSecret}")
	private String appSecret;
	@Value("${trajectoryKey.publicKey}")
	private String publicKey;

	@Override
	public EyeCommonReq trajectorybyId(Long vehicleId) {

		InTransitVehicleInfo byId = inTransitVehicleInfoMapper.selectById(vehicleId);
		Map<String, String> eyeMap = getEyeMap(byId);
		EyeCommonReq eyeCommonReq;
		try {
			 eyeCommonReq = EyeUtils.bindCommonReq(appKey, appSecret, publicKey, JSON.toJSONString(eyeMap));
		} catch (Exception e) {
			throw new RuntimeException("轨迹查询失败");
		}
		return eyeCommonReq;
	}

	public Map<String,String> getEyeMap(InTransitVehicleInfo byId) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String,String> map= new HashMap<>();
		map.put("vin",byId.getVin());
		map.put("startTime",sdf.format(byId.getStartDeliveryTime()));
		if(byId.getEstimatedArrivalTime()==null){
			map.put("endTime",sdf.format(new Date()));
		}else{
			map.put("endTime",sdf.format(byId.getEstimatedArrivalTime()));
		}
		map.put("plateNumber",byId.getLicensePlateNumber());
		return map;
	}

	@Override
	public void getEyeString(InTransitVehicleInfo item) {
		// 查询车辆信息(定位）
		Map<String, String> eyeMap = getEyeMap(item);
		try {
			EyeCommonReq eyeCommonReq = EyeUtils.bindCommonReq(appKey, appSecret, publicKey, JSON.toJSONString(eyeMap));
			//todo 调用外部接口，查询车辆位置信息
//			String carPosition = HttpUtils.sendPost(eyeCommonReq.getUrl(), JSON.toJSONString(eyeCommonReq.getData()));
			item.setCarPosition("车辆位置信息");
		} catch (Exception e) {
			item.setCarPosition("车辆位置信息获取失败...");
		}
	}

	@Override
	public IPage<InTransitVehicleOrderVO> getInTransitVehicleInfoPageOrder(Page page, InTransitVehicleInfo inTransitVehicleInfo) {

        return inTransitVehicleInfoMapper.getInTransitVehicleInfoPageOrder(page, inTransitVehicleInfo);
	}

	@Override
	public EyeCommonReq trajectorybyOrdierName(List<InTransitVehicleInfo> records) {
		List<Map<String,String>> list= new ArrayList<>();
		for (InTransitVehicleInfo inTransitVehicleInfo : records){
			list.add(getEyeMap(inTransitVehicleInfo));
		}
		EyeCommonReq eyeCommonReq;
		try {
			eyeCommonReq = EyeUtils.bindCommonReq(appKey, appSecret, publicKey, JSON.toJSONString(list));
		} catch (Exception e) {
			throw new RuntimeException("轨迹查询失败");
		}
		return eyeCommonReq;
	}
}
