139 lines
4.4 KiB
PHP
139 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Wallet\Platform;
|
|
|
|
use App\Exceptions\ModelException;
|
|
use App\Models\Wallet\Base\WalletBaseModel;
|
|
use App\Models\Wallet\Wallet\WalletCurrencyModel;
|
|
use App\Tools\Math;
|
|
use App\Tools\Times;
|
|
use App\Tools\Tools;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class WalletPlatformBalanceModel extends WalletBaseModel
|
|
{
|
|
protected $table = 'wallet_platform_balance';
|
|
protected $primaryKey = 'id';
|
|
protected $fillable = [
|
|
'id',
|
|
'platform_id',
|
|
'currency_id',
|
|
'currency_code',
|
|
'total_amount',
|
|
'frozen_amount',
|
|
'available_amount',
|
|
'created_at',
|
|
'updated_at',
|
|
];
|
|
|
|
const TYPE_ADD = 1;
|
|
const TYPE_DEC = 2;
|
|
const TYPE = [
|
|
self::TYPE_ADD => '加',
|
|
self::TYPE_DEC => '减',
|
|
];
|
|
|
|
function newPlatformBalance($platform_id, $currency_code): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
|
|
{
|
|
$resModel = $this->findItemByWhere(['platform_id' => $platform_id, 'currency_code' => $currency_code]);
|
|
if ($resModel) return $resModel;
|
|
|
|
$oWalletCurrencyModel = new WalletCurrencyModel();
|
|
$resWalletCurrencyModel = $oWalletCurrencyModel->findByCode($currency_code);
|
|
if (!$resWalletCurrencyModel) throw new ModelException('currency_code error');
|
|
|
|
|
|
$insert = [
|
|
'platform_id' => $platform_id,
|
|
'currency_id' => $oWalletCurrencyModel->id,
|
|
'currency_code' => $oWalletCurrencyModel->code,
|
|
'created_at' => Times::getNowDateTime(),
|
|
'updated_at' => Times::getNowDateTime(),
|
|
];
|
|
return $this->addItem($insert);
|
|
}
|
|
|
|
function findPlatformBalance($platform_id, $currency_code): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null
|
|
{
|
|
return $this->findItemByWhere(['platform_id' => $platform_id, 'currency_code' => $currency_code]);
|
|
}
|
|
|
|
function findPlatformBalanceOrCreate($platform_id, $currency_code): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|bool|array|null
|
|
{
|
|
$resModel = $this->findItemByWhere(['platform_id' => $platform_id, 'currency_code' => $currency_code]);
|
|
if (!$resModel) { //不存在就创建一个
|
|
$resModel = $this->newPlatformBalance($platform_id, $currency_code);
|
|
}
|
|
return $resModel;
|
|
}
|
|
function frozenAmount($id,$amount): bool|int
|
|
{
|
|
$amount = abs($amount);
|
|
$updateItem = [
|
|
'id' => $id,
|
|
'frozen_amount' => DB::raw('frozen_amount + '. $amount),
|
|
'available_amount' => DB::raw('available_amount - '. $amount),
|
|
];
|
|
return $this->updateItem($updateItem);
|
|
}
|
|
|
|
function unFrozenAmount($id,$amount): bool|int
|
|
{
|
|
$amount = abs($amount);
|
|
$updateItem = [
|
|
'id' => $id,
|
|
'frozen_amount' => DB::raw('frozen_amount - '. $amount),
|
|
'available_amount' => DB::raw('available_amount + '. $amount),
|
|
];
|
|
return $this->updateItem($updateItem);
|
|
}
|
|
|
|
function subFrozenAmount($id, $amount): bool|int
|
|
{
|
|
$amount = abs($amount);
|
|
$updateItem = [
|
|
'id' => $id,
|
|
'total_amount' => DB::raw('total_amount - '. $amount),
|
|
'frozen_amount' => DB::raw('frozen_amount - '. $amount),
|
|
];
|
|
return $this->updateItem($updateItem);
|
|
}
|
|
|
|
function addFrozenAmount($id, $amount): bool|int
|
|
{
|
|
$amount = abs($amount);
|
|
$updateItem = [
|
|
'id' => $id,
|
|
'total_amount' => DB::raw('total_amount + '. $amount),
|
|
'frozen_amount' => DB::raw('frozen_amount + '. $amount),
|
|
];
|
|
return $this->updateItem($updateItem);
|
|
}
|
|
|
|
function subAvailableAmount($id, $amount): bool|int
|
|
{
|
|
$amount = abs($amount);
|
|
$updateItem = [
|
|
'id' => $id,
|
|
'total_amount' => DB::raw('total_amount - '. $amount),
|
|
'available_amount' => DB::raw('available_amount - '. $amount),
|
|
];
|
|
return $this->updateItem($updateItem);
|
|
}
|
|
|
|
function addAvailableAmount($id, $amount): bool|int
|
|
{
|
|
$amount = abs($amount);
|
|
$updateItem = [
|
|
'id' => $id,
|
|
'total_amount' => DB::raw('total_amount + '. $amount),
|
|
'available_amount' => DB::raw('available_amount + '. $amount),
|
|
];
|
|
return $this->updateItem($updateItem);
|
|
}
|
|
|
|
|
|
|
|
}
|