public static function apply(User $user, UserBank $userBank, $money = 0) { if (!$money) { return false; } return DB::transaction(function () use($user, $userBank, $money) { $sn = 'TX' . date('ymdHis') . rand(1000, 9999); $sn .= strtoupper(baseEncode($sn)); $userWithdraw = self::saveData(['user_id' => $user->id, 'username' => $user->username, 'money' => $money, 'user_bank_id' => $userBank->id, 'bank_id' => $userBank->bank_id, 'sn' => $sn, 'status' => 1]); if (!$userWithdraw) { throw new Exception("申请失败", 1); } $cashFlow = CashFlow::userCashesOut($user, $userWithdraw, $money, 1, '提现'); if (!$cashFlow) { throw new Exception('写入流水失败'); } if (!$user->decreaseCash($money)) { throw new Exception('扣除余额失败'); } return true; }); }