无关风月
2026-02-12 ee6afc28ebf2b3904672606163b12df503f7ba67
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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();
    }
}