| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- declare(strict_types=1);
- namespace plugin\saiadmin\app\service\promotion;
- use plugin\saiadmin\app\model\water\WaterPromotionCode;
- use plugin\saiadmin\app\model\water\WaterPromotionReferral;
- use plugin\saiadmin\app\service\WaterBusiness;
- use plugin\saiadmin\exception\ApiException;
- final class PromotionQuery
- {
- public const STATUSES = ['bound' => '待首购', 'pending' => '待签收及审核', 'issued' => '已发放', 'rejected' => '不予发放'];
- public static function issuedBuckets(int $enterprise): int
- {
- return (int) WaterPromotionReferral::query()->where('inviter_id', $enterprise)->where('status', 'issued')->sum('quantity');
- }
- public static function present(WaterPromotionReferral $row, bool $internal = true): array
- {
- return [
- 'id' => (int) $row->id, 'inviter_name' => $row->inviter?->name ?? '已删除企业',
- 'enterprise_name' => $row->invited?->name ?? '已删除企业',
- 'sku_name' => $row->sku?->name ?? '已删除规格', 'quantity' => (int) $row->quantity,
- 'status' => $row->status, 'status_text' => self::STATUSES[$row->status],
- 'order_id' => !$internal || $row->order_id === null ? null : (int) $row->order_id,
- 'reference' => $internal ? $row->order?->reference : null,
- 'amount_cents' => !$internal || $row->purchase_amount_cents === null ? null : (int) $row->purchase_amount_cents,
- 'created_at' => $row->created_at, 'qualified_at' => $row->qualified_at,
- 'reviewed_at' => $row->reviewed_at, 'review_note' => $internal ? $row->review_note : '',
- 'delivery_id' => !$internal || $row->delivery_id === null ? null : (int) $row->delivery_id,
- 'revision' => (int) $row->revision,
- ];
- }
- public function dashboard(array $input): array
- {
- $policy = PromotionPolicy::current();
- $page = WaterBusiness::positive($input['page'] ?? 1, '页码');
- $limit = min(50, WaterBusiness::positive($input['limit'] ?? 20, '每页条数'));
- $status = $input['status'] ?? '';
- if (!is_string($status) || ($status !== '' && !isset(self::STATUSES[$status]))) {
- throw new ApiException('奖励状态不正确');
- }
- if (!$policy['initialized']) {
- return ['policy' => $policy, 'list' => ['data' => [], 'total' => 0], 'statuses' => self::STATUSES];
- }
- $query = WaterPromotionReferral::query()->with(['inviter', 'invited', 'sku', 'order']);
- if ($status !== '') {
- $query->where('status', $status);
- }
- $total = (clone $query)->count();
- $rows = $query->orderByDesc('id')->forPage($page, $limit)->get();
- return ['policy' => $policy, 'list' => [
- 'data' => $rows->map(fn ($row) => self::present($row))->all(), 'total' => $total,
- ], 'statuses' => self::STATUSES];
- }
- public function enterprise(int $enterprise, array $input = []): array
- {
- $policy = PromotionPolicy::current();
- $page = WaterBusiness::positive($input['page'] ?? 1, '页码');
- $base = [
- 'enabled' => $policy['enabled'], 'reason' => $policy['rules'], 'reward_type' => 'water',
- 'total_cents' => null, 'withdrawable_cents' => null, 'demo' => false,
- 'invite_code' => null, 'total_buckets' => 0, 'successful_invites' => 0,
- 'first_order_enterprises' => 0, 'bound' => null, 'items' => [], 'total' => 0,
- 'policy' => $policy,
- ];
- if (!$policy['initialized']) {
- return $base;
- }
- $base['invite_code'] = WaterPromotionCode::query()->where('enterprise_id', $enterprise)->value('code');
- $invites = WaterPromotionReferral::query()->where('inviter_id', $enterprise);
- $base['total_buckets'] = self::issuedBuckets($enterprise);
- $base['successful_invites'] = (clone $invites)->count();
- $base['first_order_enterprises'] = (clone $invites)->whereNotNull('order_id')->count();
- $base['total'] = $base['successful_invites'];
- $base['items'] = $invites->with(['inviter', 'invited', 'sku', 'order'])->orderByDesc('id')
- ->forPage($page, 20)->get()->map(fn ($row) => self::present($row, false))->all();
- $bound = WaterPromotionReferral::query()->with(['inviter', 'invited', 'sku', 'order'])
- ->where('invited_id', $enterprise)->first();
- $base['bound'] = $bound ? self::present($bound, false) : null;
- return $base;
- }
- }
|