TransferLogModel.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. use think\facade\Db;
  6. /**
  7. * 转账记录模型
  8. */
  9. class TransferLogModel extends Model
  10. {
  11. protected $name = 'merchants_user_balance_transfer_logs';
  12. protected $connection = 'fortue_tiger';
  13. protected $pk = 'id';
  14. // 状态常量
  15. const STATUS_FAILED = 0; // 失败
  16. const STATUS_SUCCESS = 1; // 成功
  17. const STATUS_PENDING = 2; // 待处理
  18. /**
  19. * 获取转账记录列表
  20. */
  21. public static function getTransferLogs($merchantId, $page = 1, $limit = 20, $filters = [])
  22. {
  23. $where = [
  24. ['app_id', '=', $merchantId]
  25. ];
  26. // 时间筛选
  27. if (!empty($filters['start_time'])) {
  28. $startTime = strtotime($filters['start_time'] . ' 00:00:00');
  29. $where[] = ['create_time', '>=', $startTime];
  30. }
  31. if (!empty($filters['end_time'])) {
  32. $endTime = strtotime($filters['end_time'] . ' 23:59:59');
  33. $where[] = ['create_time', '<=', $endTime];
  34. }
  35. // 平台ID筛选
  36. if (!empty($filters['platform_id'])) {
  37. $where[] = ['user_id', '=', $filters['platform_id']];
  38. }
  39. // 状态筛选
  40. if ($filters['status'] !== '') {
  41. $where[] = ['status', '=', $filters['status']];
  42. }
  43. // 转账流水号筛选
  44. if (!empty($filters['tx_id'])) {
  45. $where[] = ['tx_id', 'like', '%' . $filters['tx_id'] . '%'];
  46. }
  47. $query = self::where($where);
  48. // 统计总数
  49. $total = $query->count();
  50. // 获取列表数据
  51. $list = $query->field([
  52. 'id', 'user_id', 'uname', 'tx_id', 'ptx_id', 'amount',
  53. 'prev_amount', 'next_amount', 'status', 'ip',
  54. 'message', 'create_time'
  55. ])
  56. ->order('create_time', 'desc')
  57. ->page($page, $limit)
  58. ->select()
  59. ->toArray();
  60. // 获取用户信息
  61. $userIds = array_unique(array_column($list, 'user_id'));
  62. $userMap = [];
  63. if (!empty($userIds)) {
  64. // 从用户表获取用户信息
  65. $users = Db::connect('merchant_admin')
  66. ->table('merchant_user')
  67. ->whereIn('user_id', $userIds)
  68. ->field('user_id, nick_name')
  69. ->select();
  70. foreach ($users as $user) {
  71. $userMap[$user['user_id']] = $user['nick_name'];
  72. }
  73. }
  74. // 格式化数据
  75. foreach ($list as &$item) {
  76. // 格式化金额(分转元)
  77. $item['amount'] = bcdiv($item['amount'], "100", 2);
  78. $item['prev_amount'] = bcdiv($item['prev_amount'], "100", 2);
  79. $item['next_amount'] = bcdiv($item['next_amount'], "100", 2);
  80. // 状态文本
  81. $item['status_text'] = self::getStatusText($item['status']);
  82. // 商户名称
  83. $item['merchant_name'] = $userMap[$item['user_id']] ?? '';
  84. // 平台昵称(从uname获取)
  85. $item['platform_nickname'] = '玩家' . $item['uname'];
  86. }
  87. return [
  88. 'list' => $list,
  89. 'total' => $total,
  90. 'page' => $page,
  91. 'limit' => $limit
  92. ];
  93. }
  94. /**
  95. * 获取转账记录详情
  96. */
  97. public static function getTransferDetail($id, $appId)
  98. {
  99. $transfer = self::where('id', $id)
  100. ->where('app_id', $appId)
  101. ->find();
  102. if (!$transfer) {
  103. return null;
  104. }
  105. $transfer = $transfer->toArray();
  106. // 格式化数据
  107. $transfer['create_time_text'] = date('Y-m-d H:i:s', $transfer['create_time']);
  108. // 格式化金额
  109. $transfer['amount_yuan'] = round($transfer['amount'] / 100, 2);
  110. $transfer['prev_amount_yuan'] = round($transfer['prev_amount'] / 100, 2);
  111. $transfer['next_amount_yuan'] = round($transfer['next_amount'] / 100, 2);
  112. // 状态文本
  113. $transfer['status_text'] = self::getStatusText($transfer['status']);
  114. // 获取用户信息
  115. $user = Db::connect('merchant_admin')
  116. ->table('merchant_user')
  117. ->where('user_id', $transfer['user_id'])
  118. ->field('user_id, nick_name')
  119. ->find();
  120. $transfer['merchant_name'] = $user ? $user['nick_name'] : '';
  121. $transfer['platform_nickname'] = '玩家' . $transfer['uname'];
  122. return $transfer;
  123. }
  124. /**
  125. * 获取转账统计信息
  126. */
  127. public static function getTransferStatistics($appId, $filters = [])
  128. {
  129. $where = [
  130. ['app_id', '=', $appId]
  131. ];
  132. // 时间筛选
  133. if (!empty($filters['start_time'])) {
  134. $startTime = strtotime($filters['start_time'] . ' 00:00:00');
  135. $where[] = ['create_time', '>=', $startTime];
  136. }
  137. if (!empty($filters['end_time'])) {
  138. $endTime = strtotime($filters['end_time'] . ' 23:59:59');
  139. $where[] = ['create_time', '<=', $endTime];
  140. }
  141. $statistics = self::where($where)
  142. ->field([
  143. 'COUNT(*) as total_transfers',
  144. 'COUNT(DISTINCT uname) as total_players',
  145. 'SUM(CASE WHEN amount > 0 THEN amount ELSE 0 END) as total_in_amount',
  146. 'SUM(CASE WHEN amount < 0 THEN ABS(amount) ELSE 0 END) as total_out_amount',
  147. 'SUM(amount) as net_amount',
  148. 'SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as success_transfers',
  149. 'SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) as failed_transfers',
  150. 'SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as pending_transfers'
  151. ])
  152. ->find()
  153. ->toArray();
  154. // 格式化金额
  155. $statistics['total_in_amount_yuan'] = round($statistics['total_in_amount'] / 100, 2);
  156. $statistics['total_out_amount_yuan'] = round($statistics['total_out_amount'] / 100, 2);
  157. $statistics['net_amount_yuan'] = round($statistics['net_amount'] / 100, 2);
  158. // 计算成功率
  159. if ($statistics['total_transfers'] > 0) {
  160. $statistics['success_rate'] = round($statistics['success_transfers'] / $statistics['total_transfers'] * 100, 2);
  161. } else {
  162. $statistics['success_rate'] = 0;
  163. }
  164. return $statistics;
  165. }
  166. /**
  167. * 获取状态文本
  168. */
  169. public static function getStatusText($status)
  170. {
  171. $statusMap = [
  172. self::STATUS_FAILED => '失败',
  173. self::STATUS_SUCCESS => '成功',
  174. self::STATUS_PENDING => '待处理'
  175. ];
  176. return $statusMap[$status] ?? '未知';
  177. }
  178. }