118 lines
4.4 KiB
PHP
118 lines
4.4 KiB
PHP
<?php
|
||
|
||
namespace app\api\model;
|
||
|
||
use app\common\library\DatabaseRoute;
|
||
use ba\Random;
|
||
use think\facade\Db;
|
||
use think\Model;
|
||
|
||
class Cash extends Model
|
||
{
|
||
|
||
public static function selectPayDetails($cashOut, $get, $isApp, $user)
|
||
{
|
||
$db_name_cash_out = Db::connect(DatabaseRoute::getConnection('cash_out', ['user_id' => $cashOut['user_id']]))->name('cash_out');
|
||
// 构建查询条件
|
||
if ($isApp) {
|
||
// APP端:只查询用户自己的提现记录(用户类型为1)
|
||
$query = $db_name_cash_out->where('user_id', $cashOut['user_id'])
|
||
->where('user_type', 1);
|
||
} else {
|
||
// 管理端:根据用户ID或系统用户ID查询
|
||
if (!empty($cashOut['user_id'])) {
|
||
$query = $db_name_cash_out->where('user_id', $cashOut['user_id']);
|
||
} else {
|
||
if (empty($cashOut['sys_user_id'])) {
|
||
// 无有效用户ID,返回空结果
|
||
return returnErrorData('无效用户');
|
||
} else {
|
||
$query = $db_name_cash_out->where('user_id', $cashOut['sys_user_id'])
|
||
->where('user_type', 2);
|
||
}
|
||
}
|
||
}
|
||
$count = $query->count();
|
||
// 执行分页查询
|
||
$cashOutList = $query->limit(page($get['page'], $get['limit']), $get['limit'])->order('create_at', 'desc')->select()->toArray();
|
||
if (!$isApp) {
|
||
// 管理端:补充用户信息和统计数据
|
||
$userIdList = array_column($cashOutList, 'user_id');
|
||
if (!empty($userIdList)) {
|
||
// 查询用户提现总数和总金额
|
||
$cashoutSumMap = self::selectSumByUserIdList($userIdList, 1);
|
||
$cashoutVerifySumMap = self::selectSumByUserIdList($userIdList, 3);
|
||
// 查询用户名称
|
||
$userinfoMap = $user;
|
||
|
||
// 合并数据到结果集
|
||
foreach ($cashOutList as &$item) {
|
||
$userId = $item['user_id'];
|
||
// 设置用户名
|
||
$item['userName'] = $userinfoMap[$userId] ?? '';
|
||
// 设置提现统计
|
||
if (isset($cashoutSumMap[$userId])) {
|
||
$item['count'] = $cashoutSumMap[$userId]['count'];
|
||
$item['total'] = $cashoutSumMap[$userId]['total'];
|
||
}
|
||
// 设置审核通过的提现统计
|
||
if (isset($cashoutVerifySumMap[$userId])) {
|
||
$item['verifyCount'] = $cashoutVerifySumMap[$userId]['count'];
|
||
$item['verifyTotal'] = $cashoutVerifySumMap[$userId]['total'];
|
||
}
|
||
}
|
||
unset($item);
|
||
}
|
||
}
|
||
|
||
if ($isApp) {
|
||
// APP端:对敏感信息进行脱敏处理
|
||
foreach ($cashOutList as &$item) {
|
||
if (!empty($item['bank_name'])) {
|
||
// 银行卡号脱敏
|
||
$item['zhifubao'] = bankCard($item['zhifubao']);
|
||
} elseif (filter_var($item['zhifubao'], FILTER_VALIDATE_EMAIL)) {
|
||
// 邮箱脱敏
|
||
$item['zhifubao'] = email($item['zhifubao']);
|
||
} elseif (preg_match('/^1[3-9]\d{9}$/', $item['zhifubao'])) {
|
||
// 手机号脱敏
|
||
$item['zhifubao'] = maskPhoneNumber($item['zhifubao']);
|
||
}
|
||
}
|
||
unset($item);
|
||
}
|
||
|
||
return returnSuccessData([
|
||
'currPage' => $get['page'],
|
||
'pageSize' => $get['limit'],
|
||
'list' => convertToCamelCase($cashOutList),
|
||
'totalCount' => $count,
|
||
'totalPage' => ceil($count / $get['limit']),
|
||
]);
|
||
}
|
||
|
||
|
||
public static function selectSumByUserIdList($userIdList, $state)
|
||
{
|
||
|
||
$result = DatabaseRoute::getAllDbData('cash_out', function($query) use ($userIdList, $state) {
|
||
return $query
|
||
->field([
|
||
'user_id',
|
||
'ROUND(SUM(money), 2) AS total',
|
||
'COUNT(*) AS count'
|
||
])
|
||
->where('state', $state)
|
||
->whereIn('user_id', $userIdList)
|
||
->group('user_id');
|
||
})->select()->toArray();
|
||
$resultMap = [];
|
||
foreach ($result as $item) {
|
||
$resultMap[$item['user_id']] = $item;
|
||
}
|
||
|
||
return $resultMap;
|
||
}
|
||
|
||
|
||
} |