| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-02-15 20:59:55 |
| 4 | * @LastEditTime: 2025-04-27 17:58:21 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: signIn SignIn 签到 |
| 7 | */ |
| 8 | import { Injectable } from '@nestjs/common'; |
| 9 | import { InjectModel } from '@nestjs/mongoose'; |
| 10 | import { Model, RootFilterQuery } from 'mongoose'; |
| 11 | import { SignIn, SignInType } from 'src/db/schema/signIn.schema'; |
| 12 | import { QuerySignInListDto } from './dto/signIn.dto'; |
| 13 | import { paginateModel } from 'src/common/paginate/create-pagination'; |
| 14 | |
| 15 | @Injectable() |
| 16 | export class SignInService { |
| 17 | constructor( |
| 18 | @InjectModel(SignIn.name) |
| 19 | private readonly signInModel: Model<SignIn>, |
| 20 | ) {} |
| 21 | |
| 22 | /** |
| 23 | * 创建签到记录,有则更新创建时间 |
| 24 | * @param userId |
| 25 | * @param type |
| 26 | * @returns |
| 27 | */ |
| 28 | async createSignInRecord(userId: string, type: SignInType): Promise<SignIn> { |
| 29 | return await this.signInModel.findOneAndUpdate( |
| 30 | { |
| 31 | userId, |
| 32 | type, |
| 33 | }, |
| 34 | { |
| 35 | $set: { |
| 36 | userId, |
| 37 | type, |
| 38 | createTime: new Date(), |
| 39 | }, |
| 40 | }, |
| 41 | { |
| 42 | upsert: true, |
| 43 | new: true, |
| 44 | }, |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * 获取时间段内的签到列表 |
| 50 | * @param userId |
| 51 | * @param query |
| 52 | * @returns |
| 53 | */ |
| 54 | async getSignInList(userId: string, query: QuerySignInListDto) { |
| 55 | const { page, pageSize, type, time } = query; |
| 56 | const filter: RootFilterQuery<SignIn> = { userId, type }; |
| 57 | |
| 58 | if (time) { |
| 59 | filter.createTime = { |
| 60 | $gte: new Date(time[0]), |
| 61 | $lte: new Date(time[1]), |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | return paginateModel( |
| 66 | this.signInModel, |
| 67 | { |
| 68 | page, |
| 69 | pageSize, |
| 70 | }, |
| 71 | filter, |
| 72 | undefined, |
| 73 | { _id: -1 }, |
| 74 | ); |
| 75 | } |
| 76 | } |
| 77 |