|
|
@@ -1,5 +1,5 @@
|
|
|
import { PhoneListService } from './../phone-list/phone-list.service'
|
|
|
-import { Inject, Injectable, Logger, OnModuleInit, forwardRef } from '@nestjs/common'
|
|
|
+import { Inject, Injectable, Logger, OnModuleInit, forwardRef, NotFoundException } from '@nestjs/common'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { Task, TaskStatus } from './entities/task.entity'
|
|
|
import { Repository } from 'typeorm'
|
|
|
@@ -11,6 +11,8 @@ import { randomUUID } from 'crypto'
|
|
|
import { setTimeout } from 'timers/promises'
|
|
|
import { DeviceService } from '../device/device.service'
|
|
|
import { SysConfigService } from '../sys-config/sys-config.service'
|
|
|
+import { Users } from '../users/entities/users.entity'
|
|
|
+import { Balance } from '../balance/entities/balance.entities'
|
|
|
|
|
|
@Injectable()
|
|
|
export class TaskService implements OnModuleInit {
|
|
|
@@ -19,6 +21,10 @@ export class TaskService implements OnModuleInit {
|
|
|
private taskRepository: Repository<Task>,
|
|
|
@InjectRepository(TaskItem)
|
|
|
private taskItemRepository: Repository<TaskItem>,
|
|
|
+ @InjectRepository(Users)
|
|
|
+ private userRepository: Repository<Users>,
|
|
|
+ @InjectRepository(Balance)
|
|
|
+ private balanceRepository: Repository<Balance>,
|
|
|
@Inject(forwardRef(() => EventsGateway))
|
|
|
private readonly eventsGateway: EventsGateway,
|
|
|
private readonly phoneListService: PhoneListService,
|
|
|
@@ -68,20 +74,52 @@ export class TaskService implements OnModuleInit {
|
|
|
await this.taskRepository.save(task)
|
|
|
await this.runTask(task)
|
|
|
|
|
|
- const successCount = await this.taskItemRepository.countBy({
|
|
|
- taskId: id,
|
|
|
- status: 'success'
|
|
|
- })
|
|
|
- const totalCount = await this.taskItemRepository.countBy({
|
|
|
- taskId: id
|
|
|
- })
|
|
|
- if (totalCount === 0) {
|
|
|
- throw new Error('No tasks found for the given taskId.')
|
|
|
+ try {
|
|
|
+ const successCount = await this.taskItemRepository.countBy({
|
|
|
+ taskId: id,
|
|
|
+ status: 'success'
|
|
|
+ })
|
|
|
+ const totalCount = await this.taskItemRepository.countBy({
|
|
|
+ taskId: id
|
|
|
+ })
|
|
|
+ if (totalCount === 0) {
|
|
|
+ throw new Error('No tasks found for the given taskId.')
|
|
|
+ }
|
|
|
+ // 计算成功率
|
|
|
+ const successRate = ((successCount / totalCount) * 100).toFixed(1) + '%'
|
|
|
+ task.successRate = String(successRate)
|
|
|
+ await this.taskRepository.save(task)
|
|
|
+
|
|
|
+ // 获取用户信息
|
|
|
+ const user = await this.userRepository.findOneBy({
|
|
|
+ id: task.userId
|
|
|
+ })
|
|
|
+ // 已发送
|
|
|
+ const send: number = user.send
|
|
|
+ user.send = send + successCount
|
|
|
+ // 费用 = 费率*成功发送
|
|
|
+ const rate: number = user.rate
|
|
|
+ const cost: number = rate * successCount
|
|
|
+ // 从用户余额中减去费用 可能有余额不足的情况,后续优化
|
|
|
+ const latestBalance: number = (user.balance || 0) - cost
|
|
|
+ user.balance = latestBalance
|
|
|
+
|
|
|
+ // 余额表更新
|
|
|
+ const balance = await this.balanceRepository.findOne({
|
|
|
+ where: {
|
|
|
+ userId: task.userId
|
|
|
+ }
|
|
|
+ })
|
|
|
+ balance.currentBalance = latestBalance
|
|
|
+
|
|
|
+ await this.balanceRepository.save(balance)
|
|
|
+ await this.userRepository.save(user)
|
|
|
+
|
|
|
+
|
|
|
+ } catch (e) {
|
|
|
+ Logger.error('Error startTask ', e, 'RcsService')
|
|
|
}
|
|
|
- // 计算成功率
|
|
|
- const successRate = ((successCount / totalCount) * 100).toFixed(1) + '%'
|
|
|
- task.successRate = String(successRate)
|
|
|
- await this.taskRepository.save(task)
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|