PlayerLoginLogModel.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. public static function getLoginCount($merchant_id, $player_ids, $filter = []) {
  32. // 支持单个或多个player_id
  33. $player_ids = is_array($player_ids) ? $player_ids : [$player_ids];
  34. $query = self::where('merchant_id', $merchant_id)
  35. ->whereIn('player_id', $player_ids);
  36. // 处理可选过滤条件
  37. if (!empty($filter['game_id'])) {
  38. $query->where('game_id', $filter['game_id']);
  39. }
  40. if (!empty($filter['game_platform'])) {
  41. $query->where('game_platform', $filter['game_platform']);
  42. }
  43. if (!empty($filter['login_status'])) {
  44. $query->where('login_status', $filter['login_status']);
  45. }
  46. if (!empty($filter['start_time'])) {
  47. $query->where('login_time', '>=', $filter['start_time']);
  48. }
  49. if (!empty($filter['end_time'])) {
  50. $query->where('login_time', '<=', $filter['end_time']);
  51. }
  52. if (!empty($filter['login_ip'])) {
  53. $query->where('login_ip', $filter['login_ip']);
  54. }
  55. // 按player_id分组统计
  56. $result = $query->field('player_id, COUNT(*) as count')
  57. ->group('player_id')
  58. ->select()
  59. ->toArray();
  60. return array_column($result, null, 'player_id');
  61. }
  62. }