sys-config.service.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { FastifyInstance } from 'fastify'
  2. import { Like, Repository, Not } from 'typeorm'
  3. import Decimal from 'decimal.js'
  4. import { SysConfig } from '../entities/sys-config.entity'
  5. import { CreateSysConfigBody } from '../dto/sys-config.dto'
  6. import { UpdateSysConfigBody } from '../dto/sys-config.dto'
  7. import { ConfigType } from '../entities/sys-config.entity'
  8. export class SysConfigService {
  9. private app: FastifyInstance
  10. private sysConfigRepository: Repository<SysConfig>
  11. constructor(app: FastifyInstance) {
  12. this.app = app
  13. this.sysConfigRepository = app.dataSource.getRepository(SysConfig)
  14. }
  15. async getSysConfig(name: string) {
  16. const sysConfig = await this.sysConfigRepository.findOneOrFail({ where: { name } })
  17. return sysConfig
  18. }
  19. async maxTransferAmount(defaultAmount: Decimal) {
  20. const sysConfig = await this.sysConfigRepository.findOne({ where: { name: 'max_transfer_amount' } })
  21. if (sysConfig) {
  22. return new Decimal(sysConfig.value)
  23. }
  24. await this.sysConfigRepository.save({ name: 'max_transfer_amount', value: defaultAmount.toString() })
  25. return defaultAmount
  26. }
  27. async replaceType(defaultValue: number) {
  28. try {
  29. const sysConfig = await this.sysConfigRepository.findOne({ where: { name: 'replace_type' } })
  30. if (sysConfig) {
  31. return Number(sysConfig.value)
  32. }
  33. await this.sysConfigRepository.save({ name: 'replace_type', value: defaultValue.toString() })
  34. } catch (e) {
  35. this.app.log.error(e, 'get replaceType error')
  36. }
  37. return defaultValue
  38. }
  39. async getSensitiveWords() {
  40. try {
  41. const config = await this.sysConfigRepository.findOne({ where: { name: 'sensitive_words' } })
  42. if (config) {
  43. return { value: config.value }
  44. }
  45. } catch (e) {
  46. this.app.log.error(e, 'get sensitiveWords error')
  47. }
  48. return null
  49. }
  50. async updateSensitiveWords(words: string) {
  51. try {
  52. const config = await this.sysConfigRepository.findOne({ where: { name: 'sensitive_words' } })
  53. if (config) {
  54. config.value = words
  55. const sysConfig = await this.sysConfigRepository.save(config)
  56. return {
  57. value: sysConfig.value
  58. }
  59. }
  60. await this.sysConfigRepository.save({ name: 'sensitive_words', value: words })
  61. } catch (e) {
  62. this.app.log.error(e, 'update sensitiveWords error')
  63. }
  64. }
  65. async create(data: CreateSysConfigBody) {
  66. const existingConfig = await this.sysConfigRepository.findOne({ where: { name: data.name } })
  67. if (existingConfig) {
  68. throw new Error('配置名称已存在')
  69. }
  70. const config = this.sysConfigRepository.create(data)
  71. return await this.sysConfigRepository.save(config)
  72. }
  73. async update(name: string, data: UpdateSysConfigBody) {
  74. const config = await this.getSysConfig(name)
  75. Object.assign(config, data)
  76. return await this.sysConfigRepository.save(config)
  77. }
  78. async delete(name: string) {
  79. const config = await this.getSysConfig(name)
  80. return await this.sysConfigRepository.remove(config)
  81. }
  82. async list(page: number = 0, size: number = 20, name?: string, type?: ConfigType) {
  83. const where: any = {
  84. name: Not('sensitive_words')
  85. }
  86. if (name) {
  87. where.name = Like(`%${name}%`)
  88. }
  89. if (type) {
  90. where.type = type
  91. }
  92. const [data, total] = await this.sysConfigRepository.findAndCount({
  93. where,
  94. skip: page * size,
  95. take: size,
  96. order: {
  97. id: 'ASC'
  98. }
  99. })
  100. return {
  101. data,
  102. meta: {
  103. page,
  104. size,
  105. total,
  106. totalPages: Math.ceil(total / size)
  107. }
  108. }
  109. }
  110. async getConfigTypes() {
  111. return Object.values(ConfigType)
  112. }
  113. }