|
|
@@ -1,8 +1,8 @@
|
|
|
-import { Injectable, Logger } from '@nestjs/common'
|
|
|
+import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common'
|
|
|
import { Between, In, Repository } from 'typeorm'
|
|
|
-import { Balance } from './entities/balance.entities'
|
|
|
+import { Balance } from './entities/balance.entity'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
-import { BalanceRecord, BalanceType } from './entities/balance-record.entities'
|
|
|
+import { BalanceRecord, BalanceType } from './entities/balance-record.entity'
|
|
|
import { UsersService } from '../users/users.service'
|
|
|
import { Users } from '../users/entities/users.entity'
|
|
|
import { PageRequest } from '../common/dto/page-request'
|
|
|
@@ -29,22 +29,18 @@ export class BalanceService {
|
|
|
private readonly usersService: UsersService,
|
|
|
private readonly sysConfigService: SysConfigService,
|
|
|
private readonly operationLogService: OperationLogService
|
|
|
- ) {
|
|
|
- }
|
|
|
+ ) {}
|
|
|
|
|
|
async findBalanceByUserId(userId: number): Promise<Balance> {
|
|
|
return await this.balanceRepository.findOne({
|
|
|
- select: {
|
|
|
- userId: true,
|
|
|
- currentBalance: true
|
|
|
- },
|
|
|
+ select: ['userId', 'currentBalance'],
|
|
|
where: {
|
|
|
userId: userId
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- async rechargeBalance(supervisorId: number, userId: number, amount: number) {
|
|
|
+ async rechargeBalance(supervisorId: number, userId: number, amount: Decimal) {
|
|
|
try {
|
|
|
// 获取余额
|
|
|
let balance = await this.balanceRepository.findOne({
|
|
|
@@ -57,18 +53,14 @@ export class BalanceService {
|
|
|
if (!balance) {
|
|
|
balance = new Balance()
|
|
|
balance.userId = userId
|
|
|
- balance.currentBalance = 0.0
|
|
|
- balance.totalBalance = 0.0
|
|
|
+ balance.currentBalance = new Decimal(0)
|
|
|
+ balance.totalBalance = new Decimal(0)
|
|
|
}
|
|
|
|
|
|
- const currentBalanceDecimal = new Decimal(String(balance.currentBalance))
|
|
|
- const amountDecimal = new Decimal(amount)
|
|
|
const totalBalanceDecimal = new Decimal(String(balance.totalBalance))
|
|
|
- const newBalanceDecimal = currentBalanceDecimal.plus(amountDecimal)
|
|
|
- const newTotalBalanceDecimal = totalBalanceDecimal.plus(amountDecimal)
|
|
|
|
|
|
- balance.currentBalance = newBalanceDecimal.toNumber()
|
|
|
- balance.totalBalance = newTotalBalanceDecimal.toNumber()
|
|
|
+ balance.currentBalance = balance.currentBalance.plus(amount)
|
|
|
+ balance.totalBalance = totalBalanceDecimal.plus(amount)
|
|
|
|
|
|
await this.balanceRepository.save(balance)
|
|
|
|
|
|
@@ -83,13 +75,11 @@ export class BalanceService {
|
|
|
|
|
|
// user
|
|
|
const users = await this.usersService.findById(userId)
|
|
|
- if (amount > 0) {
|
|
|
+ if (amount.comparedTo(new Decimal(0)) > 0) {
|
|
|
// 筛号数量比 5
|
|
|
const ratio = await this.sysConfigService.getNumber('screen_number_ratio', 1)
|
|
|
- const newScreenBalance = new Decimal(String(users.screenBalance)).plus(
|
|
|
- amountDecimal.mul(new Decimal(ratio))
|
|
|
- )
|
|
|
- users.screenBalance = newScreenBalance.toNumber()
|
|
|
+ const newScreenBalance = new Decimal(String(users.screenBalance)).plus(amount.mul(new Decimal(ratio)))
|
|
|
+ users.screenBalance = newScreenBalance
|
|
|
}
|
|
|
users.balance = balance.currentBalance
|
|
|
await this.userRepository.save(users)
|
|
|
@@ -101,108 +91,90 @@ export class BalanceService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async transferBalance(userId: number, targetUserId: number, amount: number) {
|
|
|
- try {
|
|
|
- // 获取余额
|
|
|
- let balance = await this.balanceRepository.findOne({
|
|
|
- where: {
|
|
|
- userId: userId
|
|
|
- }
|
|
|
- })
|
|
|
- // 获取目标用户余额
|
|
|
- let targetBalance = await this.balanceRepository.findOne({
|
|
|
- where: {
|
|
|
- userId: targetUserId
|
|
|
- }
|
|
|
- })
|
|
|
- // 如果余额不存在
|
|
|
- if (!targetBalance) {
|
|
|
- targetBalance = new Balance()
|
|
|
- targetBalance.userId = targetUserId
|
|
|
- targetBalance.currentBalance = 0.0
|
|
|
- targetBalance.totalBalance = 0.0
|
|
|
+ async transferBalance(userId: number, targetUserId: number, amount: Decimal) {
|
|
|
+ if (amount.comparedTo(new Decimal(0)) <= 0) {
|
|
|
+ throw new InternalServerErrorException('划转金额必须大于0')
|
|
|
+ }
|
|
|
+ // 获取余额
|
|
|
+ let balance = await this.balanceRepository.findOne({
|
|
|
+ where: {
|
|
|
+ userId: userId
|
|
|
}
|
|
|
- // 将余额划转目标用户
|
|
|
- if (balance.currentBalance >= amount) {
|
|
|
- const amountDecimal = new Decimal(amount)
|
|
|
-
|
|
|
- const balanceDecimal = new Decimal(String(balance.currentBalance))
|
|
|
- const newBalanceDecimal = balanceDecimal.minus(amountDecimal)
|
|
|
- balance.currentBalance = newBalanceDecimal.toNumber()
|
|
|
-
|
|
|
- const targetBalanceDecimal = new Decimal(String(targetBalance.currentBalance))
|
|
|
- const targetTotalBalanceDecimal = new Decimal(String(targetBalance.totalBalance))
|
|
|
- const newTargetBalanceDecimal = targetBalanceDecimal.plus(amountDecimal)
|
|
|
- const newTargetTotalBalanceDecimal = targetTotalBalanceDecimal.plus(amountDecimal)
|
|
|
- targetBalance.currentBalance = newTargetBalanceDecimal.toNumber()
|
|
|
- targetBalance.totalBalance = newTargetTotalBalanceDecimal.toNumber()
|
|
|
-
|
|
|
- await this.balanceRepository.save(balance)
|
|
|
- await this.balanceRepository.save(targetBalance)
|
|
|
-
|
|
|
- // 划转记录
|
|
|
- const record = new BalanceRecord()
|
|
|
- record.userId = userId
|
|
|
- record.targetId = targetUserId
|
|
|
- record.balanceId = balance.id
|
|
|
- record.amount = amount
|
|
|
- record.type = BalanceType.TRANSFER
|
|
|
- await this.recordRepository.save(record)
|
|
|
-
|
|
|
- const tarRecord = new BalanceRecord()
|
|
|
- tarRecord.userId = targetUserId
|
|
|
- tarRecord.supervisorId = userId
|
|
|
- tarRecord.balanceId = targetBalance.id
|
|
|
- tarRecord.amount = amount
|
|
|
- tarRecord.type = BalanceType.RECHARGE
|
|
|
- await this.recordRepository.save(tarRecord)
|
|
|
-
|
|
|
- const users = await this.usersService.findById(userId)
|
|
|
- users.balance = balance.currentBalance
|
|
|
- await this.userRepository.save(users)
|
|
|
-
|
|
|
- const targetUsers = await this.usersService.findById(targetUserId)
|
|
|
- if (amount > 0) {
|
|
|
- // 筛号数量比 5
|
|
|
- const ratio = await this.sysConfigService.getNumber('screen_number_ratio', 1)
|
|
|
- const newScreenBalance = new Decimal(String(targetUsers.screenBalance)).plus(
|
|
|
- amountDecimal.mul(new Decimal(ratio))
|
|
|
- )
|
|
|
- targetUsers.screenBalance = newScreenBalance.toNumber()
|
|
|
- }
|
|
|
- targetUsers.balance = targetBalance.currentBalance
|
|
|
- await this.userRepository.save(targetUsers)
|
|
|
- return record
|
|
|
- } else {
|
|
|
- return null
|
|
|
+ })
|
|
|
+ // 获取目标用户余额
|
|
|
+ let targetBalance = await this.balanceRepository.findOne({
|
|
|
+ where: {
|
|
|
+ userId: targetUserId
|
|
|
}
|
|
|
- } catch (e) {
|
|
|
- Logger.error('Error transfer ', e, 'BalanceService')
|
|
|
- throw e
|
|
|
+ })
|
|
|
+ // 如果余额不存在
|
|
|
+ if (!targetBalance) {
|
|
|
+ targetBalance = new Balance()
|
|
|
+ targetBalance.userId = targetUserId
|
|
|
+ targetBalance.currentBalance = new Decimal(0)
|
|
|
+ targetBalance.totalBalance = new Decimal(0)
|
|
|
+ }
|
|
|
+ // 将余额划转目标用户
|
|
|
+ if (balance.currentBalance.comparedTo(amount) < 0) {
|
|
|
+ throw new InternalServerErrorException('余额不足')
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- async screenRecharge(userId: number, amount: number) {
|
|
|
+ balance.currentBalance = balance.currentBalance.minus(amount)
|
|
|
+ targetBalance.currentBalance = targetBalance.currentBalance.plus(amount)
|
|
|
+ targetBalance.totalBalance = targetBalance.totalBalance.plus(amount)
|
|
|
+
|
|
|
+ await this.balanceRepository.save(balance)
|
|
|
+ await this.balanceRepository.save(targetBalance)
|
|
|
+
|
|
|
+ // 划转记录
|
|
|
+ const record = new BalanceRecord()
|
|
|
+ record.userId = userId
|
|
|
+ record.targetId = targetUserId
|
|
|
+ record.balanceId = balance.id
|
|
|
+ record.amount = amount
|
|
|
+ record.type = BalanceType.TRANSFER
|
|
|
+ await this.recordRepository.save(record)
|
|
|
+
|
|
|
+ const tarRecord = new BalanceRecord()
|
|
|
+ tarRecord.userId = targetUserId
|
|
|
+ tarRecord.supervisorId = userId
|
|
|
+ tarRecord.balanceId = targetBalance.id
|
|
|
+ tarRecord.amount = amount
|
|
|
+ tarRecord.type = BalanceType.RECHARGE
|
|
|
+ await this.recordRepository.save(tarRecord)
|
|
|
+
|
|
|
const users = await this.usersService.findById(userId)
|
|
|
- const screenBalance = new Decimal(users.screenBalance)
|
|
|
- users.screenBalance = screenBalance.plus(amount).toNumber()
|
|
|
+ users.balance = balance.currentBalance
|
|
|
+ await this.userRepository.save(users)
|
|
|
|
|
|
+ const targetUsers = await this.usersService.findById(targetUserId)
|
|
|
+ if (amount.comparedTo(new Decimal(0)) > 0) {
|
|
|
+ // 筛号数量比 5
|
|
|
+ const ratio = await this.sysConfigService.getNumber('screen_number_ratio', 1)
|
|
|
+ targetUsers.screenBalance = targetUsers.screenBalance.plus(amount.mul(new Decimal(ratio)))
|
|
|
+ }
|
|
|
+ targetUsers.balance = targetBalance.currentBalance
|
|
|
+ await this.userRepository.save(targetUsers)
|
|
|
+ return record
|
|
|
+ }
|
|
|
+
|
|
|
+ async screenRecharge(userId: number, amount: Decimal) {
|
|
|
+ const users = await this.usersService.findById(userId)
|
|
|
+ users.screenBalance = users.screenBalance.plus(amount)
|
|
|
return await this.userRepository.save(users)
|
|
|
}
|
|
|
|
|
|
- async changeScreenBalance(userId: number, amount: number) {
|
|
|
- const amountDecimal = new Decimal(amount)
|
|
|
+ async changeScreenBalance(userId: number, amount: Decimal) {
|
|
|
const users = await this.usersService.findById(userId)
|
|
|
- if (users.screenBalance < amountDecimal.toNumber()) {
|
|
|
+ if (users.screenBalance.comparedTo(amount) < 0) {
|
|
|
return false
|
|
|
}
|
|
|
- const newScreenBalance = new Decimal(String(users.screenBalance)).sub(amountDecimal)
|
|
|
- users.screenBalance = newScreenBalance.toNumber()
|
|
|
+ users.screenBalance = users.screenBalance.sub(amount)
|
|
|
await this.userRepository.save(users)
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
- async feeDeduction(userId: number, cost: number, taskId: number) {
|
|
|
+ async feeDeduction(userId: number, cost: Decimal, taskId: number) {
|
|
|
try {
|
|
|
const user = await this.usersService.findById(userId)
|
|
|
// 获取余额
|
|
|
@@ -212,11 +184,7 @@ export class BalanceService {
|
|
|
}
|
|
|
})
|
|
|
|
|
|
- const currentBalanceDecimal = new Decimal(String(balance.currentBalance))
|
|
|
- const costDecimal = new Decimal(cost)
|
|
|
- const newBalanceDecimal = currentBalanceDecimal.minus(costDecimal)
|
|
|
-
|
|
|
- balance.currentBalance = newBalanceDecimal.toNumber()
|
|
|
+ balance.currentBalance = balance.currentBalance.minus(cost)
|
|
|
|
|
|
await this.balanceRepository.save(balance)
|
|
|
|
|
|
@@ -247,7 +215,7 @@ export class BalanceService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async feeRefund(userId: number, amount: number, taskId: number) {
|
|
|
+ async feeRefund(userId: number, amount: Decimal, taskId: number) {
|
|
|
try {
|
|
|
const user = await this.usersService.findById(userId)
|
|
|
// 获取余额
|
|
|
@@ -257,11 +225,7 @@ export class BalanceService {
|
|
|
}
|
|
|
})
|
|
|
|
|
|
- const currentBalanceDecimal = new Decimal(String(balance.currentBalance))
|
|
|
- const amountDecimal = new Decimal(amount)
|
|
|
- const newBalanceDecimal = currentBalanceDecimal.plus(amountDecimal)
|
|
|
-
|
|
|
- balance.currentBalance = newBalanceDecimal.toNumber()
|
|
|
+ balance.currentBalance = balance.currentBalance.plus(amount)
|
|
|
|
|
|
await this.balanceRepository.save(balance)
|
|
|
|
|
|
@@ -323,7 +287,7 @@ export class BalanceService {
|
|
|
return page
|
|
|
}
|
|
|
|
|
|
- async updateRate(userId: number, rate: number) {
|
|
|
+ async updateRate(userId: number, rate: Decimal) {
|
|
|
try {
|
|
|
let balance = await this.balanceRepository.findOne({
|
|
|
where: {
|
|
|
@@ -335,8 +299,8 @@ export class BalanceService {
|
|
|
if (!balance) {
|
|
|
balance = new Balance()
|
|
|
balance.userId = userId
|
|
|
- balance.currentBalance = 0.0
|
|
|
- balance.totalBalance = 0.0
|
|
|
+ balance.currentBalance = new Decimal(0)
|
|
|
+ balance.totalBalance = new Decimal(0)
|
|
|
}
|
|
|
|
|
|
balance.rate = rate
|
|
|
@@ -402,7 +366,14 @@ export class BalanceService {
|
|
|
worksheet.addRow({
|
|
|
id: record.id,
|
|
|
amount: record.amount,
|
|
|
- type: record.type === BalanceType.RECHARGE ? '充值' : (record.type === BalanceType.CONSUMPTION ? '消费' : (record.type === BalanceType.TRANSFER ? '划转' : '其他')),
|
|
|
+ type:
|
|
|
+ record.type === BalanceType.RECHARGE
|
|
|
+ ? '充值'
|
|
|
+ : record.type === BalanceType.CONSUMPTION
|
|
|
+ ? '消费'
|
|
|
+ : record.type === BalanceType.TRANSFER
|
|
|
+ ? '划转'
|
|
|
+ : '其他',
|
|
|
taskId: task ? task.id : '-',
|
|
|
taskName: task ? task.name : '-',
|
|
|
createdAt: record.createdAt ? moment(record.createdAt).format('YYYY-MM-DD HH:mm:ss') : ''
|
|
|
@@ -410,10 +381,8 @@ export class BalanceService {
|
|
|
})
|
|
|
|
|
|
return await workbook.xlsx.writeBuffer()
|
|
|
-
|
|
|
} else {
|
|
|
throw new Error('Permission denied!')
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|