Modules_Manage_Controllers_WithdrawController.proxy.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. declare (strict_types=1);
  3. namespace Modules\Manage\Controllers;
  4. use App\Constants\ChangeLogConstant;
  5. use App\Model\UserModel;
  6. use App\Model\WithdrawalLogModel;
  7. use App\Model\WithdrawalMethodModel;
  8. use App\Repositories\Service\QueueService;
  9. use App\Repositories\UserRepository;
  10. use Hyperf\DbConnection\Db;
  11. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  12. /**
  13. * 提现相关
  14. */
  15. class WithdrawController extends BaseController
  16. {
  17. use \Hyperf\Di\Aop\ProxyTrait;
  18. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  19. function __construct()
  20. {
  21. if (method_exists(parent::class, '__construct')) {
  22. parent::__construct(...func_get_args());
  23. }
  24. $this->__handlePropertyHandler(__CLASS__);
  25. }
  26. /**
  27. * 提现方式列表
  28. */
  29. public function userWithdrawalMethodList(ValidatorFactoryInterface $validatorFactory)
  30. {
  31. $inputData = $this->getJsonData();
  32. $rules = ['page' => 'required|int', 'page_size' => 'required|int', 'search' => 'nullable|array'];
  33. $validator = $validatorFactory->make($inputData, $rules);
  34. if ($validator->fails()) {
  35. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  36. return json_fail($validator->errors()->first());
  37. }
  38. $userUid = $inputData['search']['user_uid'];
  39. $type = $inputData['search']['type'];
  40. return json_success(WithdrawalMethodModel::query()->where(function ($q) use($userUid, $type) {
  41. if ($userUid) {
  42. $q->where('user_uid', $userUid);
  43. }
  44. if ($type) {
  45. $q->where('type', $type);
  46. }
  47. })->paginate((int) $inputData['page_size'], ['user_uid', 'type', 'username', 'account', 'bank', 'created_at'], 'page', (int) $inputData['page']));
  48. }
  49. /**
  50. * 提现记录
  51. * @return mixed
  52. */
  53. public function withdrawalLogList(ValidatorFactoryInterface $validatorFactory)
  54. {
  55. $inputData = $this->getJsonData();
  56. $rules = ['page' => 'required|int', 'page_size' => 'required|int', 'search' => 'nullable|array'];
  57. $validator = $validatorFactory->make($inputData, $rules);
  58. if ($validator->fails()) {
  59. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  60. return json_fail($validator->errors()->first());
  61. }
  62. $orderNumber = $inputData['search']['order_number'];
  63. $status = $inputData['search']['status'];
  64. return json_success(WithdrawalLogModel::query()->where('type', '!=', 4)->where(function ($q) use($orderNumber, $status) {
  65. if ($orderNumber) {
  66. $q->where('order_number', $orderNumber);
  67. }
  68. if ($status) {
  69. $q->where('status', $status);
  70. }
  71. })->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']));
  72. }
  73. /**
  74. * 余额转金币列表
  75. * @param ValidatorFactoryInterface $validatorFactory
  76. */
  77. public function BalanceChangeCoinList(ValidatorFactoryInterface $validatorFactory)
  78. {
  79. $inputData = $this->getJsonData();
  80. $rules = ['page' => 'required|int', 'page_size' => 'required|int', 'search' => 'nullable|array'];
  81. $validator = $validatorFactory->make($inputData, $rules);
  82. if ($validator->fails()) {
  83. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  84. return json_fail($validator->errors()->first());
  85. }
  86. $userUid = $inputData['search']['user_uid'];
  87. return json_success(WithdrawalLogModel::query()->where('type', '=', 4)->where(function ($q) use($userUid) {
  88. if ($userUid) {
  89. $q->where('user_uid', $userUid);
  90. }
  91. })->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']));
  92. }
  93. /**
  94. * 操作接口
  95. * @param ValidatorFactoryInterface $validatorFactory
  96. */
  97. public function operateWithdraw(ValidatorFactoryInterface $validatorFactory)
  98. {
  99. $inputData = $this->getJsonData();
  100. $rules = ['status' => 'required|int', 'id' => 'required|int', 'remark' => 'nullable|string'];
  101. $validator = $validatorFactory->make($inputData, $rules);
  102. if ($validator->fails()) {
  103. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  104. return json_fail($validator->errors()->first());
  105. }
  106. $status = $inputData['status'];
  107. //,2已到账,3失败,4冻结
  108. $remark = $inputData['remark'];
  109. //,2已到账,3失败,4冻结
  110. // 查询提现记录
  111. $withdrawalLogModel = WithdrawalLogModel::query()->where('id', $inputData['id'])->first();
  112. if (!$withdrawalLogModel) {
  113. return json_fail('没有该记录!');
  114. }
  115. if ($withdrawalLogModel->status !== 1) {
  116. return json_fail('该订单无需操作!');
  117. }
  118. $oldWithdrawalLogModel = clone $withdrawalLogModel;
  119. //查询会员
  120. $userInfo = UserModel::query()->where('code', $withdrawalLogModel->user_uid)->first();
  121. if (!$userInfo) {
  122. return json_fail('会员不存在!');
  123. }
  124. $oldUserInfo = clone $userInfo;
  125. $amount = $withdrawalLogModel->amount;
  126. // 是否是usdt提现
  127. if ($withdrawalLogModel->type === 1) {
  128. $amount = $withdrawalLogModel->usdt_to_amount;
  129. }
  130. //查询会员
  131. switch ($status) {
  132. case 2:
  133. //2已到账
  134. Db::beginTransaction();
  135. try {
  136. //会员扣除冻结
  137. $userInfo->freeze = $userInfo->freeze - $amount;
  138. if (!$userInfo->save()) {
  139. Db::rollBack();
  140. return json_fail('-001 订单出错!');
  141. }
  142. // 订单状态更换
  143. $withdrawalLogModel->status = 2;
  144. if (!$withdrawalLogModel->save()) {
  145. Db::rollBack();
  146. return json_fail('-002 订单出错!');
  147. }
  148. $this->log('操作提现(到账)订单号:' . $withdrawalLogModel->order_number, 2, ['after_items' => [$withdrawalLogModel->toArray()], 'before_items' => [$oldWithdrawalLogModel->toArray()]]);
  149. $this->log('操作提现(到账)订单扣除会员冻结资金,会员id:' . $userInfo->id, $amount, ['after_items' => [$userInfo->toArray()], 'before_items' => [$oldUserInfo->toArray()]]);
  150. make(UserRepository::class)->delUserCache($userInfo->code);
  151. Db::commit();
  152. } catch (\Exception $exception) {
  153. Db::rollBack();
  154. return json_fail('-003 订单出错!');
  155. }
  156. break;
  157. case 3:
  158. //3失败
  159. Db::beginTransaction();
  160. try {
  161. //会员扣除冻结并且追加余额
  162. $userInfo->freeze = $userInfo->freeze - $amount;
  163. $userInfo->balance = $userInfo->balance + $amount;
  164. if (!$userInfo->save()) {
  165. Db::rollBack();
  166. return json_fail('-001 订单出错!');
  167. }
  168. // 订单状态更换
  169. $withdrawalLogModel->status = 3;
  170. $withdrawalLogModel->remark = $remark;
  171. if (!$withdrawalLogModel->save()) {
  172. Db::rollBack();
  173. return json_fail('-002 订单出错!');
  174. }
  175. $this->log('操作提现(失败)订单号:' . $withdrawalLogModel->order_number, 3, ['after_items' => [$withdrawalLogModel->toArray()], 'before_items' => [$oldWithdrawalLogModel->toArray()]]);
  176. $this->log('操作提现(失败)订单扣除会员冻结资金,会员id:' . $userInfo->id, $amount, ['after_items' => [$userInfo->toArray()], 'before_items' => [$oldUserInfo->toArray()]]);
  177. Db::commit();
  178. // 帐变记录
  179. $changeData = ['user_uid' => $userInfo->code, 'currency_type' => ChangeLogConstant::CHANGE_AMOUNT, 'amount' => $amount, 'way' => ChangeLogConstant::WAY_1, 'type' => ChangeLogConstant::TYPE_21];
  180. make(QueueService::class)->saveChangeLogJob($changeData);
  181. } catch (\Exception $exception) {
  182. Db::rollBack();
  183. return json_fail('-023 订单出错!');
  184. }
  185. break;
  186. case 4:
  187. //4冻结
  188. return json_fail('目前不支持!');
  189. break;
  190. default:
  191. return json_fail('操作有误!');
  192. break;
  193. }
  194. return json_success('订单操作成功');
  195. }
  196. }