package com.xiaopeng.operation.service.impl;
|
|
import com.xiaopeng.operation.entity.XpGeoQueryEntity;
|
import com.xiaopeng.operation.repository.XpGeoQueryEntityRepository;
|
import com.xiaopeng.operation.service.IXpGeoQueryEntityService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.geo.Distance;
|
import org.springframework.data.geo.Metrics;
|
import org.springframework.data.geo.Point;
|
import org.springframework.stereotype.Service;
|
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* MongoDB 地理查询服务实现类
|
*/
|
@Service
|
public class XpGeoQueryEntityServiceImpl implements IXpGeoQueryEntityService {
|
|
@Autowired
|
private XpGeoQueryEntityRepository repository;
|
|
/**
|
* 通过经纬度查询固定范围500米内、一周内、指定时段的数据
|
* @param longitude 经度
|
* @param latitude 纬度
|
* @param time 时段(格式:HH-HH,例如:19-20)
|
* @return 符合条件的实体列表
|
*/
|
@Override
|
public List<XpGeoQueryEntity> findByLocationWithin500MetersAndLastWeekAndStatusGreater(
|
double longitude,
|
double latitude,
|
String time
|
) {
|
// 创建中心点坐标
|
Point point = new Point(longitude, latitude);
|
|
// 设置距离范围为500米
|
Distance distance = new Distance(0.5, Metrics.KILOMETERS); // 0.5公里 = 500米
|
|
// 计算一周前的时间
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(new Date());
|
calendar.add(Calendar.DAY_OF_YEAR, -7);
|
Date startTime = calendar.getTime();
|
|
// 执行查询 - 使用新的方法,包含时段过滤
|
return repository.findByLocationNearAndTimestampAfterAndTimeAndStatusGreaterThan(
|
point,
|
500,
|
startTime,
|
time, // 时段过滤
|
1 // 状态大于1
|
);
|
}
|
|
/**
|
* 保存地理查询实体
|
* @param entity 地理查询实体
|
* @return 保存后的实体
|
*/
|
@Override
|
public XpGeoQueryEntity save(XpGeoQueryEntity entity) {
|
return repository.save(entity);
|
}
|
|
/**
|
* 根据ID查询实体
|
* @param id 实体ID
|
* @return 实体
|
*/
|
@Override
|
public XpGeoQueryEntity findById(String id) {
|
return repository.findById(id).orElse(null);
|
}
|
|
/**
|
* 删除实体
|
* @param id 实体ID
|
*/
|
@Override
|
public void deleteById(String id) {
|
repository.deleteById(id);
|
}
|
|
/**
|
* 查询所有实体
|
* @return 实体列表
|
*/
|
@Override
|
public List<XpGeoQueryEntity> findAll() {
|
return repository.findAll();
|
}
|
}
|