feat: 完成券商和用户管理
This commit is contained in:
@@ -5,13 +5,14 @@ import {
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Repository, FindOptionsWhere } from 'typeorm';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { User } from './user.entity';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { QueryUserDto } from './dto/query-user.dto';
|
||||
import { ChangePasswordDto } from './dto/change-password.dto';
|
||||
import { PaginationInfo } from '@/common/dto/pagination.dto';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
@@ -119,6 +120,88 @@ export class UserService {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户(支持多种查询条件和分页)
|
||||
*/
|
||||
async findAllPaginated(queryDto: QueryUserDto): Promise<{
|
||||
list: User[];
|
||||
pagination: PaginationInfo;
|
||||
}> {
|
||||
const where: FindOptionsWhere<User> = {};
|
||||
|
||||
if (queryDto.username) {
|
||||
where.username = queryDto.username;
|
||||
}
|
||||
|
||||
if (queryDto.nickname) {
|
||||
where.nickname = queryDto.nickname;
|
||||
}
|
||||
|
||||
if (queryDto.email) {
|
||||
where.email = queryDto.email;
|
||||
}
|
||||
|
||||
if (queryDto.phone) {
|
||||
where.phone = queryDto.phone;
|
||||
}
|
||||
|
||||
if (queryDto.role) {
|
||||
where.role = queryDto.role;
|
||||
}
|
||||
|
||||
if (queryDto.status) {
|
||||
where.status = queryDto.status;
|
||||
}
|
||||
|
||||
// 分页参数
|
||||
const page = queryDto.page || 1;
|
||||
const limit = queryDto.limit || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
// 排序字段映射
|
||||
const sortBy = queryDto.sortBy || 'createdAt';
|
||||
const sortOrder = queryDto.sortOrder || 'DESC';
|
||||
|
||||
// 构建排序对象
|
||||
const order: Record<string, 'ASC' | 'DESC'> = {};
|
||||
if (sortBy === 'createdAt') {
|
||||
order.createdAt = sortOrder;
|
||||
} else if (sortBy === 'updatedAt') {
|
||||
order.updatedAt = sortOrder;
|
||||
} else if (sortBy === 'lastLoginAt') {
|
||||
order.lastLoginAt = sortOrder;
|
||||
} else {
|
||||
order.createdAt = 'DESC';
|
||||
}
|
||||
|
||||
// 添加默认排序
|
||||
order.userId = 'ASC';
|
||||
|
||||
// 查询总数
|
||||
const total = await this.userRepository.count({ where });
|
||||
|
||||
// 查询分页数据
|
||||
const list = await this.userRepository.find({
|
||||
where,
|
||||
order,
|
||||
skip,
|
||||
take: limit,
|
||||
});
|
||||
|
||||
// 计算总页数
|
||||
const total_page = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
list,
|
||||
pagination: {
|
||||
total,
|
||||
total_page,
|
||||
page_size: limit,
|
||||
current_page: page,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 username 或 email 查询单个用户
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user