GameRecord.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\controller;
  4. use app\BaseController;
  5. use think\facade\Request;
  6. use app\model\GameRecordModel;
  7. use app\model\GameModel;
  8. use app\common\CommonUtils;
  9. /**
  10. * 游戏记录控制器
  11. */
  12. class GameRecord extends BaseController
  13. {
  14. /**
  15. * 获取游戏记录列表
  16. */
  17. public function list()
  18. {
  19. $userInfo = $this->request->userInfo;
  20. // 获取查询参数
  21. $page = Request::get('page', 1, 'intval');
  22. $limit = Request::get('limit', 20, 'intval');
  23. // 筛选条件
  24. $filters = [
  25. // 时间筛选
  26. 'start_time' => Request::get('start_time', '', 'trim'),
  27. 'end_time' => Request::get('end_time', '', 'trim'),
  28. // 游戏筛选
  29. 'game_id' => Request::get('game_id', '', 'trim'),
  30. // 牌局编号筛选
  31. 'third_round_id' => Request::get('third_round_id', '', 'trim'),
  32. // 平台ID筛选
  33. 'uname' => Request::get('uname', '', 'trim'),
  34. // 玩家ID筛选
  35. 'player_id' => Request::get('user_id', '', 'trim'),
  36. // 平台昵称
  37. 'nickname' => Request::get('nickname', '', 'trim'),
  38. // 游戏玩法类型筛选
  39. 'bet_game_play_type' => Request::get('bet_game_play_type', '', 'trim'),
  40. ];
  41. try {
  42. // 获取游戏记录列表
  43. $result = GameRecordModel::getBetGameList($userInfo['merchant_id'], $page, $limit, $filters);
  44. if (empty($result['list'])) {
  45. return json_success($result, '获取成功');
  46. }
  47. // 获取游戏信息信息
  48. $game_ids = array_unique(array_column($result['list'], 'game_id'));
  49. $tempGameList = GameModel::where([['merchant_id', '=', $userInfo['merchant_id']], ['game_id', 'in', $game_ids]])->field(['game_id','title','image'])->select()->toArray();
  50. $gameList = [];
  51. foreach ($tempGameList as $item) {
  52. $game_id = $item['game_id'];
  53. $gameList[$game_id] = $item;
  54. }
  55. // 获取母单号列表
  56. $thirdGids = array_unique(array_column($result['list'], 'third_gid'));
  57. $childOrderList = [];
  58. if(count($thirdGids) > 0) {
  59. $tempCheckList = GameRecordModel::where([ ['third_gid', 'in', $thirdGids], ['action_type','=',2] ])->withoutField('result')->select()->toArray();
  60. foreach ($tempCheckList as $item) {
  61. if(!isset($childOrderList[$item['third_gid']])) {
  62. $childOrderList[$item['third_gid']] = [];
  63. }
  64. $item = $this->formatItemDataText($item);
  65. $item['id'] = 'm_' . $item['id'];
  66. $childOrderList[$item['third_gid']][] = $item;
  67. }
  68. }
  69. // 格式化数据并添加子订单
  70. $newDataList = [];
  71. foreach ($result['list'] as $item) {
  72. $thirdGid = $item['third_gid'];
  73. $game_id = $item['game_id'];
  74. $gameInfo = $gameInfoMap[$item['game_id']] ?? [];
  75. if (!empty($gameInfo))
  76. {
  77. $gameImages = CommonUtils::getGameDefaultImage($gameInfo);
  78. }
  79. $item['game_image_url'] = $gameImages['image_url'] ?? '';
  80. $item['game_title'] = $gameInfo['title'] ?? '';
  81. $item['game_type_text'] = CommonUtils::getGameTypeConfig($item['game_type']);
  82. if(isset($childOrderList[$thirdGid])) {
  83. $item['children'] = [];
  84. $childList = $childOrderList[$thirdGid];
  85. foreach ($childList as $citem) {
  86. $citem['game_type_text'] = $item['game_type_text'];
  87. $citem['game_image_url'] = $item['game_image_url'];
  88. $citem['game_title'] = $item['game_title'];
  89. $item['children'][] = $citem;
  90. }
  91. }
  92. $item['c_count'] = count($childOrderList[$thirdGid] ?? []);
  93. $item['view_layout_url'] = "/history/{$game_id}.html?sid={$thirdGid}&pf=1&lang=zh&api=".Request::host()."%2Fweb-api%2Fgame-proxy%2Fv2%2FBetDetails%2FGet";
  94. $newDataList[] = $item;
  95. }
  96. $result['list'] = $newDataList;
  97. return json_success($result, '获取成功');
  98. } catch (\Exception $e) {
  99. echo $e->getTraceAsString();
  100. return json_error([], '获取游戏记录失败:' . $e->getMessage());
  101. }
  102. }
  103. /**
  104. * 获取游戏记录详情
  105. */
  106. public function detail()
  107. {
  108. $userInfo = $this->request->userInfo;
  109. $id = Request::get('id', 0, 'intval');
  110. if (!$id) {
  111. return json_error([], '记录ID不能为空');
  112. }
  113. try {
  114. $record = GameRecordModel::getGameRecordDetail($id, $userInfo['merchant_id']);
  115. if (!$record) {
  116. return json_error([], '记录不存在');
  117. }
  118. // 获取游戏信息
  119. $gameInfo = GameModel::where('game_id', $record['game_id'])
  120. ->where('merchant_id', $userInfo['merchant_id'])
  121. ->field(['title', 'title_en', 'image', 'image_en'])
  122. ->find();
  123. if ($gameInfo) {
  124. $record['game_name'] = $gameInfo['title'];
  125. $record['game_name_en'] = $gameInfo['title_en'];
  126. $record['game_icon'] = $gameInfo['image'];
  127. $record['game_icon_en'] = $gameInfo['image_en'];
  128. } else {
  129. $record['game_name'] = $record['game_id'];
  130. $record['game_name_en'] = '';
  131. $record['game_icon'] = '';
  132. $record['game_icon_en'] = '';
  133. }
  134. return json_success($record, '获取成功');
  135. } catch (\Exception $e) {
  136. return json_error([], '获取记录详情失败:' . $e->getMessage());
  137. }
  138. }
  139. /**
  140. * 获取游戏统计信息
  141. */
  142. public function statistics()
  143. {
  144. $userInfo = $this->request->userInfo;
  145. // 筛选条件
  146. $filters = [
  147. 'start_time' => Request::get('start_time', '', 'trim'),
  148. 'end_time' => Request::get('end_time', '', 'trim'),
  149. ];
  150. // 如果没有指定时间范围,默认获取今天
  151. if (empty($filters['start_time']) && empty($filters['end_time'])) {
  152. $filters['start_time'] = date('Y-m-d');
  153. $filters['end_time'] = date('Y-m-d');
  154. }
  155. try {
  156. $statistics = GameRecordModel::getGameStatistics($userInfo['merchant_id'], $filters);
  157. return json_success([
  158. 'statistics' => $statistics,
  159. 'date_range' => [
  160. 'start_time' => $filters['start_time'],
  161. 'end_time' => $filters['end_time']
  162. ]
  163. ], '获取成功');
  164. } catch (\Exception $e) {
  165. return json_error([], '获取统计信息失败:' . $e->getMessage());
  166. }
  167. }
  168. /**
  169. * 获取游戏列表(用于筛选下拉框)
  170. */
  171. public function getGames()
  172. {
  173. $userInfo = $this->request->userInfo;
  174. try {
  175. $gameIds = GameRecordModel::getAllGameIds($userInfo['merchant_id']);
  176. // 从GameModel获取游戏详细信息
  177. $gameInfos = GameModel::whereIn('game_id', $gameIds)
  178. ->where('merchant_id', $userInfo['merchant_id'])
  179. ->field(['game_id', 'title', 'title_en', 'image', 'image_en'])
  180. ->select()
  181. ->toArray();
  182. // 创建游戏ID到游戏信息的映射
  183. $gameMap = [];
  184. foreach ($gameInfos as $gameInfo) {
  185. $gameMap[$gameInfo['game_id']] = $gameInfo;
  186. }
  187. // 构建游戏列表
  188. $games = [];
  189. foreach ($gameIds as $gameId) {
  190. if (isset($gameMap[$gameId])) {
  191. $games[] = [
  192. 'game_id' => $gameId,
  193. 'game_name' => $gameMap[$gameId]['title'],
  194. 'game_name_en' => $gameMap[$gameId]['title_en'],
  195. 'game_icon' => $gameMap[$gameId]['image'],
  196. 'game_icon_en' => $gameMap[$gameId]['image_en']
  197. ];
  198. } else {
  199. $games[] = [
  200. 'game_id' => $gameId,
  201. 'game_name' => $this->getGameName($gameId),
  202. 'game_name_en' => '',
  203. 'game_icon' => '',
  204. 'game_icon_en' => ''
  205. ];
  206. }
  207. }
  208. return json_success($games, '获取成功');
  209. } catch (\Exception $e) {
  210. return json_error([], '获取游戏列表失败:' . $e->getMessage());
  211. }
  212. }
  213. /**
  214. * 获取玩法列表(用于筛选下拉框)
  215. */
  216. public function getPlayMethods()
  217. {
  218. $userInfo = $this->request->userInfo;
  219. try {
  220. $playMethods = GameRecordModel::getAllPlayMethods($userInfo['merchant_id']);
  221. // 构建玩法列表
  222. $methods = [];
  223. foreach ($playMethods as $method) {
  224. $methods[] = [
  225. 'method_id' => $method,
  226. 'method_name' => $this->getPlayMethodName($method)
  227. ];
  228. }
  229. return json_success($methods, '获取成功');
  230. } catch (\Exception $e) {
  231. return json_error([], '获取玩法列表失败:' . $e->getMessage());
  232. }
  233. }
  234. /**
  235. * 导出游戏记录
  236. */
  237. public function export()
  238. {
  239. $userInfo = $this->request->userInfo;
  240. // 筛选条件
  241. $filters = [
  242. 'start_time' => Request::get('start_time', '', 'trim'),
  243. 'end_time' => Request::get('end_time', '', 'trim'),
  244. 'platform_id' => Request::get('platform_id', '', 'trim'),
  245. 'game_id' => Request::get('game_id', '', 'trim'),
  246. 'play_status' => Request::get('play_status', ''),
  247. 'play_method' => Request::get('play_method', '', 'trim'),
  248. 'trigger_method' => Request::get('trigger_method', '', 'trim'),
  249. 'round_number' => Request::get('round_number', '', 'trim'),
  250. ];
  251. try {
  252. // 获取所有符合条件的数据(不分页)
  253. $result = GameRecordModel::getGameRecords($userInfo['merchant_id'], 1, 100000, $filters);
  254. // 获取游戏信息映射
  255. $gameIds = array_unique(array_column($result['list'], 'game_id'));
  256. $gameInfoMap = $this->getGameInfoMap($gameIds, $userInfo['merchant_id']);
  257. // 生成CSV数据
  258. $csvData = "序号,父局号,牌局ID,创建时间,牌局编号,平台昵称,玩家ID,所属商户," .
  259. "平台ID,游戏名称,游戏玩法,调控状态,免费触发,RTP,下注金额," .
  260. "应下注,应返奖,玩家输赢,下注前,结算后,总输赢,最后结算金额,操作\n";
  261. foreach ($result['list'] as $index => $record) {
  262. $gameInfo = $gameInfoMap[$record['game_id']] ?? null;
  263. $gameName = $gameInfo ? $gameInfo['title'] : $this->getGameName($record['game_id']);
  264. $csvData .= sprintf(
  265. "%d,%s,%s,%s,%s,%s,%d,%s,%d,%s,%s,%s,%s,%s,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%s\n",
  266. $index + 1, // 序号
  267. $record['third_gid'], // 父局号
  268. $record['id'], // 牌局ID
  269. $record['create_time_text'], // 创建时间
  270. $record['third_round_id'], // 牌局编号
  271. $record['nickname'], // 平台昵称
  272. $record['uname'], // 玩家ID
  273. $userInfo['merchant_id'], // 所属商户
  274. $record['user_id'], // 平台ID
  275. $gameName, // 游戏名称
  276. $this->getPlayMethodName($record['bet_game_play_type']), // 游戏玩法
  277. $record['status_text'], // 调控状态
  278. '', // 免费触发(暂时为空)
  279. '', // RTP(暂时为空)
  280. $record['bet_amount_yuan'], // 下注金额
  281. 0.00, // 应下注
  282. 0.00, // 应返奖
  283. $record['amount_yuan'], // 玩家输赢
  284. $record['prev_amount_yuan'], // 下注前
  285. $record['next_amount_yuan'], // 结算后
  286. $record['amount_yuan'], // 总输赢
  287. $record['balance_amount_yuan'], // 最后结算金额
  288. $record['action_type_text'] // 操作
  289. );
  290. }
  291. // 返回CSV数据
  292. return response($csvData)
  293. ->header(['Content-Type' => 'text/csv; charset=utf-8'])
  294. ->header(['Content-Disposition' => 'attachment; filename="game_records_' . date('YmdHis') . '.csv"'])
  295. ->header(['Cache-Control' => 'no-cache, must-revalidate']);
  296. } catch (\Exception $e) {
  297. return json_error([], '导出游戏记录失败:' . $e->getMessage());
  298. }
  299. }
  300. /**
  301. * 获取游戏名称
  302. */
  303. private function getGameName($gameId)
  304. {
  305. // 这里可以根据实际情况配置游戏ID对应的名称
  306. $gameNames = [
  307. 'tiger' => '老虎机',
  308. 'fortue_tiger' => '财富老虎',
  309. // 可以继续添加其他游戏
  310. ];
  311. return $gameNames[$gameId] ?? $gameId;
  312. }
  313. /**
  314. * 获取玩法名称
  315. */
  316. private function getPlayMethodName($methodId)
  317. {
  318. // 这里可以根据实际情况配置玩法ID对应的名称
  319. $methodNames = [
  320. 1 => '普通模式',
  321. 2 => '免费模式',
  322. 3 => '特殊模式',
  323. // 可以继续添加其他玩法
  324. ];
  325. return $methodNames[$methodId] ?? '未知玩法';
  326. }
  327. /**
  328. * 获取游戏信息映射
  329. */
  330. private function getGameInfoMap($gameIds, $merchantId)
  331. {
  332. if (empty($gameIds)) {
  333. return [];
  334. }
  335. $gameInfos = GameModel::whereIn('game_id', $gameIds)
  336. ->where('merchant_id', $merchantId)
  337. ->field(['game_id', 'title', 'title_en', 'image', 'image_en'])
  338. ->select()
  339. ->toArray();
  340. $map = [];
  341. foreach ($gameInfos as $gameInfo) {
  342. $map[$gameInfo['game_id']] = $gameInfo;
  343. }
  344. return $map;
  345. }
  346. // 格式化信息
  347. private function formatItemDataText($item = [])
  348. {
  349. $item['bet'] = CommonUtils::convertBalance($item['bet'], false);
  350. $item['prev_amount'] = CommonUtils::convertBalance($item['prev_amount'], false);
  351. $item['next_amount'] = CommonUtils::convertBalance($item['next_amount'], false);
  352. $item['total_amount'] = COmmonUtils::convertBalance($item['total_amount'], false);
  353. $item['win_amount'] = COmmonUtils::convertBalance($item['win_amount'], false);
  354. $item['total_win_amount'] = COmmonUtils::convertBalance($item['total_win_amount'], false);
  355. $item['res_multiple'] = $item['res_multiple'] / 100;
  356. $item['bet_game_play_type'] = CommonUtils::getGamePlayType($item['bet_game_play_type']);
  357. return $item;
  358. }
  359. }