feat: 代码生成器 自定义模板 显隐控制

This commit is contained in:
cuijiawang 2025-09-27 18:33:33 +08:00
parent b4525cd1a6
commit 04da9f9c02
8 changed files with 113 additions and 1 deletions

View File

@ -185,5 +185,21 @@ public class UserTemplateController {
boolean exists = userTemplateService.checkTemplateNameExists(userId, templateName, excludeId);
return R.ok(exists);
}
/**
* 切换用户模板显示状态
*/
@PostMapping("/toggle-visibility")
public R<Void> toggleTemplateVisibility(@RequestBody Map<String, Object> params) {
try {
Long id = Long.parseLong(params.get("id").toString());
Integer isEnabled = Integer.parseInt(params.get("isEnabled").toString());
boolean success = userTemplateService.toggleTemplateVisibility(id, isEnabled);
return success ? R.ok() : R.fail("切换显示状态失败");
} catch (Exception e) {
log.error("切换用户模板显示状态失败", e);
return R.fail(e.getMessage());
}
}
}

View File

@ -50,5 +50,10 @@ public class UserTemplateDTO {
* 状态(0-草稿 1-发布 2-禁用)
*/
private Integer status;
/**
* 是否启用显示(0-隐藏 1-显示)
*/
private Integer isEnabled;
}

View File

@ -69,4 +69,9 @@ public class UserTemplate extends BaseEntity {
* 状态(0-草稿 1-发布 2-禁用)
*/
private Integer status;
/**
* 是否启用显示(0-隐藏 1-显示)
*/
private Integer isEnabled;
}

View File

@ -108,5 +108,14 @@ public interface IUserTemplateService {
* @param id 模板ID
*/
void increaseUseCount(Long id);
/**
* 切换用户模板显示状态
*
* @param id 模板ID
* @param isEnabled 是否启用显示
* @return 是否成功
*/
boolean toggleTemplateVisibility(Long id, Integer isEnabled);
}

View File

@ -3,6 +3,7 @@ package com.agileboot.codegen.service.impl;
import com.agileboot.codegen.dto.TemplateRepositoryDTO;
import com.agileboot.codegen.entity.UserTemplate;
import com.agileboot.codegen.entity.UserTemplateRepository;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.agileboot.codegen.mapper.UserTemplateMapper;
import com.agileboot.codegen.mapper.UserTemplateRepositoryMapper;
import com.agileboot.codegen.service.IGeneratorService;
@ -38,7 +39,7 @@ public class TemplateRepositoryServiceImpl implements ITemplateRepositoryService
@Override
public JSONArray getUserTemplateConfig(Long userId) {
// 获取用户启用的模板仓库配置
// 获取用户启用的模板仓库配置公开模板
List<UserTemplateRepository> repositories = repositoryMapper.selectEnabledByUserId(userId);
// 按分组组织模板
@ -56,6 +57,35 @@ public class TemplateRepositoryServiceImpl implements ITemplateRepositoryService
groupedTemplates.computeIfAbsent(group, k -> new ArrayList<>()).add(template);
}
// 添加用户自己的启用模板包括私有模板
List<UserTemplate> userEnabledTemplates = userTemplateMapper.selectList(
new LambdaQueryWrapper<UserTemplate>()
.eq(UserTemplate::getUserId, userId)
.eq(UserTemplate::getIsEnabled, 1)
.eq(UserTemplate::getStatus, 1) // 只包含发布状态的模板
);
for (UserTemplate userTemplate : userEnabledTemplates) {
// 如果是公开模板且已经在仓库配置中跳过避免重复
if (userTemplate.getIsPublic() == 1) {
boolean alreadyInRepo = repositories.stream()
.anyMatch(repo -> "user".equals(repo.getTemplateSource()) &&
userTemplate.getId().toString().equals(repo.getTemplateId()));
if (alreadyInRepo) {
continue;
}
}
String group = userTemplate.getTemplateGroup();
JSONObject template = new JSONObject();
template.put("id", userTemplate.getId().toString());
template.put("name", userTemplate.getTemplateName());
template.put("description", userTemplate.getTemplateName());
template.put("source", "user");
groupedTemplates.computeIfAbsent(group, k -> new ArrayList<>()).add(template);
}
// 如果用户没有配置任何模板先初始化用户仓库然后重新获取
if (groupedTemplates.isEmpty()) {
log.info("用户 {} 没有模板配置,自动初始化用户仓库", userId);

View File

@ -93,6 +93,7 @@ public class UserTemplateServiceImpl implements IUserTemplateService {
template.setUseCount(0);
template.setStatus(dto.getStatus() != null ? dto.getStatus() : 1);
template.setVersion(StringUtils.isNotBlank(dto.getVersion()) ? dto.getVersion() : "1.0.0");
template.setIsEnabled(dto.getIsEnabled() != null ? dto.getIsEnabled() : 1); // 默认显示
return userTemplateMapper.insert(template) > 0;
}
@ -254,6 +255,40 @@ public class UserTemplateServiceImpl implements IUserTemplateService {
userTemplateMapper.increaseUseCount(id);
}
@Override
@Transactional
public boolean toggleTemplateVisibility(Long id, Integer isEnabled) {
UserTemplate template = userTemplateMapper.selectById(id);
if (template == null) {
throw new RuntimeException("模板不存在");
}
log.info("查询到的模板信息: id={}, name={}, isEnabled={}", template.getId(), template.getTemplateName(), template.getIsEnabled());
Long currentUserId = LoginHelper.getUserId();
// 只能修改自己的模板
if (!currentUserId.equals(template.getUserId())) {
throw new RuntimeException("无权限修改此模板");
}
// 更新模板的显示状态
log.info("切换模板显示状态: id={}, 原状态={}, 新状态={}", id, template.getIsEnabled(), isEnabled);
template.setIsEnabled(isEnabled);
boolean updateResult = userTemplateMapper.updateById(template) > 0;
log.info("更新结果: {}", updateResult);
// 如果是公开模板同时更新模板仓库中的状态如果存在的话
if (template.getIsPublic() == 1) {
try {
repositoryMapper.toggleTemplateStatus(currentUserId, "user", id.toString(), isEnabled);
} catch (Exception e) {
// 如果模板仓库中不存在该记录忽略错误
log.debug("更新模板仓库状态时出错,可能记录不存在: {}", e.getMessage());
}
}
return updateResult;
}
/**
* 转换实体为VO
*/

View File

@ -73,6 +73,11 @@ public class UserTemplateVO {
*/
private String statusText;
/**
* 是否启用显示(0-隐藏 1-显示)
*/
private Integer isEnabled;
/**
* 创建人
*/

View File

@ -0,0 +1,7 @@
-- 为用户模板表添加显示隐藏控制字段
-- 用于控制私有模板的显示状态,以及公开模板创建者的显示偏好
ALTER TABLE `user_template` ADD COLUMN `is_enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用显示(0-隐藏 1-显示)' AFTER `status`;
-- 添加索引以提高查询性能
ALTER TABLE `user_template` ADD INDEX `idx_is_enabled` (`is_enabled`);