UNPKG

create-chuntianxiaozhu

Version:

春天小猪模板工具

67 lines (62 loc) 2.13 kB
import { Injectable, Logger } from '@nestjs/common'; import { Cron } from '@nestjs/schedule'; import { ScreenshotService } from '../common/screenshot.service'; import { LessThan } from 'typeorm'; import { frontDay, removeStaticFile } from 'api/utils/utils'; import { SettlementService } from 'api/common/settlement.service'; import { UserService } from 'api/common/user.service'; @Injectable() export class AppSchedule { private readonly logger = new Logger(AppSchedule.name); constructor( private screenshotService: ScreenshotService, private settlementService: SettlementService, private userService: UserService, ) {} @Cron('0 0 4 * * *') async countSettlement() { this.logger.log('定时任务执行中...'); const result = await this.screenshotService.findAll({ updateDate: LessThan(frontDay(1)), }); const ids: number[] = []; for (let i = 0; i < result.length; i++) { const item = result[i]; ids.push(item.id); removeStaticFile('screenshot', item.name); } if (ids.length) { await this.screenshotService.delete(ids); } this.logger.log('截屏清理已完成'); const settleResult: any[] = (await this.settlementService.countGroupField()) || []; const settleMap = {}; settleResult.forEach((item) => { const { uid, status, total } = item; if (settleMap[uid] == null) { settleMap[uid] = {}; } if (status == 0) { settleMap[uid].waitSettleAmount = total; } else if (status == 1) { settleMap[uid].settlingAmount = total; } else if (status == 2) { settleMap[uid].hadSettleAmount = total; } else if (status == 3) { settleMap[uid].totalSettleAmount = total; } }); Object.keys(settleMap).forEach((key) => { this.userService.updateById(Number(key), settleMap[key]); }); this.logger.log('佣金已结算完成'); this.logger.log('执行成功'); } // 暂时注释 // @Cron('0 0/30 * * * *') // async runEveryHalfHour() { // // 没半小时刷新数据 // await giteeHotUpdate('data'); // } }