first(); } /** * 进行回水操作 * @param $id * @return bool * @throws Exception */ public static function grant($id): bool { return RewardGrantService::grant('backflow', (int) $id, (string) (request()->user->username ?? 'system')); } /** * 只记录当月充值/提现,回水金额由 rewards:sync-bases 按分类亏损和规则计算。 * @param $memberId string 会员ID * @param $changeAmount float 充值或提现金额 * @return Backflow */ public static function updateOrCreate(string $memberId, float $changeAmount): Backflow { $date = Carbon::now('Asia/Shanghai')->format('Y-m'); $field = $changeAmount > 0 ? 'recharge_amount' : 'withdrawal_amount'; $backflow = static::$MODEL::firstOrCreate(['date' => $date, 'member_id' => $memberId]); if ((int) $backflow->status === 0) $backflow->increment($field, $changeAmount); return $backflow->refresh(); } public static function getWhere(array $search = []): array { $where = []; if (isset($search['id']) && !empty($search['id'])) { $where[] = ['backflow.id', '=', $search['id']]; } if (isset($search['member_id']) && !empty($search['member_id'])) { $where[] = ['backflow.member_id', '=', $search['member_id']]; } if (isset($search['date']) && !empty($search['date'])) { $where[] = ['backflow.date', '=', $search['date']]; } if (isset($search['status']) && $search['status'] != '') { $where[] = ['backflow.status', '=', $search['status']]; } if (!empty($search['first_name'])) { $where[] = ['users.first_name','like', '%'.$search['first_name'].'%']; } return $where; } public static function paginate(array $search = []): array { $date = Carbon::now('Asia/Shanghai')->format('Y-m'); $query = static::$MODEL::join('users', 'users.member_id', '=', 'backflow.member_id') ->where(static::getWhere($search)) ->where('backflow.date', '<', $date); $paginator = $query->select("backflow.*", "users.first_name","users.admin_note as user_admin_note")->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()]; } }