WithdrawController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /**
  18. * 提现方式列表
  19. */
  20. public function userWithdrawalMethodList(ValidatorFactoryInterface $validatorFactory)
  21. {
  22. $inputData = $this->getJsonData();
  23. $rules = [
  24. 'page' => 'required|int',
  25. 'page_size' => 'required|int',
  26. 'search' => 'nullable|array',
  27. ];
  28. $validator = $validatorFactory->make($inputData, $rules);
  29. if ($validator->fails()) {
  30. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  31. return json_fail($validator->errors()->first());
  32. }
  33. $userUid = $inputData['search']['user_uid'];
  34. $type = $inputData['search']['type'];
  35. return json_success(WithdrawalMethodModel::query()
  36. ->where(function ($q) use ($userUid, $type) {
  37. if ($userUid) {
  38. $q->where('user_uid', $userUid);
  39. }
  40. if ($type) {
  41. $q->where('type', $type);
  42. }
  43. })
  44. ->paginate((int)$inputData['page_size'],
  45. ['user_uid', 'type', 'username', 'account', 'bank', 'created_at'],
  46. 'page',
  47. (int)$inputData['page'])
  48. );
  49. }
  50. /**
  51. * 提现记录
  52. * @return mixed
  53. */
  54. public function withdrawalLogList(ValidatorFactoryInterface $validatorFactory)
  55. {
  56. $inputData = $this->getJsonData();
  57. $rules = [
  58. 'page' => 'required|int',
  59. 'page_size' => 'required|int',
  60. 'search' => 'nullable|array',
  61. ];
  62. $validator = $validatorFactory->make($inputData, $rules);
  63. if ($validator->fails()) {
  64. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  65. return json_fail($validator->errors()->first());
  66. }
  67. $orderNumber = $inputData['search']['order_number'];
  68. $status = $inputData['search']['status'];
  69. return json_success(WithdrawalLogModel::query()
  70. ->where('type', '!=', 4)
  71. ->where(function ($q) use ($orderNumber, $status) {
  72. if ($orderNumber) {
  73. $q->where('order_number', $orderNumber);
  74. }
  75. if ($status) {
  76. $q->where('status', $status);
  77. }
  78. })
  79. ->orderByDesc('created_at')
  80. ->paginate((int)$inputData['page_size'], [
  81. 'id',
  82. 'order_number',
  83. 'amount',
  84. 'type',
  85. 'username',
  86. 'user_uid',
  87. 'account',
  88. 'bank',
  89. 'balance_forward',
  90. 'balance_rear',
  91. 'freeze_forward',
  92. 'freeze_rear',
  93. 'usdt_to_amount',
  94. 'usdt_proportion',
  95. 'status',
  96. 'remark',
  97. 'created_at',
  98. 'updated_at'], 'page', (int)$inputData['page'])
  99. );
  100. }
  101. /**
  102. * 余额转金币列表
  103. * @param ValidatorFactoryInterface $validatorFactory
  104. */
  105. public function BalanceChangeCoinList(ValidatorFactoryInterface $validatorFactory)
  106. {
  107. $inputData = $this->getJsonData();
  108. $rules = [
  109. 'page' => 'required|int',
  110. 'page_size' => 'required|int',
  111. 'search' => 'nullable|array'
  112. ];
  113. $validator = $validatorFactory->make($inputData, $rules);
  114. if ($validator->fails()) {
  115. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  116. return json_fail($validator->errors()->first());
  117. }
  118. $userUid = $inputData['search']['user_uid'];
  119. return json_success(WithdrawalLogModel::query()
  120. ->where('type', '=', 4)
  121. ->where(function($q)use($userUid){
  122. if ($userUid){
  123. $q->where('user_uid',$userUid);
  124. }
  125. })
  126. ->paginate((int)$inputData['page_size'],
  127. [
  128. 'id',
  129. 'user_uid',
  130. 'amount',
  131. 'balance_forward',
  132. 'balance_rear',
  133. 'coins',
  134. 'forward_coins',
  135. 'rear_coins',
  136. 'coin_proportion',
  137. 'created_at'
  138. ],
  139. 'page', (int)$inputData['page']));
  140. }
  141. /**
  142. * 操作接口
  143. * @param ValidatorFactoryInterface $validatorFactory
  144. */
  145. public function operateWithdraw(ValidatorFactoryInterface $validatorFactory)
  146. {
  147. $inputData = $this->getJsonData();
  148. $rules = [
  149. 'status' => 'required|int',
  150. 'id' => 'required|int',
  151. 'remark'=>'nullable|string'
  152. ];
  153. $validator = $validatorFactory->make($inputData, $rules);
  154. if ($validator->fails()) {
  155. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  156. return json_fail($validator->errors()->first());
  157. }
  158. $status = $inputData['status'];//,2已到账,3失败,4冻结
  159. $remark = $inputData['remark'];//,2已到账,3失败,4冻结
  160. // 查询提现记录
  161. $withdrawalLogModel = WithdrawalLogModel::query()->where('id', $inputData['id'])->first();
  162. if (!$withdrawalLogModel) {
  163. return json_fail('没有该记录!');
  164. }
  165. if ($withdrawalLogModel->status !== 1) {
  166. return json_fail('该订单无需操作!');
  167. }
  168. $oldWithdrawalLogModel = clone $withdrawalLogModel;
  169. //查询会员
  170. $userInfo = UserModel::query()->where('code', $withdrawalLogModel->user_uid)->first();
  171. if (!$userInfo){
  172. return json_fail('会员不存在!');
  173. }
  174. $oldUserInfo = clone $userInfo;
  175. $amount = $withdrawalLogModel->amount;
  176. // 是否是usdt提现
  177. if ($withdrawalLogModel->type === 1) {
  178. $amount = $withdrawalLogModel->usdt_to_amount;
  179. }
  180. //查询会员
  181. switch ($status) {
  182. case 2:
  183. //2已到账
  184. Db::beginTransaction();
  185. try {
  186. //会员扣除冻结
  187. $userInfo->freeze = $userInfo->freeze - $amount;
  188. if (!$userInfo->save()) {
  189. Db::rollBack();
  190. return json_fail('-001 订单出错!');
  191. }
  192. // 订单状态更换
  193. $withdrawalLogModel->status = 2;
  194. if (!$withdrawalLogModel->save()) {
  195. Db::rollBack();
  196. return json_fail('-002 订单出错!');
  197. }
  198. $this->log('操作提现(到账)订单号:' . $withdrawalLogModel->order_number, 2, [
  199. 'after_items' => [
  200. $withdrawalLogModel->toArray(),
  201. ],
  202. 'before_items' => [
  203. $oldWithdrawalLogModel->toArray(),
  204. ],
  205. ]);
  206. $this->log('操作提现(到账)订单扣除会员冻结资金,会员id:' . $userInfo->id, $amount, [
  207. 'after_items' => [
  208. $userInfo->toArray(),
  209. ],
  210. 'before_items' => [
  211. $oldUserInfo->toArray(),
  212. ],
  213. ]);
  214. make(UserRepository::class)->delUserCache($userInfo->code);
  215. Db::commit();
  216. } catch (\Exception $exception) {
  217. Db::rollBack();
  218. return json_fail('-003 订单出错!');
  219. }
  220. break;
  221. case 3:
  222. //3失败
  223. Db::beginTransaction();
  224. try {
  225. //会员扣除冻结并且追加余额
  226. $userInfo->freeze = $userInfo->freeze - $amount;
  227. $userInfo->balance = $userInfo->balance + $amount;
  228. if (!$userInfo->save()) {
  229. Db::rollBack();
  230. return json_fail('-001 订单出错!');
  231. }
  232. // 订单状态更换
  233. $withdrawalLogModel->status = 3;
  234. $withdrawalLogModel->remark = $remark;
  235. if (!$withdrawalLogModel->save()) {
  236. Db::rollBack();
  237. return json_fail('-002 订单出错!');
  238. }
  239. $this->log('操作提现(失败)订单号:' . $withdrawalLogModel->order_number, 3, [
  240. 'after_items' => [
  241. $withdrawalLogModel->toArray(),
  242. ],
  243. 'before_items' => [
  244. $oldWithdrawalLogModel->toArray(),
  245. ],
  246. ]);
  247. $this->log('操作提现(失败)订单扣除会员冻结资金,会员id:' . $userInfo->id, $amount, [
  248. 'after_items' => [
  249. $userInfo->toArray(),
  250. ],
  251. 'before_items' => [
  252. $oldUserInfo->toArray(),
  253. ],
  254. ]);
  255. Db::commit();
  256. // 帐变记录
  257. $changeData = [
  258. 'user_uid' => $userInfo->code,
  259. 'currency_type' => ChangeLogConstant::CHANGE_AMOUNT,
  260. 'amount' => $amount,
  261. 'way' => ChangeLogConstant::WAY_1,
  262. 'type' => ChangeLogConstant::TYPE_21,
  263. ];
  264. make(QueueService::class)->saveChangeLogJob($changeData);
  265. } catch (\Exception $exception) {
  266. Db::rollBack();
  267. return json_fail('-023 订单出错!');
  268. }
  269. break;
  270. case 4:
  271. //4冻结
  272. return json_fail('目前不支持!');
  273. break;
  274. default:
  275. return json_fail('操作有误!');
  276. break;
  277. }
  278. return json_success('订单操作成功');
  279. }
  280. }