| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace App\Services;
- use App\Models\Rebate;
- use App\Models\Rebate as RebateModel;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Collection;
- use App\Support\MemberAccount;
- class RebateService extends BaseService
- {
- public static string $MODEL = Rebate::class;
- /**
- * @description: 获取查询条件
- * @param array $search
- * @return array
- */
- public static function getWhere(array $search = []): array
- {
- $where = [];
- if (isset($search['id']) && !empty($search['id'])) {
- $where[] = ['id', '=', $search['id']];
- }
- if (isset($search['member_id']) && !empty($search['member_id'])) {
- $where[] = ['member_id', '=', $search['member_id']];
- }
- if (isset($search['first_name']) && !empty($search['first_name'])) {
- $where[] = ['first_name', '=', $search['first_name']];
- }
- if (isset($search['username']) && !empty($search['username'])) {
- $where[] = ['username', '=', $search['username']];
- }
- if (isset($search['status']) && $search['status'] != '') {
- $where[] = ['status', '=', $search['status']];
- }
- if (isset($search['date']) && !empty($search['date'])) {
- $where[] = ['date', '=', $search['date']];
- }
- return $where;
- }
- /**
- * @description: 查询单条数据
- * @param array $search
- * @return RebateModel|null
- */
- public static function findOne(array $search): ?Rebate
- {
- return self::$MODEL::where(static::getWhere($search))->first();
- }
- /**
- * @description: 查询所有数据
- * @param array $search
- * @return Collection
- */
- public static function findAll(array $search = []): Collection
- {
- return self::$MODEL::where(self::getWhere($search))->get();
- }
- /**
- * @description: 分页查询
- * @param array $search
- * @return array
- */
- public static function paginate(array $search = []): array
- {
- $limit = $search['limit'] ?? 15;
- // 当天可手动提前发放,部分发放的记录继续累计。
- $today = Carbon::now('America/New_York')->format('Y-m-d');
- $paginator = static::$MODEL::where(self::getWhere($search))
- ->where('date', '<=', $today)
- ->tap(fn($query) => MemberAccount::filter($query, $search['account'] ?? null))
- ->with(['member'])
- ->orderByDesc('date')
- ->orderBy('status')
- ->orderByDesc('betting_amount')
- ->paginate($limit);
- $list = $paginator->items();
- foreach ($list as $item) {
- $item['account'] = (string)($item->member->account ?? '');
- foreach (self::grantFields($item->getAttributes()) as $key => $value) $item[$key] = $value;
- }
- return ['total' => $paginator->total(), 'data' => $list];
- }
- public static function grantFields(array $row): array
- {
- $amount = bcadd((string) ($row['amount'] ?? 0), '0', 2);
- $granted = bcadd((string) ($row['granted_amount'] ?? 0), '0', 2);
- $open = in_array((int) ($row['status'] ?? 0), Rebate::OPEN_STATUSES, true);
- $pending = $open && bccomp($amount, $granted, 2) > 0 ? bcsub($amount, $granted, 2) : '0.00';
- $today = Carbon::now('America/New_York')->format('Y-m-d');
- $date = (string) ($row['date'] ?? '');
- return [
- 'amount' => $amount, 'granted_amount' => $granted, 'pending_amount' => $pending,
- 'can_grant' => $open && $date <= $today && (bccomp($pending, '0', 2) > 0 || $date < $today),
- 'status_text' => [0 => '未发放', 1 => '已发放', 2 => '部分发放'][(int) ($row['status'] ?? 0)] ?? '',
- ];
- }
- //笔笔返 根据某天的有效投注额,和反水比例,进行笔笔返
- public static function BibiReturn(Rebate $rebate, $amount): bool
- {
- // Read the current effective amount under lock rather than paying the supplied stale snapshot.
- return RewardGrantService::grant('rebate', (int) $rebate->id, (string) (request()->user->username ?? 'system'));
- }
- }
|