| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2024-06-17 19:19:15 |
| 4 | * @LastEditTime: 2025-03-03 18:41:40 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | import { Injectable } from '@nestjs/common'; |
| 9 | import { InjectModel } from '@nestjs/mongoose'; |
| 10 | import { Model, RootFilterQuery } from 'mongoose'; |
| 11 | import { Banner } from 'src/db/schema/banner.schema'; |
| 12 | import { ResponseUtil } from 'src/global/class/correctResponse.class'; |
| 13 | import { TableUtil } from 'src/global/class/tableUtli.class'; |
| 14 | import { TableDto } from 'src/global/dto/table.dto'; |
| 15 | import { ONOFF } from 'src/global/enum/all.enum'; |
| 16 | |
| 17 | @Injectable() |
| 18 | export class BannerService { |
| 19 | constructor( |
| 20 | @InjectModel(Banner.name) |
| 21 | private readonly bannerModel: Model<Banner>, |
| 22 | ) {} |
| 23 | |
| 24 | // 创建 |
| 25 | async create(data: Banner) { |
| 26 | return await this.bannerModel.create(data); |
| 27 | } |
| 28 | |
| 29 | // 获取 |
| 30 | async getBannerInfo(id: string): Promise<Banner> { |
| 31 | const res = await this.bannerModel.findOne({ _id: id }); |
| 32 | return res; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * 获取列表 |
| 37 | * @param pageInfo |
| 38 | * @param query |
| 39 | * @returns |
| 40 | */ |
| 41 | async getBannerList(pageInfo: TableDto, query: any) { |
| 42 | const { skip, take } = TableUtil.GetSqlPaging(pageInfo); |
| 43 | const filter: RootFilterQuery<Banner> = { |
| 44 | ...(query.createTimeArray && { |
| 45 | createTime: { |
| 46 | $gte: query.createTimeArray[0], |
| 47 | $lte: query.createTimeArray[1], |
| 48 | }, |
| 49 | }), |
| 50 | }; |
| 51 | const tatal = await this.bannerModel.countDocuments(filter); |
| 52 | const data = await this.bannerModel |
| 53 | .find(filter) |
| 54 | .sort({ createTime: -1 }) |
| 55 | .skip(skip) |
| 56 | .limit(take); |
| 57 | |
| 58 | return ResponseUtil.GetCorrectResponse( |
| 59 | pageInfo.pageNo, |
| 60 | pageInfo.pageSize, |
| 61 | tatal, |
| 62 | data, |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * 获取全部 |
| 68 | * @param tag |
| 69 | * @returns |
| 70 | */ |
| 71 | async getBannerAll(tag?: string) { |
| 72 | const filter: RootFilterQuery<Banner> = { |
| 73 | ...(tag && { |
| 74 | tag, |
| 75 | }), |
| 76 | }; |
| 77 | const data = await this.bannerModel.find(filter).sort({ createTime: -1 }); |
| 78 | return data; |
| 79 | } |
| 80 | |
| 81 | // 更新信息 |
| 82 | async updateBannerInfo(id: string, data: Banner): Promise<boolean> { |
| 83 | const res = await this.bannerModel.updateOne({ _id: id }, data); |
| 84 | return res.modifiedCount > 0; |
| 85 | } |
| 86 | |
| 87 | // 更新发布状态 |
| 88 | async updateBannerPublish(id: string, isPublish: ONOFF): Promise<boolean> { |
| 89 | const res = await this.bannerModel.updateOne( |
| 90 | { _id: id }, |
| 91 | { $set: { isPublish } }, |
| 92 | ); |
| 93 | return res.modifiedCount > 0; |
| 94 | } |
| 95 | |
| 96 | // 删除 |
| 97 | async deleteBanner(id: string): Promise<boolean> { |
| 98 | const res = await this.bannerModel.deleteOne({ _id: id }); |
| 99 | return res.deletedCount > 0; |
| 100 | } |
| 101 | } |
| 102 |