Files
sam-api/app/Http/Controllers/Api/V1/BarobillUsageController.php

97 lines
3.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Api\V1;
use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\Barobill\BarobillMember;
use App\Services\Barobill\BarobillUsageService;
use Carbon\Carbon;
use Illuminate\Http\Request;
class BarobillUsageController extends Controller
{
public function __construct(
private BarobillUsageService $usageService,
) {}
/**
* 사용량 목록
*/
public function index(Request $request)
{
$data = $request->validate([
'start_date' => 'nullable|date_format:Y-m-d',
'end_date' => 'nullable|date_format:Y-m-d',
'tenant_id' => 'nullable|integer',
]);
return ApiResponse::handle(function () use ($data) {
$startDate = $data['start_date'] ?? Carbon::now()->startOfMonth()->format('Y-m-d');
$endDate = $data['end_date'] ?? Carbon::now()->format('Y-m-d');
$tenantId = $data['tenant_id'] ?? null;
$usageList = $this->usageService->getUsageList($startDate, $endDate, $tenantId);
return [
'data' => $usageList,
'stats' => $this->usageService->aggregateStats($usageList),
'meta' => [
'start_date' => $startDate,
'end_date' => $endDate,
],
];
}, __('message.fetched'));
}
/**
* 사용량 통계
*/
public function stats(Request $request)
{
$data = $request->validate([
'start_date' => 'nullable|date_format:Y-m-d',
'end_date' => 'nullable|date_format:Y-m-d',
'tenant_id' => 'nullable|integer',
]);
return ApiResponse::handle(function () use ($data) {
$startDate = $data['start_date'] ?? Carbon::now()->startOfMonth()->format('Y-m-d');
$endDate = $data['end_date'] ?? Carbon::now()->format('Y-m-d');
$usageList = $this->usageService->getUsageList($startDate, $endDate, $data['tenant_id'] ?? null);
return $this->usageService->aggregateStats($usageList);
}, __('message.fetched'));
}
/**
* 회원별 사용량 상세
*/
public function show(Request $request, int $memberId)
{
$data = $request->validate([
'start_date' => 'nullable|date_format:Y-m-d',
'end_date' => 'nullable|date_format:Y-m-d',
]);
return ApiResponse::handle(function () use ($memberId, $data) {
$member = BarobillMember::withoutGlobalScopes()->findOrFail($memberId);
$startDate = $data['start_date'] ?? Carbon::now()->startOfMonth()->format('Y-m-d');
$endDate = $data['end_date'] ?? Carbon::now()->format('Y-m-d');
return $this->usageService->getMemberUsage($member, $startDate, $endDate);
}, __('message.fetched'));
}
/**
* 과금 정책 정보
*/
public function priceInfo()
{
return ApiResponse::handle(function () {
return ['prices' => BarobillUsageService::getPriceInfo()];
}, __('message.fetched'));
}
}