MerchantsUserModel.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 $wheres
  86. * @return mixed
  87. */
  88. public static function getPlayerListByIds($merchantId, $wheres = [], $field = [])
  89. {
  90. $query = self::where('app_id', $merchantId)->where($wheres);
  91. if(!empty($field)) {
  92. $query = $query->field($field);
  93. }
  94. return $query->select()->toArray();
  95. }
  96. /**
  97. * 根据商户ID获取玩家列表
  98. */
  99. public static function getPlayerListByMerchant($merchantId, $page = 1, $limit = 10, $filters = [])
  100. {
  101. $query = self::where('app_id', $merchantId);
  102. // 应用过滤条件
  103. if (!empty($filters['uname'])) {
  104. $query->where('uname', 'like', '%' . $filters['uname'] . '%');
  105. }
  106. if (!empty($filters['nickname'])) {
  107. $query->where('nickname', 'like', '%' . $filters['nickname'] . '%');
  108. }
  109. if (!empty($filters['player_id'])) {
  110. $query->where('user_id', $filters['player_id']);
  111. }
  112. if (isset($filters['status']) && $filters['status'] !== '') {
  113. $query->where('status', $filters['status']);
  114. }
  115. if (isset($filters['adjust_status']) && $filters['adjust_status'] !== '') {
  116. $query->where('adjust_status', $filters['adjust_status']);
  117. }
  118. if (!empty($filters['login_ip'])) {
  119. $query->where('login_ip', 'like', '%' . $filters['login_ip'] . '%');
  120. }
  121. if (!empty($filters['reg_ip'])) {
  122. $query->where('reg_ip', 'like', '%' . $filters['reg_ip'] . '%');
  123. }
  124. // 登录时间范围
  125. if (!empty($filters['login_time_start'])) {
  126. $query->where('login_time', '>=', strtotime($filters['login_time_start']));
  127. }
  128. if (!empty($filters['login_time_end'])) {
  129. $query->where('login_time', '<=', strtotime($filters['login_time_end']));
  130. }
  131. $order = $filters['order'] ?? 'user_id';
  132. $sort = $filters['sort'] ?? 'desc';
  133. $query->order($order, $sort);
  134. // 获取总数
  135. $total = $query->count();
  136. // 获取列表
  137. $list = $query->page($page, $limit)->select()->toArray();
  138. $user_ids = array_column($list, 'user_id');
  139. $userBalanceConfig = MerchantsUserBalanceModel::getUsersBalanceList($merchantId, $user_ids);
  140. // 转换字段名以保持与原有接口的兼容性
  141. foreach ($list as &$item) {
  142. $userBalanceInfo = $userBalanceConfig[$item['user_id']] ?? [];
  143. $item['merchant_id'] = $item['app_id'];
  144. $item['today_login_count'] = $item['today_count'];
  145. $item['history_login_count'] = $item['history_day_count'];
  146. // 添加原系统中可能需要但新表中没有的字段
  147. $item['balance'] = $userBalanceInfo['balance'] ?? 0;
  148. $item['today_win_amount'] = 0;
  149. $item['history_win_amount'] = 0;
  150. $item['today_bet_amount'] = 0;
  151. // 移除字段
  152. unset($item['app_id']);
  153. unset($item['token']);
  154. unset($item['short_token']);
  155. }
  156. return [
  157. 'list' => $list,
  158. 'total' => $total,
  159. 'page' => $page,
  160. 'limit' => $limit
  161. ];
  162. }
  163. /**
  164. * 获取玩家详情
  165. */
  166. public static function getPlayerDetail($playerId, $merchantId)
  167. {
  168. $player = self::where('user_id', $playerId)
  169. ->where('app_id', $merchantId)
  170. ->find();
  171. if ($player) {
  172. $player = $player->toArray();
  173. // 移除敏感字段
  174. unset($player['token']);
  175. unset($player['short_token']);
  176. // 转换字段名
  177. $player['merchant_id'] = $player['app_id'];
  178. $player['today_login_count'] = $player['today_count'];
  179. $player['history_login_count'] = $player['history_day_count'];
  180. // 添加原系统中可能需要但新表中没有的字段
  181. $player['balance'] = 0;
  182. $player['today_win_amount'] = 0;
  183. $player['history_win_amount'] = 0;
  184. $player['today_bet_amount'] = 0;
  185. }
  186. return $player;
  187. }
  188. /**
  189. * 更新玩家状态
  190. */
  191. public static function updatePlayerStatus($playerIds, $merchantId, $status)
  192. {
  193. return self::where('app_id', $merchantId)
  194. ->whereIn('user_id', $playerIds)
  195. ->update(['status' => $status]);
  196. }
  197. /**
  198. * 更新玩家调控状态
  199. */
  200. public static function updatePlayerAdjustStatus($playerIds, $merchantId, $adjustStatus)
  201. {
  202. return self::where('app_id', $merchantId)
  203. ->whereIn('user_id', $playerIds)
  204. ->update(['adjust_status' => $adjustStatus]);
  205. }
  206. /**
  207. * 更新玩家禁止登录状态
  208. */
  209. public static function updatePlayerLoginStatus($playerIds, $merchantId, $disabledLogin)
  210. {
  211. return self::where('app_id', $merchantId)
  212. ->whereIn('uname', $playerIds)
  213. ->update(['disabled_login' => $disabledLogin]);
  214. }
  215. /**
  216. * 获取玩家统计信息
  217. */
  218. public static function getPlayerStatistics($merchantId, $filters = [])
  219. {
  220. $where = [
  221. ['app_id', '=', $merchantId]
  222. ];
  223. $getWhere = function(string $timeField) use ($where, $filters) {
  224. // 时间筛选
  225. if (!empty($filters['start_time'])) {
  226. $startTime = strtotime($filters['start_time']);
  227. $where[] = [$timeField, '>=', $startTime];
  228. }
  229. if (!empty($filters['end_time'])) {
  230. $endTime = strtotime($filters['end_time']);
  231. $where[] = [$timeField, '<=', $endTime];
  232. }
  233. return $where;
  234. };
  235. // 获取注册用户数
  236. $registerUsers = self::where($getWhere('create_time'))->field([
  237. "FROM_UNIXTIME(create_time, '%Y-%m-%d') as date", // 日期
  238. 'COUNT(user_id) as register_users', // 注册用户数
  239. ])
  240. ->group('date')
  241. ->select()
  242. ->toArray();
  243. // 获取登录用户数
  244. $loginUsers = self::where($getWhere('login_time'))->field([
  245. "FROM_UNIXTIME(login_time, '%Y-%m-%d') as date", // 日期
  246. 'COUNT(user_id) as login_users', // 登录用户数
  247. ])
  248. ->group('date')
  249. ->select()
  250. ->toArray();
  251. $staticData = [];
  252. $loginUsers = array_column($loginUsers, null, 'login_users');
  253. foreach ($registerUsers as $key => $row) {
  254. $row['login_users'] = $loginUsers[$key]['login_users'] ?? 0;
  255. $staticData[$key] = $row;
  256. }
  257. return $staticData;
  258. }
  259. }