wuyi 2 сар өмнө
parent
commit
b11fe5d59b

+ 16 - 3
src/services/payment.service.ts

@@ -200,11 +200,9 @@ export class PaymentService {
           } catch (teamError) {
             this.app.log.warn('Failed to find team for user:', user.parentId, teamError)
             agentId = 0
-            incomeAmount = new Decimal(0)
           }
         } else {
           agentId = 0
-          incomeAmount = new Decimal(0)
         }
 
         await this.incomeRecordsService.create({
@@ -271,8 +269,23 @@ export class PaymentService {
       // 创建收入记录
       try {
         // 计算佣金
-        let agentId = 0
+        let agentId = user.parentId
         let incomeAmount = new Decimal(price)
+        if (user.parentId && user.parentId > 0) {
+          try {
+            const team = await this.teamService.findByUserId(user.parentId)
+            if (team && team.commissionRate && team.commissionRate > 0) {
+              const commissionRate = new Decimal(team.commissionRate)
+              const hundred = new Decimal(100)
+              incomeAmount = new Decimal(price).mul(commissionRate).div(hundred)
+            }
+          } catch (teamError) {
+            this.app.log.warn('Failed to find team for user:', user.parentId, teamError)
+            agentId = 0
+          }
+        } else {
+          agentId = 0
+        }
 
         await this.incomeRecordsService.create({
           agentId,