FeedDataverseBQ/AzureFunction/GoogleStorageController.cs

100 lines
4.0 KiB
C#

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,
COALESCE(ic.bon_url, bn.bon_url) AS image_link,
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))}])
)
OR bon_ecommerce_status NOT IN ({string.Join(", ", status.Select(stat => (int)stat))}) THEN 'out_of_stock'
ELSE 'in_stock'
END AS availability,
ROUND(
bon_cash_price *
(CASE WHEN bon_price_for = {(int)PriceConversion.CONVERTION} THEN bon_converter ELSE 1 END) *
((100 + COALESCE(bon_vat_rate, 0)) / 100), 2
) AS price,
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
LEFT JOIN `{GoogleProjectId}.{GoogleDatabase}.images_clearbg` ic
ON bn.bon_commodity_indexid = ic.bon_commodity_indexid
";
BigQueryResults results = bigqueryClient.ExecuteQuery(sql, null);
return results;
}
}
}