FundService.php 12 KB

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