UserWalletTrait.php 4.4 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * trait :用户相关资金账户操作 trait
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Models\Traits
 * @package   App\Models\Traits
 * @author    Richer <yangzi1028@163.com>
 * @date      2023年5月8日14:36:47
 * @copyright 2022-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Models\Traits;

use App\Models\User\Commission;
use App\Models\User\Wallet;
use App\Models\User\WalletRecord;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * Trait UserWalletTrait
 *
 * @package App\Models\Traits
 */
trait UserWalletTrait
{
    /**
     * 现金余额账户 入账
     *
     * @param $recordable
     * @param int $amount
     * @param int $event
     * @param int $rate
     * @return mixed
     * @throws Exception
     */
    public function walletEntry($recordable, $amount = 0, $event = 0, $rate = 0)
    {
        $this->accountEntryOrCost(
            WalletRecord::INCREASE,
            $recordable,
            $amount,
            $event,
            $rate
        );
    }

    /**
     * 现金余额账户 消费
     *
     * @param $recordable
     * @param int $amount
     * @param int $event
     * @param int $rate
     * @return mixed
     * @throws Exception
     */
    public function walletCost($recordable, $amount = 0, $event = 0, $rate = 0)
    {
        $this->accountEntryOrCost(
            WalletRecord::DECREASE,
            $recordable,
            $amount,
            $event,
            $rate
        );
    }


    /**
     * 冻结账户
     *
     * @param $account
     * @param int $amount
     * @return mixed
     */
    public function frozenAmount($account, $amount = 0)
    {
        $account->frozen += $amount;
        $account->balance -= $amount;
        return $account->save();
    }

    /**
     * 钱包账户
     *
     * @return mixed
     */
    public function getWallet()
    {
        //总的钱包数据
        $account = $this->wallet()->lockForUpdate()->first();
        if (!$account) {
            $account = $this->wallet()->create([]);
        }
        return $account;
    }


    /**
     * 资金入账
     *
     * @param $type
     * @param $recordable
     * @param int $amount
     * @param int $event
     * @param int $rate
     * @return mixed
     * @throws Exception
     */
    public function accountEntryOrCost($type, $recordable, $amount = 0, $event = 0, $rate = 0)
    {
        if ($amount > 0) {
            $account = $this->getWallet();
            switch ($type) {
                case WalletRecord::INCREASE:
                    $account->income += $amount;
                    $account->balance += $amount;
                    break;
                case WalletRecord::DECREASE:
                    // 如果是扣减需要判断账号余额是否足够
                    $balance = $this->balance;
                    if ($balance < $amount) {
                        throw new Exception("操作失败,账户余额不足。");
                    }
                    $account->balance -= $amount;
                    break;
            }

            $account->save();

            // 刷新用户模型
//            $this->refresh();

            // 用户创建分销记录
            $record = [
                'user_id'   => $this->id,
                'amount'    => $amount,
                'type'      => $type,
                'event'     => $event,
                'ratio'     => $rate
            ];
            $recordable->walletRecords()->create($record);
        }
    }

    /**
     * 释放冻结资金
     *
     * @param int $amount
     * @param string $event
     * @return mixed
     */
    public function releaseFrozenAmount($amount = 0, $event = 'pass')
    {
        $account = $this->getWallet();
        // 通过
        $account->frozen -= $amount;
        if ($event === 'refuse') {
            // 拒绝
            $account->balance += $amount;
        } else {
            // 通过
            $account->withdrawn += $amount;
        }
        return $account->save();
    }
}