DeveloperAccountService.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace App\Module\OpenAPI\Services;
  3. use App\Module\OpenAPI\Models\OpenApiApp;
  4. use App\Module\Fund\Services\FundService;
  5. use App\Module\Fund\Enums\FUND_TYPE;
  6. use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
  7. use App\Module\User\Models\User;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. /**
  11. * 开发者账户管理服务
  12. *
  13. * 为每个开发者应用分配专用的充值和提取账户
  14. */
  15. class DeveloperAccountService
  16. {
  17. /**
  18. * 开发者充值账户用户ID起始值
  19. */
  20. const RECHARGE_USER_ID_START = 100000;
  21. /**
  22. * 开发者提取账户用户ID起始值
  23. */
  24. const WITHDRAW_USER_ID_START = 200000;
  25. /**
  26. * 获取开发者充值账户用户ID
  27. *
  28. * @param int $appId 应用ID
  29. * @return int
  30. */
  31. public static function getRechargeUserId(int $appId): int
  32. {
  33. return self::RECHARGE_USER_ID_START + $appId;
  34. }
  35. /**
  36. * 获取开发者提取账户用户ID
  37. *
  38. * @param int $appId 应用ID
  39. * @return int
  40. */
  41. public static function getWithdrawUserId(int $appId): int
  42. {
  43. return self::WITHDRAW_USER_ID_START + $appId;
  44. }
  45. /**
  46. * 为开发者应用创建专用账户
  47. *
  48. * @param OpenApiApp $app 应用对象
  49. * @return array 返回创建结果
  50. */
  51. public static function createDeveloperAccounts(OpenApiApp $app): array
  52. {
  53. $results = [];
  54. try {
  55. DB::beginTransaction();
  56. // 创建充值账户
  57. $rechargeResult = self::createRechargeAccount($app);
  58. $results['recharge'] = $rechargeResult;
  59. // 创建提取账户
  60. $withdrawResult = self::createWithdrawAccount($app);
  61. $results['withdraw'] = $withdrawResult;
  62. DB::commit();
  63. Log::info('开发者账户创建成功', [
  64. 'app_id' => $app->id,
  65. 'app_name' => $app->name,
  66. 'recharge_user_id' => $rechargeResult['user_id'],
  67. 'withdraw_user_id' => $withdrawResult['user_id'],
  68. ]);
  69. return [
  70. 'success' => true,
  71. 'message' => '开发者账户创建成功',
  72. 'data' => $results
  73. ];
  74. } catch (\Exception $e) {
  75. DB::rollBack();
  76. Log::error('开发者账户创建失败', [
  77. 'app_id' => $app->id,
  78. 'error' => $e->getMessage(),
  79. 'trace' => $e->getTraceAsString()
  80. ]);
  81. return [
  82. 'success' => false,
  83. 'message' => '开发者账户创建失败: ' . $e->getMessage(),
  84. 'data' => null
  85. ];
  86. }
  87. }
  88. /**
  89. * 创建充值专用账户
  90. *
  91. * @param OpenApiApp $app 应用对象
  92. * @return array
  93. */
  94. private static function createRechargeAccount(OpenApiApp $app): array
  95. {
  96. $userId = self::getRechargeUserId($app->id);
  97. // 创建用户记录(如果不存在)
  98. $user = self::createUserIfNotExists($userId, "充值账户-{$app->name}", 'recharge');
  99. // 创建钻石账户
  100. $fundService = new FundService($userId, FUND_TYPE::FUND2->value);
  101. $account = $fundService->getAccount();
  102. if (!$account) {
  103. // 如果账户不存在,初始化账户
  104. $fundService->admin(FUND_TYPE::FUND2, 0, 1, '初始化开发者充值账户');
  105. }
  106. return [
  107. 'user_id' => $userId,
  108. 'username' => $user->username,
  109. 'fund_type' => FUND_TYPE::FUND2->value,
  110. 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value,
  111. 'purpose' => 'recharge'
  112. ];
  113. }
  114. /**
  115. * 创建提取专用账户
  116. *
  117. * @param OpenApiApp $app 应用对象
  118. * @return array
  119. */
  120. private static function createWithdrawAccount(OpenApiApp $app): array
  121. {
  122. $userId = self::getWithdrawUserId($app->id);
  123. // 创建用户记录(如果不存在)
  124. $user = self::createUserIfNotExists($userId, "提取账户-{$app->name}", 'withdraw');
  125. // 创建钻石账户
  126. $fundService = new FundService($userId, FUND_TYPE::FUND2->value);
  127. $account = $fundService->getAccount();
  128. if (!$account) {
  129. // 如果账户不存在,初始化账户
  130. $fundService->admin(FUND_TYPE::FUND2, 0, 1, '初始化开发者提取账户');
  131. }
  132. return [
  133. 'user_id' => $userId,
  134. 'username' => $user->username,
  135. 'fund_type' => FUND_TYPE::FUND2->value,
  136. 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value,
  137. 'purpose' => 'withdraw'
  138. ];
  139. }
  140. /**
  141. * 创建用户记录(如果不存在)
  142. *
  143. * @param int $userId 用户ID
  144. * @param string $username 用户名
  145. * @param string $type 账户类型
  146. * @return User
  147. */
  148. private static function createUserIfNotExists(int $userId, string $username, string $type): User
  149. {
  150. $user = User::find($userId);
  151. if (!$user) {
  152. $user = new User();
  153. $user->id = $userId;
  154. $user->username = $username;
  155. $user->email = "{$type}_{$userId}@openapi.system";
  156. $user->password = bcrypt('system_account_' . time());
  157. $user->status = 1; // 激活状态
  158. $user->created_at = now();
  159. $user->updated_at = now();
  160. $user->save();
  161. }
  162. return $user;
  163. }
  164. /**
  165. * 获取开发者账户信息
  166. *
  167. * @param OpenApiApp $app 应用对象
  168. * @return array
  169. */
  170. public static function getDeveloperAccountInfo(OpenApiApp $app): array
  171. {
  172. $rechargeUserId = self::getRechargeUserId($app->id);
  173. $withdrawUserId = self::getWithdrawUserId($app->id);
  174. // 获取充值账户信息
  175. $rechargeFundService = new FundService($rechargeUserId, FUND_TYPE::FUND2->value);
  176. $rechargeAccount = $rechargeFundService->getAccount();
  177. // 获取提取账户信息
  178. $withdrawFundService = new FundService($withdrawUserId, FUND_TYPE::FUND2->value);
  179. $withdrawAccount = $withdrawFundService->getAccount();
  180. return [
  181. 'app_id' => $app->id,
  182. 'app_name' => $app->name,
  183. 'recharge_account' => [
  184. 'user_id' => $rechargeUserId,
  185. 'balance' => $rechargeAccount ? $rechargeAccount->balance : 0,
  186. 'fund_type' => FUND_TYPE::FUND2->value,
  187. 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value,
  188. ],
  189. 'withdraw_account' => [
  190. 'user_id' => $withdrawUserId,
  191. 'balance' => $withdrawAccount ? $withdrawAccount->balance : 0,
  192. 'fund_type' => FUND_TYPE::FUND2->value,
  193. 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value,
  194. ]
  195. ];
  196. }
  197. /**
  198. * 检查开发者账户是否存在
  199. *
  200. * @param OpenApiApp $app 应用对象
  201. * @return array
  202. */
  203. public static function checkDeveloperAccounts(OpenApiApp $app): array
  204. {
  205. $rechargeUserId = self::getRechargeUserId($app->id);
  206. $withdrawUserId = self::getWithdrawUserId($app->id);
  207. $rechargeUser = User::find($rechargeUserId);
  208. $withdrawUser = User::find($withdrawUserId);
  209. $rechargeFundService = new FundService($rechargeUserId, FUND_TYPE::FUND2->value);
  210. $withdrawFundService = new FundService($withdrawUserId, FUND_TYPE::FUND2->value);
  211. return [
  212. 'recharge_account_exists' => $rechargeUser !== null && $rechargeFundService->getAccount() !== null,
  213. 'withdraw_account_exists' => $withdrawUser !== null && $withdrawFundService->getAccount() !== null,
  214. 'recharge_user_id' => $rechargeUserId,
  215. 'withdraw_user_id' => $withdrawUserId,
  216. ];
  217. }
  218. }