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));
|
|
}
|
}
|
}
|