| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?php
- namespace App\Module\Transfer\Models;
- use App\Module\Transfer\Casts\TransferAppCast;
- use UCore\ModelCore;
- /**
- * 划转应用配置模型
- *
- * field start
- * @property int $id 主键ID
- * @property string $keyname 应用标识符
- * @property string $title 应用显示名称
- * @property string $description 应用描述信息
- * @property int $out_id2 外部应用ID2-开放接口
- * @property int $out_id3 外部应用ID3-三方平台ID
- * @property int $currency_id 货币类型ID
- * @property int $fund_id 资金账户类型ID
- * @property int $fund_to_uid 转入目标账户UID
- * @property int $fund_in_uid 转入来源账户UID
- * @property float $exchange_rate 汇率(钱包:业务)
- * @property string $order_callback_url 结果通知API地址(为空则不通知)
- * @property string $order_in_info_url 转入查询API地址(为空则不查询)
- * @property string $order_out_create_url 转出创建API地址(为空则不创建)
- * @property string $order_out_info_url 转出查询API地址(为空则不查询)
- * @property bool $is_enabled 是否启用(1=启用,0=禁用)
- * @property bool $allow_transfer_in 是否允许转入(1=允许,0=禁止)
- * @property bool $allow_transfer_out 是否允许转出(1=允许,0=禁止)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * @property \Carbon\Carbon $deleted_at 删除时间
- * @property float $fee_in_rate 转入手续费率(0.0000-1.0000)
- * @property float $fee_out_rate 转出手续费率(0.0000-1.0000)
- * @property float $fee_in_min 转入最低手续费
- * @property float $fee_in_max 转入最高手续费(0为不限制)
- * @property float $fee_out_min 转出最低手续费
- * @property float $fee_out_max 转出最高手续费(0为不限制)
- * @property int $fee_account_uid 手续费收取账户UID
- * field end
- */
- class TransferApp extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'transfer_apps';
- // attrlist start
- protected $fillable = [
- 'id',
- 'keyname',
- 'title',
- 'description',
- 'out_id2',
- 'out_id3',
- 'currency_id',
- 'fund_id',
- 'fund_to_uid',
- 'fund_in_uid',
- 'exchange_rate',
- 'order_callback_url',
- 'order_in_info_url',
- 'order_out_create_url',
- 'order_out_info_url',
- 'is_enabled',
- 'allow_transfer_in',
- 'allow_transfer_out',
- 'fee_in_rate',
- 'fee_out_rate',
- 'fee_in_min',
- 'fee_in_max',
- 'fee_out_min',
- 'fee_out_max',
- 'fee_account_uid',
- ];
- // attrlist end
- /**
- * 属性类型转换
- */
- protected $casts = [
- 'id' => 'integer',
- 'out_id2' => 'integer',
- 'out_id3' => 'integer',
- 'currency_id' => 'integer',
- 'fund_id' => 'integer',
- 'fund_to_uid' => 'integer',
- 'fund_in_uid' => 'integer',
- 'exchange_rate' => 'decimal:4',
- 'fee_in_rate' => 'decimal:4',
- 'fee_out_rate' => 'decimal:4',
- 'fee_in_min' => 'decimal:4',
- 'fee_in_max' => 'decimal:4',
- 'fee_out_min' => 'decimal:4',
- 'fee_out_max' => 'decimal:4',
- 'fee_account_uid' => 'integer',
- 'is_enabled' => 'boolean',
- 'allow_transfer_in' => 'boolean',
- 'allow_transfer_out' => 'boolean',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- 'deleted_at' => 'datetime',
- ];
- /**
- * 软删除
- */
- protected $dates = ['deleted_at'];
- /**
- * 隐藏字段
- */
- protected $hidden = [];
- /**
- * 关联划转订单
- */
- public function orders()
- {
- return $this->hasMany(TransferOrder::class, 'transfer_app_id');
- }
- /**
- * 获取启用状态文本
- */
- public function getEnabledTextAttribute(): string
- {
- return $this->is_enabled ? '启用' : '禁用';
- }
- /**
- * 获取启用状态颜色
- */
- public function getEnabledColorAttribute(): string
- {
- return $this->is_enabled ? 'success' : 'danger';
- }
- /**
- * 获取允许转入状态文本
- */
- public function getAllowTransferInTextAttribute(): string
- {
- return $this->allow_transfer_in ? '允许' : '禁止';
- }
- /**
- * 获取允许转入状态颜色
- */
- public function getAllowTransferInColorAttribute(): string
- {
- return $this->allow_transfer_in ? 'success' : 'danger';
- }
- /**
- * 获取允许转出状态文本
- */
- public function getAllowTransferOutTextAttribute(): string
- {
- return $this->allow_transfer_out ? '允许' : '禁止';
- }
- /**
- * 获取允许转出状态颜色
- */
- public function getAllowTransferOutColorAttribute(): string
- {
- return $this->allow_transfer_out ? 'success' : 'danger';
- }
- /**
- * 判断是否支持转入
- * 支持转入的条件:应用启用 && 允许转入 && (配置了转入查询URL 或者 没有配置任何外部API(本地处理))
- */
- public function supportsTransferIn(): bool
- {
- if (!$this->is_enabled || !$this->allow_transfer_in) {
- return false;
- }
- return !empty($this->order_in_info_url) ||
- (empty($this->order_callback_url) &&
- empty($this->order_in_info_url) &&
- empty($this->order_out_create_url) &&
- empty($this->order_out_info_url));
- }
- /**
- * 判断是否支持转出
- * 支持转出的条件:应用启用 && 允许转出 && (配置了转出创建URL 或者 没有配置任何外部API(本地处理))
- */
- public function supportsTransferOut(): bool
- {
- if (!$this->is_enabled || !$this->allow_transfer_out) {
- return false;
- }
- return !empty($this->order_out_create_url) ||
- (empty($this->order_callback_url) &&
- empty($this->order_in_info_url) &&
- empty($this->order_out_create_url) &&
- empty($this->order_out_info_url));
- }
- /**
- * 判断是否支持回调
- */
- public function supportsCallback(): bool
- {
- return !empty($this->order_callback_url);
- }
- /**
- * 获取手续费收取账户UID
- *
- * @return int 手续费收取账户UID,默认为1
- */
- public function getFeeAccountUid(): int
- {
- return $this->fee_account_uid > 0 ? $this->fee_account_uid : 1;
- }
- /**
- * 获取手续费收取账户信息
- */
- public function getFeeAccount()
- {
- $feeAccountUid = $this->getFeeAccountUid();
- return \App\Module\Fund\Services\FundService::getAccountInfo($feeAccountUid);
- }
- /**
- * 计算转入手续费
- *
- * @param string $amount 转入金额
- * @return array ['fee_rate' => 手续费率, 'fee_amount' => 手续费金额, 'actual_amount' => 实际到账金额]
- * @deprecated 模型内不应该有逻辑
- */
- public function calculateInFee(string $amount): array
- {
- return $this->calculateFee($amount, $this->fee_in_rate, $this->fee_in_min, $this->fee_in_max);
- }
- /**
- * 计算转出手续费
- *
- * @param string $amount 转出金额
- * @param array $context 额外的上下文数据(包含用户ID等信息)
- * @return array ['fee_rate' => 手续费率, 'fee_amount' => 手续费金额, 'actual_amount' => 用户总支付金额]
- * @deprecated 模型内,不应该有逻辑
- */
- public function calculateOutFee(string $amount, array $context = []): array
- {
- // 使用FeeService来计算手续费,这样可以触发事件机制
- return \App\Module\Transfer\Services\FeeService::calculateOutFee($this, $amount, $context);
- }
- /**
- * 计算手续费的通用方法(从金额中扣除手续费)
- *
- * @param string $amount 金额
- * @param float $feeRate 手续费率
- * @param float $minFee 最低手续费
- * @param float $maxFee 最高手续费
- * @return array
- */
- private function calculateFee(string $amount, float $feeRate, float $minFee, float $maxFee): array
- {
- $amountDecimal = bcmul($amount, '1', 4); // 转换为4位小数
- // 按比例计算手续费
- $feeAmount = bcmul($amountDecimal, (string)$feeRate, 4);
- // 应用最低手续费限制
- if (bccomp($feeAmount, (string)$minFee, 4) < 0) {
- $feeAmount = bcmul((string)$minFee, '1', 4);
- }
- // 应用最高手续费限制(如果设置了)
- if ($maxFee > 0 && bccomp($feeAmount, (string)$maxFee, 4) > 0) {
- $feeAmount = bcmul((string)$maxFee, '1', 4);
- }
- // 计算实际到账金额(扣除手续费后)
- $actualAmount = bcsub($amountDecimal, $feeAmount, 4);
- return [
- 'fee_rate' => $feeRate,
- 'fee_amount' => $feeAmount,
- 'actual_amount' => $actualAmount, // 对于转入:用户实际收到金额
- ];
- }
- /**
- * 检查是否启用了手续费
- *
- * @param string $type 类型:'in' 或 'out'
- * @return bool
- */
- public function isFeeEnabled(string $type): bool
- {
- if ($type === 'in') {
- return $this->fee_in_rate > 0 || $this->fee_in_min > 0;
- } elseif ($type === 'out') {
- return $this->fee_out_rate > 0 || $this->fee_out_min > 0;
- }
- return false;
- }
- }
|