WITH
listing_table AS (
SELECT
id,
agent_id
FROM
`propertyguru-datalake-v0.propertydb_staging.sg_v_listing_active`
WHERE status_code = 'ACT'
GROUP BY 1,2),
active_listing AS (
SELECT
agent_id,
owner_id AS listing_id,
media.id AS image_id,
media.status_code AS image_status
FROM
`propertyguru-datalake-v0.propertydb_staging.sg_media` media
INNER JOIN listing_table lt
ON lt.id = media.owner_id
WHERE status_code IN ('ACT','SUSP')
),
agent_table AS (
SELECT
agent_id,
mobile,
email,
firstname,
lastname,
agency_name,
status_code,
account_rule_code
FROM
`propertyguru-datalake-v0.propertydb_staging.sg_v_agent_search`
WHERE status_code = 'ACT'
),
image_confidence_table AS (
SELECT
mm.media_id,
md.media_class,
confident_level,
ms.status AS moderator_status
FROM
`propertyguru-datalake-v0.propertydb_staging.sg_media_moderator_result` mm
INNER JOIN `propertyguru-datalake-v0.production_pg_db05_propertydb.media` md
ON mm.media_id = md.id
INNER JOIN `propertyguru-datalake-v0.propertydb_staging.sg_media_moderator_status` ms
ON mm.media_id = ms.media_id
WHERE mm.reason_id = 12
AND mm.confident_level >= 0.4
),
final_image_table AS (
SELECT
media_id,
media_class,
listing_id,
confident_level,
image_status,
moderator_status,
al.agent_id
FROM image_confidence_table img
INNER JOIN active_listing al
ON img.media_id = al.image_id
),
final_table AS (
SELECT
media_id,
media_class,
listing_id,
CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT("https://sg1-cdn.pgimgs.com/listing/",CAST(listing_id AS STRING)),"/"), media_class),"."),CAST(media_id AS STRING)),".V800.jpg") AS media_URL,
confident_level,
image_status,
moderator_status,
CASE WHEN image_status = 'ACT' AND moderator_status = 'SUSPENDED' THEN "SOFT"
WHEN image_status = 'SUSP' AND moderator_status = 'SUSPENDED' THEN "HARD"
ELSE NULL
END AS suspended_status,
al.agent_id,
mobile,
email,
firstname,
lastname,
agency_name,
status_code,
account_rule_code
FROM final_image_table img
INNER JOIN agent_table al
ON img.agent_id = al.agent_id
)
SELECT * FROM final_table
WHERE suspended_status IS NOT NULL
GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16