FundService.php 5.5 KB

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