| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?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));
- // ->where('date', '<', $date)
- if (isset($search['username']) && !empty($search['username'])) {
- $username = $search['username'];
- $query->whereHas('member', function ($query) use ($username) {
- $query->where('username', $username);
- });
- }
- $query->with(['member'])
- ->orderByDesc('date')
- ->orderBy('status');
- // $paginator = $query->paginate($limit);
- $total = $query->count();
- $list = $query->forPage($search['page'], $search['limit'])->get()->toArray();
- return ['total' => $total, 'data' => $list];
- }
- }
|