提交
This commit is contained in:
164
routes/users.js
164
routes/users.js
@@ -215,7 +215,7 @@ router.get('/', auth, adminAuth, async (req, res) => {
|
||||
|
||||
// 确保参数为有效数字
|
||||
const pageNum = Math.max(1, parseInt(page) || 1);
|
||||
const limitNum = Math.max(1, Math.min(100, parseInt(limit) || 10));
|
||||
const limitNum = Math.max(1, parseInt(limit) || 10);
|
||||
const offset = Math.max(0, (pageNum - 1) * limitNum);
|
||||
|
||||
let whereConditions = [];
|
||||
@@ -504,7 +504,10 @@ router.get('/stats', auth, async (req, res) => {
|
||||
|
||||
// 活跃用户数(有订单的用户)
|
||||
const [activeUsers] = await db.execute(
|
||||
'SELECT COUNT(DISTINCT from_user_id) as count FROM transfers'
|
||||
`SELECT
|
||||
COUNT(DISTINCT from_user_id) AS count
|
||||
FROM transfers
|
||||
WHERE YEARWEEK(created_at, 1) = YEARWEEK(CURDATE() - INTERVAL 1 WEEK, 1)`
|
||||
);
|
||||
const [weekUsers] = await db.execute(`
|
||||
SELECT COUNT(DISTINCT from_user_id) as count
|
||||
@@ -1546,20 +1549,52 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
||||
[distribute.inviter]
|
||||
);
|
||||
distributeUser = distributeUser[0]
|
||||
//分销是代理
|
||||
if (distributeUser.user_type == 'agent') {
|
||||
//给代理添加2980融豆的70% 增加区域保护
|
||||
await db.execute(
|
||||
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
||||
[serviceFee * 0.7, distributeUser.id]
|
||||
);
|
||||
//记录转账记录
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.id, 'user_to_agent', 'received', serviceFee * 0.7, '用户服务费返现', 'agent']
|
||||
);
|
||||
let [agentUser] = await db.execute(`
|
||||
SELECT r.*
|
||||
FROM regional_agents as r
|
||||
LEFT JOIN users au on r.user_id = au.id
|
||||
WHERE au.user_type = 'agent'
|
||||
AND r.status = 'active'
|
||||
AND r.region_id = ${user.district_id}
|
||||
`)
|
||||
if (agentUser.length > 0 && agentUser[0].user_id !== distributeUser.id) {
|
||||
//增加区域保护
|
||||
await db.execute(`
|
||||
UPDATE users
|
||||
SET balance = balance - ?
|
||||
WHERE id = ?
|
||||
`, [serviceFee * 0.05, agentUser[0].user_id])
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (to_user_id,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, agentUser[0].user_id, 'user_to_agent', 'received', serviceFee * 0.05, '区域保护服务费返现', 'agent']
|
||||
);
|
||||
await db.execute(
|
||||
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
||||
[serviceFee * 0.65, distributeUser.id]
|
||||
);
|
||||
//记录转账记录
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (to_user_id,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.id, 'user_to_agent', 'received', serviceFee * 0.65, '用户服务费返现', 'agent']
|
||||
);
|
||||
} else {
|
||||
//给代理添加2980融豆的70% 增加区域保护
|
||||
await db.execute(
|
||||
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
||||
[serviceFee * 0.7, distributeUser.id]
|
||||
);
|
||||
//记录转账记录
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (to_user_id,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.id, 'user_to_agent', 'received', serviceFee * 0.7, '用户服务费返现', 'agent']
|
||||
);
|
||||
}
|
||||
|
||||
//记录平台利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, 3512, 'user_to_system', 'received', serviceFee * 0.3, '用户服务费返现', 'system']
|
||||
);
|
||||
//记录服务费
|
||||
@@ -1577,12 +1612,12 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
||||
);
|
||||
//记录转账记录
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.id, 'user_to_agent', 'received', serviceFee * 0.5, '用户服务费返现', 'agent_operated']
|
||||
);
|
||||
//记录平台利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id , from_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, 3512, 'user_to_system', 'received', serviceFee * 0.5, '用户服务费返现', 'system']
|
||||
);
|
||||
//记录服务费
|
||||
@@ -1615,17 +1650,17 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
||||
);
|
||||
//记录转账记录
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.inviter, 'user_to_agent', 'received', serviceFee * 0.2, '用户服务费返现', 'operated_agent']
|
||||
);
|
||||
//记录直营利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id, from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.id, 'user_to_operated', 'received', serviceFee * 0.3, '用户服务费返现', 'directly_operated']
|
||||
);
|
||||
//记录平台利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id , from_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, 3512, 'user_to_system', 'received', serviceFee * 0.5, '用户服务费返现', 'system']
|
||||
);
|
||||
}
|
||||
@@ -1642,17 +1677,17 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
||||
);
|
||||
//记录转账记录
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id , from_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.inviter, 'user_to_agent', 'received', serviceFee * 0.15, '用户服务费返现', 'agent_operated']
|
||||
);
|
||||
//记录直营利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id , from_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.id, 'user_to_operated', 'received', serviceFee * 0.35, '用户服务费返现', 'directly_operated']
|
||||
);
|
||||
//记录平台利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, 3512, 'user_to_system', 'received', serviceFee * 0.5, '用户服务费返现', 'system']
|
||||
);
|
||||
}
|
||||
@@ -1669,7 +1704,7 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
||||
);
|
||||
//记录转账记录
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id ,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.inviter, 'user_to_agent', 'received', serviceFee * 0.1, '用户服务费返现', 'agent_operated']
|
||||
);
|
||||
//记录直营利润
|
||||
@@ -1679,7 +1714,7 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
||||
);
|
||||
//记录平台利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id , from_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, 3512, 'user_to_system', 'received', serviceFee * 0.5, '用户服务费返现', 'system']
|
||||
);
|
||||
}
|
||||
@@ -1701,29 +1736,61 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
||||
userUpInfo = userUpInfo[0]
|
||||
//判断用户上级是否是代理
|
||||
if (userUpInfo && userUpInfo.user_type === 'agent') {
|
||||
let [agentUser] = await db.execute(`
|
||||
SELECT r.*
|
||||
FROM regional_agents as r
|
||||
LEFT JOIN users au on r.user_id = au.id
|
||||
WHERE au.user_type = 'agent'
|
||||
AND r.status = 'active'
|
||||
AND r.region_id = ${user.district_id}
|
||||
`)
|
||||
if (agentUser.length > 0 && agentUser[0].user_id !== distributeUser.id) {
|
||||
//增加区域保护
|
||||
await db.execute(`
|
||||
UPDATE users
|
||||
SET balance = balance - ?
|
||||
WHERE id = ?
|
||||
`, [serviceFee * 0.05, agentUser[0].user_id])
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (to_user_id,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, agentUser[0].user_id, 'user_to_agent', 'received', serviceFee * 0.05, '区域保护服务费返现', 'agent']
|
||||
);
|
||||
//给代理分配
|
||||
await db.execute(
|
||||
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
||||
[serviceFee * 0.45, userUpInfo.id]
|
||||
);
|
||||
//记录代理转账信息
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (to_user_id , from_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, userUpInfo.id, 'user_to_agent', 'received', serviceFee * 0.45, '用户服务费返现', 'agent']
|
||||
);
|
||||
}else {
|
||||
//给代理分配
|
||||
await db.execute(
|
||||
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
||||
[serviceFee * 0.5, userUpInfo.id]
|
||||
);
|
||||
//记录代理转账信息
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (to_user_id , from_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, userUpInfo.id, 'user_to_agent', 'received', serviceFee * 0.5, '用户服务费返现', 'agent']
|
||||
);
|
||||
}
|
||||
//给用户分配
|
||||
await db.execute(
|
||||
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
||||
[serviceFee * 0.2, distributeUser.id]
|
||||
);
|
||||
//给代理分配
|
||||
await db.execute(
|
||||
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
||||
[serviceFee * 0.5, userUpInfo.id]
|
||||
);
|
||||
//记录用户转账信息
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id ,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.id, 'user_to_user', 'received', serviceFee * 0.2, '用户服务费返现', 'operated']
|
||||
);
|
||||
//记录代理转账信息
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, userUpInfo.id, 'user_to_agent', 'received', serviceFee * 0.5, '用户服务费返现', 'agent']
|
||||
);
|
||||
|
||||
//记录平台利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, 3512, 'user_to_system', 'received', serviceFee * 0.3, '用户服务费返现', 'system']
|
||||
);
|
||||
} else {
|
||||
@@ -1734,12 +1801,12 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
||||
);
|
||||
//记录转账记录
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, distributeUser.id, 'user_to_user', 'received', serviceFee * 0.2, '用户服务费返现', 'operated']
|
||||
);
|
||||
//记录平台利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id ,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, 3512, 'user_to_system', 'received', serviceFee * 0.8, '用户服务费返现', 'system']
|
||||
);
|
||||
|
||||
@@ -1754,25 +1821,34 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
||||
} else {
|
||||
//判断用户此区域是否有代理
|
||||
let [agentUser] = await db.execute(`
|
||||
SELECT user_id
|
||||
FROM regional_agents
|
||||
WHERE region_id = ?
|
||||
`, [users[0].district_id])
|
||||
SELECT rg.user_id
|
||||
FROM regional_agents as rg
|
||||
LEFT JOIN users ag ON ag.id = rg.user_id
|
||||
WHERE rg.region_id = ?
|
||||
AND rg.status = 'active'
|
||||
AND ag.user_type = 'agent'
|
||||
`, [user.district_id])
|
||||
if (agentUser.length > 0) {
|
||||
//给区域代理分区域保护
|
||||
await db.execute(`
|
||||
UPDATE users
|
||||
SET balance = balance - ?
|
||||
WHERE id = ?
|
||||
`, [serviceFee * 0.05, agentUser[0].user_id])
|
||||
//给代理分成5%
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id ,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, agentUser[0].user_id, 'user_to_regional', 'received', serviceFee * 0.05, '区域保护服务费返现', 'agent']
|
||||
)
|
||||
//记录平台利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id , from_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, 3512, 'user_to_system', 'received', serviceFee * 0.95, '用户服务费返现', 'system']
|
||||
);
|
||||
} else {
|
||||
//记录平台利润
|
||||
await db.execute(
|
||||
'INSERT INTO transfers (from_user_id, to_user_id, transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
'INSERT INTO transfers (to_user_id ,from_user_id , transfer_type,status,amount,description,source_type) VALUES (?, ?, ?,?,?,?,?)',
|
||||
[userId, 3512, 'user_to_system', 'received', serviceFee, '用户服务费返现', 'system']
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user