PlayerControl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\common\CommonUtils;
  5. use app\common\CommonMerchantUserAndBalance;
  6. use app\model\GameModel;
  7. use app\model\MerchantsUserModel;
  8. use app\model\PlayerControlModel;
  9. use app\model\UserModel;
  10. use think\facade\Request;
  11. class PlayerControl extends BaseController
  12. {
  13. /**
  14. * 添加或更新 点控设置
  15. * {"account_ids":["115"],"game_ids":[1],"rtp":9900,"max_win_multi":500,"auto_cancel_rtp":100,"agent_id":6027,"win_pro":-1}
  16. */
  17. public function updatePlayerControl()
  18. {
  19. $userInfo = $this->request->userInfo;
  20. $data = Request::param([
  21. 'account_ids' => "",
  22. 'game_ids' => "",
  23. 'rtp' => 0,
  24. 'max_win_multi' => 0,
  25. 'auto_cancel_rtp' => 0
  26. ]);
  27. // 判断RTP
  28. if(empty($data['rtp'])) {
  29. return json_error([], 'RTP错误');
  30. }
  31. $account_ids = $data['account_ids'] ?? "";
  32. $game_ids = $data['game_ids'] ?? "";
  33. // 游戏平台ID和游戏ID不能为空
  34. if(empty($account_ids) || empty($game_ids)) {
  35. return json_error([], '平台ID和游戏ID不能为空');
  36. }
  37. $account_ids = explode(",", $account_ids);
  38. $game_ids = explode(",", $game_ids);
  39. $unameList = [];
  40. $emptyUnameList = [];
  41. foreach ($account_ids as $account_id) {
  42. $account_id = trim($account_id);
  43. if(!empty($account_id) && preg_match("/^[1-9]\d*$/", $account_id)) {
  44. $unameList[] = $account_id;
  45. }else {
  46. $emptyUnameList[] = $account_id;
  47. }
  48. }
  49. if(!empty($emptyUnameList)) {
  50. return json_error([], '不存在玩家:' . implode(",", $emptyUnameList));
  51. }
  52. $gameList = [];
  53. foreach ($game_ids as $game_id) {
  54. $game_id = trim($game_id);
  55. if(!empty($game_id)) {
  56. $gameList[] = $game_id;
  57. }
  58. }
  59. // 过滤掉 游戏平台ID和游戏ID不能为空
  60. if(empty($unameList) || empty($gameList)) {
  61. return json_error([], '平台ID和游戏ID不能为空');
  62. }
  63. $allGameList = GameModel::getGames($userInfo['merchant_id']);
  64. $gameIds = array_column($allGameList, 'game_id');
  65. $emptyIds = [];
  66. foreach ($gameList as $gameId) {
  67. if(!in_array($gameId, $gameIds)) {
  68. $emptyIds[] = $gameId;
  69. }
  70. }
  71. if(!empty($emptyIds)) {
  72. return json_error([], "不存在游戏:" . implode(", ", $emptyIds));
  73. }
  74. $merchantsUserList = MerchantsUserModel::getPlayerListByIds($userInfo['merchant_id'], [
  75. ['uname', 'in', $unameList]
  76. ]);
  77. $merchantsUserList = array_column($merchantsUserList, null, 'uname');
  78. $merchantsUserIds = array_column($merchantsUserList, 'user_id');
  79. $userBalanceList = CommonMerchantUserAndBalance::getMerchantUserBalance($merchantsUserIds);
  80. $userBalanceList = array_column($userBalanceList, null, 'user_id');
  81. foreach ($account_ids as $accountId) {
  82. $merchantsUserInfo = $merchantsUserList[$accountId] ?? [];
  83. if(empty($merchantsUserInfo)) {
  84. continue;
  85. }
  86. $user_id = $merchantsUserInfo['user_id'];
  87. $rtpGameLIst = CommonMerchantUserAndBalance::getMerchantUserGameRtpWinBetInfo([
  88. ['user_id', '=' , $user_id],
  89. ['game_id', 'in', $gameIds],
  90. ], ['rtp','game_id']);
  91. $gameRtp = [];
  92. foreach ($rtpGameLIst as $item) {
  93. $gameRtp[$item['game_id']] = $item['rtp'];
  94. }
  95. foreach ($game_ids as $gameId) {
  96. $old_rtp = $gameRtp[$gameId] ?? 0;
  97. $info = PlayerControlModel::getPlayerControlInfo($userInfo['merchant_id'], $user_id, $gameId);
  98. if(!empty($info) && $info->status == 0) {
  99. $info = null;
  100. }
  101. $balanceInfo = $userBalanceList[$user_id] ?? [];
  102. if(!empty($info)) {
  103. // 需要更新
  104. $info->control_rpt = bcmul( $data['rtp'], 100);
  105. $info->auto_cancel_rtp = bcmul($data['auto_cancel_rtp'], 100);
  106. $info->max_win_multi = $data['max_win_multi'];
  107. $info->old_rtp = $old_rtp;
  108. $info->save();
  109. }else {
  110. $item = [
  111. 'app_id' => $userInfo['merchant_id'],
  112. 'user_id' => $userInfo['user_id'],
  113. 'player_id' => $merchantsUserInfo['user_id'],// 玩家id
  114. 'game_id' => $gameId,
  115. 'old_rtp' => $old_rtp,
  116. 'control_rpt' => bcmul( $data['rtp'], 100),
  117. 'auto_cancel_rtp' => bcmul($data['auto_cancel_rtp'], 100), // 达到RTP值取消
  118. 'max_win_multi' => $data['max_win_multi'], // 最高倍数
  119. 'control_balance' => CommonUtils::convertBalance( $balanceInfo['balance'], false), // 设置时点控那刻当前余额
  120. ];
  121. PlayerControlModel::create($item);
  122. }
  123. }
  124. }
  125. return json_success([],'操作成功');
  126. }
  127. /**
  128. * 获取点控列表
  129. * @return \think\response\Json
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\DbException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. */
  134. public function getPlayerControlList()
  135. {
  136. // 获取当前用户信息
  137. $userInfo = $this->getUserInfo();
  138. // 获取查询参数
  139. $page = Request::get('page', 1, 'intval');
  140. $limit = Request::get('limit', 10, 'intval');
  141. // 过滤条件
  142. // 筛选条件
  143. $filters = [
  144. // 时间筛选
  145. 'start_time' => Request::get('start_time', '', 'trim'),
  146. 'end_time' => Request::get('end_time', '', 'trim'),
  147. // 游戏筛选
  148. 'game_id' => Request::get('game_id', '', 'trim'),
  149. // 平台ID筛选
  150. 'uname' => Request::get('uname', '', 'trim'),
  151. // 玩家ID筛选
  152. 'player_id' => Request::get('user_id', '', 'trim'),
  153. // 平台昵称
  154. 'nickname' => Request::get('nickname', '', 'trim'),
  155. ];
  156. $is_end = Request::get('is_end_time', 0);
  157. $controlWhere = [];
  158. $controlWhere[] = ['user_id', '=', $userInfo['user_id']];
  159. $where = [];
  160. $isFindUserId = false;
  161. // 平台ID筛选
  162. if (!empty($filters['uname'])) {
  163. $where[] = ['uname', '=', $filters['uname']];
  164. $isFindUserId = true;
  165. }
  166. // 平台昵称筛选
  167. if (!empty($filters['nickname'])) {
  168. $where[] = ['nickname', 'like', '%' . $filters['nickname'] . '%'];
  169. $isFindUserId = true;
  170. }
  171. // 玩家id筛选
  172. if (!empty($filters['player_id'])) {
  173. $where[] = ['user_id', 'like', '%' . $filters['player_id'] . '%'];
  174. $isFindUserId = true;
  175. }
  176. if($isFindUserId) {
  177. $merchantsUserIds = MerchantsUserModel::getPlayerListByIds($userInfo['merchant_id'], $where, ['user_id']);
  178. $controlWhere[] = ['user_id', 'in', array_column($merchantsUserIds, 'user_id')];
  179. }
  180. if(!empty($filters['game_id'])) {
  181. $controlWhere[] = ['game_id', 'in', explode(",", $filters['game_id'])];
  182. }
  183. if(!empty($filters['start_time']) && !empty($filters['end_time'])) {
  184. $controlWhere[] = [$is_end ? 'end_time' : 'update_time', '>=', strtotime($filters['start_time'] . " 00:00:00")];
  185. $controlWhere[] = [$is_end ? 'end_time' :'update_time', '<=', strtotime($filters['end_time'] . " 23:59:59")];
  186. }
  187. $controlWhere[] = ['status', '=', $is_end ? 0 : 1];
  188. $results = PlayerControlModel::getPlayerControlList($userInfo['merchant_id'], $page, $limit, $controlWhere);
  189. $allGameList = GameModel::getGames($userInfo['merchant_id'], [], ['id', 'game_id', 'title', 'game_platform']);
  190. $gameConfig = array_column($allGameList, null, 'game_id');
  191. $player_ids = array_column($results['list'], 'player_id');
  192. $playerConfig = CommonMerchantUserAndBalance::getMerchantUser($player_ids, ['user_id','uname','nickname']);
  193. $playerConfig = array_column($playerConfig, null, 'user_id');
  194. $userListBalance = CommonMerchantUserAndBalance::getMerchantUserBalance($player_ids);
  195. $userBalanceConfig = [];
  196. foreach ($userListBalance as $item) {
  197. $userBalanceConfig[$item['user_id']] = $item['balance'];
  198. }
  199. $rtpUserList = [];
  200. foreach ($results['list'] as $item) {
  201. $player_id = $item['player_id'];
  202. if(empty($rtpUserList[$player_id])) {
  203. $rtpUserList[$player_id] = [];
  204. }
  205. $rtpUserList[$player_id][] = $item['game_id'];
  206. }
  207. $rtpConfigList = [];
  208. foreach ($rtpUserList as $player_id => $gameIds) {
  209. $temp = CommonMerchantUserAndBalance::getMerchantUserGameRtpWinBetInfo([
  210. ['user_id', '=' , $player_id],
  211. ['game_id', 'in', $gameIds],
  212. ], ['rtp','game_id']);
  213. foreach ($temp as $item) {
  214. $game_id = $item['game_id'];
  215. $rtp = $item['rtp'];
  216. $key = "{$player_id}_{$game_id}";
  217. $rtpConfigList[$key] = $rtp;
  218. }
  219. }
  220. foreach ($results['list'] as $key => $item) {
  221. $game_id = $item['game_id'];
  222. $player_id = $item['player_id'];
  223. $one_key = "{$player_id}_{$game_id}";
  224. $gameInfo = $gameConfig[$game_id] ?? [];
  225. if (!empty($gameInfo))
  226. {
  227. $gameImages = CommonUtils::getGameDefaultImage($gameInfo);
  228. }
  229. $item['game_image_url'] = $gameImages['image_url'] ?? '';
  230. $item['game_title'] = $gameInfo['title'] ?? '';
  231. $item['game_type_text'] = CommonUtils::getGameTypeConfig($gameInfo['game_platform']);
  232. $item['balance'] = CommonUtils::convertBalance($userBalanceConfig[$player_id] ?? 0, false);
  233. $item['bet_amount'] = CommonUtils::convertBalance($item['bet_amount'] ?? 0, false);
  234. $item['total_win_amount'] = CommonUtils::convertBalance($item['total_win_amount'] ?? 0, false);
  235. $item['control_rpt'] = bcdiv($item['control_rpt'], 100, 2) . "%";
  236. $item['old_rtp'] = bcdiv($item['old_rtp'], 100, 2) . "%";
  237. $item['now_rtp'] = bcdiv($rtpConfigList[$one_key] ?? 0, 100, 2) . "%";
  238. if(!empty($item['auto_cancel_rtp'])) {
  239. $item['auto_cancel_rtp'] = bcdiv($item['auto_cancel_rtp'], 100, 2) . "%";
  240. }
  241. $playerInfo = $playerConfig[$player_id] ?? [];
  242. $item['uname'] = $playerInfo['uname'] ?? '';
  243. $item['nickname'] = $playerInfo['nickname'] ?? '';
  244. $adminInfo = UserModel::find($item['user_id']);
  245. $item['admin_name'] = "";
  246. if(!empty($adminInfo)) {
  247. $item['admin_name'] = $adminInfo['nick_name'] ?? '';
  248. }
  249. if(!empty($item['end_time'])) {
  250. $item['end_time'] = date('Y-m-d H:i:s', $item['end_time']);
  251. }
  252. $results['list'][$key] = $item;
  253. }
  254. return json_success($results);
  255. }
  256. /**
  257. * 取消点控设置
  258. * @return \think\response\Json
  259. * @throws \think\db\exception\DataNotFoundException
  260. * @throws \think\db\exception\DbException
  261. * @throws \think\db\exception\ModelNotFoundException
  262. */
  263. public function cancelPlayerControlInfo()
  264. {
  265. $userInfo = $this->getUserInfo();
  266. $control_id = Request::post('control_id','', 'trim');
  267. if(empty($control_id)) {
  268. return json_error([], '参数错误');
  269. }
  270. $info = PlayerControlModel::where(['control_id' => $control_id, 'user_id' => $userInfo['user_id']])->find();
  271. if(!empty($info)) {
  272. $info->end_time = time();
  273. $info->status = 0;
  274. $info->save();
  275. }
  276. return json_success([],'操作成功');
  277. }
  278. /**
  279. * 一键取消全部点控
  280. * @return \think\response\Json
  281. */
  282. public function cancelAllPlayerControlInfo()
  283. {
  284. $userInfo = $this->getUserInfo();
  285. PlayerControlModel::where(['user_id' => $userInfo['user_id']])->save(['status' => 0, 'end_time' => time()]);
  286. return json_success([],'操作成功');
  287. }
  288. }