TransferApp.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace App\Module\Transfer\Models;
  3. use App\Module\Transfer\Casts\TransferAppCast;
  4. use UCore\ModelCore;
  5. /**
  6. * 划转应用配置模型
  7. *
  8. * field start
  9. * @property int $id 主键ID
  10. * @property string $keyname 应用标识符
  11. * @property string $title 应用显示名称
  12. * @property string $description 应用描述信息
  13. * @property int $out_id2 开放接口ID
  14. * @property int $out_id3 三方平台ID
  15. * @property int $currency_id 货币类型ID
  16. * @property int $fund_id 资金账户类型ID
  17. * @property int $fund_to_uid 转入目标账户UID
  18. * @property int $fund_in_uid 转入来源账户UID
  19. * @property float $exchange_rate 汇率(外部应用:业务)
  20. * @property float $fee_in_rate 转入手续费率(0.0000-1.0000)
  21. * @property float $fee_out_rate 转出手续费率(0.0000-1.0000)
  22. * @property float $fee_in_min 转入最低手续费
  23. * @property float $fee_in_max 转入最高手续费(0为不限制)
  24. * @property float $fee_out_min 转出最低手续费
  25. * @property float $fee_out_max 转出最高手续费(0为不限制)
  26. * @property int $fee_account_uid 手续费收取账户UID
  27. * @property string $order_callback_url 结果通知API地址(为空则不通知)
  28. * @property string $order_in_info_url 转入查询API地址(为空则不查询)
  29. * @property string $order_out_create_url 转出创建API地址(为空则不创建)
  30. * @property string $order_out_info_url 转出查询API地址(为空则不查询)
  31. * @property bool $is_enabled 是否启用(1=启用,0=禁用)
  32. * @property \Carbon\Carbon $created_at 创建时间
  33. * @property \Carbon\Carbon $updated_at 更新时间
  34. * @property \Carbon\Carbon $deleted_at 删除时间
  35. * field end
  36. */
  37. class TransferApp extends ModelCore
  38. {
  39. /**
  40. * 数据表名
  41. */
  42. protected $table = 'transfer_apps';
  43. // attrlist start
  44. protected $fillable = [
  45. 'id',
  46. 'keyname',
  47. 'title',
  48. 'description',
  49. 'out_id2',
  50. 'out_id3',
  51. 'currency_id',
  52. 'fund_id',
  53. 'fund_to_uid',
  54. 'fund_in_uid',
  55. 'exchange_rate',
  56. 'fee_in_rate',
  57. 'fee_out_rate',
  58. 'fee_in_min',
  59. 'fee_in_max',
  60. 'fee_out_min',
  61. 'fee_out_max',
  62. 'fee_account_uid',
  63. 'order_callback_url',
  64. 'order_in_info_url',
  65. 'order_out_create_url',
  66. 'order_out_info_url',
  67. 'is_enabled',
  68. ];
  69. // attrlist end
  70. /**
  71. * 属性类型转换
  72. */
  73. protected $casts = [
  74. 'id' => 'integer',
  75. 'out_id2' => 'integer',
  76. 'out_id3' => 'integer',
  77. 'currency_id' => 'integer',
  78. 'fund_id' => 'integer',
  79. 'fund_to_uid' => 'integer',
  80. 'fund_in_uid' => 'integer',
  81. 'exchange_rate' => 'decimal:4',
  82. 'fee_in_rate' => 'decimal:4',
  83. 'fee_out_rate' => 'decimal:4',
  84. 'fee_in_min' => 'decimal:4',
  85. 'fee_in_max' => 'decimal:4',
  86. 'fee_out_min' => 'decimal:4',
  87. 'fee_out_max' => 'decimal:4',
  88. 'fee_account_uid' => 'integer',
  89. 'is_enabled' => 'boolean',
  90. 'created_at' => 'datetime',
  91. 'updated_at' => 'datetime',
  92. 'deleted_at' => 'datetime',
  93. ];
  94. /**
  95. * 软删除
  96. */
  97. protected $dates = ['deleted_at'];
  98. /**
  99. * 隐藏字段
  100. */
  101. protected $hidden = [];
  102. /**
  103. * 关联划转订单
  104. */
  105. public function orders()
  106. {
  107. return $this->hasMany(TransferOrder::class, 'transfer_app_id');
  108. }
  109. /**
  110. * 获取启用状态文本
  111. */
  112. public function getEnabledTextAttribute(): string
  113. {
  114. return $this->is_enabled ? '启用' : '禁用';
  115. }
  116. /**
  117. * 获取启用状态颜色
  118. */
  119. public function getEnabledColorAttribute(): string
  120. {
  121. return $this->is_enabled ? 'success' : 'danger';
  122. }
  123. /**
  124. * 判断是否支持转入
  125. * 支持转入的条件:配置了转入查询URL 或者 没有配置任何外部API(本地处理)
  126. */
  127. public function supportsTransferIn(): bool
  128. {
  129. return !empty($this->order_in_info_url) ||
  130. (empty($this->order_callback_url) &&
  131. empty($this->order_in_info_url) &&
  132. empty($this->order_out_create_url) &&
  133. empty($this->order_out_info_url));
  134. }
  135. /**
  136. * 判断是否支持转出
  137. * 支持转出的条件:配置了转出创建URL 或者 没有配置任何外部API(本地处理)
  138. */
  139. public function supportsTransferOut(): bool
  140. {
  141. return !empty($this->order_out_create_url) ||
  142. (empty($this->order_callback_url) &&
  143. empty($this->order_in_info_url) &&
  144. empty($this->order_out_create_url) &&
  145. empty($this->order_out_info_url));
  146. }
  147. /**
  148. * 判断是否支持回调
  149. */
  150. public function supportsCallback(): bool
  151. {
  152. return !empty($this->order_callback_url);
  153. }
  154. /**
  155. * 获取手续费收取账户UID
  156. *
  157. * @return int 手续费收取账户UID,默认为1
  158. */
  159. public function getFeeAccountUid(): int
  160. {
  161. return $this->fee_account_uid > 0 ? $this->fee_account_uid : 1;
  162. }
  163. /**
  164. * 获取手续费收取账户信息
  165. */
  166. public function getFeeAccount()
  167. {
  168. $feeAccountUid = $this->getFeeAccountUid();
  169. return \App\Module\Fund\Services\FundService::getAccountInfo($feeAccountUid);
  170. }
  171. /**
  172. * 计算转入手续费
  173. *
  174. * @param string $amount 转入金额
  175. * @return array ['fee_rate' => 手续费率, 'fee_amount' => 手续费金额, 'actual_amount' => 实际到账金额]
  176. */
  177. public function calculateInFee(string $amount): array
  178. {
  179. return $this->calculateFee($amount, $this->fee_in_rate, $this->fee_in_min, $this->fee_in_max);
  180. }
  181. /**
  182. * 计算转出手续费
  183. *
  184. * @param string $amount 转出金额
  185. * @return array ['fee_rate' => 手续费率, 'fee_amount' => 手续费金额, 'actual_amount' => 实际到账金额]
  186. */
  187. public function calculateOutFee(string $amount): array
  188. {
  189. return $this->calculateFee($amount, $this->fee_out_rate, $this->fee_out_min, $this->fee_out_max);
  190. }
  191. /**
  192. * 计算手续费的通用方法
  193. *
  194. * @param string $amount 金额
  195. * @param float $feeRate 手续费率
  196. * @param float $minFee 最低手续费
  197. * @param float $maxFee 最高手续费
  198. * @return array
  199. */
  200. private function calculateFee(string $amount, float $feeRate, float $minFee, float $maxFee): array
  201. {
  202. $amountDecimal = bcmul($amount, '1', 4); // 转换为4位小数
  203. // 按比例计算手续费
  204. $feeAmount = bcmul($amountDecimal, (string)$feeRate, 4);
  205. // 应用最低手续费限制
  206. if (bccomp($feeAmount, (string)$minFee, 4) < 0) {
  207. $feeAmount = bcmul((string)$minFee, '1', 4);
  208. }
  209. // 应用最高手续费限制(如果设置了)
  210. if ($maxFee > 0 && bccomp($feeAmount, (string)$maxFee, 4) > 0) {
  211. $feeAmount = bcmul((string)$maxFee, '1', 4);
  212. }
  213. // 计算实际到账金额
  214. $actualAmount = bcsub($amountDecimal, $feeAmount, 4);
  215. return [
  216. 'fee_rate' => $feeRate,
  217. 'fee_amount' => $feeAmount,
  218. 'actual_amount' => $actualAmount,
  219. ];
  220. }
  221. /**
  222. * 检查是否启用了手续费
  223. *
  224. * @param string $type 类型:'in' 或 'out'
  225. * @return bool
  226. */
  227. public function isFeeEnabled(string $type): bool
  228. {
  229. if ($type === 'in') {
  230. return $this->fee_in_rate > 0 || $this->fee_in_min > 0;
  231. } elseif ($type === 'out') {
  232. return $this->fee_out_rate > 0 || $this->fee_out_min > 0;
  233. }
  234. return false;
  235. }
  236. }