| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- declare (strict_types=1);
- namespace Modules\Manage\Controllers;
- use App\Constants\ChangeLogConstant;
- use App\Model\UserModel;
- use App\Model\WithdrawalLogModel;
- use App\Model\WithdrawalMethodModel;
- use App\Repositories\Service\QueueService;
- use App\Repositories\UserRepository;
- use Hyperf\DbConnection\Db;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- /**
- * 提现相关
- */
- class WithdrawController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 提现方式列表
- */
- public function userWithdrawalMethodList(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'required|int', 'page_size' => 'required|int', 'search' => 'nullable|array'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $userUid = $inputData['search']['user_uid'];
- $type = $inputData['search']['type'];
- return json_success(WithdrawalMethodModel::query()->where(function ($q) use($userUid, $type) {
- if ($userUid) {
- $q->where('user_uid', $userUid);
- }
- if ($type) {
- $q->where('type', $type);
- }
- })->paginate((int) $inputData['page_size'], ['user_uid', 'type', 'username', 'account', 'bank', 'created_at'], 'page', (int) $inputData['page']));
- }
- /**
- * 提现记录
- * @return mixed
- */
- public function withdrawalLogList(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'required|int', 'page_size' => 'required|int', 'search' => 'nullable|array'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $orderNumber = $inputData['search']['order_number'];
- $status = $inputData['search']['status'];
- return json_success(WithdrawalLogModel::query()->where('type', '!=', 4)->where(function ($q) use($orderNumber, $status) {
- if ($orderNumber) {
- $q->where('order_number', $orderNumber);
- }
- if ($status) {
- $q->where('status', $status);
- }
- })->orderByDesc('created_at')->paginate((int) $inputData['page_size'], ['id', 'order_number', 'amount', 'type', 'username', 'user_uid', 'account', 'bank', 'balance_forward', 'balance_rear', 'freeze_forward', 'freeze_rear', 'usdt_to_amount', 'usdt_proportion', 'status', 'remark', 'created_at', 'updated_at'], 'page', (int) $inputData['page']));
- }
- /**
- * 余额转金币列表
- * @param ValidatorFactoryInterface $validatorFactory
- */
- public function BalanceChangeCoinList(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'required|int', 'page_size' => 'required|int', 'search' => 'nullable|array'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $userUid = $inputData['search']['user_uid'];
- return json_success(WithdrawalLogModel::query()->where('type', '=', 4)->where(function ($q) use($userUid) {
- if ($userUid) {
- $q->where('user_uid', $userUid);
- }
- })->paginate((int) $inputData['page_size'], ['id', 'user_uid', 'amount', 'balance_forward', 'balance_rear', 'coins', 'forward_coins', 'rear_coins', 'coin_proportion', 'created_at'], 'page', (int) $inputData['page']));
- }
- /**
- * 操作接口
- * @param ValidatorFactoryInterface $validatorFactory
- */
- public function operateWithdraw(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['status' => 'required|int', 'id' => 'required|int', 'remark' => 'nullable|string'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $status = $inputData['status'];
- //,2已到账,3失败,4冻结
- $remark = $inputData['remark'];
- //,2已到账,3失败,4冻结
- // 查询提现记录
- $withdrawalLogModel = WithdrawalLogModel::query()->where('id', $inputData['id'])->first();
- if (!$withdrawalLogModel) {
- return json_fail('没有该记录!');
- }
- if ($withdrawalLogModel->status !== 1) {
- return json_fail('该订单无需操作!');
- }
- $oldWithdrawalLogModel = clone $withdrawalLogModel;
- //查询会员
- $userInfo = UserModel::query()->where('code', $withdrawalLogModel->user_uid)->first();
- if (!$userInfo) {
- return json_fail('会员不存在!');
- }
- $oldUserInfo = clone $userInfo;
- $amount = $withdrawalLogModel->amount;
- // 是否是usdt提现
- if ($withdrawalLogModel->type === 1) {
- $amount = $withdrawalLogModel->usdt_to_amount;
- }
- //查询会员
- switch ($status) {
- case 2:
- //2已到账
- Db::beginTransaction();
- try {
- //会员扣除冻结
- $userInfo->freeze = $userInfo->freeze - $amount;
- if (!$userInfo->save()) {
- Db::rollBack();
- return json_fail('-001 订单出错!');
- }
- // 订单状态更换
- $withdrawalLogModel->status = 2;
- if (!$withdrawalLogModel->save()) {
- Db::rollBack();
- return json_fail('-002 订单出错!');
- }
- $this->log('操作提现(到账)订单号:' . $withdrawalLogModel->order_number, 2, ['after_items' => [$withdrawalLogModel->toArray()], 'before_items' => [$oldWithdrawalLogModel->toArray()]]);
- $this->log('操作提现(到账)订单扣除会员冻结资金,会员id:' . $userInfo->id, $amount, ['after_items' => [$userInfo->toArray()], 'before_items' => [$oldUserInfo->toArray()]]);
- make(UserRepository::class)->delUserCache($userInfo->code);
- Db::commit();
- } catch (\Exception $exception) {
- Db::rollBack();
- return json_fail('-003 订单出错!');
- }
- break;
- case 3:
- //3失败
- Db::beginTransaction();
- try {
- //会员扣除冻结并且追加余额
- $userInfo->freeze = $userInfo->freeze - $amount;
- $userInfo->balance = $userInfo->balance + $amount;
- if (!$userInfo->save()) {
- Db::rollBack();
- return json_fail('-001 订单出错!');
- }
- // 订单状态更换
- $withdrawalLogModel->status = 3;
- $withdrawalLogModel->remark = $remark;
- if (!$withdrawalLogModel->save()) {
- Db::rollBack();
- return json_fail('-002 订单出错!');
- }
- $this->log('操作提现(失败)订单号:' . $withdrawalLogModel->order_number, 3, ['after_items' => [$withdrawalLogModel->toArray()], 'before_items' => [$oldWithdrawalLogModel->toArray()]]);
- $this->log('操作提现(失败)订单扣除会员冻结资金,会员id:' . $userInfo->id, $amount, ['after_items' => [$userInfo->toArray()], 'before_items' => [$oldUserInfo->toArray()]]);
- Db::commit();
- // 帐变记录
- $changeData = ['user_uid' => $userInfo->code, 'currency_type' => ChangeLogConstant::CHANGE_AMOUNT, 'amount' => $amount, 'way' => ChangeLogConstant::WAY_1, 'type' => ChangeLogConstant::TYPE_21];
- make(QueueService::class)->saveChangeLogJob($changeData);
- } catch (\Exception $exception) {
- Db::rollBack();
- return json_fail('-023 订单出错!');
- }
- break;
- case 4:
- //4冻结
- return json_fail('目前不支持!');
- break;
- default:
- return json_fail('操作有误!');
- break;
- }
- return json_success('订单操作成功');
- }
- }
|