Room.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Constants\Util;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Config;
  7. use App\Models\RoomUser;
  8. use App\Services\RoomService;
  9. use App\Services\SettlementService;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Validation\ValidationException;
  12. use Exception;
  13. use Telegram\Bot\Api;
  14. class Room extends Controller
  15. {
  16. /**
  17. * @api {post} /admin/room/completed 结算
  18. * @apiGroup 房间管理
  19. *
  20. * @apiUse result
  21. * @apiUse header
  22. * @apiVersion 1.0.0
  23. * @apiParam {string} room_id 房间号
  24. */
  25. public function completed()
  26. {
  27. DB::beginTransaction();
  28. try {
  29. request()->validate([
  30. 'room_id' => ['required', 'string', 'min:1'],
  31. ]);
  32. $roomId = request()->input('room_id');
  33. $list = RoomUser::where('status', 3)
  34. ->where('room_id', $roomId)
  35. ->orderBy('score', 'desc')->get();
  36. $count = RoomUser::where('room_id', $roomId)->count();
  37. if ($count > 0 && count($list) == $count) {
  38. $totalAmount = RoomUser::where('status', 3)
  39. ->where('room_id', $roomId)->sum('score');
  40. if ($totalAmount == 0) {
  41. $list = $list->toArray();
  42. $item = $list[0];
  43. $res = (new SettlementService())->submitSettle($item['member_id'], $item['score']);
  44. if ($res['status']) {
  45. $telegram = new Api(config('services.telegram.token'));
  46. foreach ($res['data'] as $r) $telegram->sendMessage($r);
  47. } else {
  48. throw new Exception("结算失败", HttpStatus::CUSTOM_ERROR);
  49. }
  50. } else {
  51. throw new Exception("经过系统结算,盈利额与亏损额度无法匹配,无法进行结算", HttpStatus::CUSTOM_ERROR);
  52. }
  53. } else {
  54. throw new Exception("用户还没有全部提交得分", HttpStatus::CUSTOM_ERROR);
  55. }
  56. DB::commit();
  57. } catch (ValidationException $e) {
  58. DB::rollBack();
  59. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  60. } catch (Exception $e) {
  61. DB::rollBack();
  62. return $this->error(intval($e->getCode()), $e->getMessage());
  63. }
  64. return $this->success();
  65. }
  66. /**
  67. * @api {post} /admin/room/setScore 设置用户的得分
  68. * @apiGroup 房间管理
  69. *
  70. * @apiUse result
  71. * @apiUse header
  72. * @apiVersion 1.0.0
  73. * @apiParam {int} id 房间详情ID
  74. * @apiParam {int} score 游戏得分
  75. */
  76. public function setScore()
  77. {
  78. DB::beginTransaction();
  79. try {
  80. request()->validate([
  81. 'id' => ['required', 'integer', 'min:1'],
  82. 'score' => ['required', 'integer']
  83. ]);
  84. $id = request()->input('id');
  85. $score = intval(request()->input('score'));
  86. $ru = RoomUser::where('id', $id)
  87. ->whereIn('status', [2,3])
  88. ->first();
  89. if (!$ru) throw new Exception('结算数据不存在', HttpStatus::CUSTOM_ERROR);
  90. $ru->score = $score;
  91. if($ru->status == 2) {
  92. $ru->status = 3; // 设置为待结算状态
  93. }
  94. $ru->save();
  95. DB::commit();
  96. } catch (ValidationException $e) {
  97. DB::rollBack();
  98. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  99. } catch (Exception $e) {
  100. DB::rollBack();
  101. return $this->error(intval($e->getCode()), $e->getMessage());
  102. }
  103. return $this->success();
  104. }
  105. /**
  106. * @api {get} /admin/room/details 对局详情
  107. * @apiGroup 房间管理
  108. *
  109. * @apiUse result
  110. * @apiUse header
  111. * @apiVersion 1.0.0
  112. * @apiParam {string} room_id 房间号
  113. *
  114. * @apiSuccess (data) {Object} data
  115. * @apiSuccess (data) {int} data.id 详情ID
  116. * @apiSuccess (data) {string} data.room_id 房间号
  117. * @apiSuccess (data) {string} data.member_id 会员ID
  118. * @apiSuccess (data) {string} data.game_id 游戏ID
  119. * @apiSuccess (data) {int} data.status 人员状态
  120. * - 0 待准备
  121. * - 1 已准备
  122. * - 2 游戏中
  123. * - 3 待结算
  124. * - 4 已结算
  125. * @apiSuccess (data) {int} data.score 得分
  126. * @apiSuccess (data) {float} data.brokerage 抽佣金额
  127. * @apiSuccess (data) {float} data.real_score 真实获得的金额
  128. * @apiSuccess (data) {string} data.screenshot 结算截图
  129. * @apiSuccess (data) {string} data.first_name 用户昵称
  130. */
  131. public function details()
  132. {
  133. try {
  134. request()->validate([
  135. 'room_id' => ['required', 'string', 'min:1']
  136. ]);
  137. $roomId = request()->input('room_id');
  138. $res = RoomUser::where('room_id', $roomId)
  139. ->with('room')->get();
  140. foreach ($res as $item) {
  141. $item['screenshot'] = Util::ensureUrl($item['screenshot']);
  142. }
  143. } catch (ValidationException $e) {
  144. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  145. } catch (Exception $e) {
  146. return $this->error(intval($e->getCode()), $e->getMessage());
  147. }
  148. return $this->success($res);
  149. }
  150. /**
  151. * @api {get} /admin/room 游戏房间
  152. * @apiGroup 房间管理
  153. *
  154. * @apiUse result
  155. * @apiUse header
  156. * @apiVersion 1.0.0
  157. *
  158. * @apiParam {int} [page=1]
  159. * @apiParam {int} [limit=10]
  160. * @apiParam {string} [room_id] 房间号
  161. * @apiParam {string} [member_id] 房主 tg会员ID
  162. * @apiParam {int} [status] 状态
  163. * - 0 创建中
  164. * - 1 创建完成
  165. * - 2 游戏中
  166. * - 3 已结算
  167. * @apiSuccess (data) {Object} data
  168. * @apiSuccess (data) {int} data.total 数量
  169. * @apiSuccess (data) {Object[]} data.data 列表
  170. * @apiSuccess (data) {int} data.data.id
  171. * @apiSuccess (data) {string} data.data.room_id 房间号
  172. * @apiSuccess (data) {int} data.data.member_id 房主 tg会员id
  173. * @apiSuccess (data) {string} data.data.game_name 游戏名称
  174. * @apiSuccess (data) {string} data.data.base_score 底分
  175. * @apiSuccess (data) {string} data.data.participants 人数
  176. * @apiSuccess (data) {string} data.data.rounds 局数
  177. * @apiSuccess (data) {string} data.data.join_count 已加入人数
  178. * @apiSuccess (data) {string} data.data.updated_at
  179. * @apiSuccess (data) {string} data.data.created_at
  180. * @apiSuccess (data) {int} data.data.status 状态
  181. * - 0 创建中
  182. * - 1 创建完成
  183. * - 2 游戏中
  184. * - 3 已结算
  185. */
  186. public function index()
  187. {
  188. try {
  189. request()->validate([
  190. 'room_id' => ['nullable', 'string', 'min:1'],
  191. 'member_id' => ['nullable', 'string', 'min:1'],
  192. 'status' => ['nullable', 'integer', 'min:0', 'max:3']
  193. ]);
  194. $search = request()->all();
  195. $search['not_status'] = 0;
  196. $search['like_room_id'] = true; // 模糊查询房间号
  197. $result = RoomService::paginate($search);
  198. } catch (ValidationException $e) {
  199. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  200. } catch (Exception $e) {
  201. return $this->error(intval($e->getCode()), $e->getMessage());
  202. }
  203. return $this->success($result);
  204. }
  205. }