MerchantsUserModel.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. use app\model\PlayerLoginLogModel;
  6. use app\model\GameBetGameModel;
  7. /**
  8. * 商户用户模型 - 连接到fortue_tiger数据库的tp_merchants_user表
  9. */
  10. class MerchantsUserModel extends Model
  11. {
  12. // 设置数据库连接
  13. protected $connection = 'fortue_tiger';
  14. // 设置表名
  15. protected $name = 'merchants_user';
  16. // 设置主键
  17. protected $pk = 'user_id';
  18. // 设置自动时间戳
  19. protected $autoWriteTimestamp = 'int';
  20. // 设置字段类型
  21. protected $type = [
  22. 'user_id' => 'int',
  23. 'app_id' => 'int',
  24. 'uname' => 'int',
  25. 'nickname' => 'string',
  26. 'cid' => 'int',
  27. 'token' => 'string',
  28. 'short_token' => 'string',
  29. 'login_time' => 'int',
  30. 'reg_ip' => 'string',
  31. 'login_ip' => 'string',
  32. 'today_count' => 'int',
  33. 'history_day_count' => 'int',
  34. 'adjust_status' => 'int',
  35. 'status' => 'int',
  36. 'disabled_login' => 'int',
  37. 'create_time' => 'int',
  38. 'update_time' => 'int',
  39. ];
  40. // 状态常量
  41. const STATUS_NORMAL = 1; // 正常
  42. const STATUS_FROZEN = 0; // 冻结
  43. // 禁止登录状态
  44. const LOGIN_ENABLED = 1; // 允许登录
  45. const LOGIN_DISABLED = 0; // 禁止登录
  46. // 调控状态常量
  47. const ADJUST_STATUS_NORMAL = 1; // 正常
  48. const ADJUST_STATUS_WIN = 2; // 放水(赢)
  49. const ADJUST_STATUS_LOSE = 3; // 收割(输)
  50. /**
  51. * 获取状态文本
  52. */
  53. public static function getStatusText($status): string
  54. {
  55. $statusMap = [
  56. self::STATUS_NORMAL => '正常',
  57. self::STATUS_FROZEN => '冻结',
  58. ];
  59. return $statusMap[$status] ?? '未知';
  60. }
  61. /**
  62. * 获取调控状态文本
  63. */
  64. public static function getAdjustStatusText($adjustStatus): string
  65. {
  66. $adjustStatusMap = [
  67. self::ADJUST_STATUS_NORMAL => '正常',
  68. self::ADJUST_STATUS_WIN => '放水',
  69. self::ADJUST_STATUS_LOSE => '收割',
  70. ];
  71. return $adjustStatusMap[$adjustStatus] ?? '未知';
  72. }
  73. /**
  74. * 获取禁止登录状态文本
  75. */
  76. public static function getDisabledLoginText($disabled): string
  77. {
  78. $disabledMap = [
  79. self::LOGIN_ENABLED => '允许',
  80. self::LOGIN_DISABLED => '禁止',
  81. ];
  82. return $disabledMap[$disabled] ?? '未知';
  83. }
  84. /**
  85. * 通过unameList批量获取指定平台用户详情
  86. * @param $merchantId
  87. * @param $wheres
  88. * @return mixed
  89. */
  90. public static function getPlayerListByIds($merchantId, $wheres = [], $field = [])
  91. {
  92. $query = self::where('app_id', $merchantId)->where($wheres);
  93. if(!empty($field)) {
  94. $query = $query->field($field);
  95. }
  96. return $query->select()->toArray();
  97. }
  98. /**
  99. * 根据商户ID获取玩家列表
  100. */
  101. public static function getPlayerListByMerchant($merchantId, $page = 1, $limit = 10, $filters = [])
  102. {
  103. $query = self::where('app_id', $merchantId);
  104. // 应用过滤条件
  105. if (!empty($filters['uname'])) {
  106. $query->where('uname', 'like', '%' . $filters['uname'] . '%');
  107. }
  108. if (!empty($filters['nickname'])) {
  109. $query->where('nickname', 'like', '%' . $filters['nickname'] . '%');
  110. }
  111. if (!empty($filters['player_id'])) {
  112. $query->where('user_id', $filters['player_id']);
  113. }
  114. if (isset($filters['status']) && $filters['status'] !== '') {
  115. $query->where('status', $filters['status']);
  116. }
  117. if (isset($filters['adjust_status']) && $filters['adjust_status'] !== '') {
  118. $query->where('adjust_status', $filters['adjust_status']);
  119. }
  120. if (!empty($filters['login_ip'])) {
  121. $query->where('login_ip', 'like', '%' . $filters['login_ip'] . '%');
  122. }
  123. if (!empty($filters['reg_ip'])) {
  124. $query->where('reg_ip', 'like', '%' . $filters['reg_ip'] . '%');
  125. }
  126. // 登录时间范围
  127. if (!empty($filters['login_time_start'])) {
  128. $query->where('login_time', '>=', strtotime($filters['login_time_start']));
  129. }
  130. if (!empty($filters['login_time_end'])) {
  131. $query->where('login_time', '<=', strtotime($filters['login_time_end']));
  132. }
  133. $order = $filters['order'] ?? 'user_id';
  134. $sort = $filters['sort'] ?? 'desc';
  135. $query->order($order, $sort);
  136. // 获取总数
  137. $total = $query->count();
  138. // 获取列表
  139. $list = $query->page($page, $limit)->select()->toArray();
  140. $user_ids = array_column($list, 'user_id');
  141. // 获取余额
  142. $userBalanceConfig = MerchantsUserBalanceModel::getUsersBalanceList($merchantId, $user_ids);
  143. // 获取今日登录次数
  144. $loginFilter = [
  145. 'start_time' => strtotime(date('Y-m-d')),
  146. 'end_time' => strtotime(date('Y-m-d') . " +1 day"),
  147. ];
  148. $loginCount = PlayerLoginLogModel::getLoginCount($merchantId, $user_ids, $loginFilter);
  149. $betGameFilter = [
  150. 'start_time' => strtotime(date('Y-m-d')),
  151. 'end_time' => strtotime(date('Y-m-d') . " +1 day"),
  152. 'user_ids' => $user_ids,
  153. ];
  154. // 获取今日输赢、今日下注
  155. $betGameSummary = GameBetGameModel::getGameUserSummary($merchantId, $betGameFilter);
  156. // 转换字段名以保持与原有接口的兼容性
  157. foreach ($list as &$item) {
  158. $userBalanceInfo = $userBalanceConfig[$item['user_id']] ?? [];
  159. $item['merchant_id'] = $item['app_id'];
  160. $item['today_login_count'] = $loginCount[$item['user_id']]['count'] ?? 0;
  161. $item['history_login_count'] = $item['history_day_count'];
  162. // 添加原系统中可能需要但新表中没有的字段
  163. $item['balance'] = $userBalanceInfo['balance'] ?? 0;
  164. $item['today_win_amount'] = $betGameSummary['game_profit'] ?? 0;
  165. $item['history_win_amount'] = (float)$item['total_win_amount'];
  166. $item['today_bet_amount'] = $betGameSummary['bet_amount'] ?? 0;;
  167. // 移除字段
  168. unset($item['app_id']);
  169. unset($item['token']);
  170. unset($item['short_token']);
  171. }
  172. return [
  173. 'list' => $list,
  174. 'total' => $total,
  175. 'page' => $page,
  176. 'limit' => $limit
  177. ];
  178. }
  179. /**
  180. * 获取玩家详情
  181. */
  182. public static function getPlayerDetail($playerId, $merchantId)
  183. {
  184. $player = self::where('user_id', $playerId)
  185. ->where('app_id', $merchantId)
  186. ->find();
  187. if ($player) {
  188. $player = $player->toArray();
  189. // 移除敏感字段
  190. unset($player['token']);
  191. unset($player['short_token']);
  192. // 转换字段名
  193. $player['merchant_id'] = $player['app_id'];
  194. $player['today_login_count'] = $player['today_count'];
  195. $player['history_login_count'] = $player['history_day_count'];
  196. // 添加原系统中可能需要但新表中没有的字段
  197. $player['balance'] = 0;
  198. $player['today_win_amount'] = 0;
  199. $player['history_win_amount'] = 0;
  200. $player['today_bet_amount'] = 0;
  201. }
  202. return $player;
  203. }
  204. /**
  205. * 更新玩家状态
  206. */
  207. public static function updatePlayerStatus($playerIds, $merchantId, $status)
  208. {
  209. return self::where('app_id', $merchantId)
  210. ->whereIn('user_id', $playerIds)
  211. ->update(['status' => $status]);
  212. }
  213. /**
  214. * 更新玩家调控状态
  215. */
  216. public static function updatePlayerAdjustStatus($playerIds, $merchantId, $adjustStatus)
  217. {
  218. return self::where('app_id', $merchantId)
  219. ->whereIn('user_id', $playerIds)
  220. ->update(['adjust_status' => $adjustStatus]);
  221. }
  222. /**
  223. * 更新玩家禁止登录状态
  224. */
  225. public static function updatePlayerLoginStatus($playerIds, $merchantId, $disabledLogin)
  226. {
  227. return self::where('app_id', $merchantId)
  228. ->whereIn('uname', $playerIds)
  229. ->update(['disabled_login' => $disabledLogin]);
  230. }
  231. /**
  232. * 获取玩家统计信息
  233. */
  234. public static function getPlayerStatistics($merchantId, $filters = [])
  235. {
  236. $where = [
  237. ['app_id', '=', $merchantId]
  238. ];
  239. $getWhere = function(string $timeField) use ($where, $filters) {
  240. // 时间筛选
  241. if (!empty($filters['start_time'])) {
  242. $startTime = strtotime($filters['start_time']);
  243. $where[] = [$timeField, '>=', $startTime];
  244. }
  245. if (!empty($filters['end_time'])) {
  246. $endTime = strtotime($filters['end_time']);
  247. $where[] = [$timeField, '<=', $endTime];
  248. }
  249. return $where;
  250. };
  251. // 获取注册用户数
  252. $registerUsers = self::where($getWhere('create_time'))->field([
  253. "FROM_UNIXTIME(create_time, '%Y-%m-%d') as date", // 日期
  254. 'COUNT(user_id) as register_users', // 注册用户数
  255. ])
  256. ->group('date')
  257. ->select()
  258. ->toArray();
  259. // 获取登录用户数
  260. $loginUsers = self::where($getWhere('login_time'))->field([
  261. "FROM_UNIXTIME(login_time, '%Y-%m-%d') as date", // 日期
  262. 'COUNT(user_id) as login_users', // 登录用户数
  263. ])
  264. ->group('date')
  265. ->select()
  266. ->toArray();
  267. $staticData = [];
  268. $loginUsers = array_column($loginUsers, null, 'login_users');
  269. foreach ($registerUsers as $key => $row) {
  270. $row['login_users'] = $loginUsers[$key]['login_users'] ?? 0;
  271. $staticData[$key] = $row;
  272. }
  273. return $staticData;
  274. }
  275. }