Helius
2021-06-16 5728be2af515b2200e782aa201ca5d4d67d9ea47
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package com.ibeetl.admin.core.web;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
 
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import com.ibeetl.admin.core.gen.AutoGen;
import com.ibeetl.admin.core.gen.HtmlGen;
import com.ibeetl.admin.core.gen.JSGen;
import com.ibeetl.admin.core.gen.JavaCodeGen;
import com.ibeetl.admin.core.gen.MavenProjectTarget;
import com.ibeetl.admin.core.gen.MdGen;
import com.ibeetl.admin.core.gen.WebTarget;
import com.ibeetl.admin.core.gen.model.Attribute;
import com.ibeetl.admin.core.gen.model.Entity;
import com.ibeetl.admin.core.service.CoreCodeGenService;
import com.ibeetl.admin.core.util.PlatformException;
 
@Controller
public class CoreCodeGenController {
    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/core/codeGen";
 
    @Autowired
    CoreCodeGenService codeGenService;
 
    @GetMapping(MODEL + "/index.do")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/core/codeGen/index.html");
        return view;
 
    }
    
    @PostMapping(MODEL + "/refresh.json")
    @ResponseBody
    public JsonResult<Boolean> refresh() {
        codeGenService.refresh();
        return JsonResult.success();
    }
 
 
    @GetMapping(MODEL + "/tableDetail.do")
    public ModelAndView tableDetail(String table) {
        ModelAndView view = new ModelAndView("/core/codeGen/edit.html");
        Entity entity = codeGenService.getEntityInfo(table);
        view.addObject("entity", entity);
        return view;
 
    }
    
    @GetMapping(MODEL + "/project.do")
    public ModelAndView project() {
        ModelAndView view = new ModelAndView("/core/codeGen/project.html");
        File file = new  File(MavenProjectTarget.detectRootPath());
        String root = file.getParent();
        //设置生成项目为当前运行项目的上一级项目
        view.addObject("path",root+File.separator+"sample");
        view.addObject("basePackage","com.corp.xxx");
        return view;
      
    }
    
    @PostMapping(MODEL + "/projectGen.json")
    @ResponseBody
    public JsonResult project(String path,String basePackage,String includeConsole) throws IOException {
        //includeConsole 当前版本忽略,总是添加一个系统管理功能,可以在pom中移除console
        
        //生成maven项目结构
        File maven = new  File(path);
        maven.mkdirs();
        File src = new File(maven,"src");
        src.mkdirs();
        File main = new File(src,"main");
        main.mkdir();
        File test = new File(src,"test");
        test.mkdir();
        File javsSource = new File(main,"java"); 
        javsSource.mkdir();
        File resource = new File(main,"resources"); 
        resource.mkdir();
        File sql = new File(resource,"sql"); 
        sql.mkdir();
        File staticFile = new File(resource,"static"); 
        staticFile.mkdir();
        File templatesFile = new File(resource,"templates"); 
        templatesFile.mkdir();
        
        String codePath = basePackage.replace(".", "/");
        File codeFile = new File(javsSource,codePath);
        codeFile.mkdirs();
        Configuration conf = Configuration.defaultConfiguration();
        String tempalteRoot = "codeTemplate/maven/";
        ClasspathResourceLoader loader = new ClasspathResourceLoader(Thread.currentThread().getContextClassLoader(),tempalteRoot);
        GroupTemplate gt = new GroupTemplate(loader,conf);
        FileWriter fw = null;
        
        //先生成入口程序
        Template mainJavaTempalte = gt.getTemplate("/main.java");
        mainJavaTempalte.binding("basePackage", basePackage);
        fw = new FileWriter(new File(codeFile,"MainApplication.java"));
        mainJavaTempalte.renderTo(fw);
       
        
        //生成pom文件
        Template pomTemplate = gt.getTemplate("/pomTemplate.xml");
        int index = basePackage.lastIndexOf(".");
        String projectGrop = basePackage.substring(0, index);
        String projectName = basePackage.substring(index+1);
        pomTemplate.binding("group", projectGrop);
        pomTemplate.binding("project", projectName);
        fw = new FileWriter(new File(maven,"pom.xml"));
        pomTemplate.renderTo(fw);
        
        //复制当前项目的配置文件
        File config = copy(resource,"application.properties");
        copy(resource,"beetl.properties");
        copy(resource,"btsql-ext.properties");
        copy(resource,"banner.txt");
   
        return JsonResult.success();
    }
    
    
    private File copy(File root,String fileName) throws IOException {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream input = loader.getResourceAsStream(fileName);
        if(input==null) {
            log.info("copy "+fileName+" error,不存在"); 
            return null;
        }
        
        File target = new File(root,fileName);
        FileOutputStream output = new FileOutputStream(target);
        try {
                
            byte[] buf = new byte[1024];        
            int bytesRead;        
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
         } finally {
             input.close();
             output.close();
         }
        return target;
    }
    
    
    
 
    @PostMapping(MODEL + "/table.json")
    @ResponseBody
    public JsonResult<List<Entity>> getTable() {
        List<Entity> list = codeGenService.getAllEntityInfo();
        return JsonResult.success(list);
    }
 
    @PostMapping(MODEL + "/tableDetail.json")
    @ResponseBody
    public JsonResult<Entity> getInfo(String table) {
        Entity entity = codeGenService.getEntityInfo(table);
        if (entity == null) {
            JsonResult.failMessage("表不存在");
        }
        return JsonResult.success(entity);
 
    }
 
    @PostMapping(MODEL + "/gen.json")
    @ResponseBody
    public JsonResult gen(EntityInfo data,String path) {
        Entity  entity = getEntitiyInfo(data);
        String urlBase = data.getUrlBase();
        String basePackage = data.getBasePackage();
        
        MavenProjectTarget target = new MavenProjectTarget(entity, basePackage);
        //生成到path目录下,按照maven工程解构生成
        File file = new File(path);
        if(!file.exists()) {
            throw new PlatformException("路径不存在 "+path);
        }
        target.setTargetPath(path);
        target.setUrlBase(urlBase);
 
        JSGen jsGen = new JSGen();
        jsGen.make(target, entity);
 
        HtmlGen htmlGen = new HtmlGen();
        htmlGen.make(target, entity);
        String  preffix =  urlBase.replace('/', '.');
        String basicFunctionCode = preffix+"."+entity.getCode();
        JavaCodeGen javaGen = new JavaCodeGen(basePackage, entity,basicFunctionCode);
        javaGen.make(target, entity);
 
        MdGen mdGen = new MdGen();
        mdGen.make(target, entity);
        if(entity.isAutoAddMenu()||entity.isAutoAddFunction()) {
            //自动增加功能点
            long  functionId = this.codeGenService.insertFunction(entity, urlBase);
            if(entity.isAutoAddMenu()) {
                this.codeGenService.insertMenu(functionId, entity, urlBase);
            }
        }
        
        
        
        return JsonResult.success();
 
    }
    
    @PostMapping(MODEL + "/getPath.json")
    @ResponseBody
    public JsonResult<String> getPath() {
        String path = MavenProjectTarget.detectRootPath();
        return JsonResult.success(path);
    }
 
    @PostMapping(MODEL + "/html.json")
    @ResponseBody
    public JsonResult<Map<String, String>> html(EntityInfo data) {
        
        Entity entity = getEntitiyInfo(data);
        String urlBase = data.getUrlBase();
        String basePackage = data.getBasePackage();
        WebTarget webTarget = new WebTarget(entity, basePackage);
        webTarget.setUrlBase(urlBase);
        HtmlGen htmlGen = new HtmlGen();
        htmlGen.make(webTarget, entity);
        Map<String, String> content = new HashMap<String, String>();
        for (Entry<Object, String> entry : webTarget.map.entrySet()) {
            AutoGen gen = (AutoGen) entry.getKey();
            String code = entry.getValue();
            content.put(gen.getName(), code);
        }
        return JsonResult.success(content);
 
    }
    
    @PostMapping(MODEL + "/js.json")
    @ResponseBody
    public JsonResult<Map<String, String>> js(EntityInfo data) {
        
        Entity entity = getEntitiyInfo(data);
        String urlBase = data.getUrlBase();
        String basePackage = data.getBasePackage();
        WebTarget webTarget = new WebTarget(entity, basePackage);
        webTarget.setUrlBase(urlBase);
        JSGen jsGen = new JSGen();
        jsGen.make(webTarget, entity);
        Map<String, String> content = new HashMap<String, String>();
        for (Entry<Object, String> entry : webTarget.map.entrySet()) {
            AutoGen gen = (AutoGen) entry.getKey();
            String code = entry.getValue();
            content.put(gen.getName(), code);
        }
        return JsonResult.success(content);
 
    }
    
    @PostMapping(MODEL + "/java.json")
    @ResponseBody
    public JsonResult<Map<String, String>> javaCode(EntityInfo data) {
        
        Entity entity = getEntitiyInfo(data);
        String urlBase = data.getUrlBase();
        String basePackage = data.getBasePackage();
        WebTarget webTarget = new WebTarget(entity, basePackage);
        webTarget.setUrlBase(urlBase);
        
        String  preffix =  urlBase.replace('/', '.');
        String basicFunctionCode = preffix+"."+entity.getCode();
        JavaCodeGen javaGen = new JavaCodeGen(basePackage,entity,basicFunctionCode);
        javaGen.make(webTarget, entity);
        Map<String, String> content = new HashMap<String, String>();
        for (Entry<Object, String> entry : webTarget.map.entrySet()) {
            AutoGen gen = (AutoGen) entry.getKey();
            String code = entry.getValue();
            content.put(gen.getName(), code);
        }
        
        
        return JsonResult.success(content);
 
    }
    
    @PostMapping(MODEL + "/sql.json")
    @ResponseBody
    public JsonResult<Map<String, String>> sql(EntityInfo data) {
        
        Entity entity = getEntitiyInfo(data);
        String urlBase = data.getUrlBase();
        String basePackage = data.getBasePackage();
        WebTarget webTarget = new WebTarget(entity, basePackage);
        webTarget.setUrlBase(urlBase);
        MdGen javaGen = new MdGen();
        javaGen.make(webTarget, entity);
        Map<String, String> content = new HashMap<String, String>();
        for (Entry<Object, String> entry : webTarget.map.entrySet()) {
            AutoGen gen = (AutoGen) entry.getKey();
            String code = entry.getValue();
            content.put(gen.getName(), code);
        }
        return JsonResult.success(content);
 
    }
    
    private Entity getEntitiyInfo(EntityInfo data) {
        Entity info = data.getEntity();
        String urlBase = data.getUrlBase();
        String basePackage = data.getBasePackage();
        Entity entity = codeGenService.getEntityInfo(info.getTableName());
        entity.setCode(info.getCode());
        entity.setName(info.getName());
        entity.setDisplayName(info.getDisplayName());
        entity.setSystem(info.getSystem());
        entity.setAttachment(data.entity.isAttachment());
        entity.setIncludeExcel(data.entity.isIncludeExcel());
        entity.setAutoAddFunction(info.isAutoAddFunction());
        entity.setAutoAddMenu(info.isAutoAddFunction());
        for (int i = 0; i < entity.getList().size(); i++) {
            Attribute attr = entity.getList().get(i);
            attr.setDisplayName(info.getList().get(i).getDisplayName());
            attr.setShowInQuery(info.getList().get(i).isShowInQuery());
            attr.setDictType(info.getList().get(i).getDictType());
            attr.setVerifyList(info.getList().get(i).getVerifyList());
            if(attr.getName().equals(data.getNameAttr())) {
                entity.setNameAttribute(attr);
            }
        }
        
        if (StringUtils.isEmpty(entity.getCode()) || StringUtils.isEmpty(entity.getSystem())) {
            throw new PlatformException("code,system不能为空");
        }
        
        
        return entity;
    }
    
 
    
 
    @GetMapping(MODEL + "/{table}/test.json")
    @ResponseBody
    public void test(@PathVariable String table) {
        String urlBase = "cms";
        Entity entity = new Entity();
        entity.setCode("blog");
        entity.setName("CmsBlog");
        entity.setDisplayName("博客");
        entity.setTableName("CMS_BLOG");
        entity.setSystem("console");
        
 
        Attribute idAttr = new Attribute();
        idAttr.setColName("id");
        idAttr.setJavaType("Long");
        idAttr.setName("id");
        idAttr.setDisplayName("编号");
        idAttr.setId(true);
        entity.setIdAttribute(idAttr);
 
        Attribute nameAttr = new Attribute();
        nameAttr.setColName("title");
        nameAttr.setJavaType("String");
        nameAttr.setName("title");
        nameAttr.setDisplayName("标题");
        nameAttr.setShowInQuery(true);
 
        Attribute contentAttr = new Attribute();
        contentAttr.setColName("content");
        contentAttr.setJavaType("String");
        contentAttr.setName("content");
        contentAttr.setDisplayName("内容");
        contentAttr.setShowInQuery(true);
 
        Attribute createTimeAttr = new Attribute();
        createTimeAttr.setColName("create_time");
        createTimeAttr.setJavaType("Date");
        createTimeAttr.setName("createTime");
        createTimeAttr.setDisplayName("创建日期");
        createTimeAttr.setShowInQuery(true);
 
        Attribute createUserIdAttr = new Attribute();
        createUserIdAttr.setColName("create_user_id");
        createUserIdAttr.setJavaType("Long");
        createUserIdAttr.setName("createUserId");
        createUserIdAttr.setDisplayName("创建人");
        createUserIdAttr.setShowInQuery(true);
 
        Attribute typeAttr = new Attribute();
        typeAttr.setColName("type");
        typeAttr.setJavaType("String");
        typeAttr.setName("type");
        typeAttr.setDisplayName("博客类型");
        typeAttr.setShowInQuery(true);
 
        entity.getList().add(idAttr);
        entity.getList().add(nameAttr);
        entity.getList().add(contentAttr);
        entity.getList().add(createTimeAttr);
        entity.getList().add(createUserIdAttr);
        entity.getList().add(typeAttr);
 
        entity.setNameAttribute(nameAttr);
        entity.setComment("这是一个测试模型");
        // ConsoleTarget target = new ConsoleTarget();
 
        String basePackage = "com.ibeetl.admin.console";
        MavenProjectTarget target = new MavenProjectTarget(entity, basePackage);
        target.setUrlBase("admin");
 
        JSGen jsGen = new JSGen();
        jsGen.make(target, entity);
 
        HtmlGen htmlGen = new HtmlGen();
        htmlGen.make(target, entity);
        String  preffix =  urlBase.replace('/', '.');
        String basicFunctionCode = preffix+"."+entity.getCode();
        JavaCodeGen javaGen = new JavaCodeGen(basePackage, entity,basicFunctionCode);
        javaGen.make(target, entity);
 
        MdGen mdGen = new MdGen();
        mdGen.make(target, entity);
 
    }
 
}
 
class EntityInfo {
    Entity entity;
    String urlBase;
    String basePackage;
    String nameAttr;
 
 
    public Entity getEntity() {
        return entity;
    }
 
    public void setEntity(Entity entity) {
        this.entity = entity;
    }
 
    public String getUrlBase() {
        return urlBase;
    }
 
    public void setUrlBase(String urlBase) {
        this.urlBase = urlBase;
    }
 
    public String getBasePackage() {
        return basePackage;
    }
 
    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }
 
    public String getNameAttr() {
        return nameAttr;
    }
 
    public void setNameAttr(String nameAttr) {
        this.nameAttr = nameAttr;
    }
 
   
 
}