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)))
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')
exec alg.usp_refresh_shadow_snapshot
@warehouse_id = #{warehouseId},
@snapshot_generation = #{generation},
@instance_id = #{instanceId};
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 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})
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});