fix: 分页查询时查询条件的number类型空值处理

This commit is contained in:
louis 2024-02-28 15:07:47 +08:00
parent 5b562e1d3b
commit 2821dc6dad
9 changed files with 42 additions and 17 deletions

View File

@ -19,7 +19,7 @@ DB_DATABASE = hxoa
DB_USERNAME = root DB_USERNAME = root
DB_PASSWORD = huaxin123 DB_PASSWORD = huaxin123
DB_SYNCHRONIZE = true DB_SYNCHRONIZE = true
DB_LOGGING = ["error"] DB_LOGGING = "all"
# redis # redis
REDIS_PORT = 6379 REDIS_PORT = 6379

View File

@ -2,7 +2,7 @@ import { InjectRedis } from '@liaoliaots/nestjs-redis'
import { Injectable } from '@nestjs/common' import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm' import { InjectRepository } from '@nestjs/typeorm'
import Redis from 'ioredis' import Redis from 'ioredis'
import { concat, isEmpty, uniq } from 'lodash' import { concat, isEmpty, isNumber, uniq } from 'lodash'
import { In, IsNull, Like, Not, Repository } from 'typeorm' import { In, IsNull, Like, Not, Repository } from 'typeorm'
@ -45,7 +45,7 @@ export class MenuService {
...(path && { path: Like(`%${path}%`) }), ...(path && { path: Like(`%${path}%`) }),
...(permission && { permission: Like(`%${permission}%`) }), ...(permission && { permission: Like(`%${permission}%`) }),
...(component && { component: Like(`%${component}%`) }), ...(component && { component: Like(`%${component}%`) }),
...(status && { status }), ...(isNumber(status) ? { status } : null),
}, },
order: { orderNo: 'ASC' }, order: { orderNo: 'ASC' },
}) })

View File

@ -19,7 +19,7 @@ import { RoleEntity } from '~/modules/system/role/role.entity'
import { MenuService } from '../menu/menu.service' import { MenuService } from '../menu/menu.service'
import { RoleDto, RoleUpdateDto } from './role.dto' import { RoleDto, RoleQueryDto, RoleUpdateDto } from './role.dto'
import { RoleInfo } from './role.model' import { RoleInfo } from './role.model'
import { RoleService } from './role.service' import { RoleService } from './role.service'
@ -44,7 +44,7 @@ export class RoleController {
@ApiOperation({ summary: '获取角色列表' }) @ApiOperation({ summary: '获取角色列表' })
@ApiResult({ type: [RoleEntity], isPage: true }) @ApiResult({ type: [RoleEntity], isPage: true })
@Perm(permissions.LIST) @Perm(permissions.LIST)
async list(@Query() dto: PagerDto) { async list(@Query() dto: RoleQueryDto) {
return this.roleService.findAll(dto) return this.roleService.findAll(dto)
} }

View File

@ -1,12 +1,14 @@
import { ApiProperty, PartialType } from '@nestjs/swagger' import { ApiProperty, IntersectionType, PartialType } from '@nestjs/swagger'
import { import {
IsArray, IsArray,
IsIn, IsIn,
IsInt,
IsOptional, IsOptional,
IsString, IsString,
Matches, Matches,
MinLength, MinLength,
} from 'class-validator' } from 'class-validator'
import { PagerDto } from '~/common/dto/pager.dto'
export class RoleDto { export class RoleDto {
@ApiProperty({ description: '角色名称' }) @ApiProperty({ description: '角色名称' })
@ -36,3 +38,11 @@ export class RoleDto {
} }
export class RoleUpdateDto extends PartialType(RoleDto) {} export class RoleUpdateDto extends PartialType(RoleDto) {}
export class RoleQueryDto extends IntersectionType(PagerDto<RoleDto>, PartialType(RoleDto)) {
@ApiProperty({ description: '状态', example: 0, required: false })
@IsInt()
@IsOptional()
status?: number
}

View File

@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common' import { Injectable } from '@nestjs/common'
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm' import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm'
import { isEmpty } from 'lodash' import { isEmpty, isNumber } from 'lodash'
import { EntityManager, In, Repository } from 'typeorm' import { EntityManager, In, Like, Repository } from 'typeorm'
import { PagerDto } from '~/common/dto/pager.dto' import { PagerDto } from '~/common/dto/pager.dto'
import { ROOT_ROLE_ID } from '~/constants/system.constant' import { ROOT_ROLE_ID } from '~/constants/system.constant'
@ -11,7 +11,7 @@ import { SseService } from '~/modules/sse/sse.service'
import { MenuEntity } from '~/modules/system/menu/menu.entity' import { MenuEntity } from '~/modules/system/menu/menu.entity'
import { RoleEntity } from '~/modules/system/role/role.entity' import { RoleEntity } from '~/modules/system/role/role.entity'
import { RoleDto, RoleUpdateDto } from './role.dto' import { RoleDto, RoleQueryDto, RoleUpdateDto } from './role.dto'
@Injectable() @Injectable()
export class RoleService { export class RoleService {
@ -31,8 +31,22 @@ export class RoleService {
async findAll({ async findAll({
page, page,
pageSize, pageSize,
}: PagerDto): Promise<Pagination<RoleEntity>> { name,
return paginate(this.roleRepository, { page, pageSize }) value,
status,
}: RoleQueryDto): Promise<Pagination<RoleEntity>> {
const queryBuilder = this.roleRepository
.createQueryBuilder('role')
.where({
...(name ? { name: Like(`%${name}%`) } : null),
...(value ? { value: Like(`%${value}%`) } : null),
...(isNumber(status) ? { status } : null),
})
return paginate<RoleEntity>(queryBuilder, {
page,
pageSize,
})
} }
/** /**

View File

@ -12,7 +12,7 @@ import { UnknownElementException } from '@nestjs/core/errors/exceptions/unknown-
import { InjectRepository } from '@nestjs/typeorm' import { InjectRepository } from '@nestjs/typeorm'
import { Queue } from 'bull' import { Queue } from 'bull'
import Redis from 'ioredis' import Redis from 'ioredis'
import { isEmpty } from 'lodash' import { isEmpty, isNumber } from 'lodash'
import { Like, Repository } from 'typeorm' import { Like, Repository } from 'typeorm'
import { BusinessException } from '~/common/exceptions/biz.exception' import { BusinessException } from '~/common/exceptions/biz.exception'
@ -103,7 +103,7 @@ export class TaskService implements OnModuleInit {
...(name ? { name: Like(`%${name}%`) } : null), ...(name ? { name: Like(`%${name}%`) } : null),
...(service ? { service: Like(`%${service}%`) } : null), ...(service ? { service: Like(`%${service}%`) } : null),
...(type ? { type } : null), ...(type ? { type } : null),
...(status ? { status } : null), ...(isNumber(status) ? { status } : null),
}) })
.orderBy('task.id', 'ASC') .orderBy('task.id', 'ASC')

View File

@ -27,7 +27,7 @@ export class UserDto {
@ApiProperty({ description: '登录账号', example: 'admin' }) @ApiProperty({ description: '登录账号', example: 'admin' })
@IsString() @IsString()
@Matches(/^[a-z0-9A-Z\W_]+$/) @Matches(/^[a-z0-9A-Z\W_]+$/)
@MinLength(4) @MinLength(1)
@MaxLength(20) @MaxLength(20)
username: string username: string
@ -95,4 +95,5 @@ export class UserQueryDto extends IntersectionType(PagerDto<UserDto>, PartialTyp
@IsInt() @IsInt()
@IsOptional() @IsOptional()
status?: number status?: number
} }

View File

@ -2,7 +2,7 @@ import { InjectRedis } from '@liaoliaots/nestjs-redis'
import { BadRequestException, Injectable } from '@nestjs/common' import { BadRequestException, Injectable } from '@nestjs/common'
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm' import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm'
import Redis from 'ioredis' import Redis from 'ioredis'
import { isEmpty, isNil } from 'lodash' import { isEmpty, isNil, isNumber } from 'lodash'
import { EntityManager, In, Like, Repository } from 'typeorm' import { EntityManager, In, Like, Repository } from 'typeorm'
@ -281,7 +281,7 @@ export class UserService {
...(username ? { username: Like(`%${username}%`) } : null), ...(username ? { username: Like(`%${username}%`) } : null),
...(nickname ? { nickname: Like(`%${nickname}%`) } : null), ...(nickname ? { nickname: Like(`%${nickname}%`) } : null),
...(email ? { email: Like(`%${email}%`) } : null), ...(email ? { email: Like(`%${email}%`) } : null),
...(status ? { status } : null), ...(isNumber(status) ? { status } : null),
}) })
if (deptId) if (deptId)

View File

@ -3,7 +3,7 @@ import { Logger as ITypeORMLogger, LoggerOptions, QueryRunner } from 'typeorm'
export class TypeORMLogger implements ITypeORMLogger { export class TypeORMLogger implements ITypeORMLogger {
private logger = new Logger(TypeORMLogger.name) private logger = new Logger(TypeORMLogger.name)
constructor(private options: LoggerOptions) {} constructor(private options: LoggerOptions) {}
logQuery(query: string, parameters?: any[], _queryRunner?: QueryRunner) { logQuery(query: string, parameters?: any[], _queryRunner?: QueryRunner) {