FundService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 float $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, float $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. $currency = CurrencyService::getCurrencyByFundId($this->fundId);
  54. if (!$currency) {
  55. return '无法获取币种信息';
  56. }
  57. # 验证并格式化金额精度
  58. if (!CurrencyService::validateAmountPrecision($amount, $currency->currencyId)) {
  59. return '金额精度超出币种支持范围';
  60. }
  61. $formattedAmount = CurrencyService::formatAmountToPrecision($amount, $currency->currencyId);
  62. # 实例化操作对象
  63. Helper::check_tr();
  64. # 先进行转账记录
  65. $circulation_id = Circulation::handle($this->userId, $this->fundId, $to_fund_id->valueInt(), $formattedAmount, $re_id, $re_type,
  66. $remark);
  67. if (is_string($circulation_id)) {
  68. return $circulation_id;
  69. }
  70. # 进行双方的资金修改
  71. # 先减少自己的
  72. $re46 = User::handle($this->userId, $this->fundId, -$formattedAmount, LOG_TYPE::CIRCULATION, $circulation_id, $remark);
  73. if (is_string($re46)) {
  74. return $re46;
  75. }
  76. # 再增加自己另一个账户
  77. $re51 = User::handle($this->userId, $to_fund_id->valueInt(), $formattedAmount, LOG_TYPE::CIRCULATION, $circulation_id, $remark);
  78. if (is_string($re51)) {
  79. return $re51;
  80. }
  81. // 获取流转记录
  82. $circulationModel = \App\Module\Fund\Models\FundCirculationModel::find($circulation_id);
  83. if (!$circulationModel) {
  84. return '流转记录不存在';
  85. }
  86. // 获取资金类型名称映射
  87. $fundNames = AccountService::getFundsDesc();
  88. // 将模型转换为DTO并返回
  89. return CirculationDto::fromModel($circulationModel, $fundNames);
  90. }
  91. /**
  92. * 获取账户信息
  93. *
  94. * @return FundAccountDto|null 账户DTO,如果账户不存在则返回null
  95. */
  96. public function getAccount(): ?FundAccountDto
  97. {
  98. // 如果账户不存在,返回null
  99. if (!$this->fundModel) {
  100. return null;
  101. }
  102. // 获取资金类型名称映射
  103. $fundNames = AccountService::getFundsDesc();
  104. // 将模型转换为DTO
  105. return FundAccountDto::fromModel($this->fundModel, $fundNames);
  106. }
  107. /**
  108. * 获取余额
  109. *
  110. * @return float 账户余额(小数形式)
  111. */
  112. public function balance(): float
  113. {
  114. // 直接返回数据库中的小数值
  115. return (float)($this->fundModel->balance ?? 0);
  116. }
  117. /**
  118. * 转账
  119. * (不同人,同账户/同币种)
  120. *
  121. * @param int $toUserId 接收用户ID
  122. * @param float $amount 金额(小数形式)
  123. * @param string $remark 备注
  124. * @return TransferResultDto|string 成功返回DTO,失败返回错误信息
  125. */
  126. public function transfer(int $toUserId, float $amount, string $remark)
  127. {
  128. # 验证金额精度
  129. $currency = CurrencyService::getCurrencyByFundId($this->fundId);
  130. if (!$currency) {
  131. return '无法获取币种信息';
  132. }
  133. # 验证并格式化金额精度
  134. if (!CurrencyService::validateAmountPrecision($amount, $currency->currencyId)) {
  135. return '金额精度超出币种支持范围';
  136. }
  137. $formattedAmount = CurrencyService::formatAmountToPrecision($amount, $currency->currencyId);
  138. # 实例化操作对象
  139. # 开启事务
  140. DB::beginTransaction();
  141. # 先进行转账记录
  142. $transfer_id = Transfer::to_user(
  143. $this->userId,
  144. $this->fundId,
  145. $toUserId,
  146. $formattedAmount,
  147. $remark);
  148. if (is_string($transfer_id)) {
  149. DB::rollBack();
  150. return $transfer_id;
  151. }
  152. # 进行双方的资金修改
  153. # 先减少自己的
  154. $re46 = User::handle($this->userId, $this->fundId, -$formattedAmount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
  155. if (is_string($re46)) {
  156. DB::rollBack();
  157. return $re46;
  158. }
  159. # 再增加别人的
  160. $re51 = User::handle($toUserId, $this->fundId, $formattedAmount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
  161. if (is_string($re51)) {
  162. DB::rollBack();
  163. return $re51;
  164. }
  165. DB::commit();
  166. // 获取转账记录
  167. $transferModel = \App\Module\Fund\Models\FundTransferModel::find($transfer_id);
  168. if (!$transferModel) {
  169. return '转账记录不存在';
  170. }
  171. // 获取资金类型名称映射
  172. $fundNames = AccountService::getFundsDesc();
  173. // 获取用户名称映射
  174. $userNames = [];
  175. $userIds = [$this->userId, $toUserId];
  176. $users = \App\Module\User\Models\User::whereIn('id', $userIds)->get();
  177. foreach ($users as $user) {
  178. $userNames[$user->id] = $user->username;
  179. }
  180. // 将模型转换为DTO并返回
  181. return TransferResultDto::fromModel($transferModel, $fundNames, $userNames);
  182. }
  183. /**
  184. * 贸易(业务关联)
  185. * 同账户/同币种,不同用户
  186. *
  187. * @param int $toUserId 接收用户ID
  188. * @param float $amount 金额(小数形式)
  189. * @param string $transfer_type 关联类型
  190. * @param string $transfer_id 关联ID
  191. * @param string $remark 备注
  192. * @return TradeResultDto|string 成功返回DTO,失败返回错误信息
  193. */
  194. public function trade(int $toUserId, float $amount, $transfer_type, $transfer_id, string $remark)
  195. {
  196. # 验证金额精度
  197. $currency = CurrencyService::getCurrency2ByFundId($this->fundId);
  198. if (!$currency) {
  199. return '无法获取币种信息';
  200. }
  201. # 验证并格式化金额精度
  202. if (!CurrencyService::validateAmountPrecision($amount, $currency->value)) {
  203. return '金额精度超出币种支持范围';
  204. }
  205. $formattedAmount = CurrencyService::formatAmountToPrecision($amount, $currency->value);
  206. # 检查事务开启
  207. Helper::check_tr();
  208. $full_transfer_id = $transfer_type.'-'.$transfer_id;
  209. # 先减少自己的
  210. $re46 = User::handle($this->userId, $this->fundId, -$formattedAmount, LOG_TYPE::TRADE, $full_transfer_id, $remark);
  211. if (is_string($re46)) {
  212. \UCore\Trace::addData('error', $re46);
  213. return $re46;
  214. }
  215. # 再增加别人的
  216. $re51 = User::handle($toUserId, $this->fundId, $formattedAmount, LOG_TYPE::TRANSFER, $full_transfer_id, $remark);
  217. if (is_string($re51)) {
  218. \UCore\Trace::addData('error', $re51);
  219. return $re51;
  220. }
  221. // 获取资金类型名称映射
  222. $fundNames = AccountService::getFundsDesc();
  223. // 获取用户名称映射
  224. $userNames = [];
  225. $userIds = [$this->userId, $toUserId];
  226. $users = \App\Module\User\Models\User::whereIn('id', $userIds)->get();
  227. foreach ($users as $user) {
  228. $userNames[$user->id] = $user->username;
  229. }
  230. // 创建DTO并返回(传入原始小数金额)
  231. return TradeResultDto::create(
  232. $this->userId,
  233. $toUserId,
  234. $this->fundId,
  235. $amount,
  236. $transfer_type,
  237. $transfer_id,
  238. $remark,
  239. $fundNames,
  240. $userNames
  241. );
  242. }
  243. /**
  244. * Admin 资金操作
  245. *
  246. * @param int $admin_id
  247. * @param FUND_TYPE $fund_id
  248. * @param float $fund_fee 金额(小数形式)
  249. * @param $remark
  250. * @return FundAdminDto|string 成功返回DTO,失败返回错误信息
  251. */
  252. public function admin_operate(int $admin_id, FUND_TYPE $fund_id, float $fund_fee, $remark)
  253. {
  254. # 验证金额精度
  255. $currency = CurrencyService::getCurrencyByFundId($fund_id->valueInt());
  256. if (!$currency) {
  257. return '无法获取币种信息';
  258. }
  259. # 验证并格式化金额精度
  260. if (!CurrencyService::validateAmountPrecision($fund_fee, $currency->currencyId)) {
  261. return '金额精度超出币种支持范围';
  262. }
  263. $formattedFundFee = CurrencyService::formatAmountToPrecision($fund_fee, $currency->currencyId);
  264. $data = [
  265. 'total_fee' => $formattedFundFee,
  266. 'status' => 1,
  267. 'user_id' => $this->userId,
  268. 'fund_id' => $fund_id->valueInt(),
  269. 'admin_id' => $admin_id,
  270. 'create_time' => time(),
  271. 'remark' => $remark
  272. ];
  273. # 启动事务
  274. DB::beginTransaction();
  275. # 写日志
  276. $fund_admin = new FundAdminModel();
  277. $fund_admin->setData($data);
  278. if ($fund_admin->save() === false) {
  279. DB::rollBack();
  280. return '_Model-error';
  281. }
  282. # 进行资金操作
  283. $re = \App\Module\Fund\Logic\User::handle($this->userId, $fund_id->value, $formattedFundFee,
  284. \App\Module\Fund\Enums\LOG_TYPE::ADMIN, $fund_admin->id, $remark);
  285. if (is_string($re)) {
  286. DB::rollBack();
  287. return $re;
  288. }
  289. DB::commit();
  290. // 获取资金类型名称映射
  291. $fundNames = AccountService::getFundsDesc();
  292. // 将模型转换为DTO并返回
  293. return FundAdminDto::fromModel($fund_admin, $fundNames);
  294. }
  295. }