返回 AiToEarn
cfg.service.ts
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: Cfg cfg
7 */
8 import { Injectable } from '@nestjs/common';
9 import { InjectModel } from '@nestjs/mongoose';
10 import { Model, RootFilterQuery } from 'mongoose';
11 import { Cfg } from 'src/db/schema/cfg.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 CfgService {
19 constructor(
20 @InjectModel(Cfg.name)
21 private readonly cfgModel: Model<Cfg>,
22 ) {}
23
24 // 创建或者更新
25 async create(data: Partial<Cfg>) {
26 const { key, ...restData } = data;
27 const options = {
28 upsert: true,
29 new: true,
30 setDefaultsOnInsert: true,
31 };
32 return await this.cfgModel.findOneAndUpdate(
33 { key },
34 { $set: restData },
35 options,
36 );
37 }
38
39 // 获取
40 async getInfoById(id: string): Promise<Cfg> {
41 const res = await this.cfgModel.findOne({ _id: id });
42 return res;
43 }
44
45 // 获取
46 async getInfoByKey(key: string): Promise<Cfg> {
47 const res = await this.cfgModel.findOne({ key });
48 return res;
49 }
50
51 /**
52 * 获取列表
53 * @param pageInfo
54 * @returns
55 */
56 async getCfgList(pageInfo: TableDto) {
57 const { skip, take } = TableUtil.GetSqlPaging(pageInfo);
58 const filter: RootFilterQuery<Cfg> = {};
59 const tatal = await this.cfgModel.countDocuments(filter);
60 const data = await this.cfgModel
61 .find(filter)
62 .sort({ createTime: -1 })
63 .skip(skip)
64 .limit(take);
65
66 return ResponseUtil.GetCorrectResponse(
67 pageInfo.pageNo,
68 pageInfo.pageSize,
69 tatal,
70 data,
71 );
72 }
73
74 // 更新信息
75 async updateValue(id: string, data: any): Promise<boolean> {
76 const res = await this.cfgModel.updateOne({ _id: id }, data);
77 return res.modifiedCount > 0;
78 }
79
80 // 更新状态
81 async updateStatus(id: string, status: ONOFF): Promise<boolean> {
82 const res = await this.cfgModel.updateOne(
83 { _id: id },
84 { $set: { status } },
85 );
86 return res.modifiedCount > 0;
87 }
88
89 // 删除
90 async del(key: string): Promise<boolean> {
91 const res = await this.cfgModel.deleteMany({ key });
92 return res.deletedCount > 0;
93 }
94 }
95
95 lines TYPESCRIPT