| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-02-15 20:59:55 |
| 4 | * @LastEditTime: 2025-04-27 14:07:03 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: 财务 |
| 7 | */ |
| 8 | import { |
| 9 | Body, |
| 10 | Controller, |
| 11 | Delete, |
| 12 | Get, |
| 13 | Param, |
| 14 | Post, |
| 15 | Query, |
| 16 | } from '@nestjs/common'; |
| 17 | import { ApiOperation, ApiTags } from '@nestjs/swagger'; |
| 18 | import { ApiResult } from '../../common/decorators/api-result.decorator'; |
| 19 | import { FinanceService } from './finance.service'; |
| 20 | import { GetToken, Public } from '../../auth/auth.guard'; |
| 21 | import { TokenInfo } from 'src/auth/interfaces/auth.interfaces'; |
| 22 | import { CreateUserWalletAccountDto } from './dto/userWalletAccount.dto'; |
| 23 | import { UserWalletAccount } from 'src/db/schema/userWalletAccount.shema'; |
| 24 | import { |
| 25 | CreateUserWalletRecordDto, |
| 26 | GetUserWalletRecordListDto, |
| 27 | } from './dto/userWalletRecord.dto'; |
| 28 | import { |
| 29 | UserWalletRecordStatus, |
| 30 | UserWalletRecordType, |
| 31 | } from 'src/db/schema/userWalletRecord.shema'; |
| 32 | import { UserWallet } from 'src/db/schema/userWallet.shema'; |
| 33 | import { ObjectId } from 'mongodb'; |
| 34 | import { AppHttpException } from 'src/filters/http-exception.filter'; |
| 35 | import { ErrHttpBack } from 'src/filters/http-exception.back-code'; |
| 36 | import { RealAuthService } from '../tools/realAuth.service'; |
| 37 | import { SceneType } from 'src/db/schema/realAuth.schema'; |
| 38 | |
| 39 | @ApiTags('finance - 财务') |
| 40 | @Controller('finance') |
| 41 | export class FinanceController { |
| 42 | constructor( |
| 43 | private readonly financeService: FinanceService, |
| 44 | private readonly realAuthService: RealAuthService, |
| 45 | ) {} |
| 46 | |
| 47 | // --------- userWalletAccount STR --------- |
| 48 | @ApiOperation({ summary: '发送创建账户的短信码' }) |
| 49 | @ApiResult({ type: String }) |
| 50 | @Public() |
| 51 | @Post('userWalletAccount/phoneCode/:phone') |
| 52 | async postCreateUserWalletAccountCode(@Param('phone') phone: string) { |
| 53 | return await this.financeService.postCreateUserWalletAccountCode(phone); |
| 54 | } |
| 55 | |
| 56 | @ApiOperation({ summary: '创建用户钱包账户' }) |
| 57 | @Post('userWalletAccount') |
| 58 | @ApiResult({ type: UserWalletAccount }) |
| 59 | async createUserWalletAccount( |
| 60 | @GetToken() token: TokenInfo, |
| 61 | @Body() body: CreateUserWalletAccountDto, |
| 62 | ) { |
| 63 | // 验证身份证 |
| 64 | const res = await this.realAuthService.realNameAuth( |
| 65 | token.id, |
| 66 | body.cardNum, |
| 67 | body.userName, |
| 68 | SceneType.UserWallet, |
| 69 | ); |
| 70 | if (!res) |
| 71 | throw new AppHttpException(ErrHttpBack.err_approve_idcard_invalid); |
| 72 | |
| 73 | return await this.financeService.createUserWalletAccount(token.id, body); |
| 74 | } |
| 75 | |
| 76 | @ApiOperation({ summary: '获取用户钱包账户列表' }) |
| 77 | @Get('userWalletAccount/list') |
| 78 | @ApiResult({ type: [UserWalletAccount] }) |
| 79 | async getUserWalletAccountList(@GetToken() token: TokenInfo) { |
| 80 | return this.financeService.getUserWalletAccountList(token.id); |
| 81 | } |
| 82 | |
| 83 | @ApiOperation({ summary: '删除用户钱包账户' }) |
| 84 | @Delete('userWalletAccount/delete/:id') |
| 85 | @ApiResult({ type: String }) |
| 86 | async deleteUserWalletAccount( |
| 87 | @GetToken() token: TokenInfo, |
| 88 | @Param('id') id: string, |
| 89 | ) { |
| 90 | return await this.financeService.deleteUserWalletAccount(token.id, id); |
| 91 | } |
| 92 | // --------- userWalletAccount END --------- |
| 93 | |
| 94 | // --------- userWalletRecord STR --------- |
| 95 | @ApiOperation({ summary: '创建用户提现记录-提交提现' }) |
| 96 | @Post('userWalletRecord') |
| 97 | @ApiResult({ type: UserWalletAccount }) |
| 98 | async addUserWalletRecord( |
| 99 | @GetToken() token: TokenInfo, |
| 100 | @Body() body: CreateUserWalletRecordDto, |
| 101 | ) { |
| 102 | const { walletAccountId, balance } = body; |
| 103 | |
| 104 | const walletAccount = |
| 105 | await this.financeService.getUserWalletAccountById(walletAccountId); |
| 106 | |
| 107 | if (!walletAccount) |
| 108 | throw new AppHttpException(ErrHttpBack.wallet_account_no_had); |
| 109 | |
| 110 | // 获取余额 |
| 111 | const userWallet = await this.financeService.getUserWalletByUserId( |
| 112 | new ObjectId(token.id), |
| 113 | ); |
| 114 | |
| 115 | // 获取待提现金额 |
| 116 | const waitCount = await this.financeService.getDoingWalletRecordCount( |
| 117 | token.id, |
| 118 | ); |
| 119 | |
| 120 | // 计算可用余额 |
| 121 | const availableBalance = |
| 122 | parseFloat(userWallet.balance.toString()) - waitCount; |
| 123 | |
| 124 | if (!userWallet || availableBalance < balance) |
| 125 | throw new AppHttpException(ErrHttpBack.wallet_balance_no_enough); |
| 126 | |
| 127 | const res = await this.financeService.createUserWalletRecord( |
| 128 | token.id, |
| 129 | walletAccount, |
| 130 | { |
| 131 | type: UserWalletRecordType.WITHDRAW, |
| 132 | balance, |
| 133 | status: UserWalletRecordStatus.WAIT, |
| 134 | }, |
| 135 | ); |
| 136 | |
| 137 | return res; |
| 138 | } |
| 139 | |
| 140 | @ApiOperation({ summary: '获取用户账户记录列表' }) |
| 141 | @Get('userWalletRecord/list') |
| 142 | @ApiResult({ type: [UserWalletAccount] }) |
| 143 | async getUserWalletRecordList( |
| 144 | @GetToken() token: TokenInfo, |
| 145 | @Query() query: GetUserWalletRecordListDto, |
| 146 | ) { |
| 147 | return this.financeService.getUserWalletRecordList(token.id, query); |
| 148 | } |
| 149 | |
| 150 | @ApiOperation({ summary: '获取用户钱包信息' }) |
| 151 | @Get('userWallet/info') |
| 152 | @ApiResult({ type: UserWallet }) |
| 153 | async getUserWalletInfo(@GetToken() token: TokenInfo) { |
| 154 | return this.financeService.getUserWalletByUserId(new ObjectId(token.id)); |
| 155 | } |
| 156 | // --------- userWalletRecord END --------- |
| 157 | } |
| 158 |