| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- namespace App\Module\OpenAPI\Services;
- use App\Module\OpenAPI\Models\OpenApiApp;
- use App\Module\Fund\Services\FundService;
- use App\Module\Fund\Enums\FUND_TYPE;
- use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
- use App\Module\User\Models\User;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 开发者账户管理服务
- *
- * 为每个开发者应用分配专用的充值和提取账户
- */
- class DeveloperAccountService
- {
- /**
- * 开发者充值账户用户ID起始值
- */
- const RECHARGE_USER_ID_START = 100000;
-
- /**
- * 开发者提取账户用户ID起始值
- */
- const WITHDRAW_USER_ID_START = 200000;
- /**
- * 获取开发者充值账户用户ID
- *
- * @param int $appId 应用ID
- * @return int
- */
- public static function getRechargeUserId(int $appId): int
- {
- return self::RECHARGE_USER_ID_START + $appId;
- }
- /**
- * 获取开发者提取账户用户ID
- *
- * @param int $appId 应用ID
- * @return int
- */
- public static function getWithdrawUserId(int $appId): int
- {
- return self::WITHDRAW_USER_ID_START + $appId;
- }
- /**
- * 为开发者应用创建专用账户
- *
- * @param OpenApiApp $app 应用对象
- * @return array 返回创建结果
- */
- public static function createDeveloperAccounts(OpenApiApp $app): array
- {
- $results = [];
-
- try {
- DB::beginTransaction();
-
- // 创建充值账户
- $rechargeResult = self::createRechargeAccount($app);
- $results['recharge'] = $rechargeResult;
-
- // 创建提取账户
- $withdrawResult = self::createWithdrawAccount($app);
- $results['withdraw'] = $withdrawResult;
-
- DB::commit();
-
- Log::info('开发者账户创建成功', [
- 'app_id' => $app->id,
- 'app_name' => $app->name,
- 'recharge_user_id' => $rechargeResult['user_id'],
- 'withdraw_user_id' => $withdrawResult['user_id'],
- ]);
-
- return [
- 'success' => true,
- 'message' => '开发者账户创建成功',
- 'data' => $results
- ];
-
- } catch (\Exception $e) {
- DB::rollBack();
-
- Log::error('开发者账户创建失败', [
- 'app_id' => $app->id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return [
- 'success' => false,
- 'message' => '开发者账户创建失败: ' . $e->getMessage(),
- 'data' => null
- ];
- }
- }
- /**
- * 创建充值专用账户
- *
- * @param OpenApiApp $app 应用对象
- * @return array
- */
- private static function createRechargeAccount(OpenApiApp $app): array
- {
- $userId = self::getRechargeUserId($app->id);
-
- // 创建用户记录(如果不存在)
- $user = self::createUserIfNotExists($userId, "充值账户-{$app->name}", 'recharge');
-
- // 创建钻石账户
- $fundService = new FundService($userId, FUND_TYPE::FUND2->value);
- $account = $fundService->getAccount();
-
- if (!$account) {
- // 如果账户不存在,初始化账户
- $fundService->admin(FUND_TYPE::FUND2, 0, 1, '初始化开发者充值账户');
- }
-
- return [
- 'user_id' => $userId,
- 'username' => $user->username,
- 'fund_type' => FUND_TYPE::FUND2->value,
- 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value,
- 'purpose' => 'recharge'
- ];
- }
- /**
- * 创建提取专用账户
- *
- * @param OpenApiApp $app 应用对象
- * @return array
- */
- private static function createWithdrawAccount(OpenApiApp $app): array
- {
- $userId = self::getWithdrawUserId($app->id);
-
- // 创建用户记录(如果不存在)
- $user = self::createUserIfNotExists($userId, "提取账户-{$app->name}", 'withdraw');
-
- // 创建钻石账户
- $fundService = new FundService($userId, FUND_TYPE::FUND2->value);
- $account = $fundService->getAccount();
-
- if (!$account) {
- // 如果账户不存在,初始化账户
- $fundService->admin(FUND_TYPE::FUND2, 0, 1, '初始化开发者提取账户');
- }
-
- return [
- 'user_id' => $userId,
- 'username' => $user->username,
- 'fund_type' => FUND_TYPE::FUND2->value,
- 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value,
- 'purpose' => 'withdraw'
- ];
- }
- /**
- * 创建用户记录(如果不存在)
- *
- * @param int $userId 用户ID
- * @param string $username 用户名
- * @param string $type 账户类型
- * @return User
- */
- private static function createUserIfNotExists(int $userId, string $username, string $type): User
- {
- $user = User::find($userId);
-
- if (!$user) {
- $user = new User();
- $user->id = $userId;
- $user->username = $username;
- $user->email = "{$type}_{$userId}@openapi.system";
- $user->password = bcrypt('system_account_' . time());
- $user->status = 1; // 激活状态
- $user->created_at = now();
- $user->updated_at = now();
- $user->save();
- }
-
- return $user;
- }
- /**
- * 获取开发者账户信息
- *
- * @param OpenApiApp $app 应用对象
- * @return array
- */
- public static function getDeveloperAccountInfo(OpenApiApp $app): array
- {
- $rechargeUserId = self::getRechargeUserId($app->id);
- $withdrawUserId = self::getWithdrawUserId($app->id);
-
- // 获取充值账户信息
- $rechargeFundService = new FundService($rechargeUserId, FUND_TYPE::FUND2->value);
- $rechargeAccount = $rechargeFundService->getAccount();
-
- // 获取提取账户信息
- $withdrawFundService = new FundService($withdrawUserId, FUND_TYPE::FUND2->value);
- $withdrawAccount = $withdrawFundService->getAccount();
-
- return [
- 'app_id' => $app->id,
- 'app_name' => $app->name,
- 'recharge_account' => [
- 'user_id' => $rechargeUserId,
- 'balance' => $rechargeAccount ? $rechargeAccount->balance : 0,
- 'fund_type' => FUND_TYPE::FUND2->value,
- 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value,
- ],
- 'withdraw_account' => [
- 'user_id' => $withdrawUserId,
- 'balance' => $withdrawAccount ? $withdrawAccount->balance : 0,
- 'fund_type' => FUND_TYPE::FUND2->value,
- 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value,
- ]
- ];
- }
- /**
- * 检查开发者账户是否存在
- *
- * @param OpenApiApp $app 应用对象
- * @return array
- */
- public static function checkDeveloperAccounts(OpenApiApp $app): array
- {
- $rechargeUserId = self::getRechargeUserId($app->id);
- $withdrawUserId = self::getWithdrawUserId($app->id);
-
- $rechargeUser = User::find($rechargeUserId);
- $withdrawUser = User::find($withdrawUserId);
-
- $rechargeFundService = new FundService($rechargeUserId, FUND_TYPE::FUND2->value);
- $withdrawFundService = new FundService($withdrawUserId, FUND_TYPE::FUND2->value);
-
- return [
- 'recharge_account_exists' => $rechargeUser !== null && $rechargeFundService->getAccount() !== null,
- 'withdraw_account_exists' => $withdrawUser !== null && $withdrawFundService->getAccount() !== null,
- 'recharge_user_id' => $rechargeUserId,
- 'withdraw_user_id' => $withdrawUserId,
- ];
- }
- }
|