feat: 新增license

This commit is contained in:
小蟑螂
2026-07-06 19:34:53 +08:00
parent 8a6cb23c07
commit ac890dcc5c
110 changed files with 10678 additions and 1368 deletions
@@ -0,0 +1,55 @@
import {
Body,
Controller,
Delete,
Get,
Param,
ParseIntPipe,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { LicenseService } from '../license/license.service';
import { CreateCustomerDto, CustomerQueryDto, UpdateCustomerDto } from './customer.dto';
import { CustomerService } from './customer.service';
@Controller('customers')
@UseGuards(JwtAuthGuard)
export class CustomerController {
constructor(
private readonly customerService: CustomerService,
private readonly licenseService: LicenseService,
) {}
@Get()
list(@Query() query: CustomerQueryDto) {
return this.customerService.list(query);
}
@Post()
create(@Body() dto: CreateCustomerDto) {
return this.customerService.create(dto);
}
@Get(':id')
findOne(@Param('id', ParseIntPipe) id: number) {
return this.customerService.findOne(id);
}
@Put(':id')
update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateCustomerDto) {
return this.customerService.update(id, dto);
}
@Delete(':id')
remove(@Param('id', ParseIntPipe) id: number) {
return this.customerService.remove(id);
}
@Get(':id/licenses')
licenses(@Param('id', ParseIntPipe) id: number) {
return this.licenseService.listByCustomer(id);
}
}
@@ -0,0 +1,68 @@
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsString, Min } from 'class-validator';
import { Type } from 'class-transformer';
export class CreateCustomerDto {
@IsString()
@IsOptional()
code?: string;
@IsString()
@IsNotEmpty()
name!: string;
@IsString()
@IsOptional()
contactName?: string;
@IsString()
@IsOptional()
contactPhone?: string;
@IsEmail()
@IsOptional()
contactEmail?: string;
@IsString()
@IsOptional()
remark?: string;
}
export class UpdateCustomerDto {
@IsString()
@IsOptional()
name?: string;
@IsString()
@IsOptional()
contactName?: string;
@IsString()
@IsOptional()
contactPhone?: string;
@IsEmail()
@IsOptional()
contactEmail?: string;
@IsInt()
@IsOptional()
status?: number;
@IsString()
@IsOptional()
remark?: string;
}
export class CustomerQueryDto {
@Type(() => Number)
@IsInt()
@Min(1)
@IsOptional()
page?: number = 1;
@Type(() => Number)
@IsInt()
@Min(1)
@IsOptional()
size?: number = 20;
}
@@ -0,0 +1,17 @@
import { Module, forwardRef } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { LcCustomer } from '../../entities/lc-customer.entity';
import { LicenseModule } from '../license/license.module';
import { CustomerController } from './customer.controller';
import { CustomerService } from './customer.service';
@Module({
imports: [
TypeOrmModule.forFeature([LcCustomer]),
forwardRef(() => LicenseModule),
],
controllers: [CustomerController],
providers: [CustomerService],
exports: [CustomerService],
})
export class CustomerModule {}
@@ -0,0 +1,67 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { LcCustomer } from '../../entities/lc-customer.entity';
import { CreateCustomerDto, CustomerQueryDto, UpdateCustomerDto } from './customer.dto';
@Injectable()
export class CustomerService {
constructor(
@InjectRepository(LcCustomer)
private readonly customerRepo: Repository<LcCustomer>,
) {}
async list(query: CustomerQueryDto) {
const page = query.page || 1;
const size = query.size || 20;
const [items, total] = await this.customerRepo.findAndCount({
order: { id: 'DESC' },
skip: (page - 1) * size,
take: size,
});
return { items, total, page, size };
}
async findOne(id: number) {
const customer = await this.customerRepo.findOne({ where: { id } });
if (!customer) throw new NotFoundException('客户不存在');
return customer;
}
async create(dto: CreateCustomerDto) {
const code = dto.code || (await this.generateCode());
const customer = this.customerRepo.create({
code,
name: dto.name,
contactName: dto.contactName ?? null,
contactPhone: dto.contactPhone ?? null,
contactEmail: dto.contactEmail ?? null,
remark: dto.remark ?? null,
status: 1,
});
return this.customerRepo.save(customer);
}
async update(id: number, dto: UpdateCustomerDto) {
const customer = await this.findOne(id);
Object.assign(customer, {
...dto,
contactName: dto.contactName ?? customer.contactName,
contactPhone: dto.contactPhone ?? customer.contactPhone,
contactEmail: dto.contactEmail ?? customer.contactEmail,
});
return this.customerRepo.save(customer);
}
async remove(id: number) {
const customer = await this.findOne(id);
await this.customerRepo.remove(customer);
return { success: true };
}
private async generateCode() {
const date = new Date().toISOString().slice(0, 10).replace(/-/g, '');
const count = await this.customerRepo.count();
return `CUST-${date}-${String(count + 1).padStart(3, '0')}`;
}
}