TelegramBotAdKeyboardController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Http\Controllers\Admin\Telegram;
  3. use Illuminate\Http\Request;
  4. use App\Services\AipHttpClient;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Models\Telegram\TelegramBot;
  8. use App\Models\Telegram\TelegramBotAd;
  9. use App\Models\Telegram\TelegramBotKeyboard;
  10. use App\Models\Telegram\TelegramBotAdKeyboard;
  11. use Telegram\Bot\Api;
  12. class TelegramBotAdKeyboardController extends Controller
  13. {
  14. public $KeyboardType = ['1' => '键盘','2' => '内联按钮'];
  15. public $InlineType = ['0' => '-','1' => 'url','2' => '回调'];
  16. public function index(Request $request)
  17. {
  18. $BotAdData = TelegramBotAd::pluck('rid', 'rid');
  19. $KeyboardType = $this->KeyboardType;
  20. $keyboardData = TelegramBotKeyboard::select(DB::raw("CONCAT('ID:',rid,'-',keyboard_name) AS keyboard_name"),'rid','keyboard_type')->pluck('keyboard_name','rid','keyboard_type');
  21. $botData = TelegramBot::pluck('bot_username','rid');
  22. return view('admin.telegram.telegrambotadkeyboard.index',compact("BotAdData","keyboardData","KeyboardType","botData"));
  23. }
  24. //列表
  25. public function getData(Request $request)
  26. {
  27. $model = TelegramBotAdKeyboard::from('telegram_bot_ad_keyboard as a')
  28. ->join('telegram_bot as b','a.bot_rid','b.rid')
  29. ->join('telegram_bot_ad as c','a.ad_rid','c.rid')
  30. ->join('telegram_bot_keyboard as d','a.keyboard_rid','d.rid')
  31. ->where(function($query) use ($request){
  32. if ($request->notice_ad != '') {
  33. $query->where('c.notice_ad', 'like' ,"%" . $request->notice_ad ."%");
  34. }
  35. if ($request->bot_rid != '') {
  36. $query->where('a.bot_rid', $request->bot_rid);
  37. }
  38. });
  39. $count = $model->count();
  40. $limit = $request->limit ?? 15;
  41. $offset = $request->page ? ($request->page - 1) * $limit : 0;
  42. $data = $model->limit($limit)->offset($offset)->select('a.*','b.bot_token','b.bot_firstname','b.bot_username','d.keyboard_name','d.keyboard_type','d.inline_type')->orderBy('a.rid','desc')->get();
  43. $keys = ["KeyboardType" => $this->KeyboardType, "InlineType" => $this->InlineType];
  44. $data = $data->map(function($query) use ($keys){
  45. $query->keyboard_type_val = $keys["KeyboardType"][$query->keyboard_type];
  46. $query->inline_type_val = $keys["InlineType"][$query->inline_type];
  47. return $query;
  48. });
  49. return ['code' => '0', 'data' => $data, 'count' => $count];
  50. }
  51. //添加
  52. public function add(Request $request)
  53. {
  54. $data = TelegramBotAdKeyboard::where('ad_rid', $request->ad_rid)->where('keyboard_rid', $request->keyboard_rid)->first();
  55. if(!empty($data)){
  56. return $this->responseData(400, '定时广告键盘已存在');
  57. }
  58. //同一个广告只能添加一种类型
  59. $keyboard = TelegramBotKeyboard::where('rid',$request->keyboard_rid)->value('keyboard_type');
  60. $adkeyboard = TelegramBotAdKeyboard::from('telegram_bot_ad_keyboard as a')
  61. ->join('telegram_bot_keyboard as b','a.keyboard_rid','b.rid')
  62. ->where('b.keyboard_type','<>',$keyboard)
  63. ->first();
  64. if($adkeyboard){
  65. return $this->responseData(400, '同一个定时广告,只能关联一种键盘类型');
  66. }
  67. $bot_rid = TelegramBotAd::where('rid',$request->ad_rid)->first();
  68. $res = TelegramBotAdKeyboard::create([
  69. 'bot_rid' => $bot_rid->bot_rid,
  70. 'ad_rid' => $request->ad_rid,
  71. 'keyboard_rid' => $request->keyboard_rid,
  72. 'create_time' => nowDate()
  73. ]);
  74. return $res ? $this->responseData(200, '添加成功') : $this->responseData(400, '添加失败');
  75. }
  76. //删除
  77. public function delete(Request $request)
  78. {
  79. $res = TelegramBotAdKeyboard::where('rid', $request->rid)->delete();
  80. return $res ? $this->responseData(200, '删除成功') : $this->responseData(400, '删除失败');
  81. }
  82. //编辑
  83. public function update(Request $request)
  84. {
  85. $data = TelegramBotAdKeyboard::where('rid', $request->rid)->first();
  86. if(empty($data)){
  87. return $this->responseData(400, '数据不存在');
  88. }
  89. $existdata = TelegramBotAdKeyboard::where('ad_rid', $request->ad_rid)->where('keyboard_rid', $request->keyboard_rid)->where('rid','<>',$request->rid)->first();
  90. if(!empty($existdata)){
  91. return $this->responseData(400, '定时广告键盘已存在');
  92. }
  93. //同一个广告只能添加一种类型
  94. $keyboard = TelegramBotKeyboard::where('rid',$request->keyboard_rid)->value('keyboard_type');
  95. $adkeyboard = TelegramBotAdKeyboard::from('telegram_bot_ad_keyboard as a')
  96. ->join('telegram_bot_keyboard as b','a.keyboard_rid','b.rid')
  97. ->where('b.keyboard_type','<>',$keyboard)
  98. ->first();
  99. if($adkeyboard){
  100. return $this->responseData(400, '同一个定时广告,只能关联一种键盘类型');
  101. }
  102. DB::beginTransaction();
  103. try {
  104. $bot_rid = TelegramBotAd::where('rid',$request->ad_rid)->first();
  105. $data->bot_rid = $bot_rid->bot_rid;
  106. $data->ad_rid = $request->ad_rid;
  107. $data->keyboard_rid = $request->keyboard_rid;
  108. $data->update_time = nowDate();
  109. $data->save();
  110. DB::commit();
  111. return $this->responseData(200, '更新成功');
  112. } catch (\Exception $e) {
  113. DB::rollBack();
  114. return $this->responseData(400, '更新失败'.$e->getMessage());
  115. }
  116. }
  117. //快捷添加
  118. public function fastadd(Request $request)
  119. {
  120. if(empty($request->ad_rid) || empty($request->keyboard_type)){
  121. return $this->responseData(400, '参数必填');
  122. }
  123. $data = TelegramBotAd::where('rid', $request->ad_rid)->first();
  124. if(empty($data)){
  125. return $this->responseData(400, '广告不存在');
  126. }
  127. $boardData = TelegramBotKeyboard::where('keyboard_type', $request->keyboard_type)->get();
  128. if($boardData->count() == 0){
  129. return $this->responseData(400, '键盘类型无键盘数据');
  130. }
  131. DB::beginTransaction();
  132. try {
  133. TelegramBotAdKeyboard::where('ad_rid', $request->ad_rid)->delete();
  134. TelegramBotAdKeyboard::insertUsing([
  135. 'bot_rid', 'ad_rid', 'keyboard_rid', 'create_time'
  136. ], TelegramBotKeyboard::selectRaw(
  137. "$data->bot_rid, $request->ad_rid, rid, sysdate()"
  138. )->where('keyboard_type', $request->keyboard_type));
  139. DB::commit();
  140. return $this->responseData(200, '添加成功');
  141. } catch (\Exception $e) {
  142. DB::rollBack();
  143. return $this->responseData(400, '添加失败'.$e->getMessage());
  144. }
  145. }
  146. }