admin
2022-03-29 fac5d01bfcddfc8edef0a5fd3d401b1fe383fe16
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
package org.yeshi.utils.generater;
 
import org.springframework.data.mongodb.core.mapping.Document;
import org.yeshi.utils.FileUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.generater.entity.ClassInfo;
import org.yeshi.utils.generater.entity.admin.AdminGeneraterInfo;
import org.yeshi.utils.generater.mybatis.Table;
import org.yeshi.utils.generater.util.NameUtil;
import org.yeshi.utils.generater.vo.ExceptionVO;
import org.yeshi.utils.generater.vo.admin.controller.AdminControllerInfoVO;
import org.yeshi.utils.generater.vo.dao.MongoDBDaoVO;
import org.yeshi.utils.generater.vo.dao.MyBatisDBDaoVO;
import org.yeshi.utils.generater.vo.service.QueryVO;
import org.yeshi.utils.generater.vo.service.ServiceImplInfoVO;
import org.yeshi.utils.generater.vo.service.ServiceInfoVO;
import org.yeshi.utils.generater.vo.xmlconfig.GenertorConfig;
import org.yeshi.utils.mongo.MongodbBaseDao;
import org.yeshi.utils.mybatis.BaseMapper;
 
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
 
/**
 * @author Administrator
 * @title: GeneraterManager
 * @description: 自动化代码生成管理器(采用解析xml配置文件的形式)
 * @date 2021/10/13 14:48
 */
 
/**
 * Title
 *
 * @Description //TODO
 * @Author hxh
 * @Date 18:11 2021/10/13
 **/
public class GeneraterManagerV2 {
 
    private static GeneraterManagerV2 instance;
 
    private String classRootDir;
    private GenertorConfig config;
 
    public static GeneraterManagerV2 getInstance() {
        if (instance == null)
            instance = new GeneraterManagerV2();
        return instance;
    }
 
 
    public void init(String classRootDir, GenertorConfig config) {
        this.classRootDir = classRootDir;
        this.config = config;
    }
 
    //获取文件夹的路径
    private String getDirPath(String packageName) {
        File f = new File(classRootDir);
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(f.getAbsolutePath(), packageName.replace(".", "/"));
        if (!f.exists()) {
            f.mkdirs();
        }
        return f.getAbsolutePath();
    }
 
    /**
     * @return void
     * @author hxh
     * @description 保存文件
     * @date 10:06 2021/10/14
     * @param: data
     * @param: path
     **/
    private void saveFile(String data, String path) throws IOException {
        if (!new File(path).exists())
            new File(path).createNewFile();
        FileUtil.saveAsFileByte(data.getBytes("UTF-8"), path);
    }
 
    public void createException(Class base) throws Exception {
        if (config.getService().getException() == null) {
            throw new Exception("尚未配置异常信息");
        }
 
        String path = getDirPath(config.getService().getException().getPkg());
        try {
            ExceptionVO vo = new ExceptionVO.Builder().setEntity(config.getEntity()).setPackageName(config.getService().getException().getPkg()).setBase(new ClassInfo(base.getSimpleName(), base.getName())).build();
            String result = GeneraterUtil.createException(vo);
            //保存
            saveFile(result, path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void createDao() throws Exception {
 
        Annotation[] as = config.getEntity().getAnnotations();
        for (Annotation a : as) {
            if (a instanceof Table) {
                createMapper();
                break;
            } else if (a instanceof Document) {
                createMongoDBDao();
                break;
            }
        }
    }
 
    /**
     * @return org.yeshi.utils.generater.vo.dao.MongoDBDaoVO
     * @author hxh
     * @description 自动生成Dao
     * @date 18:24 2021/10/13
     * @param: packageName
     **/
    public MongoDBDaoVO createMongoDBDao() throws Exception {
        if (config.getDao() == null) {
            throw new Exception("尚未配置Dao信息");
        }
 
        String daoName = NameUtil.getDefaultDaoName(config.getEntity());
        String path = getDirPath(config.getDao().getPkg());
        MongoDBDaoVO vo = new MongoDBDaoVO.Builder()
                .setBaseDao(new ClassInfo(MongodbBaseDao.class.getSimpleName(), MongodbBaseDao.class.getName()))
                .setDao(new ClassInfo(daoName, config.getDao().getPkg() + "." + daoName))
                .setEntity(config.getEntity()).setDaoData(config.getDao()).build();
        String result = GeneraterUtil.createMongoDBDao(vo);
        //保存
        saveFile(result, new File(path, daoName + ".java").getAbsolutePath());
        return vo;
    }
 
    /**
     * @return org.yeshi.utils.generater.vo.dao.MyBatisDBDaoVO
     * @author hxh
     * @description 自动生成Mapper
     * @date 18:24 2021/10/13
     * @param: packageName
     * @param: mapperDir
     **/
    public MyBatisDBDaoVO createMapper() throws Exception {
        if (config.getDao() == null) {
            throw new Exception("尚未配置Dao信息");
        }
 
        if (StringUtil.isNullOrEmpty(config.getDao().getMapperDir())) {
            throw new Exception("Mapper存放目录为空");
        }
 
        String mapperName = NameUtil.getDefaultMapperName(config.getEntity());
        String path = getDirPath(config.getDao().getPkg());
        MyBatisDBDaoVO vo = new MyBatisDBDaoVO.Builder()
                .setBaseMapper(new ClassInfo(BaseMapper.class.getSimpleName(), BaseMapper.class.getName()))
                .setMapper(new ClassInfo(mapperName, config.getDao().getPkg() + "." + mapperName))
                .setEntity(config.getEntity())
                .setDaoData(config.getDao())
                .build();
 
        String result = GeneraterUtil.createMybatisDBMapper(vo);
        //保存
        saveFile(result, new File(path, mapperName + ".java").getAbsolutePath());
        if (!new File(config.getDao().getMapperDir()).exists())
            new File(config.getDao().getMapperDir()).mkdirs();
        result = GeneraterUtil.createMybatisDBMapperXML(vo);
        saveFile(result, new File(config.getDao().getMapperDir(), mapperName + ".xml").getAbsolutePath());
        return vo;
    }
 
    /**
     * @return org.yeshi.utils.generater.vo.service.QueryVO
     * @author hxh
     * @description /创建查询Query
     * @date 18:24 2021/10/13
     * @param: packageName
     **/
    public QueryVO createQuery(String packageName) throws Exception {
        //创建Query
        String path = getDirPath(packageName);
        QueryVO queryVO = new QueryVO.Builder().setEntity(config.getEntity()).setPackageName(packageName).build();
        String result = GeneraterUtil.createQuery(queryVO);
        saveFile(result, new File(path, queryVO.getClassName() + ".java").getAbsolutePath());
        return queryVO;
    }
 
    /**
     * @return org.yeshi.utils.generater.vo.service.ServiceInfoVO
     * @author hxh
     * @description 自动生成服务
     * @date 18:23 2021/10/13
     * @param: interPackageName
     * @param: implPackageName
     * @param: query
     * @param: dao
     **/
    public ServiceInfoVO createService(ClassInfo dao) throws Exception {
 
        if (config.getService() == null) {
            throw new Exception("尚未配置service");
        }
 
        if (config.getDao() == null && dao == null) {
            throw new Exception("请在xml中配置Dao,或者传入Dao信息");
        }
 
        if (dao == null) {
            if (StringUtil.isNullOrEmpty(config.getDao().getMapperDir())) {
                String daoName = NameUtil.getDefaultDaoName(config.getEntity());
                dao = new ClassInfo(daoName, config.getDao().getPkg() + "." + daoName);
            } else {
                String daoName = NameUtil.getDefaultMapperName(config.getEntity());
                dao = new ClassInfo(daoName, config.getDao().getPkg() + "." + daoName);
            }
        }
 
        QueryVO queryVO = createQuery(config.getService().getQueryPackage());
 
        ClassInfo query = new ClassInfo(queryVO.getClassName(), queryVO.getPackageName() + "." + queryVO.getClassName());
 
        //创建service接口
        String serviceName = NameUtil.getDefaultServiceName(config.getEntity());
        String path = getDirPath(config.getService().getInterPackage());
        ServiceInfoVO vo = new ServiceInfoVO.Builder()
                .setPackageName(config.getService().getInterPackage())
                .setEntity(config.getEntity())
                .setQuery(query)
                .setService(new ClassInfo(serviceName, config.getService().getInterPackage() + "." + serviceName))
                .build();
        String result = GeneraterUtil.createService(vo);
        saveFile(result, new File(path, serviceName + ".java").getAbsolutePath());
 
        //创建service实现
        String serviceImplName = NameUtil.getDefaultServiceImplName(config.getEntity());
        path = getDirPath(config.getService().getImplPackage());
        ServiceImplInfoVO implVO = new ServiceImplInfoVO.Builder()
                .setDao(dao)
                .setPackageName(config.getService().getImplPackage())
                .setEntity(config.getEntity())
                .setQuery(query)
                .setService(new ClassInfo(serviceName, config.getService().getInterPackage() + "." + serviceName))
                .setDaoQuery(new ClassInfo("DaoQuery", dao.getClazz() + ".DaoQuery"))
                .build();
        result = GeneraterUtil.createServiceImpl(implVO);
        saveFile(result, new File(path, serviceImplName + ".java").getAbsolutePath());
        return vo;
    }
 
 
    /**
     * @return void
     * @author hxh
     * @description 自动生成管理模块
     * @date 18:23 2021/10/13
     * @param: controllerPackage
     * @param: htmlDir
     * @param: query
     * @param: service
     **/
    public void createAdmin(ClassInfo query, ClassInfo service) throws Exception {
        if (config.getAdmin() == null) {
            throw new Exception("尚未配置admin");
        }
 
        if (service == null) {
            if (config.getService() == null) {
                throw new Exception("请传入service或者在xml中配置service");
            }
            String serviceName = NameUtil.getDefaultServiceName(config.getEntity());
            service = new ClassInfo(serviceName, config.getService().getInterPackage() + "." + serviceName);
        }
 
        if (query == null) {
            if (config.getService() == null) {
                throw new Exception("请传入service或者在xml中配置service");
            }
            String queryName = NameUtil.getDefaultQueryName(config.getEntity());
            query = new ClassInfo(queryName, config.getService().getQueryPackage() + "." + queryName);
        }
 
 
        String path = getDirPath(config.getAdmin().getController().getPkg());
 
        AdminGeneraterInfo generaterInfo = new AdminGeneraterInfo.Builder().setEntityClass(config.getEntity()).setAdminData(config.getAdmin()).build();
 
 
        AdminControllerInfoVO vo = new AdminControllerInfoVO.Builder().setAdminInfo(generaterInfo).setPackageName(generaterInfo.getControllerData().getPkg()).setQueryVO(query).setService(service).build();
        String controllerName = vo.getControllerName();
        //生成controller
        String result = GeneraterUtil.createAdminController(vo);
        saveFile(result, new File(path, controllerName + ".java").getAbsolutePath());
 
 
        if (!new File(generaterInfo.getPageDir()).exists()) {
            new File(generaterInfo.getPageDir()).mkdirs();
        }
 
        String htmlNamePrefix = getHtmlNameFromEntityName(config.getEntity().getSimpleName());
 
        if (generaterInfo.getShowDataList() != null && generaterInfo.getShowDataList().size() > 0) {
            result = GeneraterUtil.createAdminPageForList(generaterInfo);
            saveFile(result, new File(generaterInfo.getPageDir(), generaterInfo.getListFileName() != null ? generaterInfo.getListFileName() : htmlNamePrefix + "_list.html").getAbsolutePath());
        }
 
        if (config.getAdmin().getPages().getAdd() != null && generaterInfo.getAddFormRows() != null && generaterInfo.getAddFormRows().size() > 0) {
            result = GeneraterUtil.createAdminPageForAdd(generaterInfo);
            saveFile(result, new File(generaterInfo.getPageDir(), generaterInfo.getAddFileName() != null ? generaterInfo.getAddFileName() : htmlNamePrefix + "_add.html").getAbsolutePath());
        }
 
        if (config.getAdmin().getPages().getUpdate() != null && generaterInfo.getUpdateFormRows() != null && generaterInfo.getUpdateFormRows().size() > 0) {
            result = GeneraterUtil.createAdminPageForUpdate(generaterInfo);
            saveFile(result, new File(generaterInfo.getPageDir(), generaterInfo.getUpdateFileName() != null ? generaterInfo.getUpdateFileName() : htmlNamePrefix + "_update.html").getAbsolutePath());
        }
    }
 
 
    /**
     * @return void
     * @author hxh
     * @description 创建整个功能模块
     * @date 18:19 2021/10/13
     * @param: daoParams
     * @param: serviceParams
     * @param: adminParams
     **/
    public void createWholeFunction() throws Exception {
        Annotation[] as = config.getEntity().getAnnotations();
        int type = -1;
        for (Annotation a : as) {
            if (a instanceof Table) {
                type = 1;
                break;
            } else if (a instanceof Document) {
                type = 2;
                break;
            }
        }
        if (type == -1) {
            throw new Exception("实体未配置mybatis或者mongodb");
        }
 
        createDao();
        createService(null);
        createAdmin(null, null);
    }
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 通过entity的名称获取html文件的前缀名称
     * @date 18:25 2021/10/13
     * @param: entityName
     **/
    private static String getHtmlNameFromEntityName(String entityName) {
        StringBuffer sb = new StringBuffer();
        StringBuffer buffer = null;
        for (int i = 0; i < entityName.length(); i++) {
            if (entityName.charAt(i) >= 65 && entityName.charAt(i) <= 90) {
                //大写字母
                if (buffer == null)
                    buffer = new StringBuffer();
                buffer.append(entityName.charAt(i));
            } else {
                if (buffer != null) {
                    sb.append("_" + buffer.toString().toLowerCase());
                    buffer = null;
                }
                sb.append(entityName.charAt(i));
            }
        }
 
        if (buffer != null) {
            sb.append("_" + buffer.toString().toLowerCase());
        }
 
        if (sb.charAt(0) == '_')
            sb = sb.replace(0, 1, "");
        return sb.toString();
    }
 
 
    public static void main(String[] args) {
        System.out.print(getHtmlNameFromEntityName("TTestEEntity"));
    }
 
 
}