2025-03-24 09:45:46 -04:00
using Microsoft.Extensions.Configuration ;
using Google.Apis.Auth.OAuth2 ;
using Google.Cloud.BigQuery.V2 ;
using System.Linq.Expressions ;
namespace OSAFeedXML
{
public interface IBigQuery
{
dynamic GetDataBigQuery ( List < Destinations > destinations , bool withoutbg = false ) ;
}
public enum Destinations
{
google = 0 ,
facebook = 1
}
internal enum PublicationStatus
{
HIDDEN = 1 ,
OFFLINE_OFFER = 2 ,
UNAVAILABLE = 3 ,
TEMPORARILY_UNAVAILABLE = 4 ,
SALE_IN_EC = 5 ,
PRESALE_IN_EC = 6
}
internal enum PriceConversion
{
NOT_CONVERTION = 0 ,
CONVERTION = 1
}
internal class GoogleBigQuery : IBigQuery
{
private readonly BigQueryClient bigqueryClient ;
private readonly string DomainRedirect ;
private readonly string GoogleProjectId , GoogleDatabase ;
public GoogleBigQuery ( )
{
string googleCredentialsJson = Environment . GetEnvironmentVariable ( "GOOGLE_CREDENTIALS_JSON" ) ? ? throw new Exception ( "Brak danych uwierzytelniających Google." ) ;
if ( string . IsNullOrEmpty ( googleCredentialsJson ) )
{
throw new InvalidOperationException ( "Brak danych uwierzytelniających Google." ) ;
}
GoogleCredential googleCredential = GoogleCredential . FromJson ( googleCredentialsJson ) ;
GoogleProjectId = Environment . GetEnvironmentVariable ( "GOOGLE_PROJECT_ID" ) ? ? throw new Exception ( "Brak identyfikatora projektu Google." ) ;
GoogleDatabase = Environment . GetEnvironmentVariable ( "GOOGLE_DATABASE" ) ? ? throw new Exception ( "Brak nazwy bazy danych Google." ) ;
bigqueryClient = BigQueryClient . Create ( GoogleProjectId , googleCredential ) ;
DomainRedirect = Environment . GetEnvironmentVariable ( "DOMAIN_REDIRECT" ) ? ? throw new Exception ( "Brak domeny przekierowania." ) ;
}
public dynamic GetDataBigQuery ( List < Destinations > Destinations , bool WithoutBg = false )
{
var status = new List < PublicationStatus > ( ) { PublicationStatus . SALE_IN_EC , PublicationStatus . PRESALE_IN_EC } ;
string sql = @ $ "
SELECT
bon_gid AS id ,
bon_ec_name AS title ,
bon_seo_description AS description ,
CONCAT ( ' { DomainRedirect } / produkt / ' , bon_slug_url , ' - - s - ' , bon_gid ) AS link ,
2025-03-24 10:48:45 -04:00
CASE WHEN { WithoutBg } THEN ic . bon_url ELSE bn . bon_url END AS image_link ,
2025-03-24 09:45:46 -04:00
CASE
WHEN bon_warehouse_state_central IS NULL OR bon_warehouse_state_central = 0 THEN ' out_of_stock '
WHEN NOT EXISTS (
SELECT 1
FROM UNNEST ( bon_marketplace ) AS marketplace_value
WHERE marketplace_value IN UNNEST ( [ { string . Join ( ", " , Destinations . Select ( dest = > ( int ) dest ) ) } ] )
2025-03-24 10:48:45 -04:00
) OR bon_ecommerce_status NOT IN ( { string . Join ( ", " , status . Select ( stat = > ( int ) stat ) ) } ) THEN ' out_of_stock '
2025-03-24 09:45:46 -04:00
ELSE ' in_stock '
END AS availability ,
2025-03-24 10:48:45 -04:00
ROUND ( ( bon_cash_price * ( CASE WHEN bon_price_for = { ( int ) PriceConversion . CONVERTION } THEN bon_converter ELSE 1 END ) * ( ( 100 + bon_vat_rate ) / 100 ) ) , 2 ) AS price ,
2025-03-24 09:45:46 -04:00
bon_category_path AS product_type ,
bon_name AS brand ,
bon_ean AS gtin ,
bon_index AS mpn ,
osa_gmc_id AS google_product_category
FROM ` { GoogleProjectId } . { GoogleDatabase } . bon_main_product ` bn
2025-03-24 10:48:45 -04:00
{ ( WithoutBg ? $"JOIN `{GoogleProjectId}.{GoogleDatabase}.images_clearbg` ic ON bn.bon_commodity_indexid = ic.bon_commodity_indexid" : "JOIN (SELECT '1' AS bon_url) AS ic ON ic.bon_url = '1'" ) }
2025-03-24 09:45:46 -04:00
";
BigQueryResults results = bigqueryClient . ExecuteQuery ( sql , null ) ;
return results ;
}
}
}