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