| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- declare (strict_types = 1);
- namespace app\model;
- use think\Model;
- use think\facade\Db;
- /**
- * 转账记录模型
- */
- class TransferLogModel extends Model
- {
- protected $name = 'merchants_user_balance_transfer_logs';
- protected $connection = 'fortue_tiger';
- protected $pk = 'id';
-
- // 状态常量
- const STATUS_FAILED = 0; // 失败
- const STATUS_SUCCESS = 1; // 成功
- const STATUS_PENDING = 2; // 待处理
-
- /**
- * 获取转账记录列表
- */
- public static function getTransferLogs($merchantId, $page = 1, $limit = 20, $filters = [])
- {
- $where = [
- ['app_id', '=', $merchantId]
- ];
-
- // 时间筛选
- if (!empty($filters['start_time'])) {
- $startTime = strtotime($filters['start_time'] . ' 00:00:00');
- $where[] = ['create_time', '>=', $startTime];
- }
-
- if (!empty($filters['end_time'])) {
- $endTime = strtotime($filters['end_time'] . ' 23:59:59');
- $where[] = ['create_time', '<=', $endTime];
- }
-
- // 平台ID筛选
- if (!empty($filters['platform_id'])) {
- $where[] = ['user_id', '=', $filters['platform_id']];
- }
-
- // 状态筛选
- if ($filters['status'] !== '') {
- $where[] = ['status', '=', $filters['status']];
- }
-
- // 转账流水号筛选
- if (!empty($filters['tx_id'])) {
- $where[] = ['tx_id', 'like', '%' . $filters['tx_id'] . '%'];
- }
-
- $query = self::where($where);
-
- // 统计总数
- $total = $query->count();
-
- // 获取列表数据
- $list = $query->field([
- 'id', 'user_id', 'uname', 'tx_id', 'ptx_id', 'amount',
- 'prev_amount', 'next_amount', 'status', 'ip',
- 'message', 'create_time'
- ])
- ->order('create_time', 'desc')
- ->page($page, $limit)
- ->select()
- ->toArray();
-
- // 获取用户信息
- $userIds = array_unique(array_column($list, 'user_id'));
- $userMap = [];
- if (!empty($userIds)) {
- // 从用户表获取用户信息
- $users = Db::connect('merchant_admin')
- ->table('merchant_user')
- ->whereIn('user_id', $userIds)
- ->field('user_id, nick_name')
- ->select();
-
- foreach ($users as $user) {
- $userMap[$user['user_id']] = $user['nick_name'];
- }
- }
-
- // 格式化数据
- foreach ($list as &$item) {
- // 格式化金额(分转元)
- $item['amount'] = bcdiv($item['amount'], "100", 2);
- $item['prev_amount'] = bcdiv($item['prev_amount'], "100", 2);
- $item['next_amount'] = bcdiv($item['next_amount'], "100", 2);
-
- // 状态文本
- $item['status_text'] = self::getStatusText($item['status']);
-
- // 商户名称
- $item['merchant_name'] = $userMap[$item['user_id']] ?? '';
-
- // 平台昵称(从uname获取)
- $item['platform_nickname'] = '玩家' . $item['uname'];
- }
-
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit
- ];
- }
-
- /**
- * 获取转账记录详情
- */
- public static function getTransferDetail($id, $appId)
- {
- $transfer = self::where('id', $id)
- ->where('app_id', $appId)
- ->find();
-
- if (!$transfer) {
- return null;
- }
-
- $transfer = $transfer->toArray();
-
- // 格式化数据
- $transfer['create_time_text'] = date('Y-m-d H:i:s', $transfer['create_time']);
-
- // 格式化金额
- $transfer['amount_yuan'] = round($transfer['amount'] / 100, 2);
- $transfer['prev_amount_yuan'] = round($transfer['prev_amount'] / 100, 2);
- $transfer['next_amount_yuan'] = round($transfer['next_amount'] / 100, 2);
-
- // 状态文本
- $transfer['status_text'] = self::getStatusText($transfer['status']);
-
- // 获取用户信息
- $user = Db::connect('merchant_admin')
- ->table('merchant_user')
- ->where('user_id', $transfer['user_id'])
- ->field('user_id, nick_name')
- ->find();
-
- $transfer['merchant_name'] = $user ? $user['nick_name'] : '';
- $transfer['platform_nickname'] = '玩家' . $transfer['uname'];
-
- return $transfer;
- }
-
- /**
- * 获取转账统计信息
- */
- public static function getTransferStatistics($appId, $filters = [])
- {
- $where = [
- ['app_id', '=', $appId]
- ];
-
- // 时间筛选
- if (!empty($filters['start_time'])) {
- $startTime = strtotime($filters['start_time'] . ' 00:00:00');
- $where[] = ['create_time', '>=', $startTime];
- }
-
- if (!empty($filters['end_time'])) {
- $endTime = strtotime($filters['end_time'] . ' 23:59:59');
- $where[] = ['create_time', '<=', $endTime];
- }
-
- $statistics = self::where($where)
- ->field([
- 'COUNT(*) as total_transfers',
- 'COUNT(DISTINCT uname) as total_players',
- 'SUM(CASE WHEN amount > 0 THEN amount ELSE 0 END) as total_in_amount',
- 'SUM(CASE WHEN amount < 0 THEN ABS(amount) ELSE 0 END) as total_out_amount',
- 'SUM(amount) as net_amount',
- 'SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as success_transfers',
- 'SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) as failed_transfers',
- 'SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as pending_transfers'
- ])
- ->find()
- ->toArray();
-
- // 格式化金额
- $statistics['total_in_amount_yuan'] = round($statistics['total_in_amount'] / 100, 2);
- $statistics['total_out_amount_yuan'] = round($statistics['total_out_amount'] / 100, 2);
- $statistics['net_amount_yuan'] = round($statistics['net_amount'] / 100, 2);
-
- // 计算成功率
- if ($statistics['total_transfers'] > 0) {
- $statistics['success_rate'] = round($statistics['success_transfers'] / $statistics['total_transfers'] * 100, 2);
- } else {
- $statistics['success_rate'] = 0;
- }
-
- return $statistics;
- }
-
- /**
- * 获取状态文本
- */
- public static function getStatusText($status)
- {
- $statusMap = [
- self::STATUS_FAILED => '失败',
- self::STATUS_SUCCESS => '成功',
- self::STATUS_PENDING => '待处理'
- ];
-
- return $statusMap[$status] ?? '未知';
- }
- }
|