You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.4 KiB
TypeScript

import { BadRequestException, Body, Controller, Get, Post } from '@nestjs/common';
import { AppService } from './app.service';
import * as fs from 'fs';
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsBoolean, IsDateString, IsNotEmpty, IsString } from 'class-validator';
import { NodeVisitorEntity } from './node-visitor.entity';
import { DataSource } from 'typeorm';
import * as bluebird from 'bluebird';
import { NodeOtherVisitorEntity } from './node-other-visitor.entity';
import * as moment from 'moment';
export class Electronics {
@ApiProperty({ description: "电子产品名称" })
@IsString()
@IsNotEmpty()
public name: string;
}
export class VisitorList {
@ApiProperty({ description: "访客姓名" })
@IsString()
@IsNotEmpty()
public name: string;
@ApiProperty({ description: "访客身份证号" })
@IsString()
@IsNotEmpty()
public identity_card_no: string;
@ApiProperty({ description: "国籍(中国/其他国籍)" })
@IsString()
@IsNotEmpty()
public nationality_type: string;
@ApiProperty({ description: "国籍名称" })
@IsString()
@IsNotEmpty()
public nationality: string;
}
export class CreateVisitor {
@ApiProperty({ description: "申请人" })
@IsString()
@IsNotEmpty()
public applicant: string;
@ApiProperty({ description: "申请人部门" })
@IsString()
@IsNotEmpty()
public applicant_department: string;
@ApiProperty({ description: "申请日期" })
@IsDateString()
@IsNotEmpty()
public apply_date: string;
@ApiProperty({ description: "申请单号" })
@IsString()
@IsNotEmpty()
public code: string;
// TODO 确定访客类型
@ApiProperty({ description: "来访类型" })
@IsString()
@IsNotEmpty()
public visitor_type: string;
@ApiProperty({ description: "来访区域" })
@IsString()
@IsNotEmpty()
public area: string;
@ApiProperty({ description: "来访单位" })
@IsString()
@IsNotEmpty()
public visitor_unit: string;
@ApiProperty({ description: "访客人数" })
@IsString()
@IsNotEmpty()
public visitor_number: string;
@ApiProperty({ description: "交通方式" })
@IsString()
@IsNotEmpty()
public transport: string;
@ApiProperty({ description: "车牌号" })
@IsString()
@IsNotEmpty()
public plate_no: string;
@ApiProperty({ description: "起始日期" })
@IsDateString()
@IsNotEmpty()
public start_date: string;
@ApiProperty({ description: "截止日期" })
@IsDateString()
@IsNotEmpty()
public end_date: string;
@ApiProperty({ description: "被访人" })
@IsString()
@IsNotEmpty()
public visited_staff: string;
@ApiProperty({ description: "被访部门" })
@IsString()
@IsNotEmpty()
public visited_deparment: string;
@ApiProperty({ description: "被访部门" })
@IsString()
@IsNotEmpty()
public purpose: string;
@ApiProperty({ description: "携带的电子产品" })
// @ApiProperty({ description: "携带的电子产品", type: [Electronics] })
@IsArray()
@IsNotEmpty()
public electronics: string;
// public electronics: Electronics[];
@ApiProperty({ description: "访客清单", type: [VisitorList] })
@IsArray()
@IsNotEmpty()
public visitor_list: VisitorList[];
@ApiProperty({ description: "访客是否有可能接触受控设备" })
@IsBoolean()
@IsNotEmpty()
public may_access_sensitive_info: boolean;
}
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private readonly dataSource: DataSource,
) { }
@Get()
getHello(): string {
return this.appService.getHello();
}
// @Get("/doc/json")
// getDoc(): string {
// const json = fs.readFileSync("swagger-spec.json", "utf-8");
// return JSON.parse(json);
// }
// @Get("/app.config")
// appConfig() {
// return {
// app_id: process.env.QUNSENSE_SUITE_ID
// }
// }
// @Get("/WW_verify_n9zX2u8E9ShFgYmx.txt")
// config(): string {
// return "n9zX2u8E9ShFgYmx";
// }
@Post("/visitor")
async create(@Body() data: CreateVisitor) {
if (new Date(data.apply_date).toString() == "Invalid Date") {
throw new BadRequestException("apply_date 格式不正确")
}
if (new Date(data.start_date).toString() == "Invalid Date") {
throw new BadRequestException("start_date 格式不正确")
}
if (new Date(data.end_date).toString() == "Invalid Date") {
throw new BadRequestException("end_date 格式不正确")
}
return await this.dataSource.transaction(async transactionalEntityManager => {
const new_node_visitor = new NodeVisitorEntity();
new_node_visitor.applicant = data.applicant;
new_node_visitor.applicant_department = data.applicant_department;
new_node_visitor.apply_date = moment(data.apply_date).format("YYYY-MM-DD");
// new_node_visitor.apply_date = new Date(data.apply_date).toLocaleDateString();
new_node_visitor.code = data.code;
new_node_visitor.visitor_type = data.visitor_type;
new_node_visitor.area = data.area;
new_node_visitor.visitor_unit = data.visitor_unit;
new_node_visitor.visitor_number = data.visitor_number;
new_node_visitor.transport = data.transport;
new_node_visitor.plate_no = data.plate_no;
// new_node_visitor.start_date = new Date(data.start_date).toLocaleDateString();
new_node_visitor.start_date = moment(data.start_date).format("YYYY-MM-DD");
// new_node_visitor.end_date = new Date(data.end_date).toLocaleDateString();
new_node_visitor.end_date = moment(data.end_date).format("YYYY-MM-DD");
new_node_visitor.visited_staff = data.visited_staff;
new_node_visitor.visited_deparment = data.visited_deparment;
new_node_visitor.purpose = data.purpose;
new_node_visitor.electronics = data.electronics;
// new_node_visitor.electronics = JSON.stringify(data.electronics);
// new_node_visitor.visitor_list = data.visitor_list;
new_node_visitor.may_access_sensitive_info = data.may_access_sensitive_info;
const result = await transactionalEntityManager.save(new_node_visitor);
await bluebird.each(data.visitor_list, async (item) => {
const new_node_other_visitor = new NodeOtherVisitorEntity();
new_node_other_visitor.name = item.name;
new_node_other_visitor.identity_card_no = item.identity_card_no;
new_node_other_visitor.nationality_type = item.nationality_type;
new_node_other_visitor.nationality = item.nationality;
})
return { statusCode: 201, data: result };
});
}
}