diff --git a/src/app.controller.ts b/src/app.controller.ts index 7d82ef7..d30da7d 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -15,6 +15,7 @@ import { v4 as uuid } from 'uuid'; import { verificationService } from './lib/face'; import { join } from 'path'; import { AreaEntity } from './area.entity'; +import { DeviceEntity } from './device.entity'; export class Electronics { @ApiProperty({ description: "电子产品名称" }) @@ -254,6 +255,74 @@ export class DeleteArea { public id: string; } +export class DeviceDto extends CommonPageArgs { + @ApiPropertyOptional({ description: "根据名称模糊搜索", required: false }) + @IsString() + @IsOptional() + search?: string +} + +export class CreateDevice { + @ApiProperty({ description: "" }) + @IsString() + @IsNotEmpty() + public name: string; + + @ApiProperty({ description: "" }) + @IsString() + @IsOptional() + public cn: string; + + @ApiProperty({ description: "" }) + @IsString() + @IsOptional() + public ip: string; + + @ApiProperty({ description: "" }) + @IsString() + @IsOptional() + public area_id: string; + + @ApiProperty({ description: "" }) + @IsString() + @IsOptional() + public address: string; +} + +export class UpdateDevice { + @ApiProperty({ description: "" }) + @IsString() + @IsNotEmpty() + public name: string; + + @ApiProperty({ description: "" }) + @IsString() + @IsOptional() + public cn: string; + + @ApiProperty({ description: "" }) + @IsString() + @IsOptional() + public ip: string; + + @ApiProperty({ description: "" }) + @IsString() + @IsOptional() + public area_id: string; + + @ApiProperty({ description: "" }) + @IsString() + @IsOptional() + public address: string; +} + +export class DeleteDevice { + @ApiProperty({ description: "" }) + @IsString() + @IsNotEmpty() + public id: string; +} + export class QueryVisitorByNumberDto { @ApiPropertyOptional({ description: "身份证号", required: true }) @@ -276,6 +345,8 @@ export class AppController { private readonly nodeVisitorRepository: Repository, @InjectRepository(AreaEntity) private readonly areaRepository: Repository, + @InjectRepository(DeviceEntity) + private readonly deviceRepository: Repository, @InjectRepository(NodeOtherVisitorEntity) private readonly nodeOtherVisitorRepository: Repository, ) { } @@ -702,6 +773,132 @@ export class AppController { return { list, count } } + @Post("/device") + async createDevice(@Body() data: CreateDevice, @Req() req: any) { + if (!req.headers.authorization) { + throw new BadRequestException("无权限") + } + try { + // console.log(`${process.env.RUST_URI}/api/viewer`) + const result = await axios.get(`${process.env.RUST_URI}/api/viewer`, { + headers: { + authorization: req.headers.authorization + } + }) + // console.log("result", result) + } catch (e) { + // console.log(e) + throw new BadRequestException("无权限, 请联系管理员") + } + return await this.dataSource.transaction(async transactionalEntityManager => { + + const device = new DeviceEntity(); + device.name = data.name; + device.cn = data.cn; + device.ip = data.ip; + device.area_id = data.area_id; + device.address = data.address; + const result = await transactionalEntityManager.save(device); + + return { statusCode: 201, data: result }; + }); + } + + @Put("/device/:id") + async updateDevice(@Param() { id }: any, @Body() data: UpdateDevice, @Req() req: any) { + if (!req.headers.authorization) { + throw new BadRequestException("无权限") + } + try { + // console.log(`${process.env.RUST_URI}/api/viewer`) + const result = await axios.get(`${process.env.RUST_URI}/api/viewer`, { + headers: { + authorization: req.headers.authorization + } + }) + // console.log("result", result) + } catch (e) { + // console.log(e) + throw new BadRequestException("无权限, 请联系管理员") + } + const device = await this.deviceRepository.findOne({ where: { id } }); + if (!device) { + throw new NotFoundException("device not found"); + } + device.name = data.name; + device.cn = data.cn; + device.ip = data.ip; + device.area_id = data.area_id; + device.address = data.address; + const result = await this.deviceRepository.save(device); + return result; + } + + @Delete("/device/:id") + async deleteDevice(@Param() { id }: any, @Req() req: any) { + if (!req.headers.authorization) { + throw new BadRequestException("无权限") + } + try { + // console.log(`${process.env.RUST_URI}/api/viewer`) + const result = await axios.get(`${process.env.RUST_URI}/api/viewer`, { + headers: { + authorization: req.headers.authorization + } + }) + // console.log("result", result) + } catch (e) { + // console.log(e) + throw new BadRequestException("无权限, 请联系管理员") + } + const result = await this.deviceRepository.findOne({ where: { id } }); + if (!result) { + throw new NotFoundException("device not found"); + } + await this.deviceRepository.delete(id); + return result; + } + + @Get("/devices") + @ApiBearerAuth() + async devices(@Query() query_data: AreaDto, @Req() req: any) { + // console.log("req", req.headers) + if (!req.headers.authorization) { + throw new BadRequestException("无权限") + } + try { + // console.log(`${process.env.RUST_URI}/api/viewer`) + const result = await axios.get(`${process.env.RUST_URI}/api/viewer`, { + headers: { + authorization: req.headers.authorization + } + }) + // console.log("result", result) + } catch (e) { + // console.log(e) + throw new BadRequestException("无权限, 请联系管理员") + } + const where: any = {}; + const query = this.areaRepository.createQueryBuilder('area'); + query.where(where); + if (query_data.search) { + const string = `%${query_data.search}%`; + const fields = ['name']; + const searchString = fields.join(' like :search OR area.'); + query.where(`area.${searchString} like :search`, { + search: string, + }); + } + const order_key = 'area.created_date'; + let order_value: any = 'DESC'; + const [list, count] = await query + .skip(query_data.skip) + .take(query_data.take) + .orderBy(order_key, order_value) + .getManyAndCount(); + return { list, count } + } + @Put("/visitor/number") async updateVisitor(@Body() data: UpdateVisitorNumber) { const node_other_visitor = await this.nodeOtherVisitorRepository.findOne({ where: { id: data.id } }) diff --git a/src/app.module.ts b/src/app.module.ts index c972617..c5aea7d 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -13,6 +13,7 @@ import { VisitorEntity } from './visitor.entity'; import { NodeVisitorEntity } from './node-visitor.entity'; import { NodeOtherVisitorEntity } from './node-other-visitor.entity'; import { AreaEntity } from './area.entity'; +import { DeviceEntity } from './device.entity'; @Module({ imports: [ @@ -24,7 +25,8 @@ import { AreaEntity } from './area.entity'; VisitorEntity, NodeVisitorEntity, NodeOtherVisitorEntity, - AreaEntity + AreaEntity, + DeviceEntity ]), ScheduleModule.forRoot(), RoleModule, diff --git a/src/device.entity.ts b/src/device.entity.ts new file mode 100644 index 0000000..93ec49c --- /dev/null +++ b/src/device.entity.ts @@ -0,0 +1,28 @@ +import { BaseEntity, Column, Entity, Index, JoinColumn, OneToMany, OneToOne } from 'typeorm'; +import { createHmac } from 'crypto'; +import { Base } from './common'; +import { ApiProperty } from '@nestjs/swagger'; + +// 设备 +@Entity({ name: 'device' }) +export class DeviceEntity extends Base { + @ApiProperty({ description: "name" }) + @Column({ length: 150, nullable: false, type: 'char' }) + public name: string; + + @ApiProperty({ description: "cn" }) + @Column({ length: 150, nullable: true, type: 'char' }) + public cn?: string; + + @ApiProperty({ description: "ip" }) + @Column({ length: 150, nullable: true, type: 'char' }) + public ip?: string; + + @ApiProperty({ description: "" }) + @Column({ length: 36, nullable: true }) + public area_id?: string; + + @ApiProperty({ description: "设备地址" }) + @Column({ length: 255, nullable: true }) + public address?: string; +}