add group by feature to datatable

This commit is contained in:
2025-04-15 10:45:47 +03:30
parent 6f896f4894
commit 8d138b8339
6 changed files with 91 additions and 129 deletions

View File

@@ -2,17 +2,22 @@
namespace App\Services\Cartables;
use App\Exceptions\ProhibitedAction;
use App\Facades\DataTable\DataTableFacade;
use App\Http\Traits\ApiResponse;
use App\Models\Province;
use App\Models\SafetyAndPrivacy;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Throwable;
class SafetyAndPrivacyTableService
{
use ApiResponse;
/**
* @throws Throwable
*/
public function dataTable(Request $request, $loadRelations = false)
{
$user = auth()->user();
@@ -27,16 +32,12 @@ class SafetyAndPrivacyTableService
$query = SafetyAndPrivacy::query()->select($fields);
}
elseif ($user->hasPermissionTo('show-safety-and-privacy-operator-cartable-province')) {
if (is_null($user->province_id)) {
return $this->errorResponse('استانی برای شما در سامانه ثبت نشده است!');
}
throw_if(is_null($user->province_id), new ProhibitedAction('استانی برای شما در سامانه ثبت نشده است!'));
$query = SafetyAndPrivacy::query()->where('province_id', '=', $user->province_id)->select($fields);
}
elseif ($user->hasPermissionTo('show-safety-and-privacy-operator-cartable-edarate-shahri')) {
if (is_null($user->edarate_shahri_id)) {
return $this->errorResponse('اداره ای برای شما در سامانه ثبت نشده است!');
}
throw_if(is_null($user->edarate_shahri_id), new ProhibitedAction('اداره ای برای شما در سامانه ثبت نشده است!'));
$query = SafetyAndPrivacy::query()->where('edare_shahri_id', '=', $user->edarate_shahri_id);
}
@@ -51,74 +52,45 @@ class SafetyAndPrivacyTableService
public function countryActivity(Request $request): array
{
$fromDate = $request->from_date ? $request->from_date . ' 00:00:00' : now()->startOfDay()->toDateTimeString();
$toDate = $request->date_to ? $request->date_to . ' 23:59:59' : now()->endOfDay()->toDateTimeString();
$axisTypeId = $request->axis_type_id;
$activities = [];
$countryQuery = SafetyAndPrivacy::query()->selectRaw('COUNT(*) as qty,info_id, step');
$country = SafetyAndPrivacy::query()
->selectRaw('COUNT(*) as qty,info_id, step')
->whereBetween('created_at', [$fromDate, $toDate])
->when($axisTypeId, function ($query) use ($axisTypeId) {
$query->where('axis_type_id', '=', $axisTypeId);
})
->groupBy('info_id', 'step')
->get();
// $country = DB::select(
// "SELECT COUNT(*) as qty,info_id, step
// FROM safety_and_privacy where created_at BETWEEN '".$fromDate."' and '".$toDate."'
// GROUP BY info_id,step"
// );
$country = DataTableFacade::run(
$countryQuery,
$request,
allowedFilters: ['*'],
allowedSortings: ['*'],
allowedGroupBy: ['info_id', 'step']
);
$countryTotalSum = 0;
$countryData = [
'name_fa' => 'کشوری',
'province_id' => -1,
'sum' => 0,
'89' => [
'1' => 0,
'2' => 0,
'3' => 0,
],
'90' => [
'1' => 0,
'2' => 0,
'3' => 0,
],
'91' => [
'1' => 0,
'2' => 0,
'3' => 0,
],
'89' => ['1' => 0, '2' => 0, '3' => 0,],
'90' => ['1' => 0, '2' => 0, '3' => 0,],
'91' => ['1' => 0, '2' => 0, '3' => 0,],
];
foreach ($country as $key => $value) {
$countryTotalSum += $value->qty;
$countryData[$value->info_id][$value->step] = $value->qty;
$countryData["sum"] = $countryTotalSum;
}
$activities[] = (object) $countryData;
$provinceQuery = Province::query()->selectRaw('province_id, province_fa, COUNT(*) as qty,info_id, step');
$provinces = DataTableFacade::run(
$provinceQuery,
$request,
allowedFilters: ['*'],
allowedSortings: ['*'],
allowedGroupBy: ['info_id', 'step', 'province_id', 'province_fa']
);
$provinces = SafetyAndPrivacy::query()
->selectRaw('province_id, province_fa, COUNT(*) as qty,info_id, step')
->whereBetween('created_at', [$fromDate, $toDate])
->when($axisTypeId, function ($query) use ($axisTypeId) {
$query->where('axis_type_id', '=', $axisTypeId);
})
->groupBy('info_id', 'step', 'province_id', 'province_fa')
->get();
// $provinces = DB::select(
// "SELECT province_id, province_fa, COUNT(*) as qty,info_id, step
// FROM safety_and_privacy where created_at BETWEEN '".$fromDate."' and '".$toDate."'
// GROUP BY info_id,step,province_id,province_fa"
// );
foreach ($provinces as $value) {
foreach ($provinces as $value)
{
$provinceId = $value->province_id;
// Check if the province already exists in the activities array
if (!isset($activities[$provinceId])) {
$activities[$provinceId] = (object) [
'province_id' => $provinceId,
@@ -129,64 +101,33 @@ class SafetyAndPrivacyTableService
'91' => ['1' => 0, '2' => 0, '3' => 0],
];
}
$activities[$provinceId]->{$value->info_id}[$value->step] = $value->qty;
$activities[$provinceId]->sum += $value->qty;
}
$activities = array_values($activities);
return [
'activities' => $activities,
'fromDate' => $fromDate,
'toDate' => $toDate,
];
return array_values($activities);
}
public function provinceActivity(Request $request): array
{
$fromDate = $request->from_date ? $request->from_date . ' 00:00:00' : now()->startOfDay()->toDateTimeString();
$toDate = $request->date_to ? $request->date_to . ' 23:59:59' : now()->endOfDay()->toDateTimeString();
$provinceId = $request->province_id;
$axisTypeId = $request->axis_type_id;
$activities = [];
$province = SafetyAndPrivacy::query()
->selectRaw('COUNT(*) as qty,info_id, step')
->where('province_id', '=', $provinceId)
->whereBetween('created_at', [$fromDate, $toDate])
->when($axisTypeId, function ($query) use ($axisTypeId) {
$query->where('axis_type_id', '=', $axisTypeId);
})
->groupBy('info_id', 'step')
->get();
// $province = DB::select('
// SELECT COUNT(*) as qty,info_id, step
// FROM safety_and_privacy where province_id ='.$provinceId.' and created_at BETWEEN "'.$fromDate.'" and "'.$toDate.'"
// GROUP BY info_id,step
// ');
$provinceQuery = Province::query()->selectRaw('COUNT(*) as qty,info_id, step');
$province = DataTableFacade::run(
$provinceQuery,
$request,
allowedFilters: ['*'],
allowedSortings: ['*'],
allowedGroupBy: ['info_id', 'step']
);
$provinceTotalSum = 0;
$provinceData = [
'name_fa' => 'استانی',
'edare_shahri_id' => -1,
'sum' => 0,
'89' => [
'1' => 0,
'2' => 0,
'3' => 0,
],
'90' => [
'1' => 0,
'2' => 0,
'3' => 0,
],
'91' => [
'1' => 0,
'2' => 0,
'3' => 0,
],
'89' => ['1' => 0, '2' => 0, '3' => 0,],
'90' => ['1' => 0, '2' => 0, '3' => 0,],
'91' => ['1' => 0, '2' => 0, '3' => 0,],
];
foreach ($province as $key => $value) {
$provinceTotalSum += $value->qty;
@@ -195,26 +136,18 @@ class SafetyAndPrivacyTableService
}
$activities[] = (object) $provinceData;
$cities = SafetyAndPrivacy::query()
->selectRaw('edare_shahri_id, COUNT(*) as qty,info_id, step')
->where('province_id', '=', $provinceId)
->whereBetween('created_at', [$fromDate, $toDate])
->when($axisTypeId, function ($query) use ($axisTypeId) {
$query->where('axis_type_id', '=', $axisTypeId);
})
->groupBy('info_id', 'step', 'edare_shahri_id')
->get();
$citiesQuery = SafetyAndPrivacy::query()->selectRaw('edare_shahri_id, COUNT(*) as qty,info_id, step');
$cities = DataTableFacade::run(
$citiesQuery,
$request,
allowedFilters: ['*'],
allowedSortings: ['*'],
allowedGroupBy: ['info_id', 'step', 'edare_shahri_id']
);
// $cities = DB::select('
// SELECT edare_shahri_id, COUNT(*) as qty,info_id, step
// FROM safety_and_privacy where province_id ='.$provinceId.' and created_at BETWEEN "'.$fromDate.'" and "'.$toDate.'"
// GROUP BY info_id,step,edare_shahri_id
// ');
foreach ($cities as $value) {
foreach ($cities as $value)
{
$cityId = $value->edare_shahri_id;
// Initialize data for each unique city
if (!isset($activities[$cityId])) {
$activities[$cityId] = (object) [
'edare_shahri_id' => $cityId,
@@ -229,12 +162,6 @@ class SafetyAndPrivacyTableService
$activities[$cityId]->sum += $value->qty;
}
$activities = array_values($activities);
return [
'activities' => $activities,
'fromDate' => $fromDate,
'toDate' => $toDate,
];
return array_values($activities);
}
}