FundService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace App\Module\Fund\Services;
  3. use App\Module\Fund\Enums\FUND_TYPE;
  4. use App\Module\Fund\Enums\LOG_TYPE;
  5. use App\Module\Fund\Models\FundAdminModel;
  6. use App\Module\Fund\Models\FundModel;
  7. use App\Module\Fund\Service\Circulation;
  8. use App\Module\Fund\Service\Transfer;
  9. use App\Module\Fund\Service\User;
  10. use Illuminate\Support\Facades\DB;
  11. use UCore\Db\Helper;
  12. class FundService
  13. {
  14. private int $userId;
  15. private int $fundId;
  16. private FundModel $fundModel;
  17. public function __construct(int $userId, $fundId)
  18. {
  19. $this->userId = $userId;
  20. if(is_int($fundId)){
  21. $this->fundId = $fundId;
  22. }else{
  23. $this->fundId = $fundId->valueInt();
  24. }
  25. $this->fundModel = FundModel::query()
  26. ->where('user_id', $userId)
  27. ->where('fund_id', $fundId)
  28. ->first();
  29. }
  30. /**
  31. * 资金流转
  32. * (同用户,同币种,不同账户)
  33. *
  34. * @param int $to_fund
  35. * @param int $amount
  36. * @param string $remark
  37. */
  38. public function circulation(FUND_TYPE $to_fund_id, int $amount, int $re_id, string $re_type, string $remark)
  39. {
  40. # 确认货币种类一致
  41. if (!CurrencyService::check($this->fundId, $to_fund_id->valueInt())) {
  42. return '币种不一致,禁止划转';
  43. }
  44. # 实例化操作对象
  45. Helper::check_tr();
  46. # 先进行转账记录
  47. $re_id = Circulation::handle($this->userId, $this->fundId, $to_fund_id->valueInt(), $amount, $re_id, $re_type,
  48. $remark);
  49. if (is_string($re_id)) {
  50. return $re_id;
  51. }
  52. # 进行双方的资金修改
  53. # 先减少自己的
  54. $re46 = User::handle($this->userId, $this->fundId, -$amount, LOG_TYPE::CIRCULATION, $re_id, $remark);
  55. if (is_string($re46)) {
  56. return $re46;
  57. }
  58. # 再增加自己另一个账户
  59. $re51 = User::handle($this->userId, $to_fund_id->valueInt(), $amount, LOG_TYPE::CIRCULATION, $re_id, $remark);
  60. if (is_string($re51)) {
  61. return $re51;
  62. }
  63. return true;
  64. }
  65. /**
  66. * 获取余额
  67. */
  68. public function balance(): int
  69. {
  70. return $this->fundModel->balance ?? 0;
  71. }
  72. /**
  73. * 转账
  74. * (不同人,同账户/同币种)
  75. */
  76. public function transfer(int $toUserId, int $amount, string $remark)
  77. {
  78. # 实例化操作对象
  79. # 开启事务
  80. DB::beginTransaction();
  81. # 先进行转账记录
  82. $transfer_id = Transfer::to_user(
  83. $this->userId,
  84. $this->fundId,
  85. $toUserId,
  86. $amount,
  87. $remark);
  88. if (is_string($transfer_id)) {
  89. DB::rollBack();
  90. return $transfer_id;
  91. }
  92. # 进行双方的资金修改
  93. # 先减少自己的
  94. $re46 = User::handle($this->userId, $this->fundId, -$amount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
  95. if (is_string($re46)) {
  96. DB::rollBack();
  97. return $re46;
  98. }
  99. # 再增加别人的
  100. $re51 = User::handle($toUserId, $this->fundId, $amount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
  101. if (is_string($re51)) {
  102. DB::rollBack();
  103. return $re51;
  104. }
  105. DB::commit();
  106. return true;
  107. }
  108. /**
  109. * 贸易(业务关联)
  110. * 同账户/同币种,不同用户
  111. *
  112. * @param int $toUserId
  113. * @param int $amount
  114. * @param string $transfer_type
  115. * @param string $transfer_id
  116. * @param string $remark
  117. * @return string|true
  118. */
  119. public function trade(int $toUserId, int $amount, $transfer_type, $transfer_id, string $remark)
  120. {
  121. # 检查事务开启
  122. Helper::check_tr();
  123. $transfer_id = $transfer_type.'-'.$transfer_id;
  124. # 先减少自己的
  125. $re46 = User::handle($this->userId, $this->fundId, -$amount, LOG_TYPE::TRADE, $transfer_id, $remark);
  126. if (is_string($re46)) {
  127. \UCore\Trace::addData('error', $re46);
  128. return $re46;
  129. }
  130. # 再增加别人的
  131. $re51 = User::handle($toUserId, $this->fundId, $amount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
  132. if (is_string($re51)) {
  133. \UCore\Trace::addData('error', $re51);
  134. return $re51;
  135. }
  136. return true;
  137. }
  138. /**
  139. * Admin 资金操作
  140. *
  141. * @param int $admin_id
  142. * @param FUND_TYPE $fund_id
  143. * @param int $fund_fee
  144. * @param $remark
  145. * @return bool|string
  146. */
  147. public function admin_operate(int $admin_id, FUND_TYPE $fund_id, int $fund_fee, $remark)
  148. {
  149. $data = [
  150. 'total_fee' => $fund_fee,
  151. 'status' => 1,
  152. 'user_id' => $this->userId,
  153. 'fund_id' => $fund_id->valueInt(),
  154. 'admin_id' => $admin_id,
  155. 'create_time' => time(),
  156. 'remark' => $remark
  157. ];
  158. # 启动事务
  159. DB::beginTransaction();
  160. # 写日志
  161. $fund_admin = new FundAdminModel();
  162. $fund_admin->setData($data);
  163. if ($fund_admin->save() === false) {
  164. DB::rollBack();
  165. return '_Model-error';
  166. }
  167. # 进行资金操作
  168. $re = \App\Module\Fund\Service\User::handle($this->userId, $fund_id->value, $fund_fee,
  169. \App\Module\Fund\Enums\LOG_TYPE::ADMIN, $fund_admin->id, $remark);
  170. if (is_string($re)) {
  171. DB::rollBack();
  172. return $re;
  173. }
  174. DB::commit();
  175. return $re;
  176. }
  177. }