MerchantsUserModel.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // 转换字段名以保持与原有接口的兼容性
  139. foreach ($list as &$item) {
  140. $item['merchant_id'] = $item['app_id'];
  141. $item['today_login_count'] = $item['today_count'];
  142. $item['history_login_count'] = $item['history_day_count'];
  143. // 添加原系统中可能需要但新表中没有的字段
  144. $item['balance'] = 0;
  145. $item['today_win_amount'] = 0;
  146. $item['history_win_amount'] = 0;
  147. $item['today_bet_amount'] = 0;
  148. // 移除字段
  149. unset($item['app_id']);
  150. unset($item['token']);
  151. unset($item['short_token']);
  152. }
  153. return [
  154. 'list' => $list,
  155. 'total' => $total,
  156. 'page' => $page,
  157. 'limit' => $limit
  158. ];
  159. }
  160. /**
  161. * 获取玩家详情
  162. */
  163. public static function getPlayerDetail($playerId, $merchantId)
  164. {
  165. $player = self::where('user_id', $playerId)
  166. ->where('app_id', $merchantId)
  167. ->find();
  168. if ($player) {
  169. $player = $player->toArray();
  170. // 移除敏感字段
  171. unset($player['token']);
  172. unset($player['short_token']);
  173. // 转换字段名
  174. $player['merchant_id'] = $player['app_id'];
  175. $player['today_login_count'] = $player['today_count'];
  176. $player['history_login_count'] = $player['history_day_count'];
  177. // 添加原系统中可能需要但新表中没有的字段
  178. $player['balance'] = 0;
  179. $player['today_win_amount'] = 0;
  180. $player['history_win_amount'] = 0;
  181. $player['today_bet_amount'] = 0;
  182. }
  183. return $player;
  184. }
  185. /**
  186. * 更新玩家状态
  187. */
  188. public static function updatePlayerStatus($playerIds, $merchantId, $status)
  189. {
  190. return self::where('app_id', $merchantId)
  191. ->whereIn('user_id', $playerIds)
  192. ->update(['status' => $status]);
  193. }
  194. /**
  195. * 更新玩家调控状态
  196. */
  197. public static function updatePlayerAdjustStatus($playerIds, $merchantId, $adjustStatus)
  198. {
  199. return self::where('app_id', $merchantId)
  200. ->whereIn('user_id', $playerIds)
  201. ->update(['adjust_status' => $adjustStatus]);
  202. }
  203. /**
  204. * 更新玩家禁止登录状态
  205. */
  206. public static function updatePlayerLoginStatus($playerIds, $merchantId, $disabledLogin)
  207. {
  208. return self::where('app_id', $merchantId)
  209. ->whereIn('uname', $playerIds)
  210. ->update(['disabled_login' => $disabledLogin]);
  211. }
  212. /**
  213. * 获取玩家统计信息
  214. */
  215. public static function getPlayerStatistics($merchantId, $filters = [])
  216. {
  217. $where = [
  218. ['app_id', '=', $merchantId]
  219. ];
  220. $getWhere = function(string $timeField) use ($where, $filters) {
  221. // 时间筛选
  222. if (!empty($filters['start_time'])) {
  223. $startTime = strtotime($filters['start_time']);
  224. $where[] = [$timeField, '>=', $startTime];
  225. }
  226. if (!empty($filters['end_time'])) {
  227. $endTime = strtotime($filters['end_time']);
  228. $where[] = [$timeField, '<=', $endTime];
  229. }
  230. return $where;
  231. };
  232. // 获取注册用户数
  233. $registerUsers = self::where($getWhere('create_time'))->field([
  234. "FROM_UNIXTIME(create_time, '%Y-%m-%d') as date", // 日期
  235. 'COUNT(user_id) as register_users', // 注册用户数
  236. ])
  237. ->group('date')
  238. ->select()
  239. ->toArray();
  240. // 获取登录用户数
  241. $loginUsers = self::where($getWhere('login_time'))->field([
  242. "FROM_UNIXTIME(login_time, '%Y-%m-%d') as date", // 日期
  243. 'COUNT(user_id) as login_users', // 登录用户数
  244. ])
  245. ->group('date')
  246. ->select()
  247. ->toArray();
  248. $staticData = [];
  249. $loginUsers = array_column($loginUsers, null, 'login_users');
  250. foreach ($registerUsers as $key => $row) {
  251. $row['login_users'] = $loginUsers[$key]['login_users'] ?? 0;
  252. $staticData[$key] = $row;
  253. }
  254. return $staticData;
  255. }
  256. }