ThirdPartController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Http\Controllers\Api\ThirdPart;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\Telegram\TelegramBotUser;
  7. use App\Models\Energy\EnergyAiBishu;
  8. use App\Models\Energy\EnergyThirdPart;
  9. class ThirdPartController extends Controller
  10. {
  11. public $thirdPlatformId = 1; //使用能量平台ID下单,只能是trongas.io对应的平台
  12. public $thirdBotId = 1; //使用机器人ID
  13. public $bishuTrxPrice = 2.5; //笔数trx一笔价格
  14. public $shanzu32000TrxPrice = 2.5; //闪租3200 trx一笔1小时价格
  15. public $shanzu65000TrxPrice = 2.9; //闪租65000 trx一笔1小时价格
  16. //查询余额
  17. public function balance(Request $request)
  18. {
  19. $data = TelegramBotUser::where('tg_uid',$request->tg_uid)->where('bot_rid',$this->thirdBotId)->first();
  20. if($data){
  21. $returnData = [];
  22. $returnData['trx_balance'] = $data->cash_trx;
  23. $returnData['usdt_balance'] = $data->cash_usdt;
  24. return $this->responseData(200, 'success', $returnData);
  25. }else{
  26. return $this->responseData(400, '用户信息不存在,请配置正确的用户ID');
  27. }
  28. }
  29. //笔数下单
  30. public function bishuorder(Request $request)
  31. {
  32. if(empty($request->maxDelegateNums) || $request->maxDelegateNums <= 0 || mb_strlen($request->receiveAddress) != 34){
  33. return $this->responseData(400, '笔数模式下单错误,检查参数');
  34. }
  35. $data = TelegramBotUser::where('tg_uid',$request->tg_uid)->where('bot_rid',$this->thirdBotId)->first();
  36. if($data){
  37. DB::beginTransaction();
  38. try {
  39. //判断trx余额是否足够
  40. if($request->maxDelegateNums * $this->bishuTrxPrice > $data->cash_trx){
  41. return $this->responseData(400, '用户trx余额不足,当前:'.$data->cash_trx.',需要:'.($request->maxDelegateNums * $this->bishuTrxPrice));
  42. }
  43. //扣除用户余额
  44. TelegramBotUser::where('tg_uid',$request->tg_uid)->where('bot_rid',$this->thirdBotId)->update(['cash_trx' => $data->cash_trx - ($request->maxDelegateNums * $this->bishuTrxPrice)]);
  45. $time = nowDate();
  46. //记录笔数
  47. $energyAiBishu = EnergyAiBishu::where('wallet_addr',$request->receiveAddress)->first();
  48. if($energyAiBishu){
  49. $save_data = [];
  50. $save_data['max_buy_quantity'] = $energyAiBishu->max_buy_quantity + $request->maxDelegateNums;
  51. EnergyAiBishu::where('rid',$energyAiBishu->rid)->update($save_data);
  52. }else{
  53. $insert_data = [];
  54. $insert_data['bot_rid'] = $this->thirdBotId;
  55. $insert_data['wallet_addr'] = $request->receiveAddress;
  56. $insert_data['status'] = 0;
  57. $insert_data['total_buy_usdt'] = 0;
  58. $insert_data['max_buy_quantity'] = $request->maxDelegateNums;
  59. $insert_data['create_time'] = $time;
  60. EnergyAiBishu::insert($insert_data);
  61. }
  62. //记录提交明细
  63. $insert_data = [];
  64. $insert_data['bot_rid'] = $this->thirdBotId;
  65. $insert_data['wallet_addr'] = $request->receiveAddress;
  66. $insert_data['order_type'] = 1;
  67. $insert_data['tg_uid'] = $request->tg_uid;
  68. $insert_data['platform_rid'] = $this->thirdPlatformId;
  69. $insert_data['cishu_energy'] = $request->maxDelegateNums;
  70. $insert_data['before_trx'] = $data->cash_trx;
  71. $insert_data['change_trx'] = $request->maxDelegateNums * $this->bishuTrxPrice;
  72. $insert_data['after_trx'] = $data->cash_trx - ($request->maxDelegateNums * $this->bishuTrxPrice);
  73. $insert_data['order_time'] = $time;
  74. $insert_data['process_status'] = 9; //无需额外处理下单
  75. EnergyThirdPart::insert($insert_data);
  76. $returnData['orderId'] = createNo();
  77. $returnData['orderMoney'] = $request->maxDelegateNums * $this->bishuTrxPrice;
  78. DB::commit();
  79. return $this->responseData(200, 'success', $returnData);
  80. } catch (\Exception $e) {
  81. DB::rollBack();
  82. return $this->responseData(400, '下单失败'.$e->getMessage());
  83. }
  84. }else{
  85. return $this->responseData(400, '用户信息不存在,请配置正确的用户ID');
  86. }
  87. }
  88. //闪租下单
  89. public function shanzuorder(Request $request)
  90. {
  91. if(empty($request->payNums) || $request->payNums < 32000 || mb_strlen($request->receiveAddress) != 34 || $request->rentTime != 1){
  92. return $this->responseData(400, '闪租模式下单错误,检查参数');
  93. }
  94. $data = TelegramBotUser::where('tg_uid',$request->tg_uid)->where('bot_rid',$this->thirdBotId)->first();
  95. if($data){
  96. DB::beginTransaction();
  97. try {
  98. if($request->payNums == 65000){
  99. $kouTrx = $this->shanzu65000TrxPrice;
  100. }else{
  101. $kouTrx = ceil($request->payNums / 32000) * $this->shanzu32000TrxPrice;
  102. }
  103. //判断trx余额是否足够
  104. if($kouTrx > $data->cash_trx){
  105. return $this->responseData(400, '用户trx余额不足,当前:'.$data->cash_trx.',需要:'.$kouTrx);
  106. }
  107. //扣除用户余额
  108. TelegramBotUser::where('tg_uid',$request->tg_uid)->where('bot_rid',$this->thirdBotId)->update(['cash_trx' => $data->cash_trx - $kouTrx]);
  109. $time = nowDate();
  110. //记录提交明细,从这个里面下单
  111. $insert_data = [];
  112. $insert_data['bot_rid'] = $this->thirdBotId;
  113. $insert_data['wallet_addr'] = $request->receiveAddress;
  114. $insert_data['order_type'] = 2;
  115. $insert_data['tg_uid'] = $request->tg_uid;
  116. $insert_data['platform_rid'] = $this->thirdPlatformId;
  117. $insert_data['cishu_energy'] = $request->payNums;
  118. $insert_data['before_trx'] = $data->cash_trx;
  119. $insert_data['change_trx'] = $kouTrx;
  120. $insert_data['after_trx'] = $data->cash_trx - $kouTrx;
  121. $insert_data['order_time'] = $time;
  122. $insert_data['process_status'] = 1; //需要处理下单
  123. EnergyThirdPart::insert($insert_data);
  124. $returnData['orderId'] = createNo();
  125. $returnData['orderMoney'] = $kouTrx;
  126. DB::commit();
  127. return $this->responseData(200, 'success', $returnData);
  128. } catch (\Exception $e) {
  129. DB::rollBack();
  130. return $this->responseData(400, '下单失败'.$e->getMessage());
  131. }
  132. }else{
  133. return $this->responseData(400, '用户信息不存在,请配置正确的用户ID');
  134. }
  135. }
  136. }