mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 20:49:00 +08:00
✨[代码] 代码生成支持通过 git 动态更新
This commit is contained in:
1 parent
69d54efdd4
commit
2da674478e
4 files changed
+271
-88
No files matched your search
+109
-87
@@ -51,99 +51,121 @@ import java.util.List;
|
||||
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
|
||||
public class GenTemplateController {
|
||||
|
||||
private final GenTemplateService genTemplateService;
|
||||
private final GenTemplateService genTemplateService;
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param page 分页对象
|
||||
* @param genTemplate 模板
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "分页查询", description = "分页查询")
|
||||
@GetMapping("/page")
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_view')")
|
||||
public R getgenTemplatePage(Page page, GenTemplateEntity genTemplate) {
|
||||
LambdaQueryWrapper<GenTemplateEntity> wrapper = Wrappers.<GenTemplateEntity>lambdaQuery()
|
||||
.like(genTemplate.getId() != null, GenTemplateEntity::getId, genTemplate.getId())
|
||||
.like(StrUtil.isNotEmpty(genTemplate.getTemplateName()), GenTemplateEntity::getTemplateName,
|
||||
genTemplate.getTemplateName());
|
||||
return R.ok(genTemplateService.page(page, wrapper));
|
||||
}
|
||||
/**
|
||||
* 分页查询
|
||||
*
|
||||
* @param page 分页对象
|
||||
* @param genTemplate 模板
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "分页查询", description = "分页查询")
|
||||
@GetMapping("/page")
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_view')")
|
||||
public R getGenTemplatePage(Page page, GenTemplateEntity genTemplate) {
|
||||
LambdaQueryWrapper<GenTemplateEntity> wrapper = Wrappers.<GenTemplateEntity>lambdaQuery()
|
||||
.like(genTemplate.getId() != null, GenTemplateEntity::getId, genTemplate.getId())
|
||||
.like(StrUtil.isNotEmpty(genTemplate.getTemplateName()), GenTemplateEntity::getTemplateName,
|
||||
genTemplate.getTemplateName());
|
||||
return R.ok(genTemplateService.page(page, wrapper));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询全部模板
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "查询全部", description = "查询全部")
|
||||
@GetMapping("/list")
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_view')")
|
||||
public R list() {
|
||||
return R.ok(genTemplateService.list(Wrappers.emptyWrapper()));
|
||||
}
|
||||
/**
|
||||
* 查询全部模板
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "查询全部", description = "查询全部")
|
||||
@GetMapping("/list")
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_view')")
|
||||
public R list() {
|
||||
return R.ok(genTemplateService.list(Wrappers.<GenTemplateEntity>lambdaQuery()
|
||||
.orderByDesc(GenTemplateEntity::getCreateTime)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id查询模板
|
||||
* @param id id
|
||||
* @return R
|
||||
*/
|
||||
@Operation(summary = "通过id查询", description = "通过id查询")
|
||||
@GetMapping("/{id}")
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_view')")
|
||||
public R getById(@PathVariable("id") Long id) {
|
||||
return R.ok(genTemplateService.getById(id));
|
||||
}
|
||||
/**
|
||||
* 通过id查询模板
|
||||
*
|
||||
* @param id id
|
||||
* @return R
|
||||
*/
|
||||
@Operation(summary = "通过id查询", description = "通过id查询")
|
||||
@GetMapping("/{id}")
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_view')")
|
||||
public R getById(@PathVariable("id") Long id) {
|
||||
return R.ok(genTemplateService.getById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增模板
|
||||
* @param genTemplate 模板
|
||||
* @return R
|
||||
*/
|
||||
@XssCleanIgnore
|
||||
@Operation(summary = "新增模板", description = "新增模板")
|
||||
@SysLog("新增模板")
|
||||
@PostMapping
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_add')")
|
||||
public R save(@RequestBody GenTemplateEntity genTemplate) {
|
||||
return R.ok(genTemplateService.save(genTemplate));
|
||||
}
|
||||
/**
|
||||
* 新增模板
|
||||
*
|
||||
* @param genTemplate 模板
|
||||
* @return R
|
||||
*/
|
||||
@XssCleanIgnore
|
||||
@Operation(summary = "新增模板", description = "新增模板")
|
||||
@SysLog("新增模板")
|
||||
@PostMapping
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_add')")
|
||||
public R save(@RequestBody GenTemplateEntity genTemplate) {
|
||||
return R.ok(genTemplateService.save(genTemplate));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改模板
|
||||
* @param genTemplate 模板
|
||||
* @return R
|
||||
*/
|
||||
@XssCleanIgnore
|
||||
@Operation(summary = "修改模板", description = "修改模板")
|
||||
@SysLog("修改模板")
|
||||
@PutMapping
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_edit')")
|
||||
public R updateById(@RequestBody GenTemplateEntity genTemplate) {
|
||||
return R.ok(genTemplateService.updateById(genTemplate));
|
||||
}
|
||||
/**
|
||||
* 修改模板
|
||||
*
|
||||
* @param genTemplate 模板
|
||||
* @return R
|
||||
*/
|
||||
@XssCleanIgnore
|
||||
@Operation(summary = "修改模板", description = "修改模板")
|
||||
@SysLog("修改模板")
|
||||
@PutMapping
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_edit')")
|
||||
public R updateById(@RequestBody GenTemplateEntity genTemplate) {
|
||||
return R.ok(genTemplateService.updateById(genTemplate));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id删除模板
|
||||
* @param ids id列表
|
||||
* @return R
|
||||
*/
|
||||
@Operation(summary = "通过id删除模板", description = "通过id删除模板")
|
||||
@SysLog("通过id删除模板")
|
||||
@DeleteMapping
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_del')")
|
||||
public R removeById(@RequestBody Long[] ids) {
|
||||
return R.ok(genTemplateService.removeBatchByIds(CollUtil.toList(ids)));
|
||||
}
|
||||
/**
|
||||
* 通过id删除模板
|
||||
*
|
||||
* @param ids id列表
|
||||
* @return R
|
||||
*/
|
||||
@Operation(summary = "通过id删除模板", description = "通过id删除模板")
|
||||
@SysLog("通过id删除模板")
|
||||
@DeleteMapping
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_del')")
|
||||
public R removeById(@RequestBody Long[] ids) {
|
||||
return R.ok(genTemplateService.removeBatchByIds(CollUtil.toList(ids)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出excel 表格
|
||||
*
|
||||
* @param genTemplate 查询条件
|
||||
* @return excel 文件流
|
||||
*/
|
||||
@ResponseExcel
|
||||
@GetMapping("/export")
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_export')")
|
||||
public List<GenTemplateEntity> export(GenTemplateEntity genTemplate) {
|
||||
return genTemplateService.list(Wrappers.query(genTemplate));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 在线更新模板
|
||||
*
|
||||
* @return R
|
||||
*/
|
||||
@Operation(summary = "在线更新模板", description = "在线更新模板")
|
||||
@GetMapping("/online")
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_view')")
|
||||
public R online() {
|
||||
return genTemplateService.onlineUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出excel 表格
|
||||
* @param genTemplate 查询条件
|
||||
* @return excel 文件流
|
||||
*/
|
||||
@ResponseExcel
|
||||
@GetMapping("/export")
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_export')")
|
||||
public List<GenTemplateEntity> export(GenTemplateEntity genTemplate) {
|
||||
return genTemplateService.list(Wrappers.query(genTemplate));
|
||||
}
|
||||
|
||||
}
|
||||
+8
@@ -19,6 +19,7 @@ package com.pig4cloud.pigx.codegen.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.pig4cloud.pigx.codegen.entity.GenTemplateEntity;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
|
||||
/**
|
||||
* 模板
|
||||
@@ -28,4 +29,11 @@ import com.pig4cloud.pigx.codegen.entity.GenTemplateEntity;
|
||||
*/
|
||||
public interface GenTemplateService extends IService<GenTemplateEntity> {
|
||||
|
||||
|
||||
/**
|
||||
* 在线更新
|
||||
*
|
||||
* @return {@link R }
|
||||
*/
|
||||
R onlineUpdate();
|
||||
}
|
||||
+117
-1
@@ -16,11 +16,31 @@
|
||||
*/
|
||||
package com.pig4cloud.pigx.codegen.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.pig4cloud.pigx.codegen.entity.GenGroupEntity;
|
||||
import com.pig4cloud.pigx.codegen.entity.GenTemplateEntity;
|
||||
import com.pig4cloud.pigx.codegen.entity.GenTemplateGroupEntity;
|
||||
import com.pig4cloud.pigx.codegen.mapper.GenGroupMapper;
|
||||
import com.pig4cloud.pigx.codegen.mapper.GenTemplateGroupMapper;
|
||||
import com.pig4cloud.pigx.codegen.mapper.GenTemplateMapper;
|
||||
import com.pig4cloud.pigx.codegen.service.GenTemplateService;
|
||||
import com.pig4cloud.pigx.codegen.util.vo.GenTemplateFileVO;
|
||||
import com.pig4cloud.pigx.common.core.exception.CheckedException;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 模板
|
||||
@@ -28,8 +48,104 @@ import org.springframework.stereotype.Service;
|
||||
* @author PIG
|
||||
* @date 2023-02-21 11:08:43
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GenTemplateServiceImpl extends ServiceImpl<GenTemplateMapper, GenTemplateEntity>
|
||||
implements GenTemplateService {
|
||||
implements GenTemplateService {
|
||||
|
||||
private static final String URL = "https://git.pig4cloud.com/pig/CGTM/raw/master";
|
||||
|
||||
private final GenTemplateGroupMapper genTemplateGroupMapper;
|
||||
|
||||
private final GenGroupMapper genGroupMapper;
|
||||
|
||||
|
||||
/**
|
||||
* 在线更新
|
||||
*
|
||||
* @return {@link R }
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public R onlineUpdate() {
|
||||
// 1. 获取 config.json + version 文件
|
||||
String configFile = getCGTMFile("config.json");
|
||||
String versionFile = getCGTMFile("VERSION");
|
||||
|
||||
// 解析 config.json
|
||||
JSONObject configJsonObj = JSONUtil.parseObj(configFile);
|
||||
|
||||
// 查询出全部的模板组名称
|
||||
Set<String> cgtmConfigGroupNames = configJsonObj.keySet();
|
||||
|
||||
// 根据模板组名称+version 查询是否存在,不存在则新增,存在跳过
|
||||
for (String cgtmConfigGroupName : cgtmConfigGroupNames) {
|
||||
boolean exists = genGroupMapper.exists(Wrappers.<GenGroupEntity>lambdaQuery()
|
||||
.eq(GenGroupEntity::getGroupName, cgtmConfigGroupName + versionFile));
|
||||
|
||||
if (exists) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 插入新的模板组(名称 + VERSION), 再解析 config.json group 里面的所有模板
|
||||
insertTemplateFiles(versionFile, configJsonObj, cgtmConfigGroupName);
|
||||
}
|
||||
|
||||
return R.ok("更新成功,版本号:" + versionFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入模板文件
|
||||
*
|
||||
* @param version 版本
|
||||
* @param configJsonObj config.json
|
||||
* @param groupName 组名称
|
||||
*/
|
||||
private void insertTemplateFiles(String version, JSONObject configJsonObj, String groupName) {
|
||||
// 创建新的 group
|
||||
GenGroupEntity genGroupEntity = new GenGroupEntity();
|
||||
genGroupEntity.setGroupName(groupName + version);
|
||||
genGroupMapper.insert(genGroupEntity);
|
||||
|
||||
// 解析json配置文件
|
||||
List<GenTemplateFileVO> templateFileVOList = configJsonObj.getBeanList(groupName, GenTemplateFileVO.class);
|
||||
for (GenTemplateFileVO genTemplateFileVO : templateFileVOList) {
|
||||
// 1. 获取模板文件
|
||||
String templateFile = getCGTMFile(genTemplateFileVO.getTemplateFile());
|
||||
|
||||
// 2. 插入模板文件
|
||||
GenTemplateEntity genTemplateEntity = new GenTemplateEntity();
|
||||
genTemplateEntity.setTemplateName(genTemplateFileVO.getTemplateName() + version);
|
||||
genTemplateEntity.setTemplateDesc(genTemplateFileVO.getTemplateName() + version);
|
||||
genTemplateEntity.setTemplateCode(templateFile);
|
||||
genTemplateEntity.setGeneratorPath(genTemplateFileVO.getGeneratorPath());
|
||||
baseMapper.insert(genTemplateEntity);
|
||||
|
||||
// 3. 插入模板组关联
|
||||
GenTemplateGroupEntity genTemplateGroupEntity = new GenTemplateGroupEntity();
|
||||
genTemplateGroupEntity.setTemplateId(genTemplateEntity.getId());
|
||||
genTemplateGroupEntity.setGroupId(genGroupEntity.getId());
|
||||
genTemplateGroupMapper.insert(genTemplateGroupEntity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取 cgtmfile
|
||||
*
|
||||
* @param fileName 文件名
|
||||
* @return {@link String }
|
||||
*/
|
||||
private String getCGTMFile(String fileName) {
|
||||
HttpResponse response = HttpRequest.get(String.format("%s/%s", URL, fileName))
|
||||
.execute();
|
||||
|
||||
if (response.getStatus() == HttpStatus.HTTP_OK || StrUtil.isNotBlank(response.body())) {
|
||||
return response.body();
|
||||
} else {
|
||||
log.warn("在线更新模板失败:{} ,Http Code:{}", fileName, response.getStatus());
|
||||
throw new CheckedException("在线更新模板失败,任务终止!");
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.pig4cloud.pigx.codegen.util.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2024/7/13
|
||||
* <p>
|
||||
* CGTM 文件路径
|
||||
* <p>
|
||||
* {
|
||||
* "templateName": "Controller",
|
||||
* "generatorPath": "${backendPath}/src/main/java/${packagePath}/${moduleName}/controller/${ClassName}Controller.java",
|
||||
* "templateDesc": "后台Controller",
|
||||
* "templateFile": "temps/Controller"
|
||||
* },
|
||||
*/
|
||||
@Data
|
||||
public class GenTemplateFileVO {
|
||||
/**
|
||||
* 模板名称
|
||||
*/
|
||||
private String templateName;
|
||||
/**
|
||||
* 路径
|
||||
*/
|
||||
private String generatorPath;
|
||||
/**
|
||||
* 模板 desc
|
||||
*/
|
||||
private String templateDesc;
|
||||
/**
|
||||
* 模板文件
|
||||
*/
|
||||
private String templateFile;
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user