提交
This commit is contained in:
@@ -12,7 +12,7 @@ const dayjs = require('dayjs');
|
|||||||
router.get('/stats', agentAuth, async (req, res) => {
|
router.get('/stats', agentAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const {id: agentId, userId} = req.agent;
|
const {id: agentId, userId} = req.agent;
|
||||||
console.log(req.agent)
|
|
||||||
|
|
||||||
// 获取下级用户统计
|
// 获取下级用户统计
|
||||||
const [userStats] = await getDB().execute(`
|
const [userStats] = await getDB().execute(`
|
||||||
@@ -125,16 +125,19 @@ router.get('/stats_agent_directly', agentAuth, async (req, res) => {
|
|||||||
FROM users
|
FROM users
|
||||||
WHERE inviter = ?
|
WHERE inviter = ?
|
||||||
AND user_type = 'directly_operated'
|
AND user_type = 'directly_operated'
|
||||||
`, [userId]);
|
`, [userId])
|
||||||
|
|
||||||
//直营商户人数
|
//直营商户人数
|
||||||
let active_users = 0
|
let active_users = 0
|
||||||
|
let operated_commission_sql = []
|
||||||
for (const id of userIds) {
|
for (const id of userIds) {
|
||||||
const [userTotal] = await getDB().execute(`
|
const [userTotal] = await getDB().execute(`
|
||||||
SELECT COUNT(*) as total_users
|
SELECT COUNT(*) as total_users
|
||||||
FROM users
|
FROM users
|
||||||
WHERE inviter = ?
|
WHERE inviter = ?
|
||||||
`, [id])
|
`, [id.id])
|
||||||
active_users += userTotal[0].total_users
|
active_users += userTotal[0].total_users
|
||||||
|
operated_commission_sql.push(`to_user_id=${id.id}`)
|
||||||
}
|
}
|
||||||
userStats[0].active_users = active_users;
|
userStats[0].active_users = active_users;
|
||||||
userStats[0].total_operated = userIds.length;
|
userStats[0].total_operated = userIds.length;
|
||||||
@@ -148,7 +151,7 @@ router.get('/stats_agent_directly', agentAuth, async (req, res) => {
|
|||||||
SELECT COUNT(*) as total_users
|
SELECT COUNT(*) as total_users
|
||||||
FROM users
|
FROM users
|
||||||
WHERE inviter = ?
|
WHERE inviter = ?
|
||||||
`, [id])//?????????????????????
|
`, [id])
|
||||||
total_share_users += userTotal[0].total_users
|
total_share_users += userTotal[0].total_users
|
||||||
}
|
}
|
||||||
userStats[0].agent_share_users = total_share_users;
|
userStats[0].agent_share_users = total_share_users;
|
||||||
@@ -157,13 +160,17 @@ router.get('/stats_agent_directly', agentAuth, async (req, res) => {
|
|||||||
SELECT id FROM users WHERE inviter=? AND user_type='directly_operated'
|
SELECT id FROM users WHERE inviter=? AND user_type='directly_operated'
|
||||||
`, [userId])
|
`, [userId])
|
||||||
let total_operated_share_users = 0
|
let total_operated_share_users = 0
|
||||||
|
let operated_user_ids = []
|
||||||
|
let operated_user_sql = []
|
||||||
for (const id of operated_share_users) {
|
for (const id of operated_share_users) {
|
||||||
const [userTotal] = await getDB().execute(`
|
const [userTotal] = await getDB().execute(`
|
||||||
SELECT COUNT(*) as total_users
|
SELECT COUNT(*) as total_users
|
||||||
FROM users
|
FROM users
|
||||||
WHERE inviter = ?
|
WHERE inviter = ?
|
||||||
`, [id])//?????????????????????
|
`, [id])
|
||||||
total_operated_share_users += userTotal[0].total_users
|
total_operated_share_users += userTotal[0].total_users
|
||||||
|
operated_user_sql.push(`to_user_id=?`)
|
||||||
|
operated_user_ids.push(userTotal[0].total_users)
|
||||||
}
|
}
|
||||||
userStats[0].operated_share_users = total_operated_share_users;
|
userStats[0].operated_share_users = total_operated_share_users;
|
||||||
|
|
||||||
@@ -179,42 +186,48 @@ router.get('/stats_agent_directly', agentAuth, async (req, res) => {
|
|||||||
WHERE to_user_id = ?
|
WHERE to_user_id = ?
|
||||||
AND transfer_type = 'user_to_agent'
|
AND transfer_type = 'user_to_agent'
|
||||||
`, [userId]);
|
`, [userId]);
|
||||||
|
// 获取直营佣金
|
||||||
|
let [operated_commission] = await getDB().execute(`
|
||||||
|
SELECT SUM(amount) as amount FROM transfers WHERE ${operated_commission_sql.join(' or ')}
|
||||||
|
`)
|
||||||
|
|
||||||
// 获取直销商户利润
|
// 获取直销商户利润
|
||||||
// let [directly_agents_income] = await getDB().execute(`
|
let [directly_agents_income] = await getDB().execute(`
|
||||||
|
SELECT SUM(amount) as amount FROM transfers WHERE to_user_id=? AND transfer_type='user_to_agent' AND source_type='agent_operated'
|
||||||
// `)
|
`,[userId])
|
||||||
commissionStats[0].directly_agents_income = 1
|
|
||||||
|
commissionStats[0].directly_agents_income = directly_agents_income[0].amount || 0
|
||||||
|
|
||||||
// 获取直销商户分享利润
|
// 获取直销商户分享利润
|
||||||
// let [active_users_income] = await getDB().execute(`
|
let [active_users_income] = await getDB().execute(`
|
||||||
|
SELECT SUM(amount) as amount
|
||||||
// `)
|
FROM transfers
|
||||||
commissionStats[0].active_users_income = 2
|
WHERE (${operated_user_sql.join(' or ')})
|
||||||
|
AND transfer_type = 'user_to_agent'
|
||||||
|
AND source_type = 'agent_operated'
|
||||||
|
`,[...operated_user_ids])
|
||||||
|
commissionStats[0].active_users_income = active_users_income[0].amount || 0
|
||||||
|
|
||||||
// 获取直营佣金
|
|
||||||
// let [operated_commission] = await getDB().execute(`
|
commissionStats[0].operated_commission = operated_commission[0].amount || 0
|
||||||
|
|
||||||
// `)
|
|
||||||
commissionStats[0].operated_commission = 3
|
|
||||||
|
|
||||||
// 获取已提现佣金
|
// 获取已提现佣金
|
||||||
// let [get_commission] = await getDB().execute(`
|
let [get_commission] = await getDB().execute(`
|
||||||
|
SELECT SUM(amount) as amount FROM transfers WHERE from_user_id=${userId} AND source_type='withdraw' AND created_at >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)
|
||||||
// `)
|
`)
|
||||||
commissionStats[0].get_commission = 4
|
commissionStats[0].get_commission = get_commission[0].amount || 0
|
||||||
|
|
||||||
// 获取待提现佣金
|
// 获取待提现佣金
|
||||||
// let [loading_commission] = await getDB().execute(`
|
let [loading_commission] = await getDB().execute(`
|
||||||
|
SELECT SUM(balance) as balance FROM users WHERE inviter=${userId}
|
||||||
// `)
|
`)
|
||||||
commissionStats[0].loading_commission = 5
|
commissionStats[0].loading_commission = Math.abs(loading_commission[0].balance) || 0
|
||||||
|
|
||||||
// 获取总收入
|
// 获取总收入
|
||||||
// let [total_commission] = await getDB().execute(`
|
let [total_commission] = await getDB().execute(`
|
||||||
|
SELECT SUM(balance) as balance FROM users WHERE id=${userId}
|
||||||
// `)
|
`)
|
||||||
commissionStats[0].total_commission = 12345
|
commissionStats[0].total_commission =Math.abs(total_commission[0].balance) || 0
|
||||||
|
|
||||||
// 获取转账统计
|
// 获取转账统计
|
||||||
const [transferStats] = await getDB().execute(`
|
const [transferStats] = await getDB().execute(`
|
||||||
@@ -255,12 +268,6 @@ router.get('/stats_agent_directly', agentAuth, async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('获取代理统计数据失败', {
|
|
||||||
error: error.message,
|
|
||||||
stack: error.stack,
|
|
||||||
agentId: req.agent?.id
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: '获取统计数据失败'
|
message: '获取统计数据失败'
|
||||||
|
|||||||
@@ -134,13 +134,13 @@ router.get('/', agentAuth, async (req, res) => {
|
|||||||
let statsResult;
|
let statsResult;
|
||||||
[statsResult] = await getDB().execute(`
|
[statsResult] = await getDB().execute(`
|
||||||
SELECT COUNT(*) as total_transfers,
|
SELECT COUNT(*) as total_transfers,
|
||||||
COUNT(CASE WHEN t.status = 'completed' THEN 1 END) as completed_transfers,
|
COUNT(CASE WHEN t.status = 'received' THEN 1 END) as completed_transfers,
|
||||||
COUNT(CASE WHEN t.status = 'pending' THEN 1 END) as pending_transfers,
|
COUNT(CASE WHEN t.status = 'pending' THEN 1 END) as pending_transfers,
|
||||||
COUNT(CASE WHEN t.status = 'failed' THEN 1 END) as failed_transfers,
|
COUNT(CASE WHEN t.status = 'failed' THEN 1 END) as failed_transfers,
|
||||||
CAST(COALESCE(SUM(CASE WHEN t.status = 'completed' THEN t.amount ELSE 0 END),
|
CAST(COALESCE(SUM(CASE WHEN t.status = 'received' THEN t.amount ELSE 0 END),
|
||||||
0) AS DECIMAL(10, 2)) as total_amount,
|
0) AS DECIMAL(10, 2)) as total_amount,
|
||||||
CAST(COALESCE(
|
CAST(COALESCE(
|
||||||
SUM(CASE WHEN t.status = 'completed' AND DATE (t.created_at) = CURDATE() THEN t.amount ELSE 0
|
SUM(CASE WHEN t.status = 'received' AND DATE (t.created_at) = CURDATE() THEN t.amount ELSE 0
|
||||||
END), 0) AS DECIMAL(10, 2)) as today_amount,
|
END), 0) AS DECIMAL(10, 2)) as today_amount,
|
||||||
COUNT(CASE WHEN DATE (t.created_at) = CURDATE() THEN 1 END) as today_transfers
|
COUNT(CASE WHEN DATE (t.created_at) = CURDATE() THEN 1 END) as today_transfers
|
||||||
FROM transfers t
|
FROM transfers t
|
||||||
|
|||||||
@@ -208,10 +208,36 @@ router.get('/directly_operated', agentAuth, async (req, res) => {
|
|||||||
const totalPages = Math.ceil(total / pageSize);
|
const totalPages = Math.ceil(total / pageSize);
|
||||||
const hasNextPage = pageNum < totalPages;
|
const hasNextPage = pageNum < totalPages;
|
||||||
const hasPrevPage = pageNum > 1;
|
const hasPrevPage = pageNum > 1;
|
||||||
|
const all_total = {
|
||||||
|
balance_total : 0
|
||||||
|
}
|
||||||
|
//直营用户融豆数量
|
||||||
|
const balance_total = await getDB().execute(`
|
||||||
|
SELECT SUM(balance) as balance FROM users u
|
||||||
|
WHERE inviter=${agentId}
|
||||||
|
`)
|
||||||
|
all_total.balance_total = balance_total[0].balance || 0;
|
||||||
|
//今日提现
|
||||||
|
const withdraw_total = await getDB().execute(`
|
||||||
|
SELECT SUM(amount) as amount
|
||||||
|
FROM transfers
|
||||||
|
WHERE source_type = 'withdraw'
|
||||||
|
AND from_user_id = ${agentId}
|
||||||
|
AND created_at >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)
|
||||||
|
`)
|
||||||
|
all_total.withdraw_total = withdraw_total[0].amount || 0
|
||||||
|
//总提现次数
|
||||||
|
const withdraw_num_total = await getDB().execute(`
|
||||||
|
SELECT COUNT(*) as total
|
||||||
|
FROM transfers
|
||||||
|
WHERE source_type = 'withdraw'
|
||||||
|
AND from_user_id = ${agentId}
|
||||||
|
`)
|
||||||
|
all_total.withdraw_num_total = withdraw_num_total[0].total || 0
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
|
all_total,
|
||||||
users,
|
users,
|
||||||
pagination: {
|
pagination: {
|
||||||
current_page: pageNum,
|
current_page: pageNum,
|
||||||
|
|||||||
Reference in New Issue
Block a user