wallet
This commit is contained in:
125
app/Models/Wallet/Platform/WalletPlatformBalanceModel.php
Normal file
125
app/Models/Wallet/Platform/WalletPlatformBalanceModel.php
Normal file
@ -0,0 +1,125 @@
|
||||
<?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 incAvailableBalance($id, $amount): bool
|
||||
{
|
||||
$amount = abs($amount);
|
||||
$oModel = $this->newQuery()->where('id', $id)->first(['total_amount', 'available_amount']);
|
||||
if (!$oModel) throw new ModelException('找不到id');
|
||||
$oModel->frozen_amount = Db::raw('total_amount + ' . $amount);
|
||||
$oModel->available_amount = Db::raw('available_amount + ' . $amount);
|
||||
return $oModel->save();
|
||||
}
|
||||
|
||||
//扣除平台余额
|
||||
function decAvailableBalance($id, $amount): bool
|
||||
{
|
||||
$amount = abs($amount);
|
||||
$oModel = $this->newQuery()->where('id', $id)->first(['total_amount', 'available_amount']);
|
||||
if (!$oModel) throw new ModelException('找不到id');
|
||||
$oModel->frozen_amount = Db::raw('total_amount - ' . $amount);
|
||||
$oModel->available_amount = Db::raw('available_amount - ' . $amount);
|
||||
return $oModel->save();
|
||||
}
|
||||
|
||||
function decFrozenAmountById($id, $amount): int
|
||||
{
|
||||
$amount = abs($amount);
|
||||
$oModel = $this->newQuery()->where('id', $id)->first(['total_amount', 'frozen_amount']);
|
||||
if (!$oModel) throw new ModelException('找不到id');
|
||||
$oModel->frozen_amount = Db::raw('frozen_amount - ' . $amount);
|
||||
$oModel->total_amount = Db::raw('total_amount - ' . $amount);
|
||||
return $oModel->save();
|
||||
}
|
||||
|
||||
function unFrozenAmountById($id, $amount): int
|
||||
{
|
||||
$amount = abs($amount);
|
||||
$oModel = $this->newQuery()->where('id', $id)->first(['frozen_amount', 'available_amount']);
|
||||
if (!$oModel) throw new ModelException('找不到id');
|
||||
$oModel->frozen_amount = Db::raw('frozen_amount - ' . $amount);
|
||||
$oModel->available_amount = Db::raw('available_amount + ' . $amount);
|
||||
return $oModel->save();
|
||||
}
|
||||
|
||||
function frozenAmountById($id, $amount): int
|
||||
{
|
||||
$amount = abs($amount);
|
||||
$oModel = $this->newQuery()->where('id', $id)->first(['frozen_amount', 'available_amount']);
|
||||
if (!$oModel) throw new ModelException('找不到id');
|
||||
$oModel->frozen_amount = Db::raw('frozen_amount + ' . $amount);
|
||||
$oModel->available_amount = Db::raw('available_amount - ' . $amount);
|
||||
return $oModel->save();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user