Compare commits
2 Commits
981320d074
...
5d50c42e3e
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d50c42e3e | |||
| 8491be681b |
3
.idea/data_source_mapping.xml
generated
3
.idea/data_source_mapping.xml
generated
@@ -2,7 +2,10 @@
|
||||
<project version="4">
|
||||
<component name="DataSourcePerFileMappings">
|
||||
<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$/services/matchingService.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
||||
<file url="file://$PROJECT_DIR$/services/transferService.js" value="5c67c46f-1d09-4751-a201-e01d3162c9fe" />
|
||||
</component>
|
||||
</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(
|
||||
allocationId,
|
||||
userId,
|
||||
transferAmount,
|
||||
description,
|
||||
voucher
|
||||
const transferId = matchingService.confirmAllocation(
|
||||
allocationId,
|
||||
userId,
|
||||
transferAmount,
|
||||
description,
|
||||
voucher
|
||||
);
|
||||
|
||||
res.json({
|
||||
@@ -324,11 +324,11 @@ router.post('/confirm-allocation/:allocationId', auth, async (req, res) => {
|
||||
data: { transferId }
|
||||
});
|
||||
|
||||
axios.post('http://localhost:5000/ocr',{
|
||||
id: allocationId
|
||||
}).then(res => {
|
||||
console.log(res.data)
|
||||
})
|
||||
// axios.post('http://localhost:5000/ocr',{
|
||||
// id: allocationId
|
||||
// }).then(res => {
|
||||
// console.log(res.data)
|
||||
// })
|
||||
|
||||
} catch (error) {
|
||||
console.error('确认分配失败:', error);
|
||||
|
||||
@@ -141,11 +141,6 @@ router.post('/send', async (req, res) => {
|
||||
// 生产环境发送真实短信
|
||||
try {
|
||||
console.log(code);
|
||||
res.json({
|
||||
success: true,
|
||||
message: '验证码发送成功'
|
||||
})
|
||||
return
|
||||
const sendSmsRequest = new Dysmsapi20170525.SendSmsRequest({
|
||||
phoneNumbers: phone,
|
||||
signName: SMS_CONFIG.signName,
|
||||
|
||||
@@ -12,6 +12,48 @@ const dayjs = require('dayjs');
|
||||
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('/',
|
||||
authenticateToken,
|
||||
validateQuery(transferSchemas.query),
|
||||
@@ -342,7 +384,6 @@ router.post('/confirm-not-received',
|
||||
);
|
||||
|
||||
|
||||
|
||||
// 获取用户转账记录
|
||||
router.get('/user/:userId', authenticateToken, async (req, res) => {
|
||||
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
|
||||
${whereClause}
|
||||
ORDER BY t.created_at
|
||||
DESC
|
||||
LIMIT ${limitNum} OFFSET ${offset}
|
||||
DESC
|
||||
LIMIT ${limitNum}
|
||||
OFFSET ${offset}
|
||||
`, countParams);
|
||||
|
||||
const [countResult] = await db.execute(`
|
||||
@@ -425,37 +467,37 @@ router.get('/stats', authenticateToken, async (req, res) => {
|
||||
if (isAdmin) {
|
||||
// 管理员可以查看全局统计
|
||||
const [totalStats] = await db.execute(`
|
||||
SELECT COUNT(*) as total_transfers,
|
||||
SUM(amount) as total_flow_amount,
|
||||
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 = '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 = '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 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 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,
|
||||
SELECT COUNT(*) as total_transfers,
|
||||
SUM(amount) as total_flow_amount,
|
||||
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 = '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 = '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 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 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 transfer_type = 'initial' AND status = 'confirmed' THEN amount
|
||||
ELSE 0 END) as initial_amount,
|
||||
ELSE 0 END) as initial_amount,
|
||||
SUM(CASE
|
||||
WHEN transfer_type = 'return' AND status = 'confirmed' THEN amount
|
||||
ELSE 0 END) as return_amount,
|
||||
ELSE 0 END) as return_amount,
|
||||
SUM(CASE
|
||||
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)
|
||||
FROM users
|
||||
WHERE role = 'user'
|
||||
AND is_system_account = 1) as total_merchant_balance,
|
||||
AND is_system_account = 1) as total_merchant_balance,
|
||||
(SELECT SUM(balance)
|
||||
FROM users
|
||||
WHERE role = 'user'
|
||||
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 ('agent') THEN amount END) as agent_total,
|
||||
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 ('operated_agent') THEN amount END) as operated_agent_total
|
||||
FROM transfers
|
||||
`);
|
||||
@@ -470,26 +512,26 @@ router.get('/stats', authenticateToken, async (req, res) => {
|
||||
(
|
||||
COALESCE((SELECT SUM(amount)
|
||||
FROM transfers
|
||||
WHERE DATE(created_at) = ?
|
||||
AND to_user_id IN (SELECT id FROM users WHERE is_system_account = 1)
|
||||
AND status = 'received'), 0) -
|
||||
WHERE DATE (created_at) = ?
|
||||
AND to_user_id IN (SELECT id FROM users WHERE is_system_account = 1)
|
||||
AND status = 'received'), 0) -
|
||||
COALESCE((SELECT SUM(amount)
|
||||
FROM transfers
|
||||
WHERE DATE(created_at) = ?
|
||||
AND from_user_id IN (SELECT id FROM users WHERE is_system_account = 1)
|
||||
AND status = 'received'), 0)
|
||||
WHERE DATE (created_at) = ?
|
||||
AND from_user_id IN (SELECT id FROM users WHERE is_system_account = 1)
|
||||
AND status = 'received'), 0)
|
||||
) as today_amount
|
||||
FROM transfers
|
||||
WHERE DATE(created_at) = ?
|
||||
WHERE DATE (created_at) = ?
|
||||
`, [todayStr, todayStr, todayStr]);
|
||||
|
||||
const [monthlyStats] = await db.execute(`
|
||||
SELECT COUNT(*) as monthly_transfers,
|
||||
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
|
||||
SELECT COUNT(*) as monthly_transfers,
|
||||
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
|
||||
FROM transfers
|
||||
WHERE YEAR(created_at) = ?
|
||||
AND MONTH(created_at) = ?
|
||||
WHERE YEAR (created_at) = ?
|
||||
AND MONTH (created_at) = ?
|
||||
`, [currentYear, currentMonth]);
|
||||
|
||||
// 获取上月统计数据用于对比
|
||||
@@ -497,12 +539,12 @@ router.get('/stats', authenticateToken, async (req, res) => {
|
||||
const lastMonthYear = currentMonth === 1 ? currentYear - 1 : currentYear;
|
||||
|
||||
const [lastMonthStats] = await db.execute(`
|
||||
SELECT COUNT(*) as last_monthly_transfers,
|
||||
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
|
||||
SELECT COUNT(*) as last_monthly_transfers,
|
||||
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
|
||||
FROM transfers
|
||||
WHERE YEAR(created_at) = ?
|
||||
AND MONTH(created_at) = ?
|
||||
WHERE YEAR (created_at) = ?
|
||||
AND MONTH (created_at) = ?
|
||||
`, [lastMonthYear, lastMonth]);
|
||||
|
||||
stats = {
|
||||
@@ -524,9 +566,9 @@ router.get('/stats', authenticateToken, async (req, res) => {
|
||||
return_amount: parseFloat(totalStats[0].return_amount || 0),
|
||||
user_to_user_amount: parseFloat(totalStats[0].user_to_user_amount || 0),
|
||||
participated_transfers: totalStats[0].participated_transfers || 0,
|
||||
total_user_balance:totalStats[0].total_user_balance || 0,
|
||||
agent_total:totalStats[0].agent_total || 0,//代理收入
|
||||
operated_agent_total:totalStats[0].operated_agent_total || 0,//直营代理收入
|
||||
total_user_balance: totalStats[0].total_user_balance || 0,
|
||||
agent_total: totalStats[0].agent_total || 0,//代理收入
|
||||
operated_agent_total: totalStats[0].operated_agent_total || 0,//直营代理收入
|
||||
},
|
||||
today: {
|
||||
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
|
||||
FROM transfers
|
||||
WHERE (from_user_id = ? OR to_user_id = ?)
|
||||
AND DATE(created_at) = ?
|
||||
AND DATE (created_at) = ?
|
||||
`, [userId, userId, userId, userId, todayStr]);
|
||||
|
||||
stats = {
|
||||
@@ -706,9 +748,9 @@ router.get('/trend', authenticateToken, async (req, res) => {
|
||||
|
||||
// 首先获取数据库中最早和最晚的转账日期
|
||||
const [dateRange] = await db.execute(`
|
||||
SELECT MIN(DATE(created_at)) as min_date,
|
||||
MAX(DATE(created_at)) as max_date,
|
||||
COUNT(*) as total_count
|
||||
SELECT MIN(DATE (created_at)) as min_date,
|
||||
MAX(DATE (created_at)) as max_date,
|
||||
COUNT(*) as total_count
|
||||
FROM transfers
|
||||
`);
|
||||
|
||||
@@ -737,13 +779,13 @@ router.get('/trend', authenticateToken, async (req, res) => {
|
||||
|
||||
// 获取指定天数内的转账趋势(从最大日期往前推)
|
||||
const [trendData] = await db.execute(`
|
||||
SELECT DATE(created_at) as date,
|
||||
COUNT(*) as count,
|
||||
SUM(amount) as amount
|
||||
SELECT DATE (created_at) as date, COUNT (*) as count, SUM (amount) as amount
|
||||
FROM transfers
|
||||
WHERE DATE(created_at) >= DATE_SUB(?, INTERVAL ? DAY)
|
||||
AND status IN ('confirmed', 'received')
|
||||
GROUP BY DATE(created_at)
|
||||
WHERE DATE (created_at) >= DATE_SUB(?
|
||||
, INTERVAL ? DAY)
|
||||
AND status IN ('confirmed'
|
||||
, 'received')
|
||||
GROUP BY DATE (created_at)
|
||||
ORDER BY date ASC
|
||||
`, [dateRange[0].max_date, daysNum - 1]);
|
||||
|
||||
@@ -1024,7 +1066,8 @@ router.get('/pending-allocations',
|
||||
JOIN matching_orders mo ON oa.id = mo.id
|
||||
${whereClause}
|
||||
ORDER BY oa.${sortField} ${sortOrder}
|
||||
LIMIT ${parseInt(limit)} OFFSET ${parseInt(offset)}
|
||||
LIMIT ${parseInt(limit)}
|
||||
OFFSET ${parseInt(offset)}
|
||||
`;
|
||||
|
||||
const [allocations] = queryParams.length > 0
|
||||
@@ -1140,19 +1183,16 @@ router.get('/daily-stats',
|
||||
|
||||
// 获取所有用户的昨日转出和今日入账统计
|
||||
let [userStats] = await db.execute(`
|
||||
SELECT u.id as user_id,
|
||||
SELECT u.id as user_id,
|
||||
u.username,
|
||||
u.real_name,
|
||||
u.phone,
|
||||
u.balance,
|
||||
COALESCE(yesterday_out.amount, 0) as yesterday_out_amount,
|
||||
COALESCE(today_in.amount, 0) as today_in_amount,
|
||||
COALESCE(confirmed_from.confirmed_amount, 0) as confirmed_from_amount,
|
||||
CASE
|
||||
WHEN (COALESCE(u.balance, 0) + COALESCE(confirmed_from.confirmed_amount, 0)) > ABS(u.balance)
|
||||
THEN ABS(u.balance)
|
||||
ELSE (COALESCE(u.balance, 0) + COALESCE(confirmed_from.confirmed_amount, 0))
|
||||
END as balance_needed
|
||||
COALESCE(yesterday_out.amount, 0) -
|
||||
COALESCE(yesterday_system_num.amount, 0) as yesterday_out_amount,
|
||||
COALESCE(today_in.amount, 0) as today_in_amount,
|
||||
COALESCE(confirmed_from.confirmed_amount, 0) as confirmed_from_amount,
|
||||
COALESCE(u.balance, 0) + COALESCE(confirmed_from.confirmed_amount, 0) as balance_needed
|
||||
FROM users u
|
||||
LEFT JOIN (SELECT from_user_id,
|
||||
SUM(amount) as amount
|
||||
@@ -1161,6 +1201,15 @@ router.get('/daily-stats',
|
||||
AND created_at <= ?
|
||||
AND status IN ('confirmed', 'received')
|
||||
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,
|
||||
SUM(amount) as amount
|
||||
FROM transfers
|
||||
@@ -1171,17 +1220,17 @@ router.get('/daily-stats',
|
||||
left join (select from_user_id,
|
||||
sum(amount) as confirmed_amount
|
||||
from transfers
|
||||
where status = 'received'
|
||||
and created_at >= ?
|
||||
and created_at <= ?
|
||||
group by from_user_id) as confirmed_from on u.id = confirmed_from.from_user_id
|
||||
WHERE u.role != 'admin'
|
||||
WHERE status IN ('confirmed', 'received')
|
||||
AND created_at >= ?
|
||||
AND created_at <= ?
|
||||
GROUP BY from_user_id) as confirmed_from on u.id = confirmed_from.from_user_id
|
||||
WHERE u.role != 'admin' AND u.user_type !='directly_operated'
|
||||
AND u.is_system_account != 1
|
||||
AND yesterday_out.amount > 0
|
||||
AND u.balance < 0
|
||||
ORDER BY balance_needed DESC, yesterday_out_amount DESC
|
||||
`, [yesterdayStartStr, yesterdayEndStr, todayStartStr, todayEndStr, todayStartStr, todayEndStr]);
|
||||
// userStats = userStats.filter(item=>item.balance_needed >= 100)
|
||||
`, [yesterdayStartStr, yesterdayEndStr, yesterdayStartStr, yesterdayEndStr,todayStartStr, todayEndStr, todayStartStr, todayEndStr]);
|
||||
// userStats = userStats.filter(item=>item.balance_needed > 100)
|
||||
userStats.forEach(item => {
|
||||
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) => {
|
||||
upload.single('file')(req, res, async (err) => {
|
||||
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 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']
|
||||
);
|
||||
|
||||
|
||||
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') {
|
||||
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 limitNum = parseInt(limit, 10) || 10;
|
||||
const offset = (pageNum - 1) * limitNum;
|
||||
@@ -283,7 +283,7 @@ class TransferService {
|
||||
|
||||
// 构建排序子句
|
||||
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 orderClause = `ORDER BY t.${sortField} ${sortOrder}`;
|
||||
@@ -304,6 +304,8 @@ class TransferService {
|
||||
`SELECT t.*,
|
||||
fu.username as from_username,
|
||||
fu.real_name as from_real_name,
|
||||
fu.balance as from_balance,
|
||||
tu.balance as to_balance,
|
||||
tu.username as to_username,
|
||||
tu.real_name as to_real_name,
|
||||
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_d ON t_d.code = tu.district_id
|
||||
${whereClause} ${orderClause}
|
||||
LIMIT ${limitNum}`,
|
||||
LIMIT ${limitNum} OFFSET ${offset}`,
|
||||
params
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user