| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace App\Module\Fund\Services;
- use App\Module\Fund\Enums\FUND_TYPE;
- use App\Module\Fund\Enums\LOG_TYPE;
- use App\Module\Fund\Models\FundAdminModel;
- use App\Module\Fund\Models\FundModel;
- use App\Module\Fund\Service\Circulation;
- use App\Module\Fund\Service\Transfer;
- use App\Module\Fund\Service\User;
- use Illuminate\Support\Facades\DB;
- use UCore\Db\Helper;
- class FundService
- {
- private int $userId;
- private int $fundId;
- private FundModel $fundModel;
- public function __construct(int $userId, $fundId)
- {
- $this->userId = $userId;
- if(is_int($fundId)){
- $this->fundId = $fundId;
- }else{
- $this->fundId = $fundId->valueInt();
- }
- $this->fundModel = FundModel::query()
- ->where('user_id', $userId)
- ->where('fund_id', $fundId)
- ->first();
- }
- /**
- * 资金流转
- * (同用户,同币种,不同账户)
- *
- * @param int $to_fund
- * @param int $amount
- * @param string $remark
- */
- public function circulation(FUND_TYPE $to_fund_id, int $amount, int $re_id, string $re_type, string $remark)
- {
- # 确认货币种类一致
- if (!CurrencyService::check($this->fundId, $to_fund_id->valueInt())) {
- return '币种不一致,禁止划转';
- }
- # 实例化操作对象
- Helper::check_tr();
- # 先进行转账记录
- $re_id = Circulation::handle($this->userId, $this->fundId, $to_fund_id->valueInt(), $amount, $re_id, $re_type,
- $remark);
- if (is_string($re_id)) {
- return $re_id;
- }
- # 进行双方的资金修改
- # 先减少自己的
- $re46 = User::handle($this->userId, $this->fundId, -$amount, LOG_TYPE::CIRCULATION, $re_id, $remark);
- if (is_string($re46)) {
- return $re46;
- }
- # 再增加自己另一个账户
- $re51 = User::handle($this->userId, $to_fund_id->valueInt(), $amount, LOG_TYPE::CIRCULATION, $re_id, $remark);
- if (is_string($re51)) {
- return $re51;
- }
- return true;
- }
- /**
- * 获取余额
- */
- public function balance(): int
- {
- return $this->fundModel->balance ?? 0;
- }
- /**
- * 转账
- * (不同人,同账户/同币种)
- */
- public function transfer(int $toUserId, int $amount, string $remark)
- {
- # 实例化操作对象
- # 开启事务
- DB::beginTransaction();
- # 先进行转账记录
- $transfer_id = Transfer::to_user(
- $this->userId,
- $this->fundId,
- $toUserId,
- $amount,
- $remark);
- if (is_string($transfer_id)) {
- DB::rollBack();
- return $transfer_id;
- }
- # 进行双方的资金修改
- # 先减少自己的
- $re46 = User::handle($this->userId, $this->fundId, -$amount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
- if (is_string($re46)) {
- DB::rollBack();
- return $re46;
- }
- # 再增加别人的
- $re51 = User::handle($toUserId, $this->fundId, $amount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
- if (is_string($re51)) {
- DB::rollBack();
- return $re51;
- }
- DB::commit();
- return true;
- }
- /**
- * 贸易(业务关联)
- * 同账户/同币种,不同用户
- *
- * @param int $toUserId
- * @param int $amount
- * @param string $transfer_type
- * @param string $transfer_id
- * @param string $remark
- * @return string|true
- */
- public function trade(int $toUserId, int $amount, $transfer_type, $transfer_id, string $remark)
- {
- # 检查事务开启
- Helper::check_tr();
- $transfer_id = $transfer_type.'-'.$transfer_id;
- # 先减少自己的
- $re46 = User::handle($this->userId, $this->fundId, -$amount, LOG_TYPE::TRADE, $transfer_id, $remark);
- if (is_string($re46)) {
- \UCore\Trace::addData('error', $re46);
- return $re46;
- }
- # 再增加别人的
- $re51 = User::handle($toUserId, $this->fundId, $amount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
- if (is_string($re51)) {
- \UCore\Trace::addData('error', $re51);
- return $re51;
- }
- return true;
- }
- /**
- * Admin 资金操作
- *
- * @param int $admin_id
- * @param FUND_TYPE $fund_id
- * @param int $fund_fee
- * @param $remark
- * @return bool|string
- */
- public function admin_operate(int $admin_id, FUND_TYPE $fund_id, int $fund_fee, $remark)
- {
- $data = [
- 'total_fee' => $fund_fee,
- 'status' => 1,
- 'user_id' => $this->userId,
- 'fund_id' => $fund_id->valueInt(),
- 'admin_id' => $admin_id,
- 'create_time' => time(),
- 'remark' => $remark
- ];
- # 启动事务
- DB::beginTransaction();
- # 写日志
- $fund_admin = new FundAdminModel();
- $fund_admin->setData($data);
- if ($fund_admin->save() === false) {
- DB::rollBack();
- return '_Model-error';
- }
- # 进行资金操作
- $re = \App\Module\Fund\Service\User::handle($this->userId, $fund_id->value, $fund_fee,
- \App\Module\Fund\Enums\LOG_TYPE::ADMIN, $fund_admin->id, $remark);
- if (is_string($re)) {
- DB::rollBack();
- return $re;
- }
- DB::commit();
- return $re;
- }
- }
|