wangrong
2022-03-23 9e3033c25283dff92244ccc0156b3ad1ee409767
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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, Get, Inject, Post, Provide, Query } 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(`  \n`) 
        columnstr.push(`  /**\n  * 获得数据\n  */\n`) 
        columnstr.push(`   @Get('/getdata', { summary: '数据接口' })\n`) 
        columnstr.push(`   async getData(@Query(ALL) params: any) {\n`) 
        columnstr.push(`      return this.ok(await this.dataService.getData(params));\n`) 
        columnstr.push(`   }\n`) 
        columnstr.push(`  /**\n  * 获得数据\n  */\n`) 
        columnstr.push(`   @Get('/postdata', { summary: '数据接口' })\n`) 
        columnstr.push(`   async postData(@Query(ALL) params: any) {\n`) 
        columnstr.push(`      return this.ok(await this.dataService.getData(params));\n`) 
        columnstr.push(`   }\n`)
        columnstr.push(`}`)
        if (fs.existsSync(`../../business/controller/admin/${table.entityName}.ts`)) {
            console.log(' file is exists')
        } else {
            console.log(' file is not exists')
            // API
            fs.writeFile(`./src/app/modules/business/controller/admin/${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());
    }
 
}