with hollow_flow_temp AS (
SELECT DISTINCT flow_card_id
FROM hollow_big_storage_cage_details
WHERE state = 100
and flow_card_id = #{flowCardId}
)
, glass_temp as (
select t1.*
from hollow_flow_temp t
INNER JOIN glass_info t1 on t.flow_card_id = t1.flow_card_id
)
, detail_temp as (
select t.*
from glass_temp t
left join hollow_big_storage_cage_details t1 on t.glass_id = t1.glass_id
where t1.glass_id is null
)
, damage_ranked AS (
SELECT t.flow_card_id,
t.layer,
t.glass_id,
t.glass_type,
t.width,
t.height,
t.filmsId,
t.thickness,
case
when type in (8, 9) and status = 1 then ''
else t1.working_procedure end as working_procedure,
ROW_NUMBER() OVER (PARTITION BY t1.glass_id ORDER BY t1.id DESC) as rn
FROM detail_temp t
inner join damage t1 on t.glass_id = t1.glass_id
)
, damage_latest AS (
SELECT *
FROM damage_ranked
WHERE rn = 1
)
select *
from damage_latest
WITH flow_card_id_info AS (
SELECT DISTINCT flow_card_id
FROM hollow_big_storage_cage_details
WHERE state = 100
),
glass_temp AS (
SELECT t.*
FROM glass_info t
INNER JOIN flow_card_id_info t1 ON t.flow_card_id = t1.flow_card_id
),
detail_temp AS (
SELECT t.*
FROM glass_temp t
WHERE NOT EXISTS (
SELECT 1
FROM hollow_big_storage_cage_details t1
WHERE t1.glass_id = t.glass_id
AND t1.state NOT IN (8,9)
)
),
damage_latest AS (
SELECT
dr.glass_id,
dr.type,
dr.status
FROM (
SELECT
t1.glass_id,
t1.type,
t1.status,
ROW_NUMBER() OVER(PARTITION BY t1.glass_id ORDER BY t1.id DESC) as rn
FROM detail_temp t
INNER JOIN damage t1 ON t.flow_card_id = t1.process_id
) dr
WHERE dr.rn = 1
),
result_temp AS (
SELECT
t.flow_card_id,
t.layer,
t.glass_type,
t.thickness,
t.filmsId,
t.width,
t.height,
COUNT(DISTINCT t.glass_id) as lack_count,
COUNT(DISTINCT CASE WHEN t1.type IN (8,9) AND t1.status = 1 THEN t.glass_id END) as damage_count
FROM detail_temp t
LEFT JOIN damage_latest t1 ON t.glass_id = t1.glass_id
GROUP BY
t.flow_card_id,
t.layer,
t.glass_type,
t.thickness,
t.filmsId,
t.width,
t.height
)
SELECT *
FROM result_temp
ORDER BY flow_card_id, layer;
select count(distinct layer)
from hollow_glass_relation_info
where flow_card_id = #{flowCardId}
with glass_temp as (
select *
from glass_info
where flow_card_id = #{flowCardId}
and glass_type = #{orderSort}
and layer = #{layer}
)
, detail_temp as (
select t.*
from glass_temp t
left join hollow_big_storage_cage_details t1 on t.glass_id = t1.glass_id and t1.state not in (8,9)
where t1.glass_id is null
)
, damage_ranked AS (
SELECT t.flow_card_id,
t.layer,
t.glass_id,
t.glass_type,
t.width,
t.height,
t.filmsId,
t.thickness,
case
when type in (8, 9) and status = 1 then ''
else t1.working_procedure end as working_procedure,
ROW_NUMBER() OVER (PARTITION BY t1.glass_id ORDER BY t1.id DESC) as rn
FROM detail_temp t
inner join damage t1 on t.glass_id = t1.glass_id
)
, damage_latest AS (
SELECT *
FROM damage_ranked
WHERE rn = 1
)
select *
from damage_latest
update hollow_glass_relation_info
set glass_id = null,
tempering_layout_id = null,
tempering_feed_sequence = null,
engineer_id = null,
state = 0
where flow_card_id = #{flowCardId}
and layer = #{layer}
and glass_id not in (
select glass_id
from hollow_big_storage_cage_details
where flow_card_id = #{flowCardId}
and layer = #{layer}
and state in (100, 102, 103, 104)
)