264 lines
15 KiB
XML
264 lines
15 KiB
XML
<?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.ruoyi.algorithm.shadow.mapper.ShadowInputMapper">
|
|
|
|
<select id="selectInboundInput" resultType="com.ruoyi.algorithm.shadow.ShadowInboundReference">
|
|
select top (1)
|
|
input.request_id as requestId,
|
|
input.raw_warehouse_id as warehouseId,
|
|
input.tray_no as trayNo,
|
|
input.project_no as projectNo,
|
|
input.product_id as productId,
|
|
input.lot_no as lotNo,
|
|
input.spare1, input.spare2, input.spare3, input.fault_flag as faultFlag,
|
|
input.detail_count as detailCount, input.field_group_count as fieldGroupCount,
|
|
input.product_scope_count as productScopeCount, input.tray_job_count as trayJobCount,
|
|
input.legacy_scope_complete as legacyScopeComplete, input.predicate_version as predicateVersion,
|
|
convert(int, coalesce((
|
|
select sum(convert(bigint, lane.available_count))
|
|
from alg.lane_state lane
|
|
where lane.warehouse_id = #{warehouseId}
|
|
and lane.snapshot_generation = #{generation}
|
|
), 0)) as shadowCandidateCount
|
|
from alg.shadow_inbound_request_snapshot input
|
|
where input.warehouse_id = #{warehouseId}
|
|
and input.snapshot_generation = #{generation}
|
|
and input.task_no = #{taskNo}
|
|
and input.tray_no = #{trayNo}
|
|
</select>
|
|
|
|
<!-- Conservative subset valid in either Legacy project-count branch. Ranking remains in the planner. -->
|
|
<select id="countInboundEligibleSpace" resultType="int">
|
|
select count(*)
|
|
<include refid="inboundEligibleSpaces"/>
|
|
and cargo.space_id = #{spaceId}
|
|
</select>
|
|
|
|
<select id="selectInboundEligibleSpaces" resultType="string">
|
|
select cargo.space_id
|
|
<include refid="inboundEligibleSpaces"/>
|
|
order by cargo.space_id
|
|
</select>
|
|
|
|
<sql id="inboundEligibleSpaces">
|
|
from alg.shadow_inbound_request_snapshot input
|
|
join alg.cargo_space_snapshot cargo
|
|
on cargo.warehouse_id = input.warehouse_id
|
|
and cargo.snapshot_generation = input.snapshot_generation
|
|
where input.warehouse_id = #{warehouseId} and input.snapshot_generation = #{generation}
|
|
and input.task_no = #{taskNo} and input.tray_no = #{trayNo}
|
|
and input.predicate_version = 1 and input.detail_count > 0
|
|
and input.field_group_count > 0 and input.product_scope_count > 0
|
|
and input.tray_job_count = 1 and input.legacy_scope_complete = 1
|
|
and nullif(ltrim(rtrim(input.project_no)), '') is not null
|
|
and input.project_no not in ('999', 'COMMON')
|
|
and cargo.space_status = '1' and cargo.locked = 'N'
|
|
and nullif(ltrim(rtrim(cargo.tray_code)), '') is null
|
|
and cargo.this_cargo_area in ('A', 'C')
|
|
and nullif(ltrim(rtrim(cargo.lane_code)), '') is not null
|
|
and nullif(ltrim(rtrim(cargo.floor_no)), '') is not null
|
|
and not exists (
|
|
select 1 from alg.inventory_item_snapshot occupied
|
|
where occupied.warehouse_id = cargo.warehouse_id
|
|
and occupied.snapshot_generation = cargo.snapshot_generation and occupied.space_id = cargo.space_id)
|
|
and not exists (
|
|
select 1 from alg.cargo_space_snapshot busy
|
|
where busy.warehouse_id = cargo.warehouse_id and busy.snapshot_generation = cargo.snapshot_generation
|
|
and ((busy.floor_no = cargo.floor_no and busy.space_status = '3')
|
|
or (busy.lane_code = cargo.lane_code and (busy.space_status in ('3', '4') or busy.locked = 'Y'))))
|
|
and (
|
|
not exists (
|
|
select 1 from alg.cargo_space_snapshot lane
|
|
where lane.warehouse_id = cargo.warehouse_id and lane.snapshot_generation = cargo.snapshot_generation
|
|
and lane.lane_code = cargo.lane_code
|
|
and (lane.space_status is null or lane.space_status <> '1'
|
|
or lane.locked is null or lane.locked <> 'N'
|
|
or nullif(ltrim(rtrim(lane.tray_code)), '') is not null
|
|
or exists (select 1 from alg.inventory_item_snapshot stock
|
|
where stock.warehouse_id = lane.warehouse_id
|
|
and stock.snapshot_generation = lane.snapshot_generation and stock.space_id = lane.space_id)))
|
|
or (input.field_group_count = 1 and exists (
|
|
select 1 from alg.cargo_space_snapshot resident
|
|
join alg.inventory_item_snapshot stock
|
|
on stock.warehouse_id = resident.warehouse_id and stock.snapshot_generation = resident.snapshot_generation
|
|
and stock.space_id = resident.space_id and stock.tray_code = resident.tray_code
|
|
where resident.warehouse_id = cargo.warehouse_id and resident.snapshot_generation = cargo.snapshot_generation
|
|
and resident.lane_code = cargo.lane_code and resident.space_status = '2'
|
|
and resident.this_cargo_area in ('A', 'C')
|
|
and stock.project_no = input.project_no
|
|
and stock.spare1 = input.spare1 and stock.spare2 = input.spare2 and stock.spare3 = input.spare3)))
|
|
</sql>
|
|
|
|
<sql id="outboundCandidatePredicate">
|
|
item.warehouse_id = #{warehouseId}
|
|
and item.snapshot_generation = #{generation}
|
|
and cargo.warehouse_id = item.warehouse_id
|
|
and cargo.snapshot_generation = item.snapshot_generation
|
|
and cargo.space_id = item.space_id and cargo.tray_code = item.tray_code
|
|
and cargo.locked = 'N'
|
|
and ((cargo.space_status = '2' and item.out_quantity = 0)
|
|
or (cargo.space_status = '4' and item.quantity > item.out_quantity))
|
|
and (nullif(input.project_no, '') is null or item.project_no = input.project_no)
|
|
and (nullif(input.product_id, '') is null or item.product_no = input.product_id)
|
|
and (nullif(input.spare1, '') is null or item.spare1 = input.spare1)
|
|
and (nullif(input.spare2, '') is null or item.spare2 = input.spare2)
|
|
and (nullif(input.spare3, '') is null or item.spare3 = input.spare3)
|
|
and (nullif(input.voltage, '') is null or item.voltage = input.voltage)
|
|
and (nullif(input.phase, '') is null or item.phase = input.phase)
|
|
and not exists (
|
|
select 1 from alg.active_job_snapshot job
|
|
where job.warehouse_id = item.warehouse_id
|
|
and job.snapshot_generation = item.snapshot_generation
|
|
and job.tray_code = item.tray_code and job.in_or_out = '26')
|
|
</sql>
|
|
|
|
<select id="selectProjectOutboundReference"
|
|
resultType="com.ruoyi.algorithm.shadow.ShadowStockReference">
|
|
select top (1)
|
|
input.stockout_detail_id as requestId,
|
|
input.raw_warehouse_id as warehouseId,
|
|
input.project_no as projectNo,
|
|
input.product_id as productId,
|
|
input.requested_quantity - input.allocated_quantity as requestedQuantity,
|
|
input.predicate_version as predicateVersion,
|
|
input.spare1, input.spare2, input.spare3, input.voltage, input.phase,
|
|
convert(varchar(40), input.synchronized_at, 126) as snapshotSynchronizedAt,
|
|
(select count(*)
|
|
from alg.inventory_item_snapshot item
|
|
join alg.cargo_space_snapshot cargo on cargo.space_id = item.space_id
|
|
where <include refid="outboundCandidatePredicate"/>)
|
|
as shadowMatchCount
|
|
from alg.shadow_outbound_request_snapshot input
|
|
where input.warehouse_id = #{warehouseId}
|
|
and input.snapshot_generation = #{generation}
|
|
and input.stockout_detail_id = #{stockoutDetailId}
|
|
and input.predicate_version = 1
|
|
</select>
|
|
|
|
<select id="selectProjectOutboundCandidates" resultType="com.ruoyi.algorithm.shadow.ShadowStockReference">
|
|
select top (201)
|
|
item.stock_id as stockId, item.tray_code as trayNo, item.space_id as spaceId,
|
|
item.project_no as projectNo, item.product_no as productId,
|
|
item.spare1, item.spare2, item.spare3, item.voltage, item.phase,
|
|
item.tray_job_count as trayJobCount,
|
|
item.quantity - item.out_quantity as availableQuantity,
|
|
(select count(*) from alg.inventory_item_snapshot trayItem
|
|
where trayItem.warehouse_id = item.warehouse_id
|
|
and trayItem.snapshot_generation = item.snapshot_generation
|
|
and trayItem.space_id = item.space_id and trayItem.tray_code = item.tray_code)
|
|
as trayItemCount
|
|
from alg.shadow_outbound_request_snapshot input
|
|
join alg.inventory_item_snapshot item on item.warehouse_id = input.warehouse_id
|
|
join alg.cargo_space_snapshot cargo on cargo.space_id = item.space_id
|
|
where input.warehouse_id = #{warehouseId}
|
|
and input.snapshot_generation = #{generation}
|
|
and input.stockout_detail_id = #{stockoutDetailId}
|
|
and input.predicate_version = 1
|
|
and <include refid="outboundCandidatePredicate"/>
|
|
order by item.stock_id
|
|
</select>
|
|
|
|
<select id="selectStockReference" resultType="com.ruoyi.algorithm.shadow.ShadowStockReference">
|
|
select top (1)
|
|
item.stock_id as stockId,
|
|
item.tray_code as trayNo,
|
|
item.space_id as spaceId,
|
|
item.warehouse_id as warehouseId,
|
|
nullif(ltrim(rtrim(item.project_no)), '') as projectNo,
|
|
item.product_no as productId,
|
|
convert(varchar(40), item.synchronized_at, 126) as snapshotSynchronizedAt,
|
|
(select count(*)
|
|
from alg.inventory_item_snapshot candidate
|
|
where candidate.warehouse_id = #{warehouseId}
|
|
and candidate.snapshot_generation = #{generation}
|
|
and candidate.stock_id = #{stockId}
|
|
and isnull(candidate.quantity, 0) > isnull(candidate.out_quantity, 0))
|
|
as shadowMatchCount
|
|
from alg.inventory_item_snapshot item
|
|
where item.warehouse_id = #{warehouseId}
|
|
and item.snapshot_generation = #{generation}
|
|
and item.stock_id = #{stockId}
|
|
and isnull(item.quantity, 0) > isnull(item.out_quantity, 0)
|
|
</select>
|
|
|
|
<select id="claimInstance" resultType="boolean">
|
|
if convert(int, #{staleSeconds}) <> 120
|
|
throw 51026, 'shadow instance stale threshold must be 120 seconds', 1;
|
|
declare @claimed bit;
|
|
exec alg.usp_claim_shadow_instance
|
|
@warehouse_id = #{warehouseId},
|
|
@instance_id = #{instanceId},
|
|
@claimed = @claimed output;
|
|
</select>
|
|
|
|
<update id="refreshSnapshot">
|
|
exec alg.usp_refresh_shadow_snapshot
|
|
@warehouse_id = #{warehouseId},
|
|
@snapshot_generation = #{generation},
|
|
@instance_id = #{instanceId};
|
|
</update>
|
|
|
|
<resultMap id="publishedSnapshot" type="com.ruoyi.algorithm.shadow.ShadowSnapshotDescriptor">
|
|
<result property="snapshotGeneration" column="snapshotGeneration"/>
|
|
<result property="warehouseId" column="warehouseId"/>
|
|
<result property="snapshotStatus" column="snapshotStatus"/>
|
|
<result property="synchronizedAt" column="synchronizedAt"
|
|
typeHandler="com.ruoyi.algorithm.shadow.mapper.ShadowUtcInstantTypeHandler"/>
|
|
<result property="sourceSignature" column="sourceSignature"/>
|
|
</resultMap>
|
|
|
|
<select id="selectPublishedSnapshot" resultMap="publishedSnapshot">
|
|
select state.snapshot_generation as snapshotGeneration,
|
|
state.warehouse_id as warehouseId,
|
|
state.snapshot_status as snapshotStatus,
|
|
state.synchronized_at as synchronizedAt,
|
|
convert(varchar(64), state.source_signature, 2) as sourceSignature
|
|
from alg.shadow_snapshot_state state with (readcommittedlock)
|
|
where state.warehouse_id = #{warehouseId}
|
|
</select>
|
|
|
|
<insert id="insertComparison">
|
|
insert into alg.decision_log (
|
|
decision_id, warehouse_id, decision_type, input_version, result_code, reason)
|
|
select try_convert(uniqueidentifier, #{decisionId}), state.warehouse_id, #{decisionType},
|
|
convert(binary(8), #{inputVersion}), #{resultCode}, #{reason}
|
|
from alg.shadow_snapshot_state state with (updlock, holdlock)
|
|
where state.warehouse_id = #{warehouseId}
|
|
and state.snapshot_status = 'PUBLISHED'
|
|
and state.snapshot_generation = #{inputVersion}
|
|
and state.synchronized_at = #{snapshotSynchronizedAt, jdbcType=TIMESTAMP, typeHandler=com.ruoyi.algorithm.shadow.mapper.ShadowUtcInstantTypeHandler}
|
|
and convert(varchar(64), state.source_signature, 2) = #{sourceSignature}
|
|
</insert>
|
|
|
|
<insert id="insertPilotAudit">
|
|
insert into alg.decision_log (
|
|
decision_id, warehouse_id, decision_type, input_version, result_code, reason)
|
|
values (try_convert(uniqueidentifier, #{decisionId}), #{warehouseId}, 'INBOUND_PILOT',
|
|
convert(binary(8), #{inputVersion}), #{resultCode}, #{reason})
|
|
</insert>
|
|
|
|
<update id="upsertRuntimeMetric">
|
|
merge alg.shadow_runtime_metric with (holdlock) as target
|
|
using (select #{warehouseId} as warehouse_id,
|
|
#{metricName} as metric_name,
|
|
#{bucketUpperMicros} as bucket_upper_micros) as source
|
|
on target.warehouse_id = source.warehouse_id
|
|
and target.metric_name = source.metric_name
|
|
and target.bucket_upper_micros = source.bucket_upper_micros
|
|
when matched then update set
|
|
metric_count = target.metric_count + #{metricCount},
|
|
metric_total_micros = target.metric_total_micros + #{metricTotalMicros},
|
|
metric_max_micros = case when target.metric_max_micros > #{metricMaxMicros}
|
|
then target.metric_max_micros else #{metricMaxMicros} end,
|
|
flushed_at = #{flushedAt, jdbcType=TIMESTAMP, typeHandler=com.ruoyi.algorithm.shadow.mapper.ShadowUtcInstantTypeHandler}
|
|
when not matched then insert (
|
|
warehouse_id, metric_name, bucket_upper_micros,
|
|
metric_count, metric_total_micros, metric_max_micros, flushed_at)
|
|
values (
|
|
source.warehouse_id, source.metric_name, source.bucket_upper_micros,
|
|
#{metricCount}, #{metricTotalMicros}, #{metricMaxMicros}, #{flushedAt, jdbcType=TIMESTAMP, typeHandler=com.ruoyi.algorithm.shadow.mapper.ShadowUtcInstantTypeHandler});
|
|
</update>
|
|
</mapper>
|