import { Inject, Provide } from "@midwayjs/decorator"
|
import * as fs from 'fs';
|
|
/**
|
* 不需要登录的后台接口
|
*/
|
@Provide()
|
export class CreateEntityUtil {
|
|
createEntity(table) {
|
console.log('生成综合实体类任务开始执行');
|
let EntityName = this.firstToUpper3(table.entityName)
|
let columnstr = [];
|
columnstr.push(`import { EntityModel } from '@midwayjs/orm';\n`)
|
columnstr.push(`import { BaseEntity } from '@cool-midway/core';\n`)
|
columnstr.push(`import { Column, Double } from 'typeorm';\n`)
|
columnstr.push(`/**
|
* ${table.comment}
|
*/\n`)
|
columnstr.push(`@EntityModel('${table.name}') \n`)
|
columnstr.push(`export class ${EntityName} extends BaseEntity { \n`)
|
table.tbcolumn.forEach(item => {
|
if (item?.table_name == table.name) {
|
let fieldType = 'string'; //nullable length primary enum enumName width
|
let matResult = item.column_type.match(/\(([^)]*)\)/);
|
let length = 0;
|
if (matResult) {
|
length = matResult[1];
|
}
|
switch (item.data_type) {
|
case 'varchar':
|
fieldType = 'string'
|
item.column_default = !isNaN(item.column_default) ? item.column_default : '';
|
columnstr.push(`@Column({ comment:'${item.column_comment}', type:'${item.data_type}', default:'${item.column_default}', length:${length}})\n`)
|
break;
|
case 'text':
|
fieldType = 'string'
|
item.column_default = !isNaN(item.column_default) ? item.column_default : '';
|
columnstr.push(`@Column({ comment:'${item.column_comment}', type:'${item.data_type}', default:'${item.column_default}'})\n`)
|
break;
|
case 'int':
|
fieldType = 'number'
|
item.column_default = !isNaN(Number(item.column_default)) ? item.column_default : 0;
|
columnstr.push(`@Column({ comment:'${item.column_comment}', type:'${item.data_type}', default:${item.column_default}, width:${length}})\n`)
|
break;
|
case 'bigint':
|
fieldType = 'number'
|
item.column_default = !isNaN(Number(item.column_default)) ? item.column_default : 0;
|
columnstr.push(`@Column({ comment:'${item.column_comment}', type:'${item.data_type}', default:${item.column_default}}, width:${length})\n`)
|
break;
|
case 'tinyint':
|
fieldType = 'number'
|
item.column_default = !isNaN(Number(item.column_default)) ? item.column_default : 0;
|
columnstr.push(`@Column({ comment:'${item.column_comment}', type:'${item.data_type}', default:${item.column_default}, width:${length}})\n`)
|
break;
|
case 'datetime':
|
fieldType = 'Date'
|
item.column_default = item.column_default != 'null' ? item.column_default : `${new Date()}`;
|
columnstr.push(`@Column({ comment:'${item.column_comment}', type:'${item.data_type}'})\n`)
|
break;
|
case 'double':
|
item.column_default = !isNaN(Number(item.column_default)) ? item.column_default : 0;
|
fieldType = 'Double'
|
columnstr.push(`@Column({ comment:'${item.column_comment}', type:'${item.data_type}', default:${item.column_default}})\n`)
|
break;
|
case 'decimal':
|
item.column_default = !isNaN(Number(item.column_default)) ? item.column_default : 0;
|
fieldType = 'Double'
|
columnstr.push(`@Column({ comment:'${item.column_comment}', type:'${item.data_type}', default:${item.column_default}})\n`)
|
break;
|
default:
|
break;
|
}
|
|
// let fieldType = item.data_type=='varchar' ? 'string' : item.data_type=='int' ? 'number' : item.data_type=='bigint' ? 'number' : item.data_type=='tinyint' ? 'number'
|
columnstr.push(`${item.column_name}: ${fieldType};\n`)
|
columnstr.push(`\n`)
|
}
|
|
});
|
columnstr.push(`}`)
|
if (fs.existsSync(`../../business/entity/${table.entityName}.ts`)) {
|
console.log(' file is exists')
|
} else {
|
console.log(' file is not exists')
|
// API
|
fs.writeFile(`./src/app/modules/business/entity/${table.entityName}.ts`,
|
columnstr.join(''), (error) => {
|
// 创建失败
|
if (error) {
|
console.log(`创建失败:${error}`)
|
}
|
// 创建成功
|
console.log(`创建成功!`)
|
})
|
}
|
console.log(`--------------------生成实体类结束${table.entityName}Service--------------------`);
|
}
|
|
|
createService(table) {
|
console.log('生成服务类开始执行');
|
let EntityName = this.firstToUpper3(table.entityName)
|
let columnstr = [];
|
columnstr.push(`import { Provide } from '@midwayjs/decorator';\n`)
|
columnstr.push(`import { BaseService } from '@cool-midway/core';\n`)
|
columnstr.push(`import { InjectEntityModel } from '@midwayjs/orm';\n`)
|
columnstr.push(`import { Repository } from 'typeorm';\n`)
|
columnstr.push(`import { DataService } from '../../base/service/dataapi/dataapi';\n`)
|
columnstr.push(`import { ${EntityName} } from '../entity/${table.entityName}';\n`)
|
columnstr.push(`/**
|
* ${table.comment}
|
*/\n`)
|
columnstr.push(`@Provide()\n`)
|
columnstr.push(`export class ${EntityName}Service extends DataService { \n`)
|
columnstr.push(` @InjectEntityModel(${EntityName}) \n`)
|
columnstr.push(` ${table.entityName}: Repository<${EntityName}> \n`);
|
columnstr.push(`}`)
|
if (fs.existsSync(`../../business/service/${table.entityName}.ts`)) {
|
console.log(' file is exists')
|
} else {
|
console.log(' file is not exists')
|
// API
|
fs.writeFile(`./src/app/modules/business/service/${table.entityName}.ts`,
|
columnstr.join(''), (error) => {
|
// 创建失败
|
if (error) {
|
console.log(`创建失败:${error}`)
|
}
|
// 创建成功
|
console.log(`创建成功!`)
|
})
|
}
|
console.log(`--------------------生成服务类结束${EntityName}Service--------------------`);
|
}
|
|
createController(table) {
|
console.log('生成控制类开始执行');
|
let EntityName = this.firstToUpper3(table.entityName)
|
let columnstr = [];
|
columnstr.push(`import { ALL, Body, Inject, Post, Provide } from '@midwayjs/decorator';\n`)
|
columnstr.push(`import { CoolController, BaseController } from '@cool-midway/core';\n`)
|
columnstr.push(`import { ${EntityName} } from '../entity/${table.entityName}';\n`)
|
columnstr.push(`import { ${EntityName}Service } from '../service/${table.entityName}';\n`)
|
columnstr.push(`import { DataController } from '../../base/controller/dataController';\n`)
|
columnstr.push(`/**
|
* ${table.comment}
|
*/\n`)
|
columnstr.push(`@Provide()\n`)
|
columnstr.push(`@CoolController({ \n`)
|
columnstr.push(` api: ['add', 'delete', 'update', 'list'],\n`)
|
columnstr.push(` entity: ${EntityName},\n`)
|
columnstr.push(` service: ${EntityName}Service,\n`)
|
columnstr.push(`})\n`)
|
columnstr.push(`export class ${EntityName}Controller extends DataController {\n`)
|
columnstr.push(` @Inject()\n`)
|
columnstr.push(` ${table.entityName}Service: ${EntityName}Service;\n`)
|
columnstr.push(`}`)
|
if (fs.existsSync(`../../business/controller/${table.entityName}.ts`)) {
|
console.log(' file is exists')
|
} else {
|
console.log(' file is not exists')
|
// API
|
fs.writeFile(`./src/app/modules/business/controller/${table.entityName}.ts`,
|
columnstr.join(''), (error) => {
|
// 创建失败
|
if (error) {
|
console.log(`创建失败:${error}`)
|
}
|
// 创建成功
|
console.log(`创建成功!`)
|
})
|
}
|
}
|
|
|
firstToUpper3(str){
|
return str.toLowerCase().replace(/( |^)[a-z]/g,(L)=>L.toUpperCase());
|
}
|
|
}
|