|
|
@@ -43,7 +43,36 @@ export class TaskService implements OnModuleInit {
|
|
|
private taskControllers: { [key: number]: AbortController } = {}
|
|
|
|
|
|
async findAllTask(req: PageRequest<Task>): Promise<Pagination<Task>> {
|
|
|
- return await paginate<Task>(this.taskRepository, req.page, req.search)
|
|
|
+ const result = await paginate<Task>(this.taskRepository, req.page, req.search)
|
|
|
+
|
|
|
+ const taskItems = result.items
|
|
|
+ if (taskItems.length > 0) {
|
|
|
+ for (const task of taskItems) {
|
|
|
+ if (task.status === TaskStatus.PENDING || (task.status === TaskStatus.COMPLETED && moment(task.createdAt).isSameOrAfter(moment().subtract(12, 'hours')))) {
|
|
|
+ const id = task.id
|
|
|
+ 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)
|
|
|
+ const sendCount = await this.taskItemRepository.countBy({
|
|
|
+ taskId: id,
|
|
|
+ status: In(['success', 'fail'])
|
|
|
+ })
|
|
|
+ task.sent = sendCount
|
|
|
+ await this.taskRepository.save(task)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result
|
|
|
}
|
|
|
|
|
|
async findAllTaskItem(req: PageRequest<TaskItem>): Promise<Pagination<TaskItem>> {
|
|
|
@@ -76,63 +105,59 @@ export class TaskService implements OnModuleInit {
|
|
|
task.status = TaskStatus.PENDING
|
|
|
await this.taskRepository.save(task)
|
|
|
await this.runTask(task)
|
|
|
+
|
|
|
+ const newTask = await this.taskRepository.findOneBy({ id })
|
|
|
+ if ([TaskStatus.COMPLETED, TaskStatus.PAUSE].includes(newTask.status)) {
|
|
|
+ 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) + '%'
|
|
|
+ newTask.successRate = String(successRate)
|
|
|
+ const sendCount = await this.taskItemRepository.countBy({
|
|
|
+ taskId: id,
|
|
|
+ status: In(['success', 'fail'])
|
|
|
+ })
|
|
|
+ newTask.sent = sendCount
|
|
|
+ await this.taskRepository.save(newTask)
|
|
|
|
|
|
- let finish = false
|
|
|
- while (!finish) {
|
|
|
- // 每隔5s查询一次
|
|
|
- await setTimeout(5000)
|
|
|
- const newTask = await this.taskRepository.findOneBy({ id })
|
|
|
- if ([TaskStatus.COMPLETED, TaskStatus.PAUSE].includes(newTask.status)) {
|
|
|
- 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 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
|
|
|
}
|
|
|
- // 计算成功率
|
|
|
- const successRate = ((successCount / totalCount) * 100).toFixed(1) + '%'
|
|
|
- newTask.successRate = String(successRate)
|
|
|
- const sendCount = await this.taskItemRepository.countBy({
|
|
|
- taskId: id,
|
|
|
- status: In(['success', 'fail'])
|
|
|
- })
|
|
|
- newTask.sent = sendCount
|
|
|
- await this.taskRepository.save(newTask)
|
|
|
+ })
|
|
|
+ balance.currentBalance = latestBalance
|
|
|
|
|
|
- // 获取用户信息
|
|
|
- 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
|
|
|
+ await this.balanceRepository.save(balance)
|
|
|
+ await this.userRepository.save(user)
|
|
|
|
|
|
- // 余额表更新
|
|
|
- const balance = await this.balanceRepository.findOne({
|
|
|
- where: {
|
|
|
- userId: task.userId
|
|
|
- }
|
|
|
- })
|
|
|
- balance.currentBalance = latestBalance
|
|
|
- finish = true
|
|
|
- await this.balanceRepository.save(balance)
|
|
|
- await this.userRepository.save(user)
|
|
|
- break
|
|
|
- } catch (e) {
|
|
|
- Logger.error('Error startTask ', e, 'RcsService')
|
|
|
- }
|
|
|
+ } catch (e) {
|
|
|
+ Logger.error('Error startTask ', e, 'RcsService')
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|