提交
This commit is contained in:
3
.idea/data_source_mapping.xml
generated
3
.idea/data_source_mapping.xml
generated
@@ -2,7 +2,10 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="DataSourcePerFileMappings">
|
<component name="DataSourcePerFileMappings">
|
||||||
<file url="file://$PROJECT_DIR$/database.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
<file url="file://$PROJECT_DIR$/database.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
||||||
|
<file url="file://$PROJECT_DIR$/routes/matching.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
||||||
|
<file url="file://$PROJECT_DIR$/routes/transfers.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
||||||
<file url="file://$PROJECT_DIR$/routes/users.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
<file url="file://$PROJECT_DIR$/routes/users.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
||||||
|
<file url="file://$PROJECT_DIR$/services/matchingService.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
||||||
<file url="file://$PROJECT_DIR$/services/transferService.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
<file url="file://$PROJECT_DIR$/services/transferService.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
6
.idea/sqldialects.xml
generated
6
.idea/sqldialects.xml
generated
@@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="SqlDialectMappings">
|
|
||||||
<file url="PROJECT" dialect="MySQL" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
@@ -310,12 +310,12 @@ router.post('/confirm-allocation/:allocationId', auth, async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 调用服务层方法,传递完整的转账信息
|
// 调用服务层方法,传递完整的转账信息
|
||||||
const transferId = await matchingService.confirmAllocation(
|
const transferId = matchingService.confirmAllocation(
|
||||||
allocationId,
|
allocationId,
|
||||||
userId,
|
userId,
|
||||||
transferAmount,
|
transferAmount,
|
||||||
description,
|
description,
|
||||||
voucher
|
voucher
|
||||||
);
|
);
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
@@ -324,11 +324,11 @@ router.post('/confirm-allocation/:allocationId', auth, async (req, res) => {
|
|||||||
data: { transferId }
|
data: { transferId }
|
||||||
});
|
});
|
||||||
|
|
||||||
axios.post('http://localhost:5000/ocr',{
|
// axios.post('http://localhost:5000/ocr',{
|
||||||
id: allocationId
|
// id: allocationId
|
||||||
}).then(res => {
|
// }).then(res => {
|
||||||
console.log(res.data)
|
// console.log(res.data)
|
||||||
})
|
// })
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('确认分配失败:', error);
|
console.error('确认分配失败:', error);
|
||||||
|
|||||||
@@ -141,11 +141,6 @@ router.post('/send', async (req, res) => {
|
|||||||
// 生产环境发送真实短信
|
// 生产环境发送真实短信
|
||||||
try {
|
try {
|
||||||
console.log(code);
|
console.log(code);
|
||||||
res.json({
|
|
||||||
success: true,
|
|
||||||
message: '验证码发送成功'
|
|
||||||
})
|
|
||||||
return
|
|
||||||
const sendSmsRequest = new Dysmsapi20170525.SendSmsRequest({
|
const sendSmsRequest = new Dysmsapi20170525.SendSmsRequest({
|
||||||
phoneNumbers: phone,
|
phoneNumbers: phone,
|
||||||
signName: SMS_CONFIG.signName,
|
signName: SMS_CONFIG.signName,
|
||||||
|
|||||||
@@ -12,6 +12,48 @@ const dayjs = require('dayjs');
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
|
||||||
|
// router.get('/tmp', async (req, res) => {
|
||||||
|
// const db = getDB();
|
||||||
|
// // 1. 查询所有转账记录,按 id 升序
|
||||||
|
// let [transfers] = await db.execute(`
|
||||||
|
// SELECT *
|
||||||
|
// FROM transfers
|
||||||
|
// WHERE status='received' or status='confirmed'
|
||||||
|
// ORDER BY id ASC`);
|
||||||
|
//
|
||||||
|
// // 2. 用对象维护每个用户的最新余额
|
||||||
|
// const userBalances = {};
|
||||||
|
//
|
||||||
|
// // 3. 遍历每条转账记录,计算余额
|
||||||
|
// for (const trx of transfers) {
|
||||||
|
// const fromId = trx.from_user_id;
|
||||||
|
// const toId = trx.to_user_id;
|
||||||
|
// const amount = Number(trx.amount);
|
||||||
|
//
|
||||||
|
// if (!(fromId in userBalances)) userBalances[fromId] = 0;
|
||||||
|
// if (!(toId in userBalances)) userBalances[toId] = 0;
|
||||||
|
//
|
||||||
|
// const fromBalance = userBalances[fromId] - amount;
|
||||||
|
// const toBalance = userBalances[toId] + amount;
|
||||||
|
//
|
||||||
|
// userBalances[fromId] = fromBalance;
|
||||||
|
// userBalances[toId] = toBalance;
|
||||||
|
//
|
||||||
|
// const balanceDiff = toBalance - fromBalance;
|
||||||
|
//
|
||||||
|
// // 4. 更新当前行的字段
|
||||||
|
// await db.execute(
|
||||||
|
// `UPDATE transfers
|
||||||
|
// SET from_user_balance = ?,
|
||||||
|
// to_user_balance = ?,
|
||||||
|
// balance_diff = ?
|
||||||
|
// WHERE id = ?`,
|
||||||
|
// [fromBalance, toBalance, amount, trx.id]
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// res.json('更新成功')
|
||||||
|
// })
|
||||||
router.get('/',
|
router.get('/',
|
||||||
authenticateToken,
|
authenticateToken,
|
||||||
validateQuery(transferSchemas.query),
|
validateQuery(transferSchemas.query),
|
||||||
@@ -342,7 +384,6 @@ router.post('/confirm-not-received',
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 获取用户转账记录
|
// 获取用户转账记录
|
||||||
router.get('/user/:userId', authenticateToken, async (req, res) => {
|
router.get('/user/:userId', authenticateToken, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -386,8 +427,9 @@ router.get('/user/:userId', authenticateToken, async (req, res) => {
|
|||||||
LEFT JOIN users to_user ON t.to_user_id = to_user.id
|
LEFT JOIN users to_user ON t.to_user_id = to_user.id
|
||||||
${whereClause}
|
${whereClause}
|
||||||
ORDER BY t.created_at
|
ORDER BY t.created_at
|
||||||
DESC
|
DESC
|
||||||
LIMIT ${limitNum} OFFSET ${offset}
|
LIMIT ${limitNum}
|
||||||
|
OFFSET ${offset}
|
||||||
`, countParams);
|
`, countParams);
|
||||||
|
|
||||||
const [countResult] = await db.execute(`
|
const [countResult] = await db.execute(`
|
||||||
@@ -425,37 +467,37 @@ router.get('/stats', authenticateToken, async (req, res) => {
|
|||||||
if (isAdmin) {
|
if (isAdmin) {
|
||||||
// 管理员可以查看全局统计
|
// 管理员可以查看全局统计
|
||||||
const [totalStats] = await db.execute(`
|
const [totalStats] = await db.execute(`
|
||||||
SELECT COUNT(*) as total_transfers,
|
SELECT COUNT(*) as total_transfers,
|
||||||
SUM(amount) as total_flow_amount,
|
SUM(amount) as total_flow_amount,
|
||||||
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending_count,
|
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending_count,
|
||||||
SUM(CASE WHEN status = 'confirmed' THEN 1 ELSE 0 END) as confirmed_count,
|
SUM(CASE WHEN status = 'confirmed' THEN 1 ELSE 0 END) as confirmed_count,
|
||||||
SUM(CASE WHEN status = 'received' THEN 1 ELSE 0 END) as received_count,
|
SUM(CASE WHEN status = 'received' THEN 1 ELSE 0 END) as received_count,
|
||||||
SUM(CASE WHEN status = 'rejected' THEN 1 ELSE 0 END) as rejected_count,
|
SUM(CASE WHEN status = 'rejected' THEN 1 ELSE 0 END) as rejected_count,
|
||||||
SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) as cancelled_count,
|
SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) as cancelled_count,
|
||||||
SUM(CASE WHEN status = 'not_received' THEN 1 ELSE 0 END) as not_received_count,
|
SUM(CASE WHEN status = 'not_received' THEN 1 ELSE 0 END) as not_received_count,
|
||||||
SUM(CASE WHEN is_overdue = 1 THEN 1 ELSE 0 END) as overdue_count,
|
SUM(CASE WHEN is_overdue = 1 THEN 1 ELSE 0 END) as overdue_count,
|
||||||
SUM(CASE WHEN is_bad_debt = 1 THEN 1 ELSE 0 END) as bad_debt_count,
|
SUM(CASE WHEN is_bad_debt = 1 THEN 1 ELSE 0 END) as bad_debt_count,
|
||||||
SUM(CASE WHEN status = 'confirmed' THEN amount ELSE 0 END) as total_amount,
|
SUM(CASE WHEN status = 'confirmed' THEN amount ELSE 0 END) as total_amount,
|
||||||
SUM(CASE WHEN is_bad_debt = 1 THEN amount ELSE 0 END) as bad_debt_amount,
|
SUM(CASE WHEN is_bad_debt = 1 THEN amount ELSE 0 END) as bad_debt_amount,
|
||||||
SUM(CASE
|
SUM(CASE
|
||||||
WHEN transfer_type = 'initial' AND status = 'confirmed' THEN amount
|
WHEN transfer_type = 'initial' AND status = 'confirmed' THEN amount
|
||||||
ELSE 0 END) as initial_amount,
|
ELSE 0 END) as initial_amount,
|
||||||
SUM(CASE
|
SUM(CASE
|
||||||
WHEN transfer_type = 'return' AND status = 'confirmed' THEN amount
|
WHEN transfer_type = 'return' AND status = 'confirmed' THEN amount
|
||||||
ELSE 0 END) as return_amount,
|
ELSE 0 END) as return_amount,
|
||||||
SUM(CASE
|
SUM(CASE
|
||||||
WHEN transfer_type = 'user_to_user' AND status = 'confirmed' THEN amount
|
WHEN transfer_type = 'user_to_user' AND status = 'confirmed' THEN amount
|
||||||
ELSE 0 END) as user_to_user_amount,
|
ELSE 0 END) as user_to_user_amount,
|
||||||
(SELECT SUM(balance)
|
(SELECT SUM(balance)
|
||||||
FROM users
|
FROM users
|
||||||
WHERE role = 'user'
|
WHERE role = 'user'
|
||||||
AND is_system_account = 1) as total_merchant_balance,
|
AND is_system_account = 1) as total_merchant_balance,
|
||||||
(SELECT SUM(balance)
|
(SELECT SUM(balance)
|
||||||
FROM users
|
FROM users
|
||||||
WHERE role = 'user'
|
WHERE role = 'user'
|
||||||
AND is_system_account != 1) as total_user_balance,
|
AND is_system_account != 1) as total_user_balance,
|
||||||
SUM(CASE WHEN source_type IN ('system') THEN amount END) as participated_transfers,
|
SUM(CASE WHEN source_type IN ('system') THEN amount END) as participated_transfers,
|
||||||
SUM(CASE WHEN source_type IN ('agent') THEN amount END) as agent_total,
|
SUM(CASE WHEN source_type IN ('agent') THEN amount END) as agent_total,
|
||||||
SUM(CASE WHEN source_type IN ('operated_agent') THEN amount END) as operated_agent_total
|
SUM(CASE WHEN source_type IN ('operated_agent') THEN amount END) as operated_agent_total
|
||||||
FROM transfers
|
FROM transfers
|
||||||
`);
|
`);
|
||||||
@@ -470,26 +512,26 @@ router.get('/stats', authenticateToken, async (req, res) => {
|
|||||||
(
|
(
|
||||||
COALESCE((SELECT SUM(amount)
|
COALESCE((SELECT SUM(amount)
|
||||||
FROM transfers
|
FROM transfers
|
||||||
WHERE DATE(created_at) = ?
|
WHERE DATE (created_at) = ?
|
||||||
AND to_user_id IN (SELECT id FROM users WHERE is_system_account = 1)
|
AND to_user_id IN (SELECT id FROM users WHERE is_system_account = 1)
|
||||||
AND status = 'received'), 0) -
|
AND status = 'received'), 0) -
|
||||||
COALESCE((SELECT SUM(amount)
|
COALESCE((SELECT SUM(amount)
|
||||||
FROM transfers
|
FROM transfers
|
||||||
WHERE DATE(created_at) = ?
|
WHERE DATE (created_at) = ?
|
||||||
AND from_user_id IN (SELECT id FROM users WHERE is_system_account = 1)
|
AND from_user_id IN (SELECT id FROM users WHERE is_system_account = 1)
|
||||||
AND status = 'received'), 0)
|
AND status = 'received'), 0)
|
||||||
) as today_amount
|
) as today_amount
|
||||||
FROM transfers
|
FROM transfers
|
||||||
WHERE DATE(created_at) = ?
|
WHERE DATE (created_at) = ?
|
||||||
`, [todayStr, todayStr, todayStr]);
|
`, [todayStr, todayStr, todayStr]);
|
||||||
|
|
||||||
const [monthlyStats] = await db.execute(`
|
const [monthlyStats] = await db.execute(`
|
||||||
SELECT COUNT(*) as monthly_transfers,
|
SELECT COUNT(*) as monthly_transfers,
|
||||||
SUM(CASE WHEN status = 'received' THEN amount ELSE 0 END) as monthly_amount,
|
SUM(CASE WHEN status = 'received' THEN amount ELSE 0 END) as monthly_amount,
|
||||||
SUM(CASE WHEN source_type IN ('system') THEN amount END) as monthly_participated_transfers
|
SUM(CASE WHEN source_type IN ('system') THEN amount END) as monthly_participated_transfers
|
||||||
FROM transfers
|
FROM transfers
|
||||||
WHERE YEAR(created_at) = ?
|
WHERE YEAR (created_at) = ?
|
||||||
AND MONTH(created_at) = ?
|
AND MONTH (created_at) = ?
|
||||||
`, [currentYear, currentMonth]);
|
`, [currentYear, currentMonth]);
|
||||||
|
|
||||||
// 获取上月统计数据用于对比
|
// 获取上月统计数据用于对比
|
||||||
@@ -497,12 +539,12 @@ router.get('/stats', authenticateToken, async (req, res) => {
|
|||||||
const lastMonthYear = currentMonth === 1 ? currentYear - 1 : currentYear;
|
const lastMonthYear = currentMonth === 1 ? currentYear - 1 : currentYear;
|
||||||
|
|
||||||
const [lastMonthStats] = await db.execute(`
|
const [lastMonthStats] = await db.execute(`
|
||||||
SELECT COUNT(*) as last_monthly_transfers,
|
SELECT COUNT(*) as last_monthly_transfers,
|
||||||
SUM(CASE WHEN status = 'confirmed' THEN amount ELSE 0 END) as last_monthly_amount,
|
SUM(CASE WHEN status = 'confirmed' THEN amount ELSE 0 END) as last_monthly_amount,
|
||||||
SUM(CASE WHEN source_type IN ('system') THEN amount END) as last_monthly_participated_transfers
|
SUM(CASE WHEN source_type IN ('system') THEN amount END) as last_monthly_participated_transfers
|
||||||
FROM transfers
|
FROM transfers
|
||||||
WHERE YEAR(created_at) = ?
|
WHERE YEAR (created_at) = ?
|
||||||
AND MONTH(created_at) = ?
|
AND MONTH (created_at) = ?
|
||||||
`, [lastMonthYear, lastMonth]);
|
`, [lastMonthYear, lastMonth]);
|
||||||
|
|
||||||
stats = {
|
stats = {
|
||||||
@@ -524,9 +566,9 @@ router.get('/stats', authenticateToken, async (req, res) => {
|
|||||||
return_amount: parseFloat(totalStats[0].return_amount || 0),
|
return_amount: parseFloat(totalStats[0].return_amount || 0),
|
||||||
user_to_user_amount: parseFloat(totalStats[0].user_to_user_amount || 0),
|
user_to_user_amount: parseFloat(totalStats[0].user_to_user_amount || 0),
|
||||||
participated_transfers: totalStats[0].participated_transfers || 0,
|
participated_transfers: totalStats[0].participated_transfers || 0,
|
||||||
total_user_balance:totalStats[0].total_user_balance || 0,
|
total_user_balance: totalStats[0].total_user_balance || 0,
|
||||||
agent_total:totalStats[0].agent_total || 0,//代理收入
|
agent_total: totalStats[0].agent_total || 0,//代理收入
|
||||||
operated_agent_total:totalStats[0].operated_agent_total || 0,//直营代理收入
|
operated_agent_total: totalStats[0].operated_agent_total || 0,//直营代理收入
|
||||||
},
|
},
|
||||||
today: {
|
today: {
|
||||||
transfers: todayStats[0].today_transfers || 0,
|
transfers: todayStats[0].today_transfers || 0,
|
||||||
@@ -565,7 +607,7 @@ router.get('/stats', authenticateToken, async (req, res) => {
|
|||||||
SUM(CASE WHEN status = 'confirmed' AND to_user_id = ? THEN amount ELSE 0 END) as today_received
|
SUM(CASE WHEN status = 'confirmed' AND to_user_id = ? THEN amount ELSE 0 END) as today_received
|
||||||
FROM transfers
|
FROM transfers
|
||||||
WHERE (from_user_id = ? OR to_user_id = ?)
|
WHERE (from_user_id = ? OR to_user_id = ?)
|
||||||
AND DATE(created_at) = ?
|
AND DATE (created_at) = ?
|
||||||
`, [userId, userId, userId, userId, todayStr]);
|
`, [userId, userId, userId, userId, todayStr]);
|
||||||
|
|
||||||
stats = {
|
stats = {
|
||||||
@@ -706,9 +748,9 @@ router.get('/trend', authenticateToken, async (req, res) => {
|
|||||||
|
|
||||||
// 首先获取数据库中最早和最晚的转账日期
|
// 首先获取数据库中最早和最晚的转账日期
|
||||||
const [dateRange] = await db.execute(`
|
const [dateRange] = await db.execute(`
|
||||||
SELECT MIN(DATE(created_at)) as min_date,
|
SELECT MIN(DATE (created_at)) as min_date,
|
||||||
MAX(DATE(created_at)) as max_date,
|
MAX(DATE (created_at)) as max_date,
|
||||||
COUNT(*) as total_count
|
COUNT(*) as total_count
|
||||||
FROM transfers
|
FROM transfers
|
||||||
`);
|
`);
|
||||||
|
|
||||||
@@ -737,13 +779,13 @@ router.get('/trend', authenticateToken, async (req, res) => {
|
|||||||
|
|
||||||
// 获取指定天数内的转账趋势(从最大日期往前推)
|
// 获取指定天数内的转账趋势(从最大日期往前推)
|
||||||
const [trendData] = await db.execute(`
|
const [trendData] = await db.execute(`
|
||||||
SELECT DATE(created_at) as date,
|
SELECT DATE (created_at) as date, COUNT (*) as count, SUM (amount) as amount
|
||||||
COUNT(*) as count,
|
|
||||||
SUM(amount) as amount
|
|
||||||
FROM transfers
|
FROM transfers
|
||||||
WHERE DATE(created_at) >= DATE_SUB(?, INTERVAL ? DAY)
|
WHERE DATE (created_at) >= DATE_SUB(?
|
||||||
AND status IN ('confirmed', 'received')
|
, INTERVAL ? DAY)
|
||||||
GROUP BY DATE(created_at)
|
AND status IN ('confirmed'
|
||||||
|
, 'received')
|
||||||
|
GROUP BY DATE (created_at)
|
||||||
ORDER BY date ASC
|
ORDER BY date ASC
|
||||||
`, [dateRange[0].max_date, daysNum - 1]);
|
`, [dateRange[0].max_date, daysNum - 1]);
|
||||||
|
|
||||||
@@ -1024,7 +1066,8 @@ router.get('/pending-allocations',
|
|||||||
JOIN matching_orders mo ON oa.id = mo.id
|
JOIN matching_orders mo ON oa.id = mo.id
|
||||||
${whereClause}
|
${whereClause}
|
||||||
ORDER BY oa.${sortField} ${sortOrder}
|
ORDER BY oa.${sortField} ${sortOrder}
|
||||||
LIMIT ${parseInt(limit)} OFFSET ${parseInt(offset)}
|
LIMIT ${parseInt(limit)}
|
||||||
|
OFFSET ${parseInt(offset)}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const [allocations] = queryParams.length > 0
|
const [allocations] = queryParams.length > 0
|
||||||
@@ -1140,19 +1183,16 @@ router.get('/daily-stats',
|
|||||||
|
|
||||||
// 获取所有用户的昨日转出和今日入账统计
|
// 获取所有用户的昨日转出和今日入账统计
|
||||||
let [userStats] = await db.execute(`
|
let [userStats] = await db.execute(`
|
||||||
SELECT u.id as user_id,
|
SELECT u.id as user_id,
|
||||||
u.username,
|
u.username,
|
||||||
u.real_name,
|
u.real_name,
|
||||||
u.phone,
|
u.phone,
|
||||||
u.balance,
|
u.balance,
|
||||||
COALESCE(yesterday_out.amount, 0) as yesterday_out_amount,
|
COALESCE(yesterday_out.amount, 0) -
|
||||||
COALESCE(today_in.amount, 0) as today_in_amount,
|
COALESCE(yesterday_system_num.amount, 0) as yesterday_out_amount,
|
||||||
COALESCE(confirmed_from.confirmed_amount, 0) as confirmed_from_amount,
|
COALESCE(today_in.amount, 0) as today_in_amount,
|
||||||
CASE
|
COALESCE(confirmed_from.confirmed_amount, 0) as confirmed_from_amount,
|
||||||
WHEN (COALESCE(u.balance, 0) + COALESCE(confirmed_from.confirmed_amount, 0)) > ABS(u.balance)
|
COALESCE(u.balance, 0) + COALESCE(confirmed_from.confirmed_amount, 0) as balance_needed
|
||||||
THEN ABS(u.balance)
|
|
||||||
ELSE (COALESCE(u.balance, 0) + COALESCE(confirmed_from.confirmed_amount, 0))
|
|
||||||
END as balance_needed
|
|
||||||
FROM users u
|
FROM users u
|
||||||
LEFT JOIN (SELECT from_user_id,
|
LEFT JOIN (SELECT from_user_id,
|
||||||
SUM(amount) as amount
|
SUM(amount) as amount
|
||||||
@@ -1161,6 +1201,15 @@ router.get('/daily-stats',
|
|||||||
AND created_at <= ?
|
AND created_at <= ?
|
||||||
AND status IN ('confirmed', 'received')
|
AND status IN ('confirmed', 'received')
|
||||||
GROUP BY from_user_id) yesterday_out ON u.id = yesterday_out.from_user_id
|
GROUP BY from_user_id) yesterday_out ON u.id = yesterday_out.from_user_id
|
||||||
|
LEFT JOIN (SELECT to_user_id,
|
||||||
|
SUM(amount) as amount
|
||||||
|
FROM transfers
|
||||||
|
WHERE created_at >= ?
|
||||||
|
AND created_at <= ?
|
||||||
|
AND transfer_type != 'user_to_user'
|
||||||
|
AND status IN ('received')
|
||||||
|
GROUP BY to_user_id) yesterday_system_num
|
||||||
|
ON u.id = yesterday_system_num.to_user_id
|
||||||
LEFT JOIN (SELECT to_user_id,
|
LEFT JOIN (SELECT to_user_id,
|
||||||
SUM(amount) as amount
|
SUM(amount) as amount
|
||||||
FROM transfers
|
FROM transfers
|
||||||
@@ -1171,17 +1220,17 @@ router.get('/daily-stats',
|
|||||||
left join (select from_user_id,
|
left join (select from_user_id,
|
||||||
sum(amount) as confirmed_amount
|
sum(amount) as confirmed_amount
|
||||||
from transfers
|
from transfers
|
||||||
where status = 'received'
|
WHERE status IN ('confirmed', 'received')
|
||||||
and created_at >= ?
|
AND created_at >= ?
|
||||||
and created_at <= ?
|
AND created_at <= ?
|
||||||
group by from_user_id) as confirmed_from on u.id = confirmed_from.from_user_id
|
GROUP BY from_user_id) as confirmed_from on u.id = confirmed_from.from_user_id
|
||||||
WHERE u.role != 'admin'
|
WHERE u.role != 'admin' AND u.user_type !='directly_operated'
|
||||||
AND u.is_system_account != 1
|
AND u.is_system_account != 1
|
||||||
AND yesterday_out.amount > 0
|
AND yesterday_out.amount > 0
|
||||||
AND u.balance < 0
|
AND u.balance < 0
|
||||||
ORDER BY balance_needed DESC, yesterday_out_amount DESC
|
ORDER BY balance_needed DESC, yesterday_out_amount DESC
|
||||||
`, [yesterdayStartStr, yesterdayEndStr, todayStartStr, todayEndStr, todayStartStr, todayEndStr]);
|
`, [yesterdayStartStr, yesterdayEndStr, yesterdayStartStr, yesterdayEndStr,todayStartStr, todayEndStr, todayStartStr, todayEndStr]);
|
||||||
// userStats = userStats.filter(item=>item.balance_needed >= 100)
|
// userStats = userStats.filter(item=>item.balance_needed > 100)
|
||||||
userStats.forEach(item => {
|
userStats.forEach(item => {
|
||||||
item.balance_needed = Math.abs(item.balance_needed)
|
item.balance_needed = Math.abs(item.balance_needed)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -51,54 +51,6 @@ const multiUpload = multer({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
* @swagger
|
|
||||||
* /upload/image:
|
|
||||||
* post:
|
|
||||||
* summary: 上传图片
|
|
||||||
* tags: [Upload]
|
|
||||||
* security:
|
|
||||||
* - bearerAuth: []
|
|
||||||
* requestBody:
|
|
||||||
* required: true
|
|
||||||
* content:
|
|
||||||
* multipart/form-data:
|
|
||||||
* schema:
|
|
||||||
* type: object
|
|
||||||
* properties:
|
|
||||||
* file:
|
|
||||||
* type: string
|
|
||||||
* format: binary
|
|
||||||
* description: 要上传的图片文件
|
|
||||||
* type:
|
|
||||||
* type: string
|
|
||||||
* enum: [avatar, product, document]
|
|
||||||
* default: document
|
|
||||||
* description: 上传文件类型
|
|
||||||
* responses:
|
|
||||||
* 200:
|
|
||||||
* description: 图片上传成功
|
|
||||||
* content:
|
|
||||||
* application/json:
|
|
||||||
* schema:
|
|
||||||
* type: object
|
|
||||||
* properties:
|
|
||||||
* success:
|
|
||||||
* type: boolean
|
|
||||||
* example: true
|
|
||||||
* url:
|
|
||||||
* type: string
|
|
||||||
* description: 上传后的文件URL
|
|
||||||
* filename:
|
|
||||||
* type: string
|
|
||||||
* description: 上传后的文件名
|
|
||||||
* 400:
|
|
||||||
* description: 请求参数错误
|
|
||||||
* 401:
|
|
||||||
* description: 未授权
|
|
||||||
* 500:
|
|
||||||
* description: 服务器错误
|
|
||||||
*/
|
|
||||||
router.post('/image', authenticateToken, (req, res) => {
|
router.post('/image', authenticateToken, (req, res) => {
|
||||||
upload.single('file')(req, res, async (err) => {
|
upload.single('file')(req, res, async (err) => {
|
||||||
if (err instanceof multer.MulterError) {
|
if (err instanceof multer.MulterError) {
|
||||||
|
|||||||
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 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);
|
const offset = Math.max(0, (pageNum - 1) * limitNum);
|
||||||
|
|
||||||
let whereConditions = [];
|
let whereConditions = [];
|
||||||
@@ -504,7 +504,10 @@ router.get('/stats', auth, async (req, res) => {
|
|||||||
|
|
||||||
// 活跃用户数(有订单的用户)
|
// 活跃用户数(有订单的用户)
|
||||||
const [activeUsers] = await db.execute(
|
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(`
|
const [weekUsers] = await db.execute(`
|
||||||
SELECT COUNT(DISTINCT from_user_id) as count
|
SELECT COUNT(DISTINCT from_user_id) as count
|
||||||
@@ -1546,20 +1549,52 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
|||||||
[distribute.inviter]
|
[distribute.inviter]
|
||||||
);
|
);
|
||||||
distributeUser = distributeUser[0]
|
distributeUser = distributeUser[0]
|
||||||
|
//分销是代理
|
||||||
if (distributeUser.user_type == 'agent') {
|
if (distributeUser.user_type == 'agent') {
|
||||||
//给代理添加2980融豆的70% 增加区域保护
|
let [agentUser] = await db.execute(`
|
||||||
await db.execute(
|
SELECT r.*
|
||||||
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
FROM regional_agents as r
|
||||||
[serviceFee * 0.7, distributeUser.id]
|
LEFT JOIN users au on r.user_id = au.id
|
||||||
);
|
WHERE au.user_type = 'agent'
|
||||||
//记录转账记录
|
AND r.status = 'active'
|
||||||
await db.execute(
|
AND r.region_id = ${user.district_id}
|
||||||
'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']
|
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(
|
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']
|
[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(
|
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']
|
[userId, distributeUser.id, 'user_to_agent', 'received', serviceFee * 0.5, '用户服务费返现', 'agent_operated']
|
||||||
);
|
);
|
||||||
//记录平台利润
|
//记录平台利润
|
||||||
await db.execute(
|
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']
|
[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(
|
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']
|
[userId, distributeUser.inviter, 'user_to_agent', 'received', serviceFee * 0.2, '用户服务费返现', 'operated_agent']
|
||||||
);
|
);
|
||||||
//记录直营利润
|
//记录直营利润
|
||||||
await db.execute(
|
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']
|
[userId, distributeUser.id, 'user_to_operated', 'received', serviceFee * 0.3, '用户服务费返现', 'directly_operated']
|
||||||
);
|
);
|
||||||
//记录平台利润
|
//记录平台利润
|
||||||
await db.execute(
|
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']
|
[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(
|
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']
|
[userId, distributeUser.inviter, 'user_to_agent', 'received', serviceFee * 0.15, '用户服务费返现', 'agent_operated']
|
||||||
);
|
);
|
||||||
//记录直营利润
|
//记录直营利润
|
||||||
await db.execute(
|
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']
|
[userId, distributeUser.id, 'user_to_operated', 'received', serviceFee * 0.35, '用户服务费返现', 'directly_operated']
|
||||||
);
|
);
|
||||||
//记录平台利润
|
//记录平台利润
|
||||||
await db.execute(
|
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']
|
[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(
|
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']
|
[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(
|
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']
|
[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]
|
userUpInfo = userUpInfo[0]
|
||||||
//判断用户上级是否是代理
|
//判断用户上级是否是代理
|
||||||
if (userUpInfo && userUpInfo.user_type === 'agent') {
|
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(
|
await db.execute(
|
||||||
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
||||||
[serviceFee * 0.2, distributeUser.id]
|
[serviceFee * 0.2, distributeUser.id]
|
||||||
);
|
);
|
||||||
//给代理分配
|
|
||||||
await db.execute(
|
|
||||||
'UPDATE users SET balance = balance - ? WHERE id = ?',
|
|
||||||
[serviceFee * 0.5, userUpInfo.id]
|
|
||||||
);
|
|
||||||
//记录用户转账信息
|
//记录用户转账信息
|
||||||
await db.execute(
|
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']
|
[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(
|
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']
|
[userId, 3512, 'user_to_system', 'received', serviceFee * 0.3, '用户服务费返现', 'system']
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
@@ -1734,12 +1801,12 @@ router.post('/:id/deduct-service-fee', auth, async (req, res) => {
|
|||||||
);
|
);
|
||||||
//记录转账记录
|
//记录转账记录
|
||||||
await db.execute(
|
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']
|
[userId, distributeUser.id, 'user_to_user', 'received', serviceFee * 0.2, '用户服务费返现', 'operated']
|
||||||
);
|
);
|
||||||
//记录平台利润
|
//记录平台利润
|
||||||
await db.execute(
|
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']
|
[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 {
|
} else {
|
||||||
//判断用户此区域是否有代理
|
//判断用户此区域是否有代理
|
||||||
let [agentUser] = await db.execute(`
|
let [agentUser] = await db.execute(`
|
||||||
SELECT user_id
|
SELECT rg.user_id
|
||||||
FROM regional_agents
|
FROM regional_agents as rg
|
||||||
WHERE region_id = ?
|
LEFT JOIN users ag ON ag.id = rg.user_id
|
||||||
`, [users[0].district_id])
|
WHERE rg.region_id = ?
|
||||||
|
AND rg.status = 'active'
|
||||||
|
AND ag.user_type = 'agent'
|
||||||
|
`, [user.district_id])
|
||||||
if (agentUser.length > 0) {
|
if (agentUser.length > 0) {
|
||||||
|
//给区域代理分区域保护
|
||||||
|
await db.execute(`
|
||||||
|
UPDATE users
|
||||||
|
SET balance = balance - ?
|
||||||
|
WHERE id = ?
|
||||||
|
`, [serviceFee * 0.05, agentUser[0].user_id])
|
||||||
//给代理分成5%
|
//给代理分成5%
|
||||||
await db.execute(
|
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']
|
[userId, agentUser[0].user_id, 'user_to_regional', 'received', serviceFee * 0.05, '区域保护服务费返现', 'agent']
|
||||||
)
|
)
|
||||||
//记录平台利润
|
//记录平台利润
|
||||||
await db.execute(
|
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']
|
[userId, 3512, 'user_to_system', 'received', serviceFee * 0.95, '用户服务费返现', 'system']
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
//记录平台利润
|
//记录平台利润
|
||||||
await db.execute(
|
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']
|
[userId, 3512, 'user_to_system', 'received', serviceFee, '用户服务费返现', 'system']
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -241,7 +241,7 @@ class TransferService {
|
|||||||
// 获取转账列表
|
// 获取转账列表
|
||||||
async getTransfers(filters = {}, pagination = {}, user_type = 'user_to_user') {
|
async getTransfers(filters = {}, pagination = {}, user_type = 'user_to_user') {
|
||||||
const db = getDB();
|
const db = getDB();
|
||||||
const {page = 1, limit = 10, sort = 'created_at', order = 'desc'} = pagination;
|
const {page = 1, limit = 10, sort = 'id', order = 'desc'} = pagination;
|
||||||
const pageNum = parseInt(page, 10) || 1;
|
const pageNum = parseInt(page, 10) || 1;
|
||||||
const limitNum = parseInt(limit, 10) || 10;
|
const limitNum = parseInt(limit, 10) || 10;
|
||||||
const offset = (pageNum - 1) * limitNum;
|
const offset = (pageNum - 1) * limitNum;
|
||||||
@@ -283,7 +283,7 @@ class TransferService {
|
|||||||
|
|
||||||
// 构建排序子句
|
// 构建排序子句
|
||||||
const validSortFields = ['id', 'amount', 'created_at', 'updated_at', 'status'];
|
const validSortFields = ['id', 'amount', 'created_at', 'updated_at', 'status'];
|
||||||
const sortField = validSortFields.includes(sort) ? sort : 'created_at';
|
const sortField = validSortFields.includes(sort) ? sort : 'id';
|
||||||
const sortOrder = order && order.toLowerCase() === 'asc' ? 'ASC' : 'DESC';
|
const sortOrder = order && order.toLowerCase() === 'asc' ? 'ASC' : 'DESC';
|
||||||
|
|
||||||
const orderClause = `ORDER BY t.${sortField} ${sortOrder}`;
|
const orderClause = `ORDER BY t.${sortField} ${sortOrder}`;
|
||||||
@@ -304,6 +304,8 @@ class TransferService {
|
|||||||
`SELECT t.*,
|
`SELECT t.*,
|
||||||
fu.username as from_username,
|
fu.username as from_username,
|
||||||
fu.real_name as from_real_name,
|
fu.real_name as from_real_name,
|
||||||
|
fu.balance as from_balance,
|
||||||
|
tu.balance as to_balance,
|
||||||
tu.username as to_username,
|
tu.username as to_username,
|
||||||
tu.real_name as to_real_name,
|
tu.real_name as to_real_name,
|
||||||
f_p.name as from_province,
|
f_p.name as from_province,
|
||||||
@@ -322,7 +324,7 @@ class TransferService {
|
|||||||
LEFT JOIN china_regions t_c ON t_c.code = tu.city
|
LEFT JOIN china_regions t_c ON t_c.code = tu.city
|
||||||
LEFT JOIN china_regions t_d ON t_d.code = tu.district_id
|
LEFT JOIN china_regions t_d ON t_d.code = tu.district_id
|
||||||
${whereClause} ${orderClause}
|
${whereClause} ${orderClause}
|
||||||
LIMIT ${limitNum}`,
|
LIMIT ${limitNum} OFFSET ${offset}`,
|
||||||
params
|
params
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user