EnergyAiBishuController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers\Admin\Energy;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\Energy\EnergyAiBishu;
  7. use App\Models\Telegram\TelegramBot;
  8. class EnergyAiBishuController extends Controller
  9. {
  10. public $Status = ['0' => '开启','1' => '关闭','2' => '管理员关闭'];
  11. public $isBuy = ['Y' => '需要下单','N' => '无需下单','B' => '下单中'];
  12. public function index(Request $request)
  13. {
  14. $Status = $this->Status;
  15. $botData = TelegramBot::pluck('bot_username','rid');
  16. return view('admin.energy.aibishu.index',compact("Status","botData"));
  17. }
  18. //列表
  19. public function getData(Request $request)
  20. {
  21. $model = EnergyAiBishu::from('energy_ai_bishu as a')
  22. ->leftJoin('telegram_bot as b','a.bot_rid','b.rid')
  23. ->where(function($query) use ($request){
  24. if ($request->tg_uid != '') {
  25. $query->where('tg_uid', 'like' ,"%" . $request->tg_uid ."%");
  26. }
  27. if ($request->wallet_addr != '') {
  28. $query->where('wallet_addr', 'like' ,"%" . $request->wallet_addr ."%");
  29. }
  30. if ($request->bot_rid != '') {
  31. $query->where('a.bot_rid', $request->bot_rid);
  32. }
  33. });
  34. $count = $model->count();
  35. $limit = $request->limit ?? 15;
  36. $offset = $request->page ? ($request->page - 1) * $limit : 0;
  37. $data = $model->limit($limit)->offset($offset)->select('a.*','b.bot_token','b.bot_firstname','b.bot_username')->orderBy('a.rid','desc')->get();
  38. $data = $data->map(function($query){
  39. $query->status_val = $this->Status[$query->status];
  40. $query->is_buy_val = $this->isBuy[$query->is_buy];
  41. return $query;
  42. });
  43. return ['code' => '0', 'data' => $data, 'count' => $count];
  44. }
  45. //编辑
  46. public function update(Request $request)
  47. {
  48. if(empty($request->wallet_addr) || empty($request->bot_rid)){
  49. return $this->responseData(400, '钱包地址不能为空');
  50. }
  51. $dataexist = EnergyAiBishu::where('wallet_addr',$request->wallet_addr)->where('rid','<>',$request->rid)->first();
  52. if(!empty($dataexist)){
  53. return $this->responseData(400, '钱包地址已存在');
  54. }
  55. $data = EnergyAiBishu::where('rid',$request->rid)->first();
  56. if(empty($data)){
  57. return $this->responseData(400, '数据不存在');
  58. }
  59. if($request->bot_rid != $data->bot_rid || $request->wallet_addr != $data->wallet_addr || $request->max_buy_quantity != $data->max_buy_quantity){
  60. llog($data->rid."的原地址为:".$data->wallet_addr."。新地址为:".$request->wallet_addr."。原机器人ID:".$data->bot_rid."。新机器人ID:".$request->bot_rid."。原笔数:".$data->max_buy_quantity."。新笔数:".$request->max_buy_quantity,"modify");
  61. }
  62. DB::beginTransaction();
  63. try {
  64. $data->bot_rid = $request->bot_rid;
  65. $data->status = $request->status;
  66. $data->wallet_addr = $request->wallet_addr;
  67. $data->tg_uid = $request->tg_uid;
  68. $data->total_buy_quantity = $request->total_buy_quantity ?? 0;
  69. $data->back_comments = $request->back_comments;
  70. $data->max_buy_quantity = $request->max_buy_quantity ?? 0;
  71. $data->update_time = nowDate();
  72. $data->save();
  73. DB::commit();
  74. return $this->responseData(200, '更新成功');
  75. } catch (\Exception $e) {
  76. DB::rollBack();
  77. return $this->responseData(400, '更新失败'.$e->getMessage());
  78. }
  79. }
  80. //删除
  81. public function delete(Request $request)
  82. {
  83. $res = EnergyAiBishu::where('rid', $request->rid)->delete();
  84. return $res ? $this->responseData(200, '删除成功') : $this->responseData(400, '删除失败');
  85. }
  86. //刷新
  87. public function refresh(Request $request)
  88. {
  89. $res = EnergyAiBishu::where('rid', $request->rid)->update(['is_buy' => 'N']);
  90. return $res ? $this->responseData(200, '刷新成功,请等待更新') : $this->responseData(400, '刷新失败');
  91. }
  92. //添加
  93. public function add(Request $request)
  94. {
  95. if(empty($request->wallet_addr) || strlen($request->wallet_addr) != 34){
  96. return $this->responseData(400, '输入有效的波场地址');
  97. }
  98. if($request->total_buy_usdt < 0){
  99. return $this->responseData(400, '总购买USDT不能小于0');
  100. }
  101. if($request->max_buy_quantity < 0){
  102. return $this->responseData(400, '最大购买次数不能小于0');
  103. }
  104. if($request->total_buy_quantity < 0){
  105. return $this->responseData(400, '已购买次数不能小于0');
  106. }
  107. if(empty($request->bot_rid)){
  108. return $this->responseData(400, '请选择机器人');
  109. }
  110. $dataexist = EnergyAiBishu::where('wallet_addr',$request->wallet_addr)->first();
  111. if(!empty($dataexist)){
  112. return $this->responseData(400, '钱包地址已存在');
  113. }
  114. $res = EnergyAiBishu::create([
  115. 'bot_rid' => $request->bot_rid,
  116. 'wallet_addr' => $request->wallet_addr,
  117. 'tg_uid' => $request->tg_uid ?? '',
  118. 'total_buy_usdt' => $request->total_buy_usdt ?? 0,
  119. 'max_buy_quantity' => $request->max_buy_quantity ?? 0,
  120. 'total_buy_quantity' => $request->total_buy_quantity ?? 0,
  121. 'back_comments' => $request->back_comments ?? '',
  122. 'create_time' => nowDate()
  123. ]);
  124. return $res ? $this->responseData(200, '添加成功') : $this->responseData(400, '添加失败');
  125. }
  126. }