PlayerControl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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([
  98. 'app_id' => $userInfo['merchant_id'],
  99. 'player_id' => $user_id,
  100. 'game_id' => $gameId,
  101. 'status' => 1
  102. ]);
  103. // if(!empty($info) && $info->status == 0) {
  104. // $info = null;
  105. // }
  106. $balanceInfo = $userBalanceList[$user_id] ?? [];
  107. if(!empty($info)) {
  108. // 需要更新
  109. $info->control_rpt = bcmul( $data['rtp'], 100);
  110. $info->auto_cancel_rtp = bcmul($data['auto_cancel_rtp'], 100);
  111. $info->max_win_multi = $data['max_win_multi'];
  112. $info->old_rtp = $old_rtp;
  113. $info->save();
  114. }else {
  115. $item = [
  116. 'app_id' => $userInfo['merchant_id'],
  117. 'user_id' => $userInfo['user_id'],
  118. 'player_id' => $merchantsUserInfo['user_id'],// 玩家id
  119. 'game_id' => $gameId,
  120. 'old_rtp' => $old_rtp,
  121. 'control_rpt' => bcmul( $data['rtp'], 100),
  122. 'auto_cancel_rtp' => bcmul($data['auto_cancel_rtp'], 100), // 达到RTP值取消
  123. 'max_win_multi' => $data['max_win_multi'], // 最高倍数
  124. 'control_balance' => CommonUtils::convertBalance( $balanceInfo['balance'], false), // 设置时点控那刻当前余额
  125. ];
  126. PlayerControlModel::create($item);
  127. }
  128. }
  129. }
  130. return json_success([],'操作成功');
  131. }
  132. /**
  133. * 获取点控列表
  134. * @return \think\response\Json
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\DbException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. */
  139. public function getPlayerControlList()
  140. {
  141. // 获取当前用户信息
  142. $userInfo = $this->getUserInfo();
  143. // 获取查询参数
  144. $page = Request::get('page', 1, 'intval');
  145. $limit = Request::get('limit', 10, 'intval');
  146. // 过滤条件
  147. // 筛选条件
  148. $filters = [
  149. // 时间筛选
  150. 'start_time' => Request::get('start_time', '', 'trim'),
  151. 'end_time' => Request::get('end_time', '', 'trim'),
  152. // 游戏筛选
  153. 'game_id' => Request::get('game_id', '', 'trim'),
  154. // 平台ID筛选
  155. 'uname' => Request::get('uname', '', 'trim'),
  156. // 玩家ID筛选
  157. 'player_id' => Request::get('user_id', '', 'trim'),
  158. // 平台昵称
  159. 'nickname' => Request::get('nickname', '', 'trim'),
  160. ];
  161. $is_end = Request::get('is_end_time', 0);
  162. $controlWhere = [];
  163. $controlWhere[] = ['user_id', '=', $userInfo['user_id']];
  164. $where = [];
  165. $isFindUserId = false;
  166. // 平台ID筛选
  167. if (!empty($filters['uname'])) {
  168. $where[] = ['uname', '=', $filters['uname']];
  169. $isFindUserId = true;
  170. }
  171. // 平台昵称筛选
  172. if (!empty($filters['nickname'])) {
  173. $where[] = ['nickname', 'like', '%' . $filters['nickname'] . '%'];
  174. $isFindUserId = true;
  175. }
  176. // 玩家id筛选
  177. if (!empty($filters['player_id'])) {
  178. $where[] = ['user_id', 'like', '%' . $filters['player_id'] . '%'];
  179. $isFindUserId = true;
  180. }
  181. if($isFindUserId) {
  182. $merchantsUserIds = MerchantsUserModel::getPlayerListByIds($userInfo['merchant_id'], $where, ['user_id']);
  183. $controlWhere[] = ['user_id', 'in', array_column($merchantsUserIds, 'user_id')];
  184. }
  185. if(!empty($filters['game_id'])) {
  186. $controlWhere[] = ['game_id', 'in', explode(",", $filters['game_id'])];
  187. }
  188. if(!empty($filters['start_time']) && !empty($filters['end_time'])) {
  189. $controlWhere[] = [$is_end ? 'end_time' : 'update_time', '>=', strtotime($filters['start_time'] . " 00:00:00")];
  190. $controlWhere[] = [$is_end ? 'end_time' :'update_time', '<=', strtotime($filters['end_time'] . " 23:59:59")];
  191. }
  192. $controlWhere[] = ['status', '=', $is_end ? 0 : 1];
  193. $results = PlayerControlModel::getPlayerControlList($userInfo['merchant_id'], $page, $limit, $controlWhere);
  194. $allGameList = GameModel::getGames($userInfo['merchant_id'], [], ['id', 'game_id', 'title', 'game_platform']);
  195. $gameConfig = array_column($allGameList, null, 'game_id');
  196. $player_ids = array_column($results['list'], 'player_id');
  197. $playerConfig = CommonMerchantUserAndBalance::getMerchantUser($player_ids, ['user_id','uname','nickname']);
  198. $playerConfig = array_column($playerConfig, null, 'user_id');
  199. $userListBalance = CommonMerchantUserAndBalance::getMerchantUserBalance($player_ids);
  200. $userBalanceConfig = [];
  201. foreach ($userListBalance as $item) {
  202. $userBalanceConfig[$item['user_id']] = $item['balance'];
  203. }
  204. $rtpUserList = [];
  205. foreach ($results['list'] as $item) {
  206. $player_id = $item['player_id'];
  207. if(empty($rtpUserList[$player_id])) {
  208. $rtpUserList[$player_id] = [];
  209. }
  210. $rtpUserList[$player_id][] = $item['game_id'];
  211. }
  212. $rtpConfigList = [];
  213. foreach ($rtpUserList as $player_id => $gameIds) {
  214. $temp = CommonMerchantUserAndBalance::getMerchantUserGameRtpWinBetInfo([
  215. ['user_id', '=' , $player_id],
  216. ['game_id', 'in', $gameIds],
  217. ], ['rtp','game_id']);
  218. foreach ($temp as $item) {
  219. $game_id = $item['game_id'];
  220. $rtp = $item['rtp'];
  221. $key = "{$player_id}_{$game_id}";
  222. $rtpConfigList[$key] = $rtp;
  223. }
  224. }
  225. foreach ($results['list'] as $key => $item) {
  226. $game_id = $item['game_id'];
  227. $player_id = $item['player_id'];
  228. $one_key = "{$player_id}_{$game_id}";
  229. $gameInfo = $gameConfig[$game_id] ?? [];
  230. if (!empty($gameInfo))
  231. {
  232. $gameImages = CommonUtils::getGameDefaultImage($gameInfo);
  233. }
  234. $item['game_image_url'] = $gameImages['image_url'] ?? '';
  235. $item['game_title'] = $gameInfo['title'] ?? '';
  236. $item['game_type_text'] = CommonUtils::getGameTypeConfig($gameInfo['game_platform']);
  237. $item['balance'] = CommonUtils::convertBalance($userBalanceConfig[$player_id] ?? 0, false);
  238. $item['bet_amount'] = CommonUtils::convertBalance($item['bet_amount'] ?? 0, false);
  239. $item['total_win_amount'] = CommonUtils::convertBalance($item['total_win_amount'] ?? 0, false);
  240. $item['control_rpt'] = bcdiv($item['control_rpt'], 100, 2) . "%";
  241. $item['old_rtp'] = bcdiv($item['old_rtp'], 100, 2) . "%";
  242. $item['now_rtp'] = bcdiv($rtpConfigList[$one_key] ?? 0, 100, 2) . "%";
  243. if(!empty($item['auto_cancel_rtp'])) {
  244. $item['auto_cancel_rtp'] = bcdiv($item['auto_cancel_rtp'], 100, 2) . "%";
  245. }
  246. $playerInfo = $playerConfig[$player_id] ?? [];
  247. $item['uname'] = $playerInfo['uname'] ?? '';
  248. $item['nickname'] = $playerInfo['nickname'] ?? '';
  249. $adminInfo = UserModel::find($item['user_id']);
  250. $item['admin_name'] = "";
  251. if(!empty($adminInfo)) {
  252. $item['admin_name'] = $adminInfo['nick_name'] ?? '';
  253. }
  254. if(!empty($item['end_time'])) {
  255. $item['end_time'] = date('Y-m-d H:i:s', $item['end_time']);
  256. }
  257. $results['list'][$key] = $item;
  258. }
  259. return json_success($results);
  260. }
  261. /**
  262. * 取消点控设置
  263. * @return \think\response\Json
  264. * @throws \think\db\exception\DataNotFoundException
  265. * @throws \think\db\exception\DbException
  266. * @throws \think\db\exception\ModelNotFoundException
  267. */
  268. public function cancelPlayerControlInfo()
  269. {
  270. $userInfo = $this->getUserInfo();
  271. $control_id = Request::post('control_id','', 'trim');
  272. if(empty($control_id)) {
  273. return json_error([], '参数错误');
  274. }
  275. $info = PlayerControlModel::where(['control_id' => $control_id, 'user_id' => $userInfo['user_id']])->find();
  276. if(!empty($info)) {
  277. $info->end_time = time();
  278. $info->status = 0;
  279. $info->save();
  280. }
  281. return json_success([],'操作成功');
  282. }
  283. /**
  284. * 一键取消全部点控
  285. * @return \think\response\Json
  286. */
  287. public function cancelAllPlayerControlInfo()
  288. {
  289. $userInfo = $this->getUserInfo();
  290. PlayerControlModel::where(['user_id' => $userInfo['user_id'], 'status' => 1])->save(['status' => 0, 'end_time' => time()]);
  291. return json_success([],'操作成功');
  292. }
  293. }