MerchantsUserModel.php 8.9 KB

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