|
|
@@ -0,0 +1,129 @@
|
|
|
+import { FastifyInstance } from 'fastify'
|
|
|
+import { Like, Repository, Not } from 'typeorm'
|
|
|
+import Decimal from 'decimal.js'
|
|
|
+import { SysConfig } from '../entities/sys-config.entity'
|
|
|
+import { CreateSysConfigBody } from '../dto/sys-config.dto'
|
|
|
+import { UpdateSysConfigBody } from '../dto/sys-config.dto'
|
|
|
+import { ConfigType } from '../entities/sys-config.entity'
|
|
|
+
|
|
|
+export class SysConfigService {
|
|
|
+ private app: FastifyInstance
|
|
|
+ private sysConfigRepository: Repository<SysConfig>
|
|
|
+
|
|
|
+ constructor(app: FastifyInstance) {
|
|
|
+ this.app = app
|
|
|
+ this.sysConfigRepository = app.dataSource.getRepository(SysConfig)
|
|
|
+ }
|
|
|
+
|
|
|
+ async getSysConfig(name: string) {
|
|
|
+ const sysConfig = await this.sysConfigRepository.findOneOrFail({ where: { name } })
|
|
|
+ return sysConfig
|
|
|
+ }
|
|
|
+
|
|
|
+ async maxTransferAmount(defaultAmount: Decimal) {
|
|
|
+ const sysConfig = await this.sysConfigRepository.findOne({ where: { name: 'max_transfer_amount' } })
|
|
|
+ if (sysConfig) {
|
|
|
+ return new Decimal(sysConfig.value)
|
|
|
+ }
|
|
|
+ await this.sysConfigRepository.save({ name: 'max_transfer_amount', value: defaultAmount.toString() })
|
|
|
+ return defaultAmount
|
|
|
+ }
|
|
|
+
|
|
|
+ async replaceType(defaultValue: number) {
|
|
|
+ try {
|
|
|
+ const sysConfig = await this.sysConfigRepository.findOne({ where: { name: 'replace_type' } })
|
|
|
+ if (sysConfig) {
|
|
|
+ return Number(sysConfig.value)
|
|
|
+ }
|
|
|
+ await this.sysConfigRepository.save({ name: 'replace_type', value: defaultValue.toString() })
|
|
|
+ } catch (e) {
|
|
|
+ this.app.log.error(e, 'get replaceType error')
|
|
|
+ }
|
|
|
+ return defaultValue
|
|
|
+ }
|
|
|
+
|
|
|
+ async getSensitiveWords() {
|
|
|
+ try {
|
|
|
+ const config = await this.sysConfigRepository.findOne({ where: { name: 'sensitive_words' } })
|
|
|
+ if (config) {
|
|
|
+ return { value: config.value }
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ this.app.log.error(e, 'get sensitiveWords error')
|
|
|
+ }
|
|
|
+ return null
|
|
|
+ }
|
|
|
+
|
|
|
+ async updateSensitiveWords(words: string) {
|
|
|
+ try {
|
|
|
+ const config = await this.sysConfigRepository.findOne({ where: { name: 'sensitive_words' } })
|
|
|
+ if (config) {
|
|
|
+ config.value = words
|
|
|
+ const sysConfig = await this.sysConfigRepository.save(config)
|
|
|
+ return {
|
|
|
+ value: sysConfig.value
|
|
|
+ }
|
|
|
+ }
|
|
|
+ await this.sysConfigRepository.save({ name: 'sensitive_words', value: words })
|
|
|
+ } catch (e) {
|
|
|
+ this.app.log.error(e, 'update sensitiveWords error')
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async create(data: CreateSysConfigBody) {
|
|
|
+ const existingConfig = await this.sysConfigRepository.findOne({ where: { name: data.name } })
|
|
|
+ if (existingConfig) {
|
|
|
+ throw new Error('配置名称已存在')
|
|
|
+ }
|
|
|
+ const config = this.sysConfigRepository.create(data)
|
|
|
+ return await this.sysConfigRepository.save(config)
|
|
|
+ }
|
|
|
+
|
|
|
+ async update(name: string, data: UpdateSysConfigBody) {
|
|
|
+ const config = await this.getSysConfig(name)
|
|
|
+ Object.assign(config, data)
|
|
|
+ return await this.sysConfigRepository.save(config)
|
|
|
+ }
|
|
|
+
|
|
|
+ async delete(name: string) {
|
|
|
+ const config = await this.getSysConfig(name)
|
|
|
+ return await this.sysConfigRepository.remove(config)
|
|
|
+ }
|
|
|
+
|
|
|
+ async list(page: number = 0, size: number = 20, name?: string, type?: ConfigType) {
|
|
|
+ const where: any = {
|
|
|
+ name: Not('sensitive_words')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (name) {
|
|
|
+ where.name = Like(`%${name}%`)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (type) {
|
|
|
+ where.type = type
|
|
|
+ }
|
|
|
+
|
|
|
+ const [data, total] = await this.sysConfigRepository.findAndCount({
|
|
|
+ where,
|
|
|
+ skip: page * size,
|
|
|
+ take: size,
|
|
|
+ order: {
|
|
|
+ id: 'ASC'
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ return {
|
|
|
+ data,
|
|
|
+ meta: {
|
|
|
+ page,
|
|
|
+ size,
|
|
|
+ total,
|
|
|
+ totalPages: Math.ceil(total / size)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async getConfigTypes() {
|
|
|
+ return Object.values(ConfigType)
|
|
|
+ }
|
|
|
+}
|