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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?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="cn.iocoder.yudao.module.shipment.dal.mysql.BoxMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
-->
<resultMap id="missNumber" type="cn.iocoder.yudao.module.shipment.dal.dataobject.MissNumberDO">
<result column="missing_num_start" property="missingNumStart" />
<result column="missing_num_end" property="missingNumEnd" />
</resultMap>
<select id="getMissNumber" resultMap="missNumber">
SELECT
t1.num + 1 AS missing_num_start,
( SELECT MIN( num ) - 1 FROM ecw_box WHERE num > t1.num and deleted=0 and t1.deleted=0
and dest_country_id = #{destCountryId}
and num >= #{minNumber}
<if test="startCountryId != null">
and start_country_id = #{startCountryId}
</if>
<if test="startWarehouseId != null">
and start_warehouse_id = #{startWarehouseId}
</if>
) AS missing_num_end
FROM
ecw_box t1
WHERE
NOT EXISTS ( SELECT * FROM ecw_box WHERE num = t1.num + 1 and deleted=0 and t1.deleted=0
and dest_country_id = #{destCountryId}
and num >= #{minNumber}
<if test="startCountryId != null">
and start_country_id = #{startCountryId}
</if>
<if test="startWarehouseId != null">
and start_warehouse_id = #{startWarehouseId}
</if>
)
AND ( SELECT MIN( num ) FROM ecw_box WHERE num > t1.num and deleted=0 and t1.deleted=0
and dest_country_id = #{destCountryId}
and num >= #{minNumber}
<if test="startCountryId != null">
and start_country_id = #{startCountryId}
</if>
<if test="startWarehouseId != null">
and start_warehouse_id = #{startWarehouseId}
</if>
) IS NOT NULL
AND t1.num >= #{minNumber}
and t1.deleted = 0
and t1.dest_country_id = #{destCountryId}
<if test="startCountryId != null">
and t1.start_country_id = #{startCountryId}
</if>
<if test="startWarehouseId != null">
and t1.start_warehouse_id = #{startWarehouseId}
</if>
;
</select>
<select id="getMinNumDeleteBox" resultType="long">
select min(num) from ecw_box where deleted = 1
and create_time > '2023-05-07 00:00:00'
and dest_country_id = #{destCountryId}
<if test="startCountryId != null">
and start_country_id = #{startCountryId}
</if>
<if test="startWarehouseId != null">
and start_warehouse_id = #{startWarehouseId}
</if>
and num not in (
SELECT num FROM ecw_box
WHERE deleted = 0
and create_time > '2023-05-07 00:00:00'
and dest_country_id = #{destCountryId}
<if test="startCountryId != null">
and start_country_id = #{startCountryId}
</if>
<if test="startWarehouseId != null">
and start_warehouse_id = #{startWarehouseId}
</if>
)
</select>
</mapper>
<!-- SELECT-->
<!-- MIN( missing_num_start ) AS smallest_missing_num-->
<!-- FROM-->
<!-- (-->
<!-- SELECT-->
<!-- t1.num + 1 AS missing_num_start-->
<!-- FROM-->
<!-- ecw_box t1-->
<!-- WHERE-->
<!-- NOT EXISTS ( SELECT * FROM ecw_box WHERE num = t1.num + 1 )-->
<!-- AND t1.num > 0 and t1.deleted=0-->
<!-- AND ( SELECT MIN( num ) FROM ecw_box WHERE num > t1.num ) IS NOT NULL-->
<!-- ) AS missing_nums;-->
<!-- SELECT-->
<!-- missing_num_start,-->
<!-- missing_num_end-->
<!-- FROM-->
<!-- (-->
<!-- SELECT-->
<!-- num + 1 AS missing_num_start,-->
<!-- LEAD ( num ) OVER ( ORDER BY num ) - 1 AS missing_num_end-->
<!-- FROM-->
<!-- ecw_box-->
<!-- WHERE-->
<!-- num > 0-->
<!-- AND LEAD ( num ) OVER ( ORDER BY num ) IS NOT NULL-->
<!-- ) t-->
<!-- WHERE-->
<!-- missing_num_end > missing_num_start - 1;-->