PlayerModel.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. /**
  6. * 商户玩家模型
  7. */
  8. class PlayerModel extends Model
  9. {
  10. // 设置表名
  11. protected $name = 'merchant_player';
  12. // 设置主键
  13. protected $pk = 'player_id';
  14. // 设置自动时间戳
  15. protected $autoWriteTimestamp = 'int';
  16. // 设置字段类型
  17. protected $type = [
  18. 'player_id' => 'int',
  19. 'nickname' => 'string',
  20. 'merchant_id' => 'int',
  21. 'reg_ip' => 'string',
  22. 'login_ip' => 'string',
  23. 'today_win_amount' => 'float',
  24. 'history_win_amount' => 'float',
  25. 'today_bet_amount' => 'float',
  26. 'balance' => 'float',
  27. 'today_login_count' => 'int',
  28. 'history_login_count' => 'int',
  29. 'adjust_status' => 'int',
  30. 'status' => 'int',
  31. 'login_time' => 'int',
  32. 'create_time' => 'int',
  33. 'update_time' => 'int',
  34. ];
  35. // 状态常量
  36. const STATUS_NORMAL = 0; // 正常
  37. const STATUS_FROZEN = 1; // 冻结
  38. const STATUS_DELETED = 2; // 删除
  39. // 调控状态常量
  40. const ADJUST_STATUS_NORMAL = 0; // 正常
  41. const ADJUST_STATUS_WIN = 1; // 放水(赢)
  42. const ADJUST_STATUS_LOSE = 2; // 收割(输)
  43. /**
  44. * 获取状态文本
  45. */
  46. public static function getStatusText($status): string
  47. {
  48. $statusMap = [
  49. self::STATUS_NORMAL => '正常',
  50. self::STATUS_FROZEN => '冻结',
  51. self::STATUS_DELETED => '删除',
  52. ];
  53. return $statusMap[$status] ?? '未知';
  54. }
  55. /**
  56. * 获取调控状态文本
  57. */
  58. public static function getAdjustStatusText($adjustStatus): string
  59. {
  60. $adjustStatusMap = [
  61. self::ADJUST_STATUS_NORMAL => '正常',
  62. self::ADJUST_STATUS_WIN => '放水',
  63. self::ADJUST_STATUS_LOSE => '收割',
  64. ];
  65. return $adjustStatusMap[$adjustStatus] ?? '未知';
  66. }
  67. /**
  68. * 根据商户ID获取玩家列表
  69. */
  70. public static function getPlayerListByMerchant($merchantId, $page = 1, $limit = 10, $filters = [])
  71. {
  72. $query = self::where('merchant_id', $merchantId);
  73. // 应用过滤条件
  74. if (!empty($filters['nickname'])) {
  75. $query->where('nickname', 'like', '%' . $filters['nickname'] . '%');
  76. }
  77. if (!empty($filters['player_id'])) {
  78. $query->where('player_id', $filters['player_id']);
  79. }
  80. if (isset($filters['status']) && $filters['status'] !== '') {
  81. $query->where('status', $filters['status']);
  82. }
  83. if (isset($filters['adjust_status']) && $filters['adjust_status'] !== '') {
  84. $query->where('adjust_status', $filters['adjust_status']);
  85. }
  86. if (!empty($filters['login_ip'])) {
  87. $query->where('login_ip', 'like', '%' . $filters['login_ip'] . '%');
  88. }
  89. if (!empty($filters['reg_ip'])) {
  90. $query->where('reg_ip', 'like', '%' . $filters['reg_ip'] . '%');
  91. }
  92. // 余额范围
  93. if (!empty($filters['balance_min'])) {
  94. $query->where('balance', '>=', $filters['balance_min']);
  95. }
  96. if (!empty($filters['balance_max'])) {
  97. $query->where('balance', '<=', $filters['balance_max']);
  98. }
  99. // 今日输赢范围
  100. if (!empty($filters['today_win_min'])) {
  101. $query->where('today_win_amount', '>=', $filters['today_win_min']);
  102. }
  103. if (!empty($filters['today_win_max'])) {
  104. $query->where('today_win_amount', '<=', $filters['today_win_max']);
  105. }
  106. // 历史输赢范围
  107. if (!empty($filters['history_win_min'])) {
  108. $query->where('history_win_amount', '>=', $filters['history_win_min']);
  109. }
  110. if (!empty($filters['history_win_max'])) {
  111. $query->where('history_win_amount', '<=', $filters['history_win_max']);
  112. }
  113. // 登录时间范围
  114. if (!empty($filters['login_time_start'])) {
  115. $query->where('login_time', '>=', strtotime($filters['login_time_start']));
  116. }
  117. if (!empty($filters['login_time_end'])) {
  118. $query->where('login_time', '<=', strtotime($filters['login_time_end']));
  119. }
  120. // 创建时间范围
  121. if (!empty($filters['create_time_start'])) {
  122. $query->where('create_time', '>=', strtotime($filters['create_time_start']));
  123. }
  124. if (!empty($filters['create_time_end'])) {
  125. $query->where('create_time', '<=', strtotime($filters['create_time_end']));
  126. }
  127. // 排序
  128. $order = $filters['order'] ?? 'player_id';
  129. $sort = $filters['sort'] ?? 'desc';
  130. $query->order($order, $sort);
  131. // 获取总数
  132. $total = $query->count();
  133. // 获取列表
  134. $list = $query->page($page, $limit)->select();
  135. return [
  136. 'list' => $list,
  137. 'total' => $total,
  138. 'page' => $page,
  139. 'limit' => $limit
  140. ];
  141. }
  142. /**
  143. * 获取玩家详情
  144. */
  145. public static function getPlayerDetail($playerId, $merchantId)
  146. {
  147. return self::where('player_id', $playerId)
  148. ->where('merchant_id', $merchantId)
  149. ->find();
  150. }
  151. /**
  152. * 更新玩家状态
  153. */
  154. public static function updatePlayerStatus($playerId, $merchantId, $status)
  155. {
  156. return self::where('player_id', $playerId)
  157. ->where('merchant_id', $merchantId)
  158. ->update(['status' => $status]);
  159. }
  160. /**
  161. * 更新玩家调控状态
  162. */
  163. public static function updatePlayerAdjustStatus($playerId, $merchantId, $adjustStatus)
  164. {
  165. return self::where('player_id', $playerId)
  166. ->where('merchant_id', $merchantId)
  167. ->update(['adjust_status' => $adjustStatus]);
  168. }
  169. /**
  170. * 获取玩家统计信息
  171. */
  172. public static function getPlayerStatistics($merchantId)
  173. {
  174. $today = strtotime(date('Y-m-d'));
  175. return [
  176. 'total_players' => self::where('merchant_id', $merchantId)->count(),
  177. 'active_players' => self::where('merchant_id', $merchantId)
  178. ->where('status', self::STATUS_NORMAL)
  179. ->count(),
  180. 'today_new_players' => self::where('merchant_id', $merchantId)
  181. ->where('create_time', '>=', $today)
  182. ->count(),
  183. 'today_login_players' => self::where('merchant_id', $merchantId)
  184. ->where('login_time', '>=', $today)
  185. ->count(),
  186. 'total_balance' => self::where('merchant_id', $merchantId)->sum('balance'),
  187. 'today_bet_amount' => self::where('merchant_id', $merchantId)->sum('today_bet_amount'),
  188. 'today_win_amount' => self::where('merchant_id', $merchantId)->sum('today_win_amount'),
  189. 'history_win_amount' => self::where('merchant_id', $merchantId)->sum('history_win_amount'),
  190. ];
  191. }
  192. /**
  193. * 批量更新玩家状态
  194. */
  195. public static function batchUpdateStatus($playerIds, $merchantId, $status)
  196. {
  197. return self::where('merchant_id', $merchantId)
  198. ->whereIn('player_id', $playerIds)
  199. ->update(['status' => $status]);
  200. }
  201. /**
  202. * 批量更新玩家调控状态
  203. */
  204. public static function batchUpdateAdjustStatus($playerIds, $merchantId, $adjustStatus)
  205. {
  206. return self::where('merchant_id', $merchantId)
  207. ->whereIn('player_id', $playerIds)
  208. ->update(['adjust_status' => $adjustStatus]);
  209. }
  210. }