diff --git a/app/Actions/Dealer/CalculatePurchaseAmountOfCurrentPeriod.php b/app/Actions/Dealer/CalculatePurchaseAmountOfCurrentPeriod.php new file mode 100644 index 00000000..7d2cd309 --- /dev/null +++ b/app/Actions/Dealer/CalculatePurchaseAmountOfCurrentPeriod.php @@ -0,0 +1,47 @@ +day >= 20) { + $startAt = $tz->copy()->setDay(20)->startOfDay(); + } elseif ($tz->day >= 5) { + $startAt = $tz->copy()->setDay(5)->startOfDay(); + } else { + $startAt = $tz->copy()->subMonthNoOverflow()->setDay(20)->startOfDay(); + } + + return Cache::remember($this->prefix($startAt).':'.$dealer->user_id, 3600, function () use ($dealer, $startAt) { + return $this->calculatePurchaseAmount->handle($dealer, $startAt); + }); + } + + /** + * @param \Illuminate\Support\Carbon $tz + * @return string + */ + protected function prefix(Carbon $tz): string + { + return $tz->rawFormat('ymd').'_dealer_purchase_amount'; + } +} diff --git a/app/Endpoint/Api/Http/Controllers/Dealer/FansController.php b/app/Endpoint/Api/Http/Controllers/Dealer/FansController.php index d3e040f2..3d1dcb0a 100644 --- a/app/Endpoint/Api/Http/Controllers/Dealer/FansController.php +++ b/app/Endpoint/Api/Http/Controllers/Dealer/FansController.php @@ -2,9 +2,11 @@ namespace App\Endpoint\Api\Http\Controllers\Dealer; +use App\Actions\Dealer\CalculatePurchaseAmountOfCurrentPeriod; use App\Endpoint\Api\Http\Controllers\Controller; use App\Endpoint\Api\Http\Resources\Dealer\DealerFansResource; use App\Helpers\Paginator as PaginatorHelper; +use App\Models\Dealer; use Illuminate\Http\Request; class FansController extends Controller @@ -13,23 +15,41 @@ class FansController extends Controller * 统计当前信息 * * @param Request $request + * @param CalculatePurchaseAmountOfCurrentPeriod $calculatePurchaseAmountOfCurrentPeriod * */ - public function statistics(Request $request) - { + public function statistics( + Request $request, + CalculatePurchaseAmountOfCurrentPeriod $calculatePurchaseAmountOfCurrentPeriod + ) { + $user = $request->user(); + return response()->json([ - 'fans_num'=>$request->user()->fans()->count(), - 'group_sales_value'=>$request->user()->dealer->team_sales_value, + 'fans_num'=> $user->fans()->count(), + 'total_purchase_amount'=> $calculatePurchaseAmountOfCurrentPeriod->handle($user->dealer), ]); } public function index(Request $request) { $perPage = PaginatorHelper::resolvePerPage('per_page', 20, 50); + $fans = $request->user()->fans() ->with(['userInfo', 'dealer']) ->latest('id') ->simplePaginate($perPage); + return DealerFansResource::collection($fans); } + + public function show( + $id, + CalculatePurchaseAmountOfCurrentPeriod $calculatePurchaseAmountOfCurrentPeriod + ) { + $dealer = Dealer::where('user_id', $id)->firstOrFail(); + + return response()->json([ + 'total_purchase_amount' => $calculatePurchaseAmountOfCurrentPeriod->handle($dealer), + ]); + } } diff --git a/app/Endpoint/Api/Http/Resources/Dealer/DealerFansResource.php b/app/Endpoint/Api/Http/Resources/Dealer/DealerFansResource.php index 6e12fd89..25174055 100644 --- a/app/Endpoint/Api/Http/Resources/Dealer/DealerFansResource.php +++ b/app/Endpoint/Api/Http/Resources/Dealer/DealerFansResource.php @@ -15,6 +15,7 @@ class DealerFansResource extends JsonResource public function toArray($request) { return [ + 'id' => $this->id, 'phone' => $this->phone, 'nickname' => (string) $this->whenLoaded('userInfo', $this->userInfo->nickname, ''), 'avatar' => (string) $this->whenLoaded('userInfo', $this->userInfo->avatar, ''), diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index 1b5f9d0c..7da84fbf 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -229,6 +229,7 @@ Route::group([ //业绩统计 Route::get('fans/statistics', [Dealer\FansController::class, 'statistics']); Route::get('fans', [Dealer\FansController::class, 'index']); + Route::get('fans/{fan}', [Dealer\FansController::class, 'show']); //我的库存 Route::get('user-products', [Dealer\UserProductController::class, 'index']); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index d23dee0a..3bf420c6 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -22,6 +22,7 @@ class AppServiceProvider extends ServiceProvider */ public $singletons = [ \App\Actions\Dealer\CalculatePurchaseAmount::class => \App\Actions\Dealer\CalculatePurchaseAmount::class, + \App\Actions\Dealer\CalculatePurchaseAmountOfCurrentPeriod::class => \App\Actions\Dealer\CalculatePurchaseAmountOfCurrentPeriod::class, ]; /**