| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace App\Services\Agent;
- use App\Models\Agent\Agent;
- use App\Models\Agent\AgentApplication;
- use App\Models\Agent\AgentEquityOrder;
- use App\Models\Agent\AgentToken;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Str;
- /** 代理账号(只有一级代理)。分成方案由代理在独立后台申请,总后台审核后生效。 */
- class AgentService
- {
- public function create(array $data, ?int $createdBy = null): Agent
- {
- return $this->createAccount($data, $createdBy);
- }
- public function createFromApplication(AgentApplication $application, array $options, int $createdBy): Agent
- {
- if ($application->status !== 'pending' || !$application->password_hash || !$application->withdraw_password_hash) {
- throw new \RuntimeException('申请已处理或凭证不完整');
- }
- return $this->createAccount(['username' => $application->username] + $options, $createdBy, [
- 'password' => $application->password_hash, 'withdraw_password' => $application->withdraw_password_hash,
- ]);
- }
- private function createAccount(array $data, ?int $createdBy, ?array $hashes = null): Agent
- {
- return DB::transaction(function () use ($data, $createdBy, $hashes): Agent {
- $type = (int) ($data['type'] ?? Agent::TYPE_JOINT);
- if (!isset(Agent::TYPE_NAMES[$type])) throw new \InvalidArgumentException('代理类型错误');
- $agent = Agent::query()->create([
- 'parent_id' => null,
- 'path' => '/',
- 'level' => Agent::LEVEL_ONE,
- 'type' => $type,
- 'username' => trim((string) $data['username']),
- 'password' => $hashes['password'] ?? Hash::make((string) $data['password']),
- 'withdraw_password' => $hashes['withdraw_password'] ?? (empty($data['withdraw_password']) ? '' : Hash::make((string) $data['withdraw_password'])),
- 'real_name' => trim((string) ($data['real_name'] ?? '')),
- 'invitation_code' => $this->invitationCode(),
- 'deposit_fee_rate' => (string) ($data['deposit_fee_rate'] ?? 0),
- 'withdraw_fee_rate' => (string) ($data['withdraw_fee_rate'] ?? 0),
- 'status' => (int) ($data['status'] ?? Agent::STATUS_ENABLED),
- 'created_by' => $createdBy,
- ] + array_intersect_key($data, ['language' => true]));
- $agent->path = '/' . $agent->id . '/';
- $agent->save();
- return $agent->fresh(['profitCommissionProfile']);
- });
- }
- public function update(Agent $agent, array $data): Agent
- {
- return DB::transaction(function () use ($agent, $data): Agent {
- $agent = Agent::query()->lockForUpdate()->findOrFail($agent->id);
- if (array_key_exists('type', $data) && $data['type'] !== null && (int) $data['type'] !== (int) $agent->type) {
- $this->assertTypeChangeable($agent);
- $agent->type = (int) $data['type'];
- }
- if (array_key_exists('real_name', $data)) {
- $agent->real_name = trim((string) ($data['real_name'] ?? ''));
- }
- foreach (['username', 'language', 'status', 'deposit_fee_rate', 'withdraw_fee_rate'] as $field) {
- if (array_key_exists($field, $data) && $data[$field] !== null) {
- $agent->{$field} = $data[$field];
- }
- }
- if (!empty($data['password'])) {
- $agent->password = Hash::make((string) $data['password']);
- }
- if (!empty($data['withdraw_password'])) {
- $agent->withdraw_password = Hash::make((string) $data['withdraw_password']);
- }
- $agent->save();
- if (!empty($data['password']) || (int) $agent->status === Agent::STATUS_DISABLED) {
- AgentToken::query()->where('agent_id', $agent->id)->delete();
- }
- return $agent->fresh(['profitCommissionProfile']);
- });
- }
- /** 已有资金、方案或股份的代理不能切换类型,避免保证金与股份口径混在一起。 */
- private function assertTypeChangeable(Agent $agent): void
- {
- if (bccomp((string) $agent->balance, '0', 4) !== 0 || bccomp((string) $agent->frozen_balance, '0', 4) !== 0
- || bccomp((string) $agent->margin_amount, '0', 4) !== 0 || bccomp((string) $agent->equity_percent, '0', 4) !== 0
- || $agent->profit_commission_profile_id
- || AgentEquityOrder::query()->where('agent_id', $agent->id)->where('status', AgentEquityOrder::STATUS_PENDING)->exists()) {
- throw new \RuntimeException('代理已有余额、保证金、方案或股份,不能修改类型');
- }
- }
- private function invitationCode(): string
- {
- do {
- $code = strtoupper(Str::random(10));
- } while (Agent::query()->where('invitation_code', $code)->exists());
- return $code;
- }
- }
|