| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2024-08-31 19:15:15 |
| 4 | * @LastEditTime: 2024-09-18 11:42:41 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | import { Injectable, Inject } from '@nestjs/common'; |
| 9 | import { Redis } from 'ioredis'; |
| 10 | import { REDIS_CLIENT } from './redis.constant'; |
| 11 | |
| 12 | @Injectable() |
| 13 | export class RedisService { |
| 14 | constructor(@Inject(REDIS_CLIENT) private readonly client: Redis) {} |
| 15 | |
| 16 | /** |
| 17 | * 设置key-value |
| 18 | * @param key |
| 19 | * @param value |
| 20 | * @param seconds |
| 21 | * @returns |
| 22 | */ |
| 23 | async setKey(key: string, value: any, seconds?: number): Promise<boolean> { |
| 24 | value = JSON.stringify(value); |
| 25 | if (!seconds) return !!(await this.client.set(key, value)); |
| 26 | |
| 27 | return !!(await this.client.set(key, value, 'EX', seconds)); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * 获取值 |
| 32 | * @param key |
| 33 | * @returns |
| 34 | */ |
| 35 | async get(key: string, isObj = true): Promise<any> { |
| 36 | const data = await this.client.get(key); |
| 37 | if (!data) return null; |
| 38 | return isObj ? JSON.parse(data) : data; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * 清除值 |
| 43 | * @param key |
| 44 | * @returns |
| 45 | */ |
| 46 | async del(key: string): Promise<boolean> { |
| 47 | const data = await this.client.del(key); |
| 48 | return !!data; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * 设置过期时间 |
| 53 | * @param key |
| 54 | * @param times |
| 55 | * @returns |
| 56 | */ |
| 57 | async setPexire(key: string, times = 0): Promise<boolean> { |
| 58 | const data = await this.client.pexpire(key, times); |
| 59 | return data === 1; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * 生成订单号 |
| 64 | * @param h 前置标识 |
| 65 | */ |
| 66 | async generateOrderNumber(h: string = '') { |
| 67 | try { |
| 68 | // 获取今天的日期,格式化为 YYYYMMDD |
| 69 | const today = new Date(); |
| 70 | const datePrefix = `${today.getFullYear()}${today.getMonth() + 1}${today.getDate()}`; |
| 71 | |
| 72 | // 使用 Redis 的 INCR 命令生成递增的序列号 |
| 73 | const sequence = await this.client.incr(`order_seq:${h}${datePrefix}`); |
| 74 | |
| 75 | // 格式化序列号,确保它有固定的位数,例如8位 |
| 76 | const formattedSequence = sequence.toString().padStart(8, '0'); |
| 77 | |
| 78 | // 拼接日期前缀和序列号,形成完整的订单号 |
| 79 | const orderNumber = `${h}${datePrefix}-${formattedSequence}`; |
| 80 | |
| 81 | return orderNumber; |
| 82 | } catch (error) { |
| 83 | console.error('Error generating order number:', error); |
| 84 | throw error; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * 重置订单号 |
| 90 | * @param h 前置标识 |
| 91 | */ |
| 92 | async resetOrderNumber(h: string = '') { |
| 93 | try { |
| 94 | const today = new Date(); |
| 95 | const datePrefix = `${today.getFullYear()}${today.getMonth() + 1}${today.getDate()}`; |
| 96 | |
| 97 | // 删除前一天的序列号键 |
| 98 | await this.client.del(`order_seq:${h}${datePrefix}`); |
| 99 | } catch (error) { |
| 100 | console.error('Error resetting order sequence:', error); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 |