<?php
namespace Core\Payment;

use App\Models\Order;
use App\Models\Payment;

/** 订单支付成功后的统一入账逻辑（模拟/网关回调/后台手动共用） */
class OrderService
{
    public static function markPaid(string $orderNo, string $tradeNo, string $channel): bool
    {
        $order = new Order();
        $o = $order->where('order_no', $orderNo);
        if (!$o || $o['status'] === 'paid') return false;
        $order->update($o['id'], [
            'status'            => 'paid',
            'paid_at'           => date('Y-m-d H:i:s'),
            'gateway_trade_no'  => $tradeNo,
        ]);
        (new Payment())->insert([
            'order_id'   => $o['id'],
            'order_no'   => $orderNo,
            'channel'    => $channel,
            'amount'     => $o['amount'],
            'trade_no'   => $tradeNo,
            'status'     => 'paid',
            'created_at' => date('Y-m-d H:i:s'),
            'paid_at'    => date('Y-m-d H:i:s'),
        ]);
        return true;
    }
}
