EnergyAiTrusteeshipController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Telegram\TelegramBot;
  7. use App\Models\Energy\EnergyAiTrusteeship;
  8. class EnergyAiTrusteeshipController 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.aitrusteeship.index',compact("Status","botData"));
  17. }
  18. //列表
  19. public function getData(Request $request)
  20. {
  21. $model = EnergyAiTrusteeship::from('energy_ai_trusteeship 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->per_buy_energy_quantity) || $request->per_buy_energy_quantity < 32000){
  49. return $this->responseData(400, '每次购买能量数量需大于等于32000');
  50. }
  51. if(empty($request->min_energy_quantity) || $request->min_energy_quantity <= 0){
  52. return $this->responseData(400, '能量低于值购买需大于0');
  53. }
  54. if(empty($request->wallet_addr)){
  55. return $this->responseData(400, '钱包地址不能为空');
  56. }
  57. if(empty($request->tg_uid)){
  58. return $this->responseData(400, 'tg用户ID不能为空');
  59. }
  60. $dataexist = EnergyAiTrusteeship::where('wallet_addr',$request->wallet_addr)->where('rid','<>',$request->rid)->first();
  61. if(!empty($dataexist)){
  62. return $this->responseData(400, '钱包地址已存在');
  63. }
  64. $data = EnergyAiTrusteeship::where('rid',$request->rid)->first();
  65. if(empty($data)){
  66. return $this->responseData(400, '数据不存在');
  67. }
  68. DB::beginTransaction();
  69. try {
  70. $data->status = $request->status;
  71. $data->wallet_addr = $request->wallet_addr;
  72. $data->tg_uid = $request->tg_uid;
  73. $data->min_energy_quantity = $request->min_energy_quantity;
  74. $data->per_buy_energy_quantity = $request->per_buy_energy_quantity;
  75. $data->back_comments = $request->back_comments;
  76. $data->max_buy_quantity = $request->max_buy_quantity ?? 0;
  77. $data->update_time = nowDate();
  78. $data->save();
  79. DB::commit();
  80. return $this->responseData(200, '更新成功');
  81. } catch (\Exception $e) {
  82. DB::rollBack();
  83. return $this->responseData(400, '更新失败'.$e->getMessage());
  84. }
  85. }
  86. //删除
  87. public function delete(Request $request)
  88. {
  89. $res = EnergyAiTrusteeship::where('rid', $request->rid)->delete();
  90. return $res ? $this->responseData(200, '删除成功') : $this->responseData(400, '删除失败');
  91. }
  92. //刷新
  93. public function refresh(Request $request)
  94. {
  95. $res = EnergyAiTrusteeship::where('rid', $request->rid)->update(['is_buy' => 'N']);
  96. return $res ? $this->responseData(200, '刷新成功,请等待更新') : $this->responseData(400, '刷新失败');
  97. }
  98. }