IceRtMileStatClient.java 6.4 KB
package com.glsx.ice.iceClient;

import Ice.Communicator;
import com.glsx.config.SystemProperty;
import com.glsx.ice.HDaoMile.*;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.util.*;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * 计算里程
 * @author Administrator
 *
 */
@Slf4j
public class IceRtMileStatClient {
	// ice接口服务器地址
	private static String ICE_URL = SystemProperty.getProperty("ice.mileUrl");
	static Communicator ic = Ice.Util.initialize();
	static Ice.ObjectPrx base = ic.stringToProxy(ICE_URL).ice_timeout(Integer.parseInt(SystemProperty.getProperty("ice.connectTimeout")));
	static DaoMileServicePrx mileagePrx = DaoMileServicePrxHelper.checkedCast(base);

	/**
	 * 通过经纬度查询所属行政区域
	 *
	 * @return
	 */
	public static Map<String, Object> getJourneyByGpsRecords(long sn, int dType, int beginTime, int endTime) {
		boolean result = false;
		try {
			if (mileagePrx == null) {
				throw new Exception("Invalid proxy");
			}

			MileStatEndResponseHolder res = new MileStatEndResponseHolder(new MileStatEndResponse());
			MileStatEndRequest req = new MileStatEndRequest();
			req.userID = sn;
			req.apType = dType;
			req.beginTime = beginTime;
			req.endTime = endTime;

			mileagePrx.endUserMileStat(req, res);
			Map<String,Object> map = null;
			if (res != null && res.value != null && res.value.oprResult == 0) {
				MileStatEndResponse val = res.value;
				map.put("sn",sn);
				map.put("mileage",val.mileage);
				map.put("start_gpsinfo",convertGpsPos(val.beginPos));
				map.put("end_gpsinfo",convertGpsPos(val.endPos));
				map.put("over80km_percent",val.over80Percent);
				map.put("over120km_percent",val.over120Percent);
				map.put("maxspeed",val.maxSpeed);
				return map;
			}

		} catch (Ice.LocalException e) {
			log.error("invoke ice getJourneyByGpsRecords ICE_exception" + e.getMessage(), e);
		} catch (Exception e) {
			log.error("invoke ice getJourneyByGpsRecords exception" + e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 通过经纬度查询所属行政区域
	 *
	 * @return
	 */
	public static Map<String, Object> getJourneyRecords(long sn, int dType, java.util.Date bgnDate, java.util.Date endDate) {
		try {
			if (mileagePrx == null) {
				throw new Exception("Invalid proxy");
			}

			GetJourneyRecordsResHolder res = new GetJourneyRecordsResHolder(new GetJourneyRecordsRes());
			GetJourneyRecordsReq req = new GetJourneyRecordsReq();
			req.userID = sn;
			req.apType = dType;

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(bgnDate);					//放入Date类型数据
			com.glsx.ice.HDaoMile.Date bdate = new com.glsx.ice.HDaoMile.Date(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE));
			req.bgnDate = bdate;

			calendar.setTime(endDate);					//放入Date类型数据
			com.glsx.ice.HDaoMile.Date eDate = new com.glsx.ice.HDaoMile.Date(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE));
			req.endDate = eDate;

			mileagePrx.getJourneyRecords(req, res);
			Map<String,Object> map = null;
			if (res != null && res.value != null && res.value.oprResult == 0) {
				JourneyRecord[] vals = res.value.jourRecords;
				map.put("sn",sn);

				List<Map<String,Object>> journeyrecords =Arrays.stream(vals).map(jrecord->{
					Map<String,Object> jmap = new HashMap<>();
					jmap.put("bgntime",jrecord.bgnTime);
					jmap.put("endtime",jrecord.endTime);
					jmap.put("duration",jrecord.duration);
					jmap.put("bgnaddress",jrecord.bgnAddress);
					jmap.put("bgngps",convertGpsLoc(jrecord.bgnGps));
					jmap.put("endaddress",jrecord.endAddress);
					jmap.put("endgps",convertGpsLoc(jrecord.endGps));
					jmap.put("mileage",jrecord.mileage);
					return jmap;
				}).collect(Collectors.toList());
				map.put("journeyrecords", journeyrecords);
				return map;
			}

		} catch (Ice.LocalException e) {
			log.error("invoke ice getJourneyRecords ICE_exception" + e.getMessage(), e);
		} catch (Exception e) {
			log.error("invoke ice getJourneyRecords exception" + e.getMessage(), e);
		}
		return null;
	}

	public static Map<String, Object> getDailyMileageRecords(long sn, Integer dType, java.util.Date bgnDate, java.util.Date endDate) {
		try {
			if (mileagePrx == null) {
				throw new Exception("Invalid proxy");
			}

			GetDailyMileRecordsResHolder res = new GetDailyMileRecordsResHolder(new GetDailyMileRecordsRes());
			GetDailyMileRecordsReq req = new GetDailyMileRecordsReq();
			req.userID = sn;
			req.apType = dType;

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(bgnDate);					//放入Date类型数据
			com.glsx.ice.HDaoMile.Date bdate = new com.glsx.ice.HDaoMile.Date(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE));
			req.bgnDate = bdate;

			calendar.setTime(endDate);					//放入Date类型数据
			com.glsx.ice.HDaoMile.Date eDate = new com.glsx.ice.HDaoMile.Date(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE));
			req.endDate = eDate;

			mileagePrx.getDailyMileageRecords(req, res);
			Map<String,Object> map = null;
			if (res != null && res.value != null && res.value.oprResult == 0) {
				DailyMileRecord[] vals = res.value.dailMileRecords;
				map.put("sn",sn);

				List<Map<String,Object>> dailyrecords =Arrays.stream(vals).map(drecord->{
					Map<String,Object> jmap = new HashMap<>();
					jmap.put("jourcount",drecord.jourCount);
					jmap.put("mileage",drecord.mileage);
					jmap.put("duration",drecord.duration);
					jmap.put("jourdate",drecord.jourDate);
					return jmap;
				}).collect(Collectors.toList());
				map.put("dailyrecords", dailyrecords);
				return map;
			}

		} catch (Ice.LocalException e) {
			log.error("invoke ice getDailyMileageRecords ICE_exception" + e.getMessage(), e);
		} catch (Exception e) {
			log.error("invoke ice getDailyMileageRecords exception" + e.getMessage(), e);
		}
		return null;
	}

	private static Object convertGpsLoc(GpsLoc bgnGps) {
		Map<String,Object> map = new HashMap<>();
		map.put("lng",bgnGps.lng);
		map.put("lat",bgnGps.lat);
		map.put("glat",bgnGps.glat);
		map.put("glng",bgnGps.glng);
		map.put("blng",bgnGps.blng);
		map.put("blat",bgnGps.blat);
		return map;
	}

	private static Map<String,Object> convertGpsPos(GpsPos beginPos) {
		Map<String,Object> map = new HashMap<>();
		map.put("gpstime",beginPos.gpsTime);
		map.put("lng",beginPos.lng);
		map.put("lat",beginPos.lat);
		map.put("addr",beginPos.addr);
		return map;
	}


}