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'); $limit = isset($search['limit']) ? $search['limit'] : 15; $paginator = self::$MODEL::where(self::getWhere($search)) // ->where('date', '<', $date) ->orderByDesc('date') ->orderBy('status') ->paginate($limit); return ['total' => $paginator->total(), 'data' => $paginator->items()]; } }