|
|
|
@ -7,7 +7,7 @@ import { RedisService } from '@common/service/redis/redis.service'; |
|
|
|
|
import { pacAuthUser, pacCoreDict, pacCoreService } from '@entities/schema'; |
|
|
|
|
import { and, asc, desc, eq, isNull, like, or, SQL, sql } from 'drizzle-orm'; |
|
|
|
|
import { Snowflake } from '@service/snowflake/snowflake.service'; |
|
|
|
|
import { GetPacCoreDictAllDto, PacCoreDictIdDto, PacCoreDictKeyDto, PacCoreDictTargetListDto } from '@app/core-dict/dto/get-core-dict.dto'; |
|
|
|
|
import { GetPacCoreDictAllDto, PacCoreDictTargetListDto } from '@app/core-dict/dto/get-core-dict.dto'; |
|
|
|
|
import { ConfigService } from '@nestjs/config'; |
|
|
|
|
import { likeQuery } from '@utils/likeQuery'; |
|
|
|
|
import { QueryBuilder } from 'drizzle-orm/mysql-core'; |
|
|
|
@ -26,10 +26,10 @@ export class CoreDictService { |
|
|
|
|
dictName: pacCoreDict.dictName, |
|
|
|
|
dictIcon: pacCoreDict.dictIcon, |
|
|
|
|
dictType: pacCoreDict.dictType, |
|
|
|
|
ownOfPac: pacCoreDict.ownOfPac, |
|
|
|
|
root: pacCoreDict.root, |
|
|
|
|
orderNum: pacCoreDict.orderNum, |
|
|
|
|
status: pacCoreDict.status, |
|
|
|
|
serviceOf: pacCoreDict.serviceOf, |
|
|
|
|
serviceKey: pacCoreDict.serviceKey, |
|
|
|
|
serviceName: pacCoreService.serviceName, |
|
|
|
|
haveChildren: pacCoreDict.haveChildren, |
|
|
|
|
createby: pacCoreDict.createby, |
|
|
|
@ -64,7 +64,12 @@ export class CoreDictService { |
|
|
|
|
* DESC: 创建字典内容 |
|
|
|
|
* DATE: 2024-05-31 19:12:47 - |
|
|
|
|
* */ |
|
|
|
|
public async create(createCoreDictDto: CreateCoreDictDto, userInfo) { |
|
|
|
|
public async create(createCoreDictDto: CreateCoreDictDto, pacInfo: PacInfoType) { |
|
|
|
|
// ! 判断是否是root账户,非root账户无法设置为root
|
|
|
|
|
if (this.config.get<number>('masterId') !== pacInfo.userId && isTrueEnum(createCoreDictDto.root)) { |
|
|
|
|
throw new HttpException('没有权限创建原始字典!', HttpStatus.UNAUTHORIZED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ! 加目标锁
|
|
|
|
|
const lock = await this.redisService.distributedLock('DICT' + createCoreDictDto.dictKey, createCoreDictDto.dictKey); |
|
|
|
|
|
|
|
|
@ -80,7 +85,7 @@ export class CoreDictService { |
|
|
|
|
if (result.length > 0) throw new HttpException('字典标识重复!', HttpStatus.CONFLICT); |
|
|
|
|
|
|
|
|
|
// ! 添加字典数据
|
|
|
|
|
const newPacCoreDict = await this.addDictData(createCoreDictDto, userInfo.userId); |
|
|
|
|
const newPacCoreDict = await this.addDictData(createCoreDictDto, pacInfo.userId); |
|
|
|
|
|
|
|
|
|
// ! 解锁
|
|
|
|
|
lock(); |
|
|
|
@ -120,8 +125,10 @@ export class CoreDictService { |
|
|
|
|
* DESC: 查找字典项详细信息 |
|
|
|
|
* DATE: 2024-06-04 19:21:40 - |
|
|
|
|
* */ |
|
|
|
|
public async findOne(pacCoreDictIdDto: PacCoreDictIdDto) { |
|
|
|
|
return (await this.getMore(pacCoreDictIdDto.dictId))[0]; |
|
|
|
|
public async findOne(dictId: string) { |
|
|
|
|
const result = await this.getMore(dictId); |
|
|
|
|
if (result.length == 0) throw new HttpException('未找到目标字典详细信息!', HttpStatus.BAD_REQUEST); |
|
|
|
|
return result[0]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** Service |
|
|
|
@ -129,8 +136,8 @@ export class CoreDictService { |
|
|
|
|
* DESC: 查找字典下的列表或树 |
|
|
|
|
* DATE: 2024-06-04 19:21:40 - |
|
|
|
|
* */ |
|
|
|
|
public async findTargetList(pacCoreDictKeyDto: PacCoreDictKeyDto, pacCoreDictTargetListDto: PacCoreDictTargetListDto) { |
|
|
|
|
const [result] = await this.getTargetDictList(pacCoreDictKeyDto, pacCoreDictTargetListDto); |
|
|
|
|
public async findTargetList(dictKey: string, pacCoreDictTargetListDto: PacCoreDictTargetListDto) { |
|
|
|
|
const [result] = await this.getTargetDictList(dictKey, pacCoreDictTargetListDto); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -139,7 +146,21 @@ export class CoreDictService { |
|
|
|
|
* DESC: 更新字典 |
|
|
|
|
* DATE: 2024-06-04 23:24:18 - |
|
|
|
|
* */ |
|
|
|
|
public async update(pacCoreDictIdDto: PacCoreDictIdDto, updateCoreDictDto: UpdateCoreDictDto, pacInfo: PacInfoType) { |
|
|
|
|
public async update(dictId: string, updateCoreDictDto: UpdateCoreDictDto, pacInfo: PacInfoType) { |
|
|
|
|
// ! 判断是否是root账户,非root账户无法设置为root
|
|
|
|
|
if (this.config.get<number>('masterId') !== pacInfo.userId && updateCoreDictDto.root !== undefined) { |
|
|
|
|
throw new HttpException('没有权限修改原始字典!', HttpStatus.UNAUTHORIZED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ! 产看目标字典是否存在
|
|
|
|
|
const oldDict = await this.getMore(dictId); |
|
|
|
|
if (oldDict.length == 0) throw new HttpException('未找到目标字典,无法修改!', HttpStatus.BAD_REQUEST); |
|
|
|
|
|
|
|
|
|
// ! root数据,非root用户不允许修改
|
|
|
|
|
if (oldDict[0].root && this.config.get<number>('masterId') !== pacInfo.userId) { |
|
|
|
|
throw new HttpException('原始字典,权限不足无法修改!', HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ! 上锁
|
|
|
|
|
const lock = await this.redisService.distributedLock('DICT' + updateCoreDictDto.dictKey, updateCoreDictDto.dictKey); |
|
|
|
|
|
|
|
|
@ -152,12 +173,12 @@ export class CoreDictService { |
|
|
|
|
const checkRepeat = await this.getDictDataForDictKey(updateCoreDictDto.dictKey); |
|
|
|
|
|
|
|
|
|
// ? 判断是否存在重名的字典,但是不包括自己
|
|
|
|
|
if (checkRepeat.length > 0 && checkRepeat[0].dictId != pacCoreDictIdDto.dictId) { |
|
|
|
|
if (checkRepeat.length > 0 && checkRepeat[0].dictId != dictId) { |
|
|
|
|
throw new HttpException('服务繁忙,字典标识重复!', HttpStatus.CONFLICT); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ! 更新数据
|
|
|
|
|
const result = await this.updateDictData(pacCoreDictIdDto, updateCoreDictDto, pacInfo); |
|
|
|
|
const result = await this.updateDictData(dictId, updateCoreDictDto, pacInfo); |
|
|
|
|
|
|
|
|
|
// ! 解锁
|
|
|
|
|
lock(); |
|
|
|
@ -178,18 +199,23 @@ export class CoreDictService { |
|
|
|
|
* DESC: 删除字典 |
|
|
|
|
* DATE: 2024-06-04 22:01:11 - |
|
|
|
|
* */ |
|
|
|
|
public async remove(pacCoreDictIdDto: PacCoreDictIdDto, pacInfo: PacInfoType) { |
|
|
|
|
public async remove(dictId: string, pacInfo: PacInfoType) { |
|
|
|
|
// ! 查看目标字典项
|
|
|
|
|
const result = await this.getMore(pacCoreDictIdDto.dictId); |
|
|
|
|
const result = await this.getMore(dictId); |
|
|
|
|
|
|
|
|
|
// ? 判断是否存在
|
|
|
|
|
if (result.length > 0) { |
|
|
|
|
// ! root数据,非root用户不允许删除
|
|
|
|
|
if (result[0].root && this.config.get<number>('masterId') !== pacInfo.userId) { |
|
|
|
|
throw new HttpException('原始字典,权限不足无法删除!', HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ! 存在目标字典项
|
|
|
|
|
|
|
|
|
|
// ? 判断是否存在子项
|
|
|
|
|
if (result[0].haveChildren == 0) { |
|
|
|
|
// ! 删除字典
|
|
|
|
|
await this.deleteDictItem(pacCoreDictIdDto, pacInfo); |
|
|
|
|
await this.deleteDictItem(dictId, pacInfo); |
|
|
|
|
} else { |
|
|
|
|
throw new HttpException('该字典存在子项!', HttpStatus.BAD_REQUEST); |
|
|
|
|
} |
|
|
|
@ -216,9 +242,6 @@ export class CoreDictService { |
|
|
|
|
// ! 生成雪花id,用于字典主键
|
|
|
|
|
const id = await this.snowflake.generate(); |
|
|
|
|
|
|
|
|
|
// ? 判断身份是否是pac,如果是pac才能写入是pac的数据
|
|
|
|
|
const isPac = this.config.get<number>('masterId') == userId && isTrueEnum(createCoreDictDto.ownOfPac); |
|
|
|
|
|
|
|
|
|
// ! 定义写入的字典数据
|
|
|
|
|
const newDictData: typeof pacCoreDict.$inferInsert = { |
|
|
|
|
pid: createCoreDictDto.pid, |
|
|
|
@ -228,9 +251,9 @@ export class CoreDictService { |
|
|
|
|
dictName: createCoreDictDto.dictName, |
|
|
|
|
dictIcon: createCoreDictDto.dictIcon, |
|
|
|
|
dictType: createCoreDictDto.dictType, |
|
|
|
|
ownOfPac: isPac ? 0 : 1, |
|
|
|
|
root: isTrueEnum(createCoreDictDto) ? 1 : 0, |
|
|
|
|
orderNum: createCoreDictDto.orderNum, |
|
|
|
|
serviceOf: createCoreDictDto.serviceOf, |
|
|
|
|
serviceKey: createCoreDictDto.serviceKey, |
|
|
|
|
createby: userId, |
|
|
|
|
createtime: sql`now()` as any, |
|
|
|
|
}; |
|
|
|
@ -276,13 +299,13 @@ export class CoreDictService { |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// ? 是否查pac的数据
|
|
|
|
|
wl.push(eq(pacCoreDict.ownOfPac, isTrueEnum(data['ownOfPac']) ? 0 : 1).if(isExistKey(data, 'ownOfPac') && isTrueEnum(data['ownOfPac']))); |
|
|
|
|
wl.push(eq(pacCoreDict.root, isTrueEnum(data['root']) ? 0 : 1).if(isExistKey(data, 'root') && isTrueEnum(data['root']))); |
|
|
|
|
|
|
|
|
|
// ? 按照层级查
|
|
|
|
|
wl.push(eq(pacCoreDict.pid, data.hierarchy).if(isExistKey(data, 'hierarchy'))); |
|
|
|
|
|
|
|
|
|
// ? 是否存在目标service
|
|
|
|
|
wl.push(eq(pacCoreDict.serviceOf, data.serviceOf).if(isExistKey(data, 'serviceOf'))); |
|
|
|
|
wl.push(eq(pacCoreDict.serviceKey, data.serviceKey).if(isExistKey(data, 'serviceKey'))); |
|
|
|
|
|
|
|
|
|
// ? 是否查字典类型
|
|
|
|
|
wl.push(eq(pacCoreDict.dictType, data.dictType).if(isExistKey(data, 'dictType'))); |
|
|
|
@ -311,7 +334,7 @@ export class CoreDictService { |
|
|
|
|
// 使用基础查询构建分页查询
|
|
|
|
|
const paginatedQuery = this.getDict(getCoreDictAllDto, this.dictMoreDataType) |
|
|
|
|
.leftJoin(pacAuthUser, eq(pacCoreDict.createby, pacAuthUser.userId)) |
|
|
|
|
.leftJoin(pacCoreService, eq(pacCoreService.serviceKey, pacCoreDict.serviceOf)) |
|
|
|
|
.leftJoin(pacCoreService, eq(pacCoreService.serviceKey, pacCoreDict.serviceKey)) |
|
|
|
|
.limit(getCoreDictAllDto.pageSize) |
|
|
|
|
.offset(offset); |
|
|
|
|
|
|
|
|
@ -328,12 +351,12 @@ export class CoreDictService { |
|
|
|
|
.select(this.dictMoreDataType) |
|
|
|
|
.from(pacCoreDict) |
|
|
|
|
.leftJoin(pacAuthUser, eq(pacCoreDict.createby, pacAuthUser.userId)) |
|
|
|
|
.leftJoin(pacCoreService, eq(pacCoreService.serviceKey, pacCoreDict.serviceOf)) |
|
|
|
|
.leftJoin(pacCoreService, eq(pacCoreService.serviceKey, pacCoreDict.serviceKey)) |
|
|
|
|
.where(and(eq(pacCoreDict.dictId, dictId), isNull(pacCoreDict.deleteby))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// DB 通过Key查找目标字典的信息?树结构
|
|
|
|
|
private getTargetDictList(pacCoreDictKeyDto: PacCoreDictKeyDto, pacCoreDictTargetListDto: PacCoreDictTargetListDto) { |
|
|
|
|
// DB 通过Key查找目标字典的信息?树结构 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
|
|
|
|
private getTargetDictList(dictKey: string, pacCoreDictTargetListDto: PacCoreDictTargetListDto) { |
|
|
|
|
if (isExistKey(pacCoreDictTargetListDto, 'isTree') && isTrueEnum(pacCoreDictTargetListDto['isTree'])) { |
|
|
|
|
// ! 基础层级
|
|
|
|
|
const baseQueryBuilder = new QueryBuilder(); |
|
|
|
@ -343,7 +366,7 @@ export class CoreDictService { |
|
|
|
|
level: sql`0`.as('level'), |
|
|
|
|
}) |
|
|
|
|
.from(pacCoreDict) |
|
|
|
|
.where(and(isNull(pacCoreDict.deleteby), eq(pacCoreDict.dictKey, pacCoreDictKeyDto.dictKey))); |
|
|
|
|
.where(and(isNull(pacCoreDict.deleteby), eq(pacCoreDict.dictKey, dictKey))); |
|
|
|
|
|
|
|
|
|
// ! 递归层级
|
|
|
|
|
const recursiveQueryBuilder = new QueryBuilder(); |
|
|
|
@ -368,26 +391,24 @@ export class CoreDictService { |
|
|
|
|
.select(this.dictMoreDataType) |
|
|
|
|
.from(pacCoreDict) |
|
|
|
|
.leftJoin(pacAuthUser, eq(pacCoreDict.createby, pacAuthUser.userId)) |
|
|
|
|
.leftJoin(pacCoreService, eq(pacCoreService.serviceKey, pacCoreDict.serviceOf)) |
|
|
|
|
.where(and(eq(pacCoreDict.dictKey, pacCoreDictKeyDto.dictKey), isNull(pacCoreDict.deleteby))); |
|
|
|
|
.leftJoin(pacCoreService, eq(pacCoreService.serviceKey, pacCoreDict.serviceKey)) |
|
|
|
|
.where(and(eq(pacCoreDict.dictKey, dictKey), isNull(pacCoreDict.deleteby))); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// DB 通过ID删除字典项
|
|
|
|
|
private deleteDictItem(pacCoreDictIdDto: PacCoreDictIdDto, pacInfo: PacInfoType) { |
|
|
|
|
private deleteDictItem(dictId: string, pacInfo: PacInfoType) { |
|
|
|
|
return this.mysqlService.db |
|
|
|
|
.update(pacCoreDict) |
|
|
|
|
.set({ |
|
|
|
|
deletetime: sql`now()`, |
|
|
|
|
deleteby: pacInfo.userId, |
|
|
|
|
}) |
|
|
|
|
.where(eq(pacCoreDict.dictId, pacCoreDictIdDto.dictId)); |
|
|
|
|
.where(eq(pacCoreDict.dictId, dictId)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// DB 更新字典
|
|
|
|
|
private updateDictData(pacCoreDictIdDto: PacCoreDictIdDto, updateCoreDictDto: UpdateCoreDictDto, pacInfo: PacInfoType) { |
|
|
|
|
// ? 判断身份是否是pac,如果是pac才能写入是pac的数据
|
|
|
|
|
const isPac = this.config.get<number>('masterId') == pacInfo.userId && isTrueEnum(updateCoreDictDto.ownOfPac); |
|
|
|
|
private updateDictData(dictId: string, updateCoreDictDto: UpdateCoreDictDto, pacInfo: PacInfoType) { |
|
|
|
|
return this.mysqlService.db |
|
|
|
|
.update(pacCoreDict) |
|
|
|
|
.set({ |
|
|
|
@ -398,11 +419,11 @@ export class CoreDictService { |
|
|
|
|
dictType: updateCoreDictDto.dictType, |
|
|
|
|
orderNum: updateCoreDictDto.orderNum, |
|
|
|
|
status: updateCoreDictDto.status, |
|
|
|
|
ownOfPac: isPac ? 0 : 1, |
|
|
|
|
serviceOf: updateCoreDictDto.serviceOf, |
|
|
|
|
root: isTrueEnum(updateCoreDictDto.root) ? 1 : 0, |
|
|
|
|
serviceKey: updateCoreDictDto.serviceKey, |
|
|
|
|
updateby: pacInfo.userId, |
|
|
|
|
updatetime: sql`now()`, |
|
|
|
|
}) |
|
|
|
|
.where(eq(pacCoreDict.dictId, pacCoreDictIdDto.dictId)); |
|
|
|
|
.where(eq(pacCoreDict.dictId, dictId)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|