|
|
@@ -239,6 +239,51 @@ export class BalanceService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ async feeRefund(userId: number, amount: number, taskId: number){
|
|
|
+ try {
|
|
|
+ const user = await this.usersService.findById(userId)
|
|
|
+ // 获取余额
|
|
|
+ let balance = await this.balanceRepository.findOne({
|
|
|
+ where: {
|
|
|
+ userId: userId
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ const currentBalanceDecimal = new Decimal(String(balance.currentBalance))
|
|
|
+ const amountDecimal = new Decimal(amount)
|
|
|
+ const newBalanceDecimal = currentBalanceDecimal.plus(amountDecimal)
|
|
|
+
|
|
|
+ balance.currentBalance = newBalanceDecimal.toNumber()
|
|
|
+
|
|
|
+ await this.balanceRepository.save(balance)
|
|
|
+
|
|
|
+ // 退款记录
|
|
|
+ const record = new BalanceRecord()
|
|
|
+ record.userId = userId
|
|
|
+ record.balanceId = balance.id
|
|
|
+ record.taskId = taskId
|
|
|
+ record.amount = amount
|
|
|
+ record.type = BalanceType.REFUND
|
|
|
+ await this.recordRepository.save(record)
|
|
|
+
|
|
|
+ user.balance = balance.currentBalance
|
|
|
+ await this.userRepository.save(user)
|
|
|
+
|
|
|
+ const req = {
|
|
|
+ user: {
|
|
|
+ userId: userId,
|
|
|
+ username: user.username
|
|
|
+ }
|
|
|
+ }
|
|
|
+ await this.operationLogService.create(req, record, 'BalanceRecord', OperationType.INSERT, '任务退款')
|
|
|
+
|
|
|
+ return 'Refund success!'
|
|
|
+ } catch (e) {
|
|
|
+ Logger.error('Error Refund ', e, 'BalanceService')
|
|
|
+ throw new Error('Refund failure!')
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async findAllRecords(): Promise<BalanceRecord[]> {
|
|
|
return await this.recordRepository.find()
|
|
|
}
|