wallet tron transactions
This commit is contained in:
169
app/Models/Wallet/Platform/WalletPlatformBindModel.php
Normal file
169
app/Models/Wallet/Platform/WalletPlatformBindModel.php
Normal file
@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Wallet\Platform;
|
||||
|
||||
use App\Bean\Model\Wallet\Wallet\WalletPlatformBindBean;
|
||||
use App\Exceptions\ModelException;
|
||||
use App\Models\Wallet\Base\WalletBaseModel;
|
||||
use App\Models\Wallet\Wallet\WalletAddressModel;
|
||||
use App\Models\Wallet\Wallet\WalletCurrencyModel;
|
||||
use App\Tools\Logs;
|
||||
use App\Tools\Math;
|
||||
use App\Tools\Times;
|
||||
use App\Tools\Tools;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WalletPlatformBindModel extends WalletBaseModel
|
||||
{
|
||||
protected $table = 'wallet_platform_bind';
|
||||
protected $primaryKey = 'id';
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'platform_id',
|
||||
'wallet_address_id',
|
||||
'currency_code',
|
||||
'uid',
|
||||
'created_at',
|
||||
];
|
||||
|
||||
public function getBindInfo($platformId, $walletAddressId, $currencyCode, $uid): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null
|
||||
{
|
||||
return $this->newQuery()
|
||||
->where('platform_id', $platformId)
|
||||
->where('wallet_address_id', $walletAddressId)
|
||||
->where('currency_code', $currencyCode)
|
||||
->where('uid', $uid)
|
||||
->first();
|
||||
}
|
||||
|
||||
function checkIsBind($platformId, $uid, $currencyCode): bool
|
||||
{
|
||||
$res = $this->findBind($platformId, $uid, $currencyCode,['id']);
|
||||
if($res) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function findBind($platformId, $uid, $currencyCode,$cols = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null
|
||||
{
|
||||
if(empty($uid)) $uid = 0; //平台默认充提地址
|
||||
return $this->newQuery()
|
||||
->where('platform_id', $platformId)
|
||||
->where('currency_code', $currencyCode)
|
||||
->where('uid', $uid)
|
||||
->first($cols)
|
||||
;
|
||||
}
|
||||
|
||||
//查找空余钱包地址分配给平台
|
||||
function addBindWallet($platformId, $uid = null, $currencyCode = WalletCurrencyModel::CODE_USDT_TRC20): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
|
||||
{
|
||||
$res = $this->checkIsBind($platformId, $uid, $currencyCode);
|
||||
if($res) return false;
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
//检测是否已经绑定
|
||||
$this->getBindInfo( $platformId, $uid, $currencyCode);
|
||||
$oWalletAddressModel = new WalletAddressModel();
|
||||
$resWalletAddressModel = $oWalletAddressModel->findFreeAddress($currencyCode);
|
||||
if ($resWalletAddressModel) {
|
||||
throw new ModelException("can not find free address");
|
||||
}
|
||||
$bean = new WalletPlatformBindBean();
|
||||
$bean->setPlatformId($platformId);
|
||||
$bean->setCurrencyCode($currencyCode);
|
||||
$bean->setUid($uid);
|
||||
$bean->setWalletAddressId($resWalletAddressModel->id);
|
||||
$bean->setCreatedAt(Times::getNowDateTime());
|
||||
$res = $this->addItem($bean->toArrayNotNull());
|
||||
if(!$res) throw new ModelException("add bind fail");
|
||||
//更新钱包地址状态
|
||||
$updateItem = [
|
||||
'id' => $resWalletAddressModel->id,
|
||||
'use_status' => WalletAddressModel::USE_STATUS_USING,
|
||||
];
|
||||
$res = $oWalletAddressModel->updateItem($updateItem);
|
||||
if(!$res) throw new ModelException("change use_status fail wallet_address_id:{$resWalletAddressModel->id}");
|
||||
|
||||
DB::commit();
|
||||
Logs::SuccLog(__FUNCTION__, $bean->toArrayNotNull());
|
||||
return true;
|
||||
}catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//解绑
|
||||
function unBindWithUid($platformId, $uid, $currencyCode = WalletCurrencyModel::CODE_USDT_TRC20,$remark = ''): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
|
||||
{
|
||||
$res = $this->checkIsBind($platformId, $uid, $currencyCode);
|
||||
if(!$res) return true;
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$oWalletAddressModel = new WalletAddressModel();
|
||||
$oWalletPlatformBindModel = new WalletPlatformBindModel();
|
||||
$resWalletPlatformBindModel = $oWalletPlatformBindModel->newQuery()
|
||||
->where('platform_id', $platformId)
|
||||
->where('currency_code', $currencyCode)
|
||||
->where('uid', $uid)
|
||||
->first();
|
||||
if (!$resWalletPlatformBindModel) return true;
|
||||
|
||||
//废弃原有地址
|
||||
$updateItem = [
|
||||
'id' => $resWalletPlatformBindModel->id,
|
||||
'use_status' => WalletAddressModel::USE_STATUS_USED,
|
||||
];
|
||||
$res = $oWalletAddressModel->updateItem($updateItem);
|
||||
if(!$res) throw new ModelException("change used use_status fail wallet_address_id:{$resWalletPlatformBindModel->wallet_address_id}");
|
||||
|
||||
//删除原有绑定记录
|
||||
$res = $oWalletPlatformBindModel->delItem($resWalletPlatformBindModel->id);
|
||||
if(!$res) throw new ModelException("delete bind fail");
|
||||
|
||||
//原有绑定记录添加到历史
|
||||
$oWalletPlatformBindHistoryModel = new WalletPlatformBindHistoryModel();
|
||||
$res = $oWalletPlatformBindHistoryModel->addHistory($resWalletPlatformBindModel->toArray(),$remark);
|
||||
if(!$res) throw new ModelException("add history fail");
|
||||
|
||||
DB::commit();
|
||||
Logs::SuccLog(__FUNCTION__, func_get_args());
|
||||
return true;
|
||||
}catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//绑定检测是否已绑定,未绑定则绑定,已绑定则解绑再绑定新地址
|
||||
function bindWallet( $platformId, $currencyCode = WalletCurrencyModel::CODE_USDT_TRC20, $uid = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$res = $this->unBindWithUid($platformId, $uid, $currencyCode);
|
||||
if(!$res) throw new ModelException("unBindWithUid fail");
|
||||
|
||||
$res = $this->addBindWallet($platformId, $uid, $currencyCode);
|
||||
if(!$res) throw new ModelException("addBindWallet fail");
|
||||
|
||||
DB::commit();
|
||||
Logs::SuccLog(__FUNCTION__, func_get_args());
|
||||
return true;
|
||||
}catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user