PlayerControl.php 12 KB

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