Files
cycle_api/app/Models/Wallet/PlatformUser/WalletPlatformUserWalletAddrModel.php
2024-03-08 00:58:23 +08:00

82 lines
3.5 KiB
PHP

<?php
namespace App\Models\Wallet\PlatformUser;
use App\Exceptions\ModelException;
use App\Models\Wallet\Base\WalletBaseModel;
use App\Models\Wallet\Wallet\WalletAddrModel;
use App\Tools\Times;
use Illuminate\Support\Facades\DB;
class WalletPlatformUserWalletAddrModel extends WalletBaseModel
{
protected $table = 'wallet_platform_user_wallet_addr';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'platform_id',
'uid',
'username',
'nickname',
'wallet_addr_id',
'wallet_addr',
'currency_id',
'currency_code',
'created_at',
];
//根据平台用户生成新充值地址,或者解绑旧有地址
function genUserWalletAddr($platform_id, $uid, $currency_code,$mode = WalletPlatformUserWalletAddrHistoryModel::TYPE_BIND): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
{
//先删除原有地址
$resOldModel = $this->findUserWalletAddr($platform_id, $uid, $currency_code);
if($mode == WalletPlatformUserWalletAddrHistoryModel::TYPE_BIND && $resOldModel){ //默认检测是否已经绑定过
throw new ModelException('已经绑定过地址');
}
try {
DB::beginTransaction();
//从钱包地址表取值
$walletAddrModel = new WalletAddrModel();
$oWalletPlatformUserWalletAddrHistoryModel = new WalletPlatformUserWalletAddrHistoryModel();
$walletAddrModel = $walletAddrModel->findUnusedAddrPutUsing($currency_code);
if(!$walletAddrModel) throw new ModelException('获取新地址失败');
if($mode == WalletPlatformUserWalletAddrHistoryModel::TYPE_UNBIND){
if($resOldModel){
$res = $this->delItem($resOldModel->id);
if(!$res) throw new ModelException('删除原有地址失败');
$res = $oWalletPlatformUserWalletAddrHistoryModel->addUserWalletAddrHistory($resOldModel->toArray(),WalletPlatformUserWalletAddrHistoryModel::TYPE_UNBIND);
if(!$res) throw new ModelException('增加历史绑定地址失败');
}
}
$insert = [
'platform_id' => $platform_id,
'uid' => $uid,
'wallet_addr_id' => $walletAddrModel->id,
'wallet_addr' => $walletAddrModel->addr,
'currency_id' => $walletAddrModel->currency_id,
'currency_code' => $walletAddrModel->currency_code,
'created_at' => Times::getNowDateTime(),
];
$resModel = $this->addItem($insert);
if($resModel) $oWalletPlatformUserWalletAddrHistoryModel->addUserWalletAddrHistory($resModel->toArray(),WalletPlatformUserWalletAddrHistoryModel::TYPE_BIND);
DB::commit();
}catch (\Exception $e) {
DB::rollBack();
throw $e;
}
return $resModel;
}
function findUserWalletAddr($platform_id, $uid, $currency_code): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->findItemByWhere(['platform_id' => $platform_id, 'uid' => $uid, 'currency_code' => $currency_code]);
}
function findByWalletAddrId($wallet_addr_id): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->findItemByWhere(['from_wallet_addr_id' => $wallet_addr_id]);
}
}