forked from wangziqi/gongxue-base
- 后端: NestJS 11 + TypeORM + JWT认证 + SQLite/MySQL - 前端: React 19 + Ant Design 6 + Vite 8 + ECharts - 功能模块: 数据面板、学生管理、宿舍管理、入住管理、费用录入、账单管理、教室管理、押金管理、操作日志、账号管理 - 支持Docker一键部署
51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository, Not } from 'typeorm';
|
|
import { Tenant } from '../entities/tenant.entity';
|
|
import { CreateTenantDto, UpdateTenantDto } from './dto/tenant.dto';
|
|
|
|
// 预设色板(避开红绿盲敏感色,保证差异度)
|
|
const COLOR_PALETTE = [
|
|
'#ff7875', '#ffa940', '#ffc53d', '#73d13d', '#36cfc9',
|
|
'#40a9ff', '#597ef7', '#9254de', '#f759ab', '#8c8c8c',
|
|
];
|
|
|
|
@Injectable()
|
|
export class TenantsService {
|
|
constructor(@InjectRepository(Tenant) private repo: Repository<Tenant>) {}
|
|
|
|
async findAll(query?: { includeArchived?: boolean }) {
|
|
const where: any = {};
|
|
if (!query?.includeArchived) where.status = Not('archived');
|
|
return this.repo.find({ where, order: { createdAt: 'DESC' } });
|
|
}
|
|
|
|
async findOne(id: number) {
|
|
const tenant = await this.repo.findOne({ where: { id } });
|
|
if (!tenant) throw new NotFoundException('租赁方不存在');
|
|
return tenant;
|
|
}
|
|
|
|
async create(dto: CreateTenantDto) {
|
|
// 颜色未指定则自动分配(按当前租赁方数量取模)
|
|
let color = dto.color;
|
|
if (!color) {
|
|
const total = await this.repo.count();
|
|
color = COLOR_PALETTE[total % COLOR_PALETTE.length];
|
|
}
|
|
return this.repo.save(this.repo.create({ ...dto, color }));
|
|
}
|
|
|
|
async update(id: number, dto: UpdateTenantDto) {
|
|
await this.findOne(id);
|
|
await this.repo.update(id, dto);
|
|
return this.repo.findOne({ where: { id } });
|
|
}
|
|
|
|
async remove(id: number) {
|
|
await this.findOne(id);
|
|
await this.repo.update(id, { status: 'archived' });
|
|
return { message: '已归档' };
|
|
}
|
|
}
|