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

import Ice.Communicator;
import com.glsx.base.enums.MapTypeEnum;
import com.glsx.ice.HDaoMile.Date;
import com.glsx.ice.HDaoMile.*;
import com.glsx.utils.SystemProperty;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.*;
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) {
        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;
            //返回最后GPS记录时间
            req.bRetGpsTime = true;

            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));
                //80~120km所占百分比
                map.put("over80km_percent", val.over80Percent);
                //120km以上所占百分比
                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 apType, 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 = apType;
            //  缺省是false,为true时,表示响应结果中携带中文地址
            req.bReqAddr = true;
            //开始日期
            //req.bgnDate = bgnDate;
            //结束日期
           // req.endDate = endDate;
            //0:原始坐标   1:转换高德和百度坐标  2:转换高德坐标  3:转换百度坐标
            req.extraMapReq = MapTypeEnum.DOUBLE_TYPE.getId();

            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;
    }

    public static Map<String, Object> getTotalMileageRecords(long sn, Integer apType) {
        try {
            if (mileagePrx == null) {
                throw new Exception("Invalid proxy");
            }

            GetTotalMileageResHolder res = new GetTotalMileageResHolder(new GetTotalMileageRes());
            GetTotalMileageReq req = new GetTotalMileageReq();
            req.userID = sn;
            req.apType = apType;
            //默认0,非0:获取日里程记录
            //req.bReqDailyRecord
            mileagePrx.getToatlMileage(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("sn", drecord.jourCount);
                    //行驶总里程
                    jmap.put("totalmileage", drecord.mileage);
                    //行驶总时长
                    jmap.put("totalduration", drecord.duration);
                    //行驶总开车次数
                    jmap.put("totaljourcount", drecord.jourDate);
                    return jmap;
                }).collect(Collectors.toList());
                map.put("totalmileagerecords", 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;
    }

    public static Map<String, Object> getMonthResideRecords(long sn, Integer dType, String bgn_month, String end_month) {
        try {
            if (mileagePrx == null) {
                throw new Exception("Invalid proxy");
            }

            GetMonthResideRecordsResHolder res = new GetMonthResideRecordsResHolder(new GetMonthResideRecordsRes());
            GetMonthResideRecordsReq req = new GetMonthResideRecordsReq();
            req.userID = sn;
            req.apType = dType;

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");//注意月份是MM

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(simpleDateFormat.parse(bgn_month));                    //放入Date类型数据
            Month bm = new Month(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH));
            //开始月份 如: 2019-08
            req.bgnMonth = bm;

            calendar.setTime(simpleDateFormat.parse(end_month));                    //放入Date类型数据
            Month em = new Month(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH));
            //结束月份 如:2019-10
            req.endMonth = em;

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

                List<Map<String, Object>> monthRecords = Arrays.stream(vals).map(mrecord -> {
                    Map<String, Object> jmap = new HashMap<>();
                    //第一常驻地位置信息
                    jmap.put("fstresideloc", mrecord.fstResideLoc);
                    //第一常驻地停留时长
                    jmap.put("fstresideduration", mrecord.fstResideDuration);
                    //第一常驻地停留次数
                    jmap.put("fstresidetimes", mrecord.fstResideTimes);
                    //第二常驻地位置信息
                    jmap.put("sndresideloc", mrecord.sndResideLoc);
                    //第二常驻地停留时长
                    jmap.put("sndresideduration", mrecord.sndResideDuration);
                    //第二常驻地停留次数
                    jmap.put("sndresidetimes", mrecord.sndResideTimes);
                    //第三常驻地位置信息
                    jmap.put("trdresideloc", mrecord.trdResideLoc);
                    //第三常驻地停留时长
                    jmap.put("trdresideduration", mrecord.trdResideDuration);
                    //第三常驻地停留次数
                    jmap.put("trdresidetimes", mrecord.trdResideTimes);
                    //常驻地所属月份
                    jmap.put("whichmonth", mrecord.whichMonth);

                    return jmap;
                }).collect(Collectors.toList());
                map.put("monthresiderecord", monthRecords);
                return map;
            }

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


	public static Map<String, Object> getDeviceStayRecords(long sn, Integer dType, String start_date, String end_date, Integer period_type, Integer max_duration, Integer min_duration) {
		try {
			if (mileagePrx == null) {
				throw new Exception("Invalid proxy");
			}

			GetStayPointRecordsResHolder res = new GetStayPointRecordsResHolder(new GetStayPointRecordsRes());
			GetStayPointRecordsReq req = new GetStayPointRecordsReq();
			req.userID = sn;
			req.apType = dType;
			req.periodType = period_type;
			req.maxDuration = max_duration;
			req.minDuration = min_duration;
			req.extraMapReq = MapTypeEnum.DOUBLE_TYPE.getId();

			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");//注意月份是MM

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(simpleDateFormat.parse(start_date));                    //放入Date类型数据
			Date bm = new Date(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE));
			req.bgnDate = bm;

			calendar.setTime(simpleDateFormat.parse(end_date));                    //放入Date类型数据
			Date em = new Date(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE));
			req.endDate = em;

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

				List<Map<String, Object>> stayRecords = Arrays.stream(vals).map(srecord -> {
					Map<String, Object> jmap = new HashMap<>();
					//开始停留时间
					jmap.put("bgntime", srecord.bgnTime);
					//结束停留时间
					jmap.put("endtime", srecord.endTime);
					//停留时长
					jmap.put("duration", srecord.duration);
					//第二常驻地位置信息
					jmap.put("staygps", convertGpsLoc(srecord.stayGps));
					return jmap;
				}).collect(Collectors.toList());
				map.put("staypointrecord", stayRecords);
				return map;
			}

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

    private static Object convertGpsLoc(GpsLoc bgnGps) {
        Map<String, Object> map = new HashMap<>();
        //经度
        map.put("lng", bgnGps.lng / 1000000D);
        //纬度
        map.put("lat", bgnGps.lat / 1000000D);
        //高德经度
        map.put("glng", bgnGps.glng / 1000000D);
        //高德纬度
        map.put("glat", bgnGps.glat / 1000000D);
        //百度经度
        map.put("blng", bgnGps.blng / 1000000D);
        //百度纬度
        map.put("blat", bgnGps.blat / 1000000D);
        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 / 1000000D);
        //纬度
        map.put("lat", beginPos.lat / 1000000D);
        //地址描述
        map.put("addr", beginPos.addr);
        return map;
    }
}