This commit is contained in:
2025-09-17 14:01:10 +08:00
parent 5576032ad2
commit 31913dceed
3 changed files with 73 additions and 40 deletions

View File

@@ -12,7 +12,7 @@ const dayjs = require('dayjs');
router.get('/stats', agentAuth, async (req, res) => {
try {
const {id: agentId, userId} = req.agent;
console.log(req.agent)
// 获取下级用户统计
const [userStats] = await getDB().execute(`
@@ -125,16 +125,19 @@ router.get('/stats_agent_directly', agentAuth, async (req, res) => {
FROM users
WHERE inviter = ?
AND user_type = 'directly_operated'
`, [userId]);
`, [userId])
//直营商户人数
let active_users = 0
let operated_commission_sql = []
for (const id of userIds) {
const [userTotal] = await getDB().execute(`
SELECT COUNT(*) as total_users
FROM users
WHERE inviter = ?
`, [id])
`, [id.id])
active_users += userTotal[0].total_users
operated_commission_sql.push(`to_user_id=${id.id}`)
}
userStats[0].active_users = active_users;
userStats[0].total_operated = userIds.length;
@@ -148,7 +151,7 @@ router.get('/stats_agent_directly', agentAuth, async (req, res) => {
SELECT COUNT(*) as total_users
FROM users
WHERE inviter = ?
`, [id])//?????????????????????
`, [id])
total_share_users += userTotal[0].total_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'
`, [userId])
let total_operated_share_users = 0
let operated_user_ids = []
let operated_user_sql = []
for (const id of operated_share_users) {
const [userTotal] = await getDB().execute(`
SELECT COUNT(*) as total_users
FROM users
WHERE inviter = ?
`, [id])//?????????????????????
`, [id])
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;
@@ -179,42 +186,48 @@ router.get('/stats_agent_directly', agentAuth, async (req, res) => {
WHERE to_user_id = ?
AND transfer_type = 'user_to_agent'
`, [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(`
// `)
commissionStats[0].directly_agents_income = 1
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 = directly_agents_income[0].amount || 0
// 获取直销商户分享利润
// let [active_users_income] = await getDB().execute(`
// `)
commissionStats[0].active_users_income = 2
let [active_users_income] = await getDB().execute(`
SELECT SUM(amount) as amount
FROM transfers
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 = 3
commissionStats[0].operated_commission = operated_commission[0].amount || 0
// 获取已提现佣金
// let [get_commission] = await getDB().execute(`
// `)
commissionStats[0].get_commission = 4
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 = get_commission[0].amount || 0
// 获取待提现佣金
// let [loading_commission] = await getDB().execute(`
// `)
commissionStats[0].loading_commission = 5
let [loading_commission] = await getDB().execute(`
SELECT SUM(balance) as balance FROM users WHERE inviter=${userId}
`)
commissionStats[0].loading_commission = Math.abs(loading_commission[0].balance) || 0
// 获取总收入
// let [total_commission] = await getDB().execute(`
// `)
commissionStats[0].total_commission = 12345
let [total_commission] = await getDB().execute(`
SELECT SUM(balance) as balance FROM users WHERE id=${userId}
`)
commissionStats[0].total_commission =Math.abs(total_commission[0].balance) || 0
// 获取转账统计
const [transferStats] = await getDB().execute(`
@@ -255,12 +268,6 @@ router.get('/stats_agent_directly', agentAuth, async (req, res) => {
});
} catch (error) {
logger.error('获取代理统计数据失败', {
error: error.message,
stack: error.stack,
agentId: req.agent?.id
});
res.status(500).json({
success: false,
message: '获取统计数据失败'