| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Services;
- use App\Models\Backflow;
- use App\Models\Config;
- use Carbon\Carbon;
- class BackflowService extends BaseService
- {
- public static string $MODEL = Backflow::class;
- /**
- * @param $memberId string 会员ID
- * @param $changeAmount float 充值或提现金额
- * @return Backflow
- */
- public static function updateOrCreate(string $memberId, float $changeAmount): Backflow
- {
- $data['date'] = Carbon::now('Asia/Shanghai')->format('Y-m');
- $data['backflow_ratio'] = Config::where('field', 'huishui_percentage')->first()->val;
- $data['member_id'] = $memberId;
- if ($changeAmount > 0) $data['recharge_amount'] = $changeAmount;
- else $data['withdrawal_amount'] = $changeAmount;
- $backflow = static::$MODEL::where('date', $data['date'])
- ->where('member_id', $memberId)->first();
- if ($backflow) {
- if ($changeAmount > 0) $field = "recharge_amount";
- else $field = 'withdrawal_amount';
- $backflow->backflow_ratio = $data['backflow_ratio'];
- $backflow->increment($field, $changeAmount);
- $backflow->save();
- } else {
- $backflow = static::$MODEL::create($data);
- }
- $restriction = Config::where('field', 'huishui_restriction')->first()->val;
- $difference = bcadd($backflow->recharge_amount, $backflow->withdrawal_amount, 2);
- $difference = abs($difference);
- if ($difference >= $restriction) {
- $backflow->amount = bcmul($difference, $backflow->backflow_ratio, 2);
- } else {
- $backflow->amount = 0;
- }
- $backflow->save();
- return $backflow;
- }
- 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['date']) && !empty($search['date'])) {
- $where[] = ['date', '=', $search['date']];
- }
- if (isset($search['status']) && $search['status'] != '') {
- $where[] = ['status', '=', $search['status']];
- }
- return $where;
- }
- public static function paginate(array $search = []): array
- {
- $date = Carbon::now('Asia/Shanghai')->format('Y-m');
- $query = static::$MODEL::where(static::getWhere($search));
- $query->where('date', '<', $date);
- if (isset($search['username']) && !empty($search['username'])) {
- $username = $search['username'];
- $query = $query->whereHas('user', function ($query1) use ($username) {
- $query1->where('username', $username);
- });
- }
- $paginator = $query
- ->with(['user'])->orderByDesc('date')->orderBy('status')
- ->paginate($search['limit']);
- // $count = $query->count();
- // $query->with(['user'])->orderByDesc('date')->orderBy('status');
- // $list = $query->forPage($search['page'], $search['limit'])->get()->toArray();
- // return ['total' => $count, 'data' => $list];
- return ['total' => $paginator->total(), 'data' => $paginator->items()];
- }
- }
|