PlayerLoginLogModel.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. /**
  6. * 玩家登录日志模型
  7. */
  8. class PlayerLoginLogModel extends Model
  9. {
  10. // 设置数据库连接
  11. protected $connection = 'fortue_tiger';
  12. // 设置表名
  13. protected $name = 'player_login_log';
  14. // 设置主键
  15. protected $pk = 'id';
  16. // 设置自动时间戳
  17. protected $autoWriteTimestamp = 'int';
  18. // 设置字段类型
  19. protected $type = [
  20. 'merchant_id' => 'int',
  21. 'player_id' => 'int',
  22. 'game_id' => 'int',
  23. 'game_platform' => 'string',
  24. 'login_ip' => 'string',
  25. 'login_time' => 'int',
  26. 'login_status' => 'int',
  27. ];
  28. /**
  29. * 获取查询条件
  30. */
  31. private static function _getWhere($query, $filter) {
  32. // 处理可选过滤条件
  33. if (!empty($filter['game_id'])) {
  34. $query->where('game_id', $filter['game_id']);
  35. }
  36. if (!empty($filter['game_platform'])) {
  37. $query->where('game_platform', $filter['game_platform']);
  38. }
  39. if (!empty($filter['login_status'])) {
  40. $query->where('login_status', $filter['login_status']);
  41. }
  42. if (!empty($filter['start_time'])) {
  43. $startTime = strtotime($filter['start_time']);
  44. $query->where('login_time', '>=', $startTime);
  45. }
  46. if (!empty($filter['end_time'])) {
  47. $endTime = strtotime($filter['end_time']);
  48. $query->where('login_time', '<=', $endTime);
  49. }
  50. if (!empty($filter['login_ip'])) {
  51. $query->where('login_ip', $filter['login_ip']);
  52. }
  53. return $query;
  54. }
  55. /**
  56. * 获取每日登录用户数
  57. */
  58. public static function getLoginDateCount($merchant_id, $filter = []) {
  59. $query = self::where('merchant_id', $merchant_id);
  60. // 按player_id分组统计
  61. $result = self::_getWhere($query, $filter)->field("FROM_UNIXTIME(login_time, '%Y-%m-%d') as date, COUNT(*) as count")
  62. ->group('date')
  63. ->select()
  64. ->toArray();
  65. return array_column($result, null, 'date');
  66. }
  67. /**
  68. * 获取每小时登录用户数
  69. */
  70. public static function getLoginHourCount($merchant_id, $filter = []) {
  71. $query = self::where('merchant_id', $merchant_id);
  72. // 按player_id分组统计
  73. $result = self::_getWhere($query, $filter)->field("FROM_UNIXTIME(login_time, '%Y%m%d%H') as hour, COUNT(*) as count")
  74. ->group('hour')
  75. ->select()
  76. ->toArray();
  77. return $result;
  78. }
  79. /**
  80. * 获取玩家登录次数
  81. */
  82. public static function getLoginPlayerCount($merchant_id, $player_ids, $filter = []) {
  83. // 支持单个或多个player_id
  84. $player_ids = is_array($player_ids) ? $player_ids : [$player_ids];
  85. $query = self::where('merchant_id', $merchant_id)
  86. ->whereIn('player_id', $player_ids);
  87. // 按player_id分组统计
  88. $result = self::_getWhere($query, $filter)->field('player_id, COUNT(*) as count')
  89. ->group('player_id')
  90. ->select()
  91. ->toArray();
  92. return array_column($result, null, 'player_id');
  93. }
  94. }