|
|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
|
|
namespace Modules\EcommerceFinance\Repositories;
|
|
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQuery;
|
|
|
|
|
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
|
|
|
|
|
|
|
|
|
|
@ -14,34 +15,58 @@ class PlatformPromotionAccountRepository
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dashboard(array $filters, int $limit = 200): array
|
|
|
|
|
public function dashboard(array $filters, int $limit = 200, ?string $platform = null): array
|
|
|
|
|
{
|
|
|
|
|
if ($platform !== null && ! in_array($platform, ['jd', 'douyin', 'dewu', 'vip'], true)) {
|
|
|
|
|
throw new InvalidArgumentException('未知的推广账户平台。');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$month = $filters['month'];
|
|
|
|
|
$shop = $filters['shop_name'] ?? null;
|
|
|
|
|
$account = $filters['account'] ?? null;
|
|
|
|
|
$limit = min(max($limit, 1), 5000);
|
|
|
|
|
$query = $this->queries->forSource(self::SOURCE);
|
|
|
|
|
|
|
|
|
|
$jd = $this->query($query, "SELECT TOP {$limit} data_month, shop_name, account, login_pin, in_out_type_name, trade_type_name, amount, balance_amount, settle_datetime, show_date, trade_no, remark FROM JSDataMiddlePlatform.dbo.jd_jzt_finance_records WHERE data_month = ?", [$month], $shop, $account, 'shop_name');
|
|
|
|
|
$jdSummary = $this->query($query, "SELECT COALESCE(SUM(CASE WHEN in_out_type='IN' THEN amount ELSE 0 END),0) income, COALESCE(SUM(CASE WHEN in_out_type='OUT' THEN amount ELSE 0 END),0) expense, COUNT(*) record_count FROM JSDataMiddlePlatform.dbo.jd_jzt_finance_records WHERE data_month = ?", [$month], $shop, $account, 'shop_name', true)[0] ?? [];
|
|
|
|
|
$jd = [];
|
|
|
|
|
$jdSummary = [];
|
|
|
|
|
$douyin = [];
|
|
|
|
|
$douyinSummary = [];
|
|
|
|
|
$douyinStatus = [];
|
|
|
|
|
$dewu = [];
|
|
|
|
|
$dewuSummary = [];
|
|
|
|
|
$vipSales = [];
|
|
|
|
|
$vipTax = [];
|
|
|
|
|
$vipMarketing = [];
|
|
|
|
|
$vipSummary = [];
|
|
|
|
|
|
|
|
|
|
$douyin = $this->query($query, "SELECT TOP {$limit} shopName, account, qianchuanAavid, qianchuanAccountName, statDate, cost, income, transferOut, transferIn, balance, status, remark FROM JSDataMiddlePlatform.dbo.dy_qianchuan_finance_transactions WHERE dataMonth = ? AND rowType = N'daily' AND status = N'success'", [$month . '-01'], $shop, $account, 'shopName');
|
|
|
|
|
$douyinSummary = $this->query($query, "SELECT COALESCE(SUM(cost),0) cost, COALESCE(SUM(income),0) income, COALESCE(SUM(transferOut),0) transfer_out, COALESCE(SUM(transferIn),0) transfer_in, COUNT(DISTINCT qianchuanAavid) account_count FROM JSDataMiddlePlatform.dbo.dy_qianchuan_finance_transactions WHERE dataMonth = ? AND rowType = N'daily' AND status = N'success'", [$month . '-01'], $shop, $account, 'shopName', true)[0] ?? [];
|
|
|
|
|
$douyinStatus = $this->query($query, "SELECT COUNT(*) status_count FROM JSDataMiddlePlatform.dbo.dy_qianchuan_finance_transactions WHERE dataMonth = ? AND rowType = N'status' AND status = N'no_permission'", [$month . '-01'], $shop, $account, 'shopName', true)[0] ?? [];
|
|
|
|
|
if ($platform === null || $platform === 'jd') {
|
|
|
|
|
$jd = $this->query($query, "SELECT TOP {$limit} data_month, shop_name, account, login_pin, in_out_type_name, trade_type_name, amount, balance_amount, settle_datetime, show_date, trade_no, remark FROM JSDataMiddlePlatform.dbo.jd_jzt_finance_records WHERE data_month = ?", [$month], $shop, $account, 'shop_name');
|
|
|
|
|
$jdSummary = $this->query($query, "SELECT COALESCE(SUM(CASE WHEN in_out_type='IN' THEN amount ELSE 0 END),0) income, COALESCE(SUM(CASE WHEN in_out_type='OUT' THEN amount ELSE 0 END),0) expense, COUNT(*) record_count FROM JSDataMiddlePlatform.dbo.jd_jzt_finance_records WHERE data_month = ?", [$month], $shop, $account, 'shop_name', true)[0] ?? [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dewu = $this->query($query, "SELECT TOP {$limit} data_month, shop_name, account, amount_type, flow_type, trade_amount_yuan, balance_yuan, trade_date, date_time, remark FROM JSDataMiddlePlatform.dbo.dw_ad_finance_records WHERE data_month = ?", [$month], $shop, $account, 'shop_name');
|
|
|
|
|
$dewuSummary = $this->query($query, 'SELECT COALESCE(SUM(total_income_amount),0) income, COALESCE(SUM(total_expense_amount),0) expense, COALESCE(SUM(net_amount),0) net, COALESCE(SUM(record_count),0) record_count FROM JSDataMiddlePlatform.dbo.dw_ad_finance_month_summaries WHERE data_month = ?', [$month], $shop, $account, 'shop_name', true)[0] ?? [];
|
|
|
|
|
if ($platform === null || $platform === 'douyin') {
|
|
|
|
|
$douyin = $this->query($query, "SELECT TOP {$limit} shopName, account, qianchuanAavid, qianchuanAccountName, statDate, cost, income, transferOut, transferIn, balance, status, remark FROM JSDataMiddlePlatform.dbo.dy_qianchuan_finance_transactions WHERE dataMonth = ? AND rowType = N'daily' AND status = N'success'", [$month . '-01'], $shop, $account, 'shopName');
|
|
|
|
|
$douyinSummary = $this->query($query, "SELECT COALESCE(SUM(cost),0) cost, COALESCE(SUM(income),0) income, COALESCE(SUM(transferOut),0) transfer_out, COALESCE(SUM(transferIn),0) transfer_in, COUNT(DISTINCT qianchuanAavid) account_count FROM JSDataMiddlePlatform.dbo.dy_qianchuan_finance_transactions WHERE dataMonth = ? AND rowType = N'daily' AND status = N'success'", [$month . '-01'], $shop, $account, 'shopName', true)[0] ?? [];
|
|
|
|
|
$douyinStatus = $this->query($query, "SELECT COUNT(*) status_count FROM JSDataMiddlePlatform.dbo.dy_qianchuan_finance_transactions WHERE dataMonth = ? AND rowType = N'status' AND status = N'no_permission'", [$month . '-01'], $shop, $account, 'shopName', true)[0] ?? [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$vipSales = $this->query($query, "SELECT TOP {$limit} shopName, dataMonth, monthName, previousYearSalesAmount, salesTarget, actualSalesAmount, updatedAt FROM JSDataMiddlePlatform.dbo.vip_monthlySaleProgress WHERE dataMonth = ?", [$month], $shop, null, 'shopName');
|
|
|
|
|
$vipTax = $this->query($query, "SELECT TOP {$limit} shopName, storeName, reportAmount, returnOrderCount, returnServiceAmount, returnSkuAmount, saleOrderCount, salesServiceAmount, salesSkuAmount, transactionNum, transactionTime FROM JSDataMiddlePlatform.dbo.vip_taxDeclarationDetail WHERE dataMonth = ?", [$month], $shop, null, 'shopName');
|
|
|
|
|
$vipMarketing = $this->query($query, "SELECT TOP {$limit} account, accountName, advertiserId, dataMonth, totalCostYuan, totalImpressionCount, recordCount, updatedAt FROM JSDataMiddlePlatform.dbo.vip_marketingMonthlySummary WHERE dataMonth = ?", [$month], null, $account, 'account');
|
|
|
|
|
$vipSummary = [
|
|
|
|
|
'sales' => $this->sum($vipSales, 'actualSalesAmount'),
|
|
|
|
|
'target' => $this->sum($vipSales, 'salesTarget'),
|
|
|
|
|
'tax' => $this->sum($vipTax, 'reportAmount'),
|
|
|
|
|
'marketing_cost' => $this->sum($vipMarketing, 'totalCostYuan'),
|
|
|
|
|
'impressions' => $this->sum($vipMarketing, 'totalImpressionCount'),
|
|
|
|
|
];
|
|
|
|
|
if ($platform === null || $platform === 'dewu') {
|
|
|
|
|
$dewu = $this->query($query, "SELECT TOP {$limit} data_month, shop_name, account, amount_type, flow_type, trade_amount_yuan, balance_yuan, trade_date, date_time, remark FROM JSDataMiddlePlatform.dbo.dw_ad_finance_records WHERE data_month = ?", [$month], $shop, $account, 'shop_name');
|
|
|
|
|
$dewuSummary = $this->query($query, 'SELECT COALESCE(SUM(total_income_amount),0) income, COALESCE(SUM(total_expense_amount),0) expense, COALESCE(SUM(net_amount),0) net, COALESCE(SUM(record_count),0) record_count FROM JSDataMiddlePlatform.dbo.dw_ad_finance_month_summaries WHERE data_month = ?', [$month], $shop, $account, 'shop_name', true)[0] ?? [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($platform === null || $platform === 'vip') {
|
|
|
|
|
$vipSales = $this->query($query, "SELECT TOP {$limit} shopName, dataMonth, monthName, previousYearSalesAmount, salesTarget, actualSalesAmount, updatedAt FROM JSDataMiddlePlatform.dbo.vip_monthlySaleProgress WHERE dataMonth = ?", [$month], $shop, null, 'shopName');
|
|
|
|
|
$vipTax = $this->query($query, "SELECT TOP {$limit} shopName, storeName, reportAmount, returnOrderCount, returnServiceAmount, returnSkuAmount, saleOrderCount, salesServiceAmount, salesSkuAmount, transactionNum, transactionTime FROM JSDataMiddlePlatform.dbo.vip_taxDeclarationDetail WHERE dataMonth = ?", [$month], $shop, null, 'shopName');
|
|
|
|
|
$vipMarketing = $this->query($query, "SELECT TOP {$limit} account, accountName, advertiserId, dataMonth, totalCostYuan, totalImpressionCount, recordCount, updatedAt FROM JSDataMiddlePlatform.dbo.vip_marketingMonthlySummary WHERE dataMonth = ?", [$month], null, $account, 'account');
|
|
|
|
|
$vipSummary = [
|
|
|
|
|
'sales' => $this->sum($vipSales, 'actualSalesAmount'),
|
|
|
|
|
'target' => $this->sum($vipSales, 'salesTarget'),
|
|
|
|
|
'tax' => $this->sum($vipTax, 'reportAmount'),
|
|
|
|
|
'marketing_cost' => $this->sum($vipMarketing, 'totalCostYuan'),
|
|
|
|
|
'impressions' => $this->sum($vipMarketing, 'totalImpressionCount'),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'jd' => $jd,
|
|
|
|
|
|