FundService.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace App\Module\Fund\Services;
  3. use App\Module\Fund\Dto\CirculationDto;
  4. use App\Module\Fund\Dto\FundAccountDto;
  5. use App\Module\Fund\Dto\FundAdminDto;
  6. use App\Module\Fund\Dto\TradeResultDto;
  7. use App\Module\Fund\Dto\TransferResultDto;
  8. use App\Module\Fund\Enums\FUND_TYPE;
  9. use App\Module\Fund\Enums\LOG_TYPE;
  10. use App\Module\Fund\Models\FundAdminModel;
  11. use App\Module\Fund\Models\FundModel;
  12. use App\Module\Fund\Logic\Circulation;
  13. use App\Module\Fund\Logic\Transfer;
  14. use App\Module\Fund\Logic\User;
  15. use Illuminate\Support\Facades\DB;
  16. use UCore\Db\Helper;
  17. class FundService
  18. {
  19. private int $userId;
  20. private int $fundId;
  21. private ?FundModel $fundModel;
  22. public function __construct(int $userId, $fundId)
  23. {
  24. $this->userId = $userId;
  25. if(is_int($fundId)){
  26. $this->fundId = $fundId;
  27. }else{
  28. $this->fundId = $fundId->valueInt();
  29. }
  30. $this->fundModel = FundModel::query()
  31. ->where('user_id', $userId)
  32. ->where('fund_id', $fundId)
  33. ->first();
  34. }
  35. /**
  36. * 资金流转
  37. * (同用户,同币种,不同账户)
  38. *
  39. * @param FUND_TYPE $to_fund_id 目标资金类型
  40. * @param int $amount 金额
  41. * @param int $re_id 关联ID
  42. * @param string $re_type 关联类型
  43. * @param string $remark 备注
  44. * @return CirculationDto|string 成功返回DTO,失败返回错误信息
  45. */
  46. public function circulation(FUND_TYPE $to_fund_id, int $amount, int $re_id, string $re_type, string $remark)
  47. {
  48. # 确认货币种类一致
  49. if (!CurrencyService::check($this->fundId, $to_fund_id->valueInt())) {
  50. return '币种不一致,禁止划转';
  51. }
  52. # 实例化操作对象
  53. Helper::check_tr();
  54. # 先进行转账记录
  55. $circulation_id = Circulation::handle($this->userId, $this->fundId, $to_fund_id->valueInt(), $amount, $re_id, $re_type,
  56. $remark);
  57. if (is_string($circulation_id)) {
  58. return $circulation_id;
  59. }
  60. # 进行双方的资金修改
  61. # 先减少自己的
  62. $re46 = User::handle($this->userId, $this->fundId, -$amount, LOG_TYPE::CIRCULATION, $circulation_id, $remark);
  63. if (is_string($re46)) {
  64. return $re46;
  65. }
  66. # 再增加自己另一个账户
  67. $re51 = User::handle($this->userId, $to_fund_id->valueInt(), $amount, LOG_TYPE::CIRCULATION, $circulation_id, $remark);
  68. if (is_string($re51)) {
  69. return $re51;
  70. }
  71. // 获取流转记录
  72. $circulationModel = \App\Module\Fund\Models\FundCirculationModel::find($circulation_id);
  73. if (!$circulationModel) {
  74. return '流转记录不存在';
  75. }
  76. // 获取资金类型名称映射
  77. $fundNames = AccountService::getFundsDesc();
  78. // 将模型转换为DTO并返回
  79. return CirculationDto::fromModel($circulationModel, $fundNames);
  80. }
  81. /**
  82. * 获取账户信息
  83. *
  84. * @return FundAccountDto|null 账户DTO,如果账户不存在则返回null
  85. */
  86. public function getAccount(): ?FundAccountDto
  87. {
  88. // 如果账户不存在,返回null
  89. if (!$this->fundModel) {
  90. return null;
  91. }
  92. // 获取资金类型名称映射
  93. $fundNames = AccountService::getFundsDesc();
  94. // 将模型转换为DTO
  95. return FundAccountDto::fromModel($this->fundModel, $fundNames);
  96. }
  97. /**
  98. * 获取余额
  99. *
  100. * @return int 账户余额
  101. */
  102. public function balance(): int
  103. {
  104. return $this->fundModel->balance ?? 0;
  105. }
  106. /**
  107. * 转账
  108. * (不同人,同账户/同币种)
  109. *
  110. * @param int $toUserId 接收用户ID
  111. * @param int $amount 金额
  112. * @param string $remark 备注
  113. * @return TransferResultDto|string 成功返回DTO,失败返回错误信息
  114. */
  115. public function transfer(int $toUserId, int $amount, string $remark)
  116. {
  117. # 实例化操作对象
  118. # 开启事务
  119. DB::beginTransaction();
  120. # 先进行转账记录
  121. $transfer_id = Transfer::to_user(
  122. $this->userId,
  123. $this->fundId,
  124. $toUserId,
  125. $amount,
  126. $remark);
  127. if (is_string($transfer_id)) {
  128. DB::rollBack();
  129. return $transfer_id;
  130. }
  131. # 进行双方的资金修改
  132. # 先减少自己的
  133. $re46 = User::handle($this->userId, $this->fundId, -$amount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
  134. if (is_string($re46)) {
  135. DB::rollBack();
  136. return $re46;
  137. }
  138. # 再增加别人的
  139. $re51 = User::handle($toUserId, $this->fundId, $amount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
  140. if (is_string($re51)) {
  141. DB::rollBack();
  142. return $re51;
  143. }
  144. DB::commit();
  145. // 获取转账记录
  146. $transferModel = \App\Module\Fund\Models\FundTransferModel::find($transfer_id);
  147. if (!$transferModel) {
  148. return '转账记录不存在';
  149. }
  150. // 获取资金类型名称映射
  151. $fundNames = AccountService::getFundsDesc();
  152. // 获取用户名称映射
  153. $userNames = [];
  154. $userIds = [$this->userId, $toUserId];
  155. $users = \App\Module\User\Models\User::whereIn('id', $userIds)->get();
  156. foreach ($users as $user) {
  157. $userNames[$user->id] = $user->username;
  158. }
  159. // 将模型转换为DTO并返回
  160. return TransferResultDto::fromModel($transferModel, $fundNames, $userNames);
  161. }
  162. /**
  163. * 贸易(业务关联)
  164. * 同账户/同币种,不同用户
  165. *
  166. * @param int $toUserId 接收用户ID
  167. * @param int $amount 金额
  168. * @param string $transfer_type 关联类型
  169. * @param string $transfer_id 关联ID
  170. * @param string $remark 备注
  171. * @return TradeResultDto|string 成功返回DTO,失败返回错误信息
  172. */
  173. public function trade(int $toUserId, int $amount, $transfer_type, $transfer_id, string $remark)
  174. {
  175. # 检查事务开启
  176. Helper::check_tr();
  177. $full_transfer_id = $transfer_type.'-'.$transfer_id;
  178. # 先减少自己的
  179. $re46 = User::handle($this->userId, $this->fundId, -$amount, LOG_TYPE::TRADE, $full_transfer_id, $remark);
  180. if (is_string($re46)) {
  181. \UCore\Trace::addData('error', $re46);
  182. return $re46;
  183. }
  184. # 再增加别人的
  185. $re51 = User::handle($toUserId, $this->fundId, $amount, LOG_TYPE::TRANSFER, $full_transfer_id, $remark);
  186. if (is_string($re51)) {
  187. \UCore\Trace::addData('error', $re51);
  188. return $re51;
  189. }
  190. // 获取资金类型名称映射
  191. $fundNames = AccountService::getFundsDesc();
  192. // 获取用户名称映射
  193. $userNames = [];
  194. $userIds = [$this->userId, $toUserId];
  195. $users = \App\Module\User\Models\User::whereIn('id', $userIds)->get();
  196. foreach ($users as $user) {
  197. $userNames[$user->id] = $user->username;
  198. }
  199. // 创建DTO并返回
  200. return TradeResultDto::create(
  201. $this->userId,
  202. $toUserId,
  203. $this->fundId,
  204. $amount,
  205. $transfer_type,
  206. $transfer_id,
  207. $remark,
  208. $fundNames,
  209. $userNames
  210. );
  211. }
  212. /**
  213. * Admin 资金操作
  214. *
  215. * @param int $admin_id
  216. * @param FUND_TYPE $fund_id
  217. * @param int $fund_fee
  218. * @param $remark
  219. * @return FundAdminDto|string 成功返回DTO,失败返回错误信息
  220. */
  221. public function admin_operate(int $admin_id, FUND_TYPE $fund_id, int $fund_fee, $remark)
  222. {
  223. $data = [
  224. 'total_fee' => $fund_fee,
  225. 'status' => 1,
  226. 'user_id' => $this->userId,
  227. 'fund_id' => $fund_id->valueInt(),
  228. 'admin_id' => $admin_id,
  229. 'create_time' => time(),
  230. 'remark' => $remark
  231. ];
  232. # 启动事务
  233. DB::beginTransaction();
  234. # 写日志
  235. $fund_admin = new FundAdminModel();
  236. $fund_admin->setData($data);
  237. if ($fund_admin->save() === false) {
  238. DB::rollBack();
  239. return '_Model-error';
  240. }
  241. # 进行资金操作
  242. $re = \App\Module\Fund\Logic\User::handle($this->userId, $fund_id->value, $fund_fee,
  243. \App\Module\Fund\Enums\LOG_TYPE::ADMIN, $fund_admin->id, $remark);
  244. if (is_string($re)) {
  245. DB::rollBack();
  246. return $re;
  247. }
  248. DB::commit();
  249. // 获取资金类型名称映射
  250. $fundNames = AccountService::getFundsDesc();
  251. // 将模型转换为DTO并返回
  252. return FundAdminDto::fromModel($fund_admin, $fundNames);
  253. }
  254. }