| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace App\Services\Agent;
- use App\Models\Agent\Agent;
- use App\Models\Agent\AgentEquityOrder;
- use App\Models\Agent\AgentSetting;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Str;
- /**
- * 入股:代理在独立后台选择购买比例,金额 = 比例 × 每 1% 价格。
- * 余额足够立即扣款开通;不够则生成待付款订单,USDT 充值到账后自动扣款开通,无需审核。
- * 分红每月由总后台根据报表人工发放。
- */
- class AgentEquityService
- {
- public function __construct(private AgentWalletService $wallets)
- {
- }
- public function overview(Agent $agent): array
- {
- $setting = AgentSetting::current();
- $pending = AgentEquityOrder::query()->where('agent_id', $agent->id)->where('status', AgentEquityOrder::STATUS_PENDING)->first();
- $held = bcadd((string) $agent->equity_percent, '0', 4);
- $remaining = bcsub((string) $setting->equity_max_percent, $held, 4);
- return [
- 'enabled' => (int) $setting->equity_enabled,
- 'price_per_percent' => (string) $setting->equity_price_per_percent,
- 'min_percent' => (string) $setting->equity_min_percent,
- 'max_percent' => (string) $setting->equity_max_percent,
- 'equity_percent' => $held,
- 'remaining_percent' => bccomp($remaining, '0', 4) > 0 ? $remaining : '0.0000',
- 'balance' => (string) $agent->balance,
- 'pending_order' => $pending,
- ];
- }
- public function order(Agent $agent, string $percent): AgentEquityOrder
- {
- $order = DB::transaction(function () use ($agent, $percent) {
- $agent = Agent::query()->lockForUpdate()->findOrFail($agent->id);
- if (!$agent->isEquity()) throw new \RuntimeException('只有入股代理可以购买股份');
- $setting = AgentSetting::current();
- if (!(int) $setting->equity_enabled || bccomp((string) $setting->equity_price_per_percent, '0', 4) <= 0) {
- throw new \RuntimeException('入股暂未开放');
- }
- if (bccomp($percent, (string) $setting->equity_min_percent, 4) < 0 || bccomp($percent, (string) $setting->equity_max_percent, 4) > 0) {
- throw new \InvalidArgumentException("购买比例须在 {$setting->equity_min_percent}% 至 {$setting->equity_max_percent}% 之间");
- }
- if (AgentEquityOrder::query()->where('agent_id', $agent->id)->where('status', AgentEquityOrder::STATUS_PENDING)->exists()) {
- throw new \RuntimeException('已有待付款的入股订单,请先充值或取消');
- }
- if (bccomp(bcadd((string) $agent->equity_percent, $percent, 4), (string) $setting->equity_max_percent, 4) > 0) {
- throw new \RuntimeException("累计持股不能超过 {$setting->equity_max_percent}%");
- }
- return AgentEquityOrder::query()->create([
- 'order_no' => 'AE' . now()->format('YmdHis') . strtoupper(Str::random(6)),
- 'agent_id' => $agent->id, 'percent' => bcadd($percent, '0', 4),
- 'price_per_percent' => (string) $setting->equity_price_per_percent,
- 'amount' => bcmul($percent, (string) $setting->equity_price_per_percent, 4),
- 'status' => AgentEquityOrder::STATUS_PENDING,
- ]);
- });
- $this->fulfillPending((int) $agent->id);
- return $order->fresh();
- }
- public function cancel(Agent $agent, int $orderId): AgentEquityOrder
- {
- return DB::transaction(function () use ($agent, $orderId) {
- $order = AgentEquityOrder::query()->where('agent_id', $agent->id)->lockForUpdate()->findOrFail($orderId);
- if ($order->status !== AgentEquityOrder::STATUS_PENDING) throw new \RuntimeException('只有待付款订单可以取消');
- $order->status = AgentEquityOrder::STATUS_CANCELLED;
- $order->save();
- return $order;
- });
- }
- /** 余额足够时完成待付款订单(下单后、充值到账后调用)。 */
- public function fulfillPending(int $agentId): int
- {
- return DB::transaction(function () use ($agentId) {
- $agent = Agent::query()->lockForUpdate()->findOrFail($agentId);
- if (!$agent->isEquity()) return 0;
- $done = 0;
- $orders = AgentEquityOrder::query()->where('agent_id', $agentId)->where('status', AgentEquityOrder::STATUS_PENDING)
- ->orderBy('id')->lockForUpdate()->get();
- foreach ($orders as $order) {
- if (bccomp((string) $agent->balance, (string) $order->amount, 4) < 0) break;
- $this->wallets->adjust($agentId, (string) $order->amount, 'equity_purchase', '购买股份 ' . $order->percent . '%',
- 'equity-order:' . $order->id, 'agent', $agentId, 'agent_equity_order', (int) $order->id);
- $agent = Agent::query()->lockForUpdate()->findOrFail($agentId);
- $agent->equity_percent = bcadd((string) $agent->equity_percent, (string) $order->percent, 4);
- $agent->save();
- $order->status = AgentEquityOrder::STATUS_COMPLETED;
- $order->completed_at = now();
- $order->save();
- ++$done;
- }
- return $done;
- });
- }
- }
|