You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.3 KiB
69 lines
2.3 KiB
<?php |
|
|
|
namespace App\Repositories; |
|
|
|
use App\Models\ViolationParking; |
|
use Illuminate\Support\Facades\DB; |
|
|
|
class ViolationParkingRepository |
|
{ |
|
public function buildQuery(array $filters) |
|
{ |
|
$query = ViolationParking::query(); |
|
|
|
if (!empty($filters['device'])) { |
|
$query->whereIn('serialnumber', $filters['device']); |
|
} |
|
|
|
if (!empty($filters['searchByFromdate'])) { |
|
$query->where('datatime', '>=', $filters['searchByFromdate'] . ' 00:00:00'); |
|
} |
|
|
|
if (!empty($filters['searchByTodate'])) { |
|
$query->where('datatime', '<=', $filters['searchByTodate'] . ' 23:59:59'); |
|
} |
|
|
|
if (!empty($filters['location'])) { |
|
$query->where('serialnumber', $filters['location']); |
|
} |
|
|
|
if (!empty($filters['carnumber'])) { |
|
$query->where('carnumber', 'like', '%' . $filters['carnumber'] . '%'); |
|
} |
|
|
|
if (!empty($filters['unreportreason'])) { |
|
$query->where('unreportreason', 'like', '%' . $filters['unreportreason'] . '%'); |
|
} |
|
|
|
if (!empty($filters['processcheckStatus'])) { |
|
$status = $filters['processcheckStatus']; |
|
if ($status == 99) { |
|
$query->whereIn('processcheck', [0, 1, 2]); |
|
} else { |
|
$query->where('processcheck', $status); |
|
} |
|
} |
|
|
|
// 排除違規臨時停車 |
|
$query->where('violationtype', '!=', '違規臨時停車'); |
|
|
|
if (!empty($filters['statisticstype'])) { |
|
switch ($filters['statisticstype']) { |
|
case 2: |
|
$query->groupBy('serialnumber', 'cartype') |
|
->select('*', DB::raw('count(*) as count')); |
|
break; |
|
case 3: |
|
$query->groupBy(DB::raw("CAST(DATE_FORMAT(datatime, '%H') AS SIGNED)"), 'serialnumber', 'violationtype') |
|
->select('*', DB::raw("COUNT(*) as count, CONCAT(CAST(DATE_FORMAT(datatime,'%H') as SIGNED),'-',CAST(DATE_FORMAT(DATE_ADD(datatime,INTERVAL +1 HOUR),'%H')) as SIGNED) AS period")) |
|
->orderBy(DB::raw("CAST(DATE_FORMAT(datatime, '%H') AS SIGNED)"), 'ASC'); |
|
break; |
|
default: |
|
$query->select('*'); |
|
break; |
|
} |
|
} |
|
|
|
return $query; |
|
} |
|
}
|
|
|