升级商城逻辑
This commit is contained in:
@@ -695,6 +695,7 @@ class MatchingService {
|
||||
u.balance as current_balance
|
||||
FROM users u
|
||||
WHERE u.is_system_account = FALSE
|
||||
AND u.is_distribute = TRUE
|
||||
AND u.id != ?
|
||||
AND u.balance < -100
|
||||
AND u.audit_status = 'approved'
|
||||
@@ -715,8 +716,7 @@ class MatchingService {
|
||||
// 查询用户的分配订单金额统计
|
||||
const [orderStatusResult] = await db.execute(
|
||||
`SELECT
|
||||
SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) as pending_amount,
|
||||
SUM(CASE WHEN status = 'processing' THEN amount ELSE 0 END) as processing_amount
|
||||
SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) as pending_amount
|
||||
FROM transfers
|
||||
WHERE to_user_id = ?`,
|
||||
[user.user_id]
|
||||
@@ -730,6 +730,14 @@ class MatchingService {
|
||||
WHERE to_user_id = ?`,
|
||||
[user.user_id]
|
||||
);
|
||||
//查询用户给其他用户已确认的金额统计(要减去,因为款项还没回来)
|
||||
const [orderStatusConfirmedResultFrom] = await db.execute(
|
||||
`SELECT
|
||||
SUM(CASE WHEN status = 'confirmed' THEN amount ELSE 0 END) as confirmed_amount
|
||||
FROM transfers
|
||||
WHERE from_user_id = ?`,
|
||||
[user.user_id]
|
||||
);
|
||||
// 查询用户当天在matching_orders表中打出去的款项
|
||||
const today = getLocalDateString();
|
||||
const [todayOutflowResult] = await db.execute(
|
||||
@@ -741,22 +749,23 @@ class MatchingService {
|
||||
);
|
||||
|
||||
// 添加分配金额信息到用户对象
|
||||
const orderStatus = orderStatusResult[0] || { pending_amount: 0, processing_amount: 0 };
|
||||
const orderStatus = orderStatusResult[0] || { pending_amount: 0 };
|
||||
const todayOutflow = todayOutflowResult[0] || { today_outflow: 0 };
|
||||
const orderStatusConfirmedFrom = orderStatusConfirmedResultFrom[0] || { confirmed_amount: 0 };
|
||||
const orderStatusConfirmed = orderStatusConfirmedResult[0] || { confirmed_amount: 0 };
|
||||
user.today_outflow = parseFloat(todayOutflow.today_outflow) || 0;
|
||||
user.pending_amount = parseFloat(orderStatus.pending_amount) || 0;
|
||||
user.processing_amount = parseFloat(orderStatus.processing_amount) || 0;
|
||||
user.confirmed_amount = parseFloat(orderStatusConfirmed.confirmed_amount) || 0;
|
||||
user.has_active_allocations = user.current_balance + user.pending_amount + user.processing_amount + user.confirmed_amount + user.today_outflow;
|
||||
user.has_active_allocations = user.current_balance + user.pending_amount + user.confirmed_amount + user.today_outflow - orderStatusConfirmedFrom.confirmed_amount;
|
||||
|
||||
|
||||
|
||||
// 所有查询到的用户都是负余额用户,直接添加到可用列表
|
||||
}
|
||||
userBalanceResult = userBalanceResult.filter(user => user.has_active_allocations < -100);
|
||||
userBalanceResult = userBalanceResult.sort((a, b) => a.has_active_allocations - b.has_active_allocations);
|
||||
for (const user of userBalanceResult) {
|
||||
if (user.has_active_allocations < -100 && maxTransfers > availableUsers.length + 1) {
|
||||
if ( maxTransfers > availableUsers.length + 1) {
|
||||
if (minTransfers === 3 && availableUsers.length < 3) {
|
||||
availableUsers.push(user);
|
||||
}
|
||||
@@ -851,7 +860,107 @@ class MatchingService {
|
||||
// 如果还有剩余金额且分配数量不足最小笔数,最后分配给虚拟用户
|
||||
const availableVirtualUsers = virtualUsersResult
|
||||
|
||||
// 如果需要分配给虚拟用户,使用随机分配算法
|
||||
// 如果有剩余金额,优先检查现有非虚拟用户是否还能消化
|
||||
if (remainingAmount > 0) {
|
||||
// 筛选出非虚拟用户分配记录
|
||||
|
||||
if (allocations.length > 0) {
|
||||
let totalAvailableCapacity = 0;
|
||||
const userCapacities = [];
|
||||
|
||||
// 计算每个用户的剩余可分配容量
|
||||
for (const allocation of allocations) {
|
||||
// 获取用户当前的实际余额状态(使用has_active_allocations作为实际可分配余额)
|
||||
const maxSafeAmount = Math.abs(allocation.availableForAllocation);
|
||||
const remainingCapacity = maxSafeAmount - allocation.amount;
|
||||
|
||||
if (remainingCapacity > 0) {
|
||||
userCapacities.push({
|
||||
allocation,
|
||||
capacity: remainingCapacity
|
||||
});
|
||||
totalAvailableCapacity += remainingCapacity;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`现有用户剩余容量: ${totalAvailableCapacity}, 待分配金额: ${remainingAmount}`);
|
||||
|
||||
// 如果现有用户能够消化剩余金额
|
||||
if (totalAvailableCapacity >= remainingAmount && userCapacities.length > 0) {
|
||||
// 按平均分配给这些用户,但需要检查每个用户的分配上限
|
||||
const averageAmount = Math.floor(remainingAmount / userCapacities.length);
|
||||
let distributedAmount = 0;
|
||||
let remainingToDistribute = remainingAmount;
|
||||
|
||||
for (let i = 0; i < userCapacities.length; i++) {
|
||||
const { allocation, capacity } = userCapacities[i];
|
||||
|
||||
// 计算本次可分配的金额
|
||||
let amountToAdd = 0;
|
||||
|
||||
if (i === userCapacities.length - 1) {
|
||||
// 最后一个用户分配剩余的所有金额,但不能超过其容量
|
||||
amountToAdd = Math.min(remainingToDistribute, capacity);
|
||||
} else {
|
||||
// 其他用户按平均分配,但不能超过其容量
|
||||
amountToAdd = Math.min(averageAmount, capacity);
|
||||
}
|
||||
|
||||
if (amountToAdd > 0) {
|
||||
allocation.amount += amountToAdd;
|
||||
distributedAmount += amountToAdd;
|
||||
remainingToDistribute -= amountToAdd;
|
||||
console.log(`为用户${allocation.userId}追加分配${amountToAdd}元,总分配${allocation.amount}元,剩余容量${capacity - amountToAdd}元`);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新实际分配的剩余金额
|
||||
remainingAmount = remainingToDistribute;
|
||||
|
||||
if (remainingAmount === 0) {
|
||||
console.log('剩余金额已全部分配给现有用户');
|
||||
} else {
|
||||
console.log(`部分剩余金额已分配给现有用户,仍有${remainingAmount}元未分配`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果仍有剩余金额,检查是否有未分配的用户可以消化剩余金额
|
||||
if (remainingAmount > 0) {
|
||||
// 获取已分配的用户ID列表
|
||||
const allocatedUserIds = new Set(allocations.map(a => a.userId));
|
||||
|
||||
// 从原始用户列表中找到未分配的用户
|
||||
const unallocatedUsers = priorityUsers.filter(user => !allocatedUserIds.has(user.user_id));
|
||||
|
||||
if (unallocatedUsers.length > 0) {
|
||||
console.log(`发现${unallocatedUsers.length}个未分配的用户,剩余金额: ${remainingAmount}`);
|
||||
|
||||
// 查找可分配金额大于剩余金额的用户
|
||||
for (const user of unallocatedUsers) {
|
||||
const maxSafeAmount = Math.abs(user.has_active_allocations);
|
||||
|
||||
if (maxSafeAmount >= remainingAmount) {
|
||||
// 找到合适的用户,分配剩余金额
|
||||
allocations.push({
|
||||
userId: user.user_id,
|
||||
username: user.username || `User${user.user_id}`,
|
||||
amount: remainingAmount,
|
||||
userType: 'priority_user',
|
||||
currentBalance: user.current_balance,
|
||||
availableForAllocation: user.has_active_allocations
|
||||
});
|
||||
|
||||
console.log(`为未分配用户${user.user_id}分配剩余金额${remainingAmount}元`);
|
||||
remainingAmount = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果仍有剩余金额,分配给虚拟用户
|
||||
if (remainingAmount > 0 && availableVirtualUsers.length > 0) {
|
||||
const maxPossibleTransfers = Math.min((minTransfers - allocations.length) <= 0 ? 1 : minTransfers - allocations.length, availableVirtualUsers.length);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user