xiongzhu 2 лет назад
Родитель
Сommit
787898f564

+ 1 - 1
src/commission/commission.service.ts

@@ -5,7 +5,7 @@ import { Repository } from 'typeorm'
 import { CommissionRecord } from './entities/commission-record.entity'
 import { Users } from '../users/entities/users.entity'
 import { UserBalanceService } from '../user-balance/user-balance.service'
-import { Type as BalanceType } from 'src/user-balance/entities/balance-record.entity'
+import { BalanceType as BalanceType } from 'src/user-balance/entities/balance-record.entity'
 
 @Injectable()
 export class CommissionService {

+ 11 - 0
src/user-balance/dto/withdraw-apply.dto.ts

@@ -0,0 +1,11 @@
+import { IsNumber, Min } from 'class-validator'
+
+export class WithdrawApplyDto {
+    @IsNumber()
+    @Min(1)
+    amount: number
+
+    remark?: string
+
+    extra?: string
+}

+ 3 - 3
src/user-balance/entities/balance-record.entity.ts

@@ -1,7 +1,7 @@
 import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, ValueTransformer } from 'typeorm'
 import { Big } from 'big.js'
 
-export enum Type {
+export enum BalanceType {
     COMMISSION = 'COMMISSION',
     WITHDRAW = 'WITHDRAW'
 }
@@ -23,8 +23,8 @@ export class BalanceRecord {
     @Column({ type: 'decimal', precision: 19, scale: 2 })
     lastBalance: number
 
-    @Column({ type: 'enum', enum: Type })
-    type: Type
+    @Column({ type: 'enum', enum: BalanceType })
+    type: BalanceType
 
     @Column({ nullable: true })
     remark: string

+ 27 - 0
src/user-balance/entities/withdraw.entity.ts

@@ -0,0 +1,27 @@
+import { Column, CreateDateColumn, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'
+
+export enum WithdrawStatus {
+    PENDING = 'PENDING',
+    SUCCESS = 'SUCCESS',
+    FAILED = 'FAILED'
+}
+
+export class Withdraw {
+    @PrimaryGeneratedColumn()
+    id: number
+
+    @Column()
+    userId: number
+
+    @Column({ type: 'decimal', precision: 19, scale: 2 })
+    amount: number
+
+    @Column({ type: 'enum', enum: WithdrawStatus })
+    status: WithdrawStatus
+
+    @CreateDateColumn()
+    createdAt: Date
+
+    @UpdateDateColumn()
+    updatedAt: Date
+}

+ 8 - 2
src/user-balance/user-balance.controller.ts

@@ -1,6 +1,7 @@
-import { Controller, Get, Post, Req } from '@nestjs/common'
+import { Body, Controller, Get, Post, Req } from '@nestjs/common'
 import { UserBalanceService } from './user-balance.service'
 import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
+import { WithdrawApplyDto } from './dto/withdraw-apply.dto'
 
 @ApiTags('userBalance')
 @ApiBearerAuth()
@@ -12,9 +13,14 @@ export class UserBalanceController {
     async getBalance(@Req() req) {
         return await this.userBalanceService.getBalance(req.user.id)
     }
- 
+
     @Get('/records/get')
     async getRecords(@Req() req) {
         return await this.userBalanceService.getRecords(req.user.id)
     }
+
+    @Post('/withdraw')
+    async withdraw(@Req() req, @Body() body: WithdrawApplyDto) {
+        return await this.userBalanceService.applyWithdraw(req.user.id, body.amount, body.remark, body.extra)
+    }
 }

+ 11 - 4
src/user-balance/user-balance.service.ts

@@ -1,8 +1,8 @@
-import { Injectable } from '@nestjs/common'
+import { Injectable, InternalServerErrorException } from '@nestjs/common'
 import { InjectRepository } from '@nestjs/typeorm'
 import { UserBalance } from './entities/user-balance.entity'
 import { Repository } from 'typeorm'
-import { BalanceRecord, Type } from './entities/balance-record.entity'
+import { BalanceRecord, BalanceType } from './entities/balance-record.entity'
 
 @Injectable()
 export class UserBalanceService {
@@ -26,8 +26,11 @@ export class UserBalanceService {
         return userBalance
     }
 
-    async modifyBalance(userId: number, amount: number, type: Type, remark?: string, extra?: string) {
+    async modifyBalance(userId: number, amount: number, type: BalanceType, remark?: string, extra?: string) {
         const userBalance = await this.getBalance(userId)
+        if (userBalance.balance + amount < 0) {
+            throw new Error('余额不足')
+        }
         await this.balanceRecordRepository.save(
             new BalanceRecord({
                 userId,
@@ -44,7 +47,7 @@ export class UserBalanceService {
         await this.userBalanceRepository.save(userBalance)
     }
 
-    async getRecords(userId: number, ) {
+    async getRecords(userId: number) {
         return await this.balanceRecordRepository.find({
             where: {
                 userId
@@ -54,4 +57,8 @@ export class UserBalanceService {
             }
         })
     }
+
+    async applyWithdraw(userId: number, amount: number, remark?: string, extra?: string) {
+        return await this.modifyBalance(userId, -amount, BalanceType.WITHDRAW, remark, extra)
+    }
 }

+ 5 - 5
src/users/users.controller.ts

@@ -1,23 +1,18 @@
 import {
     Controller,
-    Put,
     Get,
     Body,
     Param,
     HttpStatus,
     NotFoundException,
-    Delete,
     BadRequestException,
     Req,
     Post
 } from '@nestjs/common'
 import { UsersService } from './users.service'
 import { UserProfileDto } from './dto/user-profile.dto'
-import { UserUpdateDto } from './dto/user-update.dto'
 import { IUsers } from './interfaces/users.interface'
 import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
-import { HasRoles } from '../auth/roles.decorator'
-import { Role } from '../model/role.enum'
 
 @ApiTags('users')
 @Controller('users')
@@ -62,4 +57,9 @@ export class UsersController {
             status: HttpStatus.OK
         }
     }
+
+    @Get('/invites')
+    public async getInvites(@Req() req): Promise<any> {
+        return await this.usersService.invites(req.user.userId)
+    }
 }

+ 8 - 0
src/users/users.service.ts

@@ -139,4 +139,12 @@ export class UsersService {
         const user = await this.findById(id)
         await this.userRepository.remove(user)
     }
+
+    public async invites(userId: number) {
+        return await this.userRepository.find({
+            where: {
+                invitor: userId
+            }
+        })
+    }
 }