ParkVehicleTraceMapper.xml
3.85 KB
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zteits.parking.module.park.dal.mysql.trace.ParkVehicleTraceMapper">
<!--
车辆追溯只读 SQL
口径:
1. order_park.data_state = 1(未删除),park_in_time 落在 [begin, end];
2. 车牌精确匹配走 order_park_index_car_number;后缀匹配只在精确匹配落空时调用一次;
3. 车场名 / 区县来自 parking_lot,坐标取该车场一条有效 parking_lot_geo
(GCJ02 → 百度 → WGS84 优先),coord_type 原样返回由前端转换;
4. 时长 = 出场 − 入场(分钟,负数按 0),未出场为 NULL;
5. 金额 = order_act_fee(分),不读任何汇总字段。
-->
<sql id="stayCondition">
o.data_state = 1
AND o.car_number = #{plate}
AND o.park_in_time >= #{begin}
AND o.park_in_time <= #{end}
</sql>
<select id="countStays" resultType="int">
SELECT COUNT(*)
FROM order_park o
WHERE <include refid="stayCondition"/>
</select>
<select id="selectStays"
resultType="com.zteits.parking.module.park.dal.mysql.trace.row.TraceStayRow">
SELECT o.order_id,
o.pl_no,
TRIM(REPLACE(REPLACE(l.pl_name, CHAR(10), ''), CHAR(13), '')) AS pl_name,
o.park_in_time,
o.park_out_time,
CASE
WHEN o.park_out_time IS NULL THEN NULL
ELSE GREATEST(TIMESTAMPDIFF(MINUTE, o.park_in_time, o.park_out_time), 0)
END AS duration_minutes,
o.order_act_fee AS fee,
o.order_state,
o.berth_no,
l.pl_country_name AS district,
g.lon_id AS lng,
g.lat_id AS lat,
g.lon_lat_type AS coord_type
FROM order_park o
LEFT JOIN parking_lot l ON l.pl_no = o.pl_no
LEFT JOIN parking_lot_geo g ON g.geo_id = (
SELECT g2.geo_id
FROM parking_lot_geo g2
WHERE g2.pl_no = o.pl_no
AND g2.data_status = 1
ORDER BY CASE g2.lon_lat_type WHEN 3 THEN 0 WHEN 1 THEN 1 WHEN 2 THEN 2 ELSE 3 END,
g2.geo_id
LIMIT 1
)
WHERE <include refid="stayCondition"/>
ORDER BY o.park_in_time DESC, o.order_id DESC
LIMIT #{limit}
</select>
<!-- 后缀匹配唯一命中判定:DISTINCT + LIMIT 能借助车牌索引提前结束,不要改成 GROUP BY -->
<select id="selectPlateCandidates" resultType="java.lang.String">
SELECT DISTINCT o.car_number
FROM order_park o
WHERE o.data_state = 1
AND o.car_number LIKE CONCAT('%', #{plate})
AND o.park_in_time >= #{begin}
AND o.park_in_time <= #{end}
LIMIT #{limit}
</select>
<!--
输入候选:先取范围内最近 scanLimit 条记录,再在后缀上过滤。
LIKE 前导 % 用不上索引,若不先截断就会在每次输入时扫全表。
-->
<select id="selectRecentPlates"
resultType="com.zteits.parking.module.park.dal.mysql.trace.row.TracePlateRow">
SELECT t.car_number AS plate,
MAX(t.park_in_time) AS last_in_time
FROM (
SELECT o.car_number, o.park_in_time
FROM order_park o
WHERE o.data_state = 1
AND o.park_in_time >= #{begin}
AND o.park_in_time <= #{end}
ORDER BY o.park_in_time DESC
LIMIT #{scanLimit}
) t
WHERE t.car_number LIKE CONCAT('%', #{keyword}, '%')
GROUP BY t.car_number
ORDER BY MAX(t.park_in_time) DESC
LIMIT #{limit}
</select>
</mapper>