允许查询日期范围数据

This commit is contained in:
hgc 2024-12-23 17:06:11 +08:00
parent 0d5405d21a
commit a65af85741
2 changed files with 232 additions and 126 deletions

View File

@ -46,8 +46,20 @@ class AdController
$keyword = $request->input('keyword', ''); // 关键字搜索
$dateRange = $request->input('date_range', 'Today'); // 日期范围
// 获取自定义日期范围
$startDate = $request->input('start_date', null); // 开始日期,默认为 null
$endDate = $request->input('end_date', null); // 结束日期,默认为 null
// 你可以进一步验证日期格式(可选)
// if ($startDate && !strtotime($startDate)) {
// return response()->json(['error' => 'Invalid start date format'], 400);
// }
// if ($endDate && !strtotime($endDate)) {
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
// 调用 Service 层查询
$result = $this->googleAdsReportService::getAdList($page, $pageSize, $keyword, $dateRange);
$result = $this->googleAdsReportService::getAdList($page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
return $this->successResponse($result);
}
@ -59,8 +71,20 @@ class AdController
$keyword = $request->input('keyword', ''); // 关键字搜索
$dateRange = $request->input('date_range', 'Today'); // 日期范围
// 获取自定义日期范围
$startDate = $request->input('start_date', null); // 开始日期,默认为 null
$endDate = $request->input('end_date', null); // 结束日期,默认为 null
// 你可以进一步验证日期格式(可选)
// if ($startDate && !strtotime($startDate)) {
// return response()->json(['error' => 'Invalid start date format'], 400);
// }
// if ($endDate && !strtotime($endDate)) {
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
// 调用 Service 层查询
$result = $this->googleAdsReportService::getCampaignList($page, $pageSize, $keyword, $dateRange);
$result = $this->googleAdsReportService::getCampaignList($page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
return $this->successResponse($result);
}
@ -69,8 +93,20 @@ class AdController
$keyword = $request->input('keyword', ''); // 获取关键字参数
$dateRange = $request->input('date_range', ''); // 获取日期范围参数
// 获取自定义日期范围
$startDate = $request->input('start_date', null); // 开始日期,默认为 null
$endDate = $request->input('end_date', null); // 结束日期,默认为 null
// 你可以进一步验证日期格式(可选)
// if ($startDate && !strtotime($startDate)) {
// return response()->json(['error' => 'Invalid start date format'], 400);
// }
// if ($endDate && !strtotime($endDate)) {
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
// 调用 service 层导出数据
return $this->googleAdsReportService::exportAdListToExcel($keyword, $dateRange);
return $this->googleAdsReportService::exportAdListToExcel($keyword, $dateRange, $startDate, $endDate);
}
public function exportCampaignsToExcel(Request $request)
@ -78,8 +114,20 @@ class AdController
$keyword = $request->input('keyword', ''); // 获取关键字参数
$dateRange = $request->input('date_range', ''); // 获取日期范围参数
// 获取自定义日期范围
$startDate = $request->input('start_date', null); // 开始日期,默认为 null
$endDate = $request->input('end_date', null); // 结束日期,默认为 null
// 你可以进一步验证日期格式(可选)
// if ($startDate && !strtotime($startDate)) {
// return response()->json(['error' => 'Invalid start date format'], 400);
// }
// if ($endDate && !strtotime($endDate)) {
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
// 调用 service 层导出数据
return $this->googleAdsReportService::exportCampaignsToExcel($keyword, $dateRange);
return $this->googleAdsReportService::exportCampaignsToExcel($keyword, $dateRange, $startDate, $endDate);
}
public function exportGroupsToExcel(Request $request)
@ -87,8 +135,20 @@ class AdController
$keyword = $request->input('keyword', ''); // 获取关键字参数
$dateRange = $request->input('date_range', ''); // 获取日期范围参数
// 获取自定义日期范围
$startDate = $request->input('start_date', null); // 开始日期,默认为 null
$endDate = $request->input('end_date', null); // 结束日期,默认为 null
// 你可以进一步验证日期格式(可选)
// if ($startDate && !strtotime($startDate)) {
// return response()->json(['error' => 'Invalid start date format'], 400);
// }
// if ($endDate && !strtotime($endDate)) {
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
// 调用 service 层导出数据
return $this->googleAdsReportService::exportAdGroupsToExcel($keyword, $dateRange);
return $this->googleAdsReportService::exportAdGroupsToExcel($keyword, $dateRange, $startDate, $endDate);
}
public function listGroups(Request $request)
@ -99,8 +159,20 @@ class AdController
$keyword = $request->input('keyword', ''); // 关键字搜索
$dateRange = $request->input('date_range', 'Today'); // 日期范围
// 获取自定义日期范围
$startDate = $request->input('start_date', null); // 开始日期,默认为 null
$endDate = $request->input('end_date', null); // 结束日期,默认为 null
// 你可以进一步验证日期格式(可选)
// if ($startDate && !strtotime($startDate)) {
// return response()->json(['error' => 'Invalid start date format'], 400);
// }
// if ($endDate && !strtotime($endDate)) {
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
// 调用 Service 层查询
$result = $this->googleAdsReportService::getAdGroupList($page, $pageSize, $keyword, $dateRange);
$result = $this->googleAdsReportService::getAdGroupList($page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
return $this->successResponse($result);
}

View File

@ -17,7 +17,7 @@ class GoogleAdsReportService
/**
* 获取广告列表
*/
public static function getAdList($page, $pageSize, $keyword, $dateRange)
public static function getAdList($page, $pageSize, $keyword, $dateRange, $startDate = null, $endDate = null)
{
// 基础查询:广告表和日数据表联接
$query = Ad::alias('a')
@ -35,6 +35,10 @@ class GoogleAdsReportService
}
});
// 如果提供了 startDate 和 endDate则根据范围查询
if ($startDate && $endDate) {
$query->whereBetween('d.date', [$startDate, $endDate]);
} else {
// 根据日期维度添加聚合条件
switch ($dateRange) {
case 'Today':
@ -55,6 +59,7 @@ class GoogleAdsReportService
default:
break;
}
}
// 获取分页数据
$ads = $query->paginate($pageSize, false, ['page' => $page]);
@ -75,7 +80,7 @@ class GoogleAdsReportService
* @param string $dateRange
* @return void
*/
public static function exportAdListToExcel($keyword, $dateRange)
public static function exportAdListToExcel($keyword, $dateRange, $startDate = null, $endDate = null)
{
// 获取所有的广告数据
$query = Ad::alias('a')
@ -93,6 +98,11 @@ class GoogleAdsReportService
}
});
// 如果提供了 startDate 和 endDate则根据范围查询
if ($startDate && $endDate) {
$query->whereBetween('d.date', [$startDate, $endDate]);
$dateRange = $startDate. 'to'. $endDate;
} else {
// 根据日期维度添加聚合条件
switch ($dateRange) {
case 'Today':
@ -113,6 +123,7 @@ class GoogleAdsReportService
default:
break;
}
}
// 获取所有广告数据
$ads = $query->select();
@ -192,7 +203,7 @@ class GoogleAdsReportService
/**
* 获取广告系列列表
*/
public static function getCampaignList($page, $pageSize, $keyword, $dateRange)
public static function getCampaignList($page, $pageSize, $keyword, $dateRange, $startDate = null, $endDate = null)
{
// 基础查询:广告活动和日数据表联接
$query = Campaign::alias('c')
@ -208,6 +219,10 @@ class GoogleAdsReportService
}
});
// 如果提供了 startDate 和 endDate则根据范围查询
if ($startDate && $endDate) {
$query->whereBetween('d.date', [$startDate, $endDate]);
} else {
// 根据日期维度添加聚合条件
switch ($dateRange) {
case 'Today':
@ -228,6 +243,7 @@ class GoogleAdsReportService
default:
break;
}
}
// 获取分页数据
$campaigns = $query->paginate($pageSize, false, ['page' => $page]);
@ -249,7 +265,7 @@ class GoogleAdsReportService
* @param string $dateRange
* @return void
*/
public static function exportCampaignsToExcel($keyword, $dateRange)
public static function exportCampaignsToExcel($keyword, $dateRange, $startDate = null, $endDate = null)
{
// 获取所有的广告系列数据
$query = Campaign::alias('c')
@ -265,6 +281,11 @@ class GoogleAdsReportService
}
});
// 如果提供了 startDate 和 endDate则根据范围查询
if ($startDate && $endDate) {
$query->whereBetween('d.date', [$startDate, $endDate]);
$dateRange = $startDate. 'to'. $endDate;
} else {
// 根据日期维度添加聚合条件
switch ($dateRange) {
case 'Today':
@ -285,6 +306,7 @@ class GoogleAdsReportService
default:
break;
}
}
// 获取所有广告系列数据
$campaigns = $query->select();
@ -354,7 +376,7 @@ class GoogleAdsReportService
/**
* 获取广告系列列表
*/
public static function getAdGroupList($page, $pageSize, $keyword, $dateRange)
public static function getAdGroupList($page, $pageSize, $keyword, $dateRange, $startDate = null, $endDate = null)
{
// 初始化查询
$query = AdGroup::alias('ag')
@ -371,7 +393,11 @@ class GoogleAdsReportService
}
});
// 根据日期范围添加聚合条件
// 如果提供了 startDate 和 endDate则根据范围查询
if ($startDate && $endDate) {
$query->whereBetween('d.date', [$startDate, $endDate]);
} else {
// 根据日期维度添加聚合条件
switch ($dateRange) {
case 'Today':
$query->where('d.date', '=', date('Y-m-d'));
@ -391,6 +417,7 @@ class GoogleAdsReportService
default:
break;
}
}
// 分页查询
try {
@ -422,7 +449,7 @@ class GoogleAdsReportService
/**
* 将广告组数据导出到 Excel 文件
*/
public static function exportAdGroupsToExcel($keyword = '', $dateRange = 'Today')
public static function exportAdGroupsToExcel($keyword = '', $dateRange = 'Today', $startDate = null, $endDate = null)
{
// 初始化查询
$query = AdGroup::alias('ag')
@ -439,7 +466,13 @@ class GoogleAdsReportService
}
});
// 根据日期范围添加聚合条件
// 如果提供了 startDate 和 endDate则根据范围查询
if ($startDate && $endDate) {
$query->whereBetween('d.date', [$startDate, $endDate]);
$dateRange = $startDate. 'to'. $endDate;
} else {
// 根据日期维度添加聚合条件
switch ($dateRange) {
case 'Today':
$query->where('d.date', '=', date('Y-m-d'));
@ -459,6 +492,7 @@ class GoogleAdsReportService
default:
break;
}
}
// 获取所有广告组数据
$ad_groups = $query->select();