wuyi 2 лет назад
Родитель
Сommit
0ada6a55d1

+ 3 - 0
src/moments/entities/moments.entity.ts

@@ -22,6 +22,9 @@ export class Moments {
     @Column({ default: 0 })
     momentsLikeCount: number
 
+    @Column({ default: true })
+    isDel: boolean
+
     @CreateDateColumn()
     createdAt: Date
 

+ 4 - 0
src/moments/moments.controller.ts

@@ -45,6 +45,10 @@ export class MomentsController {
     @HasRoles(Role.Admin)
     public async manualCreate() {
         await this.momentsService.automaticallySendMoments()
+        setTimeout(() => {
+            this.momentsService.delay()
+        }, 60 * 1000)
+
     }
 
 }

+ 57 - 5
src/moments/moments.service.ts

@@ -7,7 +7,7 @@ import {
     InternalServerErrorException,
     UnauthorizedException
 } from '@nestjs/common'
-import { In, Repository, UpdateResult } from 'typeorm'
+import { FindManyOptions, In, Repository, UpdateResult } from 'typeorm'
 import { InjectRepository } from '@nestjs/typeorm'
 import { Moments } from './entities/moments.entity'
 import { paginate, Pagination } from 'nestjs-typeorm-paginate'
@@ -36,13 +36,48 @@ export class MomentsService {
         private readonly chatService: ChatService,
         private readonly likesService: LikesService
     ) {
-        setInterval(() => {
-            this.automaticallySendMoments();
-        }, 24 * 60 * 60 * 1000);
+        this.delay()
+        const now = new Date()
+        const nextDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime()
+        const delayTime = nextDay - now.getTime()
+        setTimeout(() => {
+            setInterval(() => {
+                this.automaticallySendMoments()
+                setTimeout(() => {
+                    this.delay()
+                }, 3 * 60 * 1000)
+            }, 24 * 60 * 60 * 1000)
+        }, delayTime)
     }
 
     async findAll(req: PageRequest<Moments>, userId: number): Promise<Pagination<Moments>> {
-        const page = await paginate<Moments>(this.momentsRepository, req.page, req.search)
+
+        const query = await this.momentsRepository.createQueryBuilder('moments')
+            .where('moments.isDel = false')
+
+        if (req.search) {
+            const searchOptions = req.search as FindManyOptions<Moments>;
+            if ('where' in req.search) {
+                if ('where' in searchOptions && searchOptions.where) {
+                    for (const [key, value] of Object.entries(searchOptions.where)) {
+                        query.andWhere(`moments.${key} = ${value}`);
+                    }
+                }
+            }
+
+            if ('order' in req.search) {
+                if ('order' in searchOptions && searchOptions.order) {
+                    for (const [key, value] of Object.entries(searchOptions.order)) {
+                        query.addOrderBy(`moments.${key}`, value as 'ASC' | 'DESC');
+                    }
+                }
+            }
+        }
+
+        const options = { ...req.page }
+        const page = await paginate<Moments>(query, options)
+
+        //const page = await paginate<Moments>(this.momentsRepository, req.page, req.search)
         const ids = page.items.reduce((ids: number[], val: Moments) => {
             if (!ids.includes(val.userId)) {
                 ids.push(val.userId);
@@ -115,7 +150,24 @@ export class MomentsService {
             momentsDto.userId = role.id
             await this.momentsRepository.save(momentsDto)
         })
+    }
 
+
+    public async delay() {
+        const momentsList = await this.momentsRepository.createQueryBuilder('moments')
+            .where('moments.isDel = true')
+            .getMany()
+
+        momentsList.forEach(moments => {
+            const hours = Math.floor(Math.random() * 12)
+            const minutes = Math.floor(Math.random() * 60)
+            console.log(moments.userId + '的动态将在' + hours + '小时' + minutes + '分后发布')
+            setTimeout(async () => {
+                moments.isDel = false
+                await this.momentsRepository.save(moments)
+            }, hours * 60 * 1000 + minutes * 1000)
+        })
     }
 
+
 }