xiangpei
2025-04-18 ccadf9480d4e6a9dcc227a2a0b1f9ae0612e36fd
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
package com.monkeylessey.gen.engine;
 
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.monkeylessey.enums.general.MenuTypeEnum;
import com.monkeylessey.enums.general.StatusEnum;
import com.monkeylessey.framework.utils.SecurityUtil;
import com.monkeylessey.gen.domain.GenerateData;
import com.monkeylessey.gen.enums.GenFileTypeEnum;
import com.monkeylessey.gen.handler.FormHandler;
import com.monkeylessey.gen.template.VueTemplate;
import com.monkeylessey.gen.utils.WordsUtils;
import com.monkeylessey.sys.domain.entity.SysMenu;
import com.monkeylessey.sys.mapper.SysMenuMapper;
 
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * @author xp
 * @version 1.0
 * @date 2022/5/17
 */
public class MyVelocityTemplateEngine extends VelocityTemplateEngine {
 
    private GenerateData data;
    private SysMenuMapper sysMenuMapper;
 
    public MyVelocityTemplateEngine(GenerateData data, SysMenuMapper sysMenuMapper) {
        this.data = data;
        this.sysMenuMapper = sysMenuMapper;
    }
 
    private Integer count = 0;
 
    @Override
    protected void outputCustomFile( Map<String, String> customFile,  TableInfo tableInfo,  Map<String, Object> objectMap) {
        String entityName = tableInfo.getEntityName();
        String forApiFileName = WordsUtils.firstWordToLower(entityName);
        String otherPath = getPathInfo(OutputFile.other);
        customFile.forEach((key, value) -> {
            String fileName = "";
            if (GenFileTypeEnum.FORM.getValue().equals(key)) {
                fileName = String.format((otherPath + File.separator + "form" + File.separator + entityName  + "%s" + suffixJavaOrKt()), key);
            }
            else if (GenFileTypeEnum.VO.getValue().equals(key)) {
                fileName = String.format((otherPath + File.separator + "vo" + File.separator + entityName  + "%s" + suffixJavaOrKt()), key);
            }
            else if (GenFileTypeEnum.QUERY.getValue().equals(key)) {
                fileName = String.format((otherPath + File.separator + "query" + File.separator + entityName  + "%s" + suffixJavaOrKt()), key);
            }
            else if (GenFileTypeEnum.VIEW.getValue().equals(key) ||
                    GenFileTypeEnum.DIALOG.getValue().equals(key) ||
                    GenFileTypeEnum.TABLE.getValue().equals(key))
            {
                fileName = String.format((otherPath + File.separator + "vue" + File.separator + entityName  + "%s" + ".vue"), key);
            }
            else if (GenFileTypeEnum.STORE.getValue().equals(key)) {
                fileName = String.format((otherPath + File.separator + "vue" + File.separator + "index.js"));
            } else if (GenFileTypeEnum.API.getValue().equals(key)) {
                fileName = String.format((otherPath + File.separator + "vue" + File.separator + forApiFileName  + ".js"));
            }
            outputFile(new File(fileName), objectMap, value, getConfigBuilder().getInjectionConfig().isFileOverride());
        });
    }
 
    @Override
    public void writer(Map<String, Object> objectMap, String templatePath, File outputFile) throws Exception {
        // 获取table
        TableInfo tableInfo = (TableInfo) objectMap.get("table");
        // 取出Service接口的名字,进行首字母小写处理
        String serviceNameFirstWord = tableInfo.getServiceName().substring(0,1).toLowerCase();
        String serviceNameFirstWordToLower = serviceNameFirstWord + tableInfo.getServiceName().substring(1);
        objectMap.put("serviceNameFirstWordToLower", serviceNameFirstWordToLower);
 
        // 取出mapper接口的名字,进行首字母小写处理
        String mapperNameFirstWord = tableInfo.getMapperName().substring(0, 1).toLowerCase();
        String mapperNameFirstWordToLower = mapperNameFirstWord + tableInfo.getMapperName().substring(1);
        objectMap.put("mapperNameFirstWordToLower", mapperNameFirstWordToLower);
 
        // 对Form、VO、query的处理
        objectMap = new FormHandler().handler(objectMap, data);
        super.writer(objectMap, templatePath, outputFile);
 
        count += 1;
        if (count == 1) {
 
            String menuPath = "/" + objectMap.get("controllerMappingHyphen");
            String routerName = ((VueTemplate) objectMap.get("vueInfo")).getName();
            String routerComponent = ((VueTemplate) objectMap.get("vueInfo")).getViewName();
            Date now = new Date();
            String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
 
            SysMenu hasMenu = new LambdaQueryChainWrapper<>(sysMenuMapper)
                    .eq(SysMenu::getMenuName, ((TableInfo) objectMap.get("table")).getComment())
                    .eq(SysMenu::getMenuPath, menuPath)
                    .one();
            if (Objects.nonNull(hasMenu)) {
                return;
            }
            SysMenu menu = new SysMenu();
            menu.setMenuName(((TableInfo) objectMap.get("table")).getComment());
            menu.setMenuType(MenuTypeEnum.MENU);
            menu.setStatus(StatusEnum.ACTIVE);
            menu.setMenuIcon(null);
            menu.setMenuPath(menuPath);
            menu.setRouterName(routerName);
            menu.setRouterComponent(routerComponent);
            menu.setPermission(null);
            menu.setOrderNum(100);
            menu.setParentId("0");
            menu.setGmtCreate(now);
            menu.setCreateBy(SecurityUtil.getCurrentUserName());
            menu.setGmtUpdate(now);
            menu.setDeleted(0);
            sysMenuMapper.insert(menu);
 
            // 然后添加6个操作按钮
            String lowerName = ((VueTemplate) objectMap.get("vueInfo")).getLowerName();
            List<String> permissons = Arrays.asList(lowerName + ":add", lowerName + ":edit", lowerName + ":del",
                    lowerName + ":del:batch", lowerName + ":page", lowerName + ":list", lowerName + "detail");
            for (int i = 0; i < permissons.size(); i++) {
                String per = permissons.get(i);
                String menuName = "";
                if (per.equals(lowerName + ":add")) {
                    menuName = "添加";
                } else if (per.equals(lowerName + ":edit")) {
                    menuName = "修改";
                } else if (per.equals(lowerName + ":del")) {
                    menuName = "删除";
                } else if (per.equals(lowerName + ":del:batch")) {
                    menuName = "批量删除";
                } else if (per.equals(lowerName + ":page")) {
                    menuName = "分页查询";
                } else if (per.equals(lowerName + ":list")) {
                    menuName = "列表查询";
                } else if (per.equals(lowerName + "detail")) {
                    menuName = "查看详情";
                }
                SysMenu button = new SysMenu();
                button.setMenuName(menuName);
                button.setMenuType(MenuTypeEnum.BUTTON);
                button.setStatus(StatusEnum.ACTIVE);
                button.setPermission(per);
                button.setOrderNum(i);
                button.setParentId(menu.getId());
                button.setGmtCreate(now);
                button.setCreateBy(SecurityUtil.getCurrentUserName());
                button.setGmtUpdate(now);
                button.setDeleted(0);
                sysMenuMapper.insert(button);
            }
//            System.out.println("以下为添加菜单的SQL:");
//            System.out.println(String.format("INSERT INTO sys_menu (menu_name, menu_type, STATUS, menu_icon, menu_path, router_name, router_component, permission, order_num, parent_id, gmt_create, create_by, gmt_update, deleted) VALUES" + "(%s, '1', '0', NULL, %s, %s, %s, NULL, 1, 0, %s, 'admin', %s, 0)",
//                    ((TableInfo) objectMap.get("table")).getComment(), menuPath, routerName, routerComponent, dateStr, dateStr));
 
        }
    }
}