|
|
@@ -1,18 +1,36 @@
|
|
|
-import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'
|
|
|
+import { Injectable, InternalServerErrorException, NotFoundException, OnModuleInit } from '@nestjs/common'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { Repository } from 'typeorm'
|
|
|
-import { SysConfig } from './entities/sys-config.entity'
|
|
|
-import { PageRequest } from 'src/common/dto/page-request'
|
|
|
+import { SysConfig, SysConfigType } from './entities/sys-config.entity'
|
|
|
+import { PageRequest } from '../common/dto/page-request'
|
|
|
import { Pagination, paginate } from 'nestjs-typeorm-paginate'
|
|
|
|
|
|
@Injectable()
|
|
|
-export class SysConfigService {
|
|
|
+export class SysConfigService implements OnModuleInit {
|
|
|
constructor(
|
|
|
@InjectRepository(SysConfig)
|
|
|
private readonly sysConfigRepository: Repository<SysConfig>
|
|
|
) {
|
|
|
;(async function init() {})()
|
|
|
}
|
|
|
+ async onModuleInit() {
|
|
|
+ if (!(await this.sysConfigRepository.findOneBy({ name: 'vote_delay' }))) {
|
|
|
+ await this.sysConfigRepository.save({
|
|
|
+ name: 'vote_delay',
|
|
|
+ value: '0',
|
|
|
+ type: SysConfigType.Number,
|
|
|
+ remark: '投票延迟时间'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ if (!(await this.sysConfigRepository.findOneBy({ name: 'vote_wait_time' }))) {
|
|
|
+ await this.sysConfigRepository.save({
|
|
|
+ name: 'vote_wait_time',
|
|
|
+ value: '30',
|
|
|
+ type: SysConfigType.Number,
|
|
|
+ remark: '投票等待时间'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
async findAll(req: PageRequest<SysConfig>) {
|
|
|
try {
|
|
|
@@ -41,4 +59,14 @@ export class SysConfigService {
|
|
|
throw new InternalServerErrorException(error.message)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ async getNumber(name: string, defaultValue: number): Promise<number> {
|
|
|
+ const conf = await this.sysConfigRepository.findOneBy({ name })
|
|
|
+ return parseInt(conf.value) || defaultValue
|
|
|
+ }
|
|
|
+
|
|
|
+ async getString(name: string, defaultValue: string): Promise<string> {
|
|
|
+ const conf = await this.sysConfigRepository.findOneBy({ name })
|
|
|
+ return conf.value || defaultValue
|
|
|
+ }
|
|
|
}
|