feat(m6): add JSON template CRUD with versioning

This commit is contained in:
2026-05-25 01:36:40 +08:00
parent ae880c47b2
commit 46f28d2d97
8 changed files with 412 additions and 1 deletions
@@ -1,6 +1,7 @@
package cn.craftlabs.platform.api.integration; package cn.craftlabs.platform.api.integration;
import cn.craftlabs.platform.api.persistence.integration.PlatformBitanswerIdMapping; import cn.craftlabs.platform.api.persistence.integration.PlatformBitanswerIdMapping;
import cn.craftlabs.platform.api.persistence.integration.PlatformJsonTemplate;
import cn.craftlabs.platform.api.service.IntegrationCatalogService; import cn.craftlabs.platform.api.service.IntegrationCatalogService;
import cn.craftlabs.platform.api.web.dto.IntegrationEnvironmentRequest; import cn.craftlabs.platform.api.web.dto.IntegrationEnvironmentRequest;
import cn.craftlabs.platform.api.web.dto.IntegrationEnvironmentResponse; import cn.craftlabs.platform.api.web.dto.IntegrationEnvironmentResponse;
@@ -117,4 +118,33 @@ public class IntegrationCatalogController {
integrationCatalogService.deleteIdMapping(id); integrationCatalogService.deleteIdMapping(id);
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }
@GetMapping("/json-templates")
public ResponseEntity<java.util.List<PlatformJsonTemplate>> listJsonTemplates() {
return ResponseEntity.ok(integrationCatalogService.listJsonTemplates());
}
@GetMapping("/json-templates/{id}")
public ResponseEntity<PlatformJsonTemplate> getJsonTemplate(@PathVariable Long id) {
PlatformJsonTemplate t = integrationCatalogService.getJsonTemplate(id);
return t != null ? ResponseEntity.ok(t) : ResponseEntity.notFound().build();
}
@PostMapping("/json-templates")
public ResponseEntity<PlatformJsonTemplate> createJsonTemplate(@RequestBody PlatformJsonTemplate body) {
return ResponseEntity.ok(integrationCatalogService.createJsonTemplate(body));
}
@PutMapping("/json-templates/{id}")
public ResponseEntity<PlatformJsonTemplate> updateJsonTemplate(
@PathVariable Long id, @RequestBody PlatformJsonTemplate body) {
PlatformJsonTemplate result = integrationCatalogService.updateJsonTemplate(id, body);
return result != null ? ResponseEntity.ok(result) : ResponseEntity.notFound().build();
}
@DeleteMapping("/json-templates/{id}")
public ResponseEntity<Void> deleteJsonTemplate(@PathVariable Long id) {
integrationCatalogService.deleteJsonTemplate(id);
return ResponseEntity.ok().build();
}
} }
@@ -0,0 +1,109 @@
package cn.craftlabs.platform.api.persistence.integration;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.time.OffsetDateTime;
@TableName("platform_json_template")
public class PlatformJsonTemplate {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer version;
@TableField("template_content")
private String templateContent;
@TableField("schema_version")
private Integer schemaVersion;
@TableField("change_notes")
private String changeNotes;
@TableField("created_by")
private String createdBy;
@TableField("created_at")
private OffsetDateTime createdAt;
@TableField("updated_at")
private OffsetDateTime updatedAt;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public String getTemplateContent() {
return templateContent;
}
public void setTemplateContent(String templateContent) {
this.templateContent = templateContent;
}
public Integer getSchemaVersion() {
return schemaVersion;
}
public void setSchemaVersion(Integer schemaVersion) {
this.schemaVersion = schemaVersion;
}
public String getChangeNotes() {
return changeNotes;
}
public void setChangeNotes(String changeNotes) {
this.changeNotes = changeNotes;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public OffsetDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(OffsetDateTime createdAt) {
this.createdAt = createdAt;
}
public OffsetDateTime getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(OffsetDateTime updatedAt) {
this.updatedAt = updatedAt;
}
}
@@ -0,0 +1,7 @@
package cn.craftlabs.platform.api.persistence.integration;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface PlatformJsonTemplateMapper extends BaseMapper<PlatformJsonTemplate> {}
@@ -4,6 +4,8 @@ import cn.craftlabs.platform.api.persistence.integration.PlatformBitanswerIdMapp
import cn.craftlabs.platform.api.persistence.integration.PlatformBitanswerIdMappingMapper; import cn.craftlabs.platform.api.persistence.integration.PlatformBitanswerIdMappingMapper;
import cn.craftlabs.platform.api.persistence.integration.PlatformIntegrationEnvironment; import cn.craftlabs.platform.api.persistence.integration.PlatformIntegrationEnvironment;
import cn.craftlabs.platform.api.persistence.integration.PlatformIntegrationEnvironmentMapper; import cn.craftlabs.platform.api.persistence.integration.PlatformIntegrationEnvironmentMapper;
import cn.craftlabs.platform.api.persistence.integration.PlatformJsonTemplate;
import cn.craftlabs.platform.api.persistence.integration.PlatformJsonTemplateMapper;
import cn.craftlabs.platform.api.persistence.integration.PlatformProductLine; import cn.craftlabs.platform.api.persistence.integration.PlatformProductLine;
import cn.craftlabs.platform.api.persistence.integration.PlatformProductLineMapper; import cn.craftlabs.platform.api.persistence.integration.PlatformProductLineMapper;
import cn.craftlabs.platform.api.web.dto.IntegrationEnvironmentRequest; import cn.craftlabs.platform.api.web.dto.IntegrationEnvironmentRequest;
@@ -27,14 +29,17 @@ public class IntegrationCatalogService {
private final PlatformProductLineMapper productLineMapper; private final PlatformProductLineMapper productLineMapper;
private final PlatformIntegrationEnvironmentMapper environmentMapper; private final PlatformIntegrationEnvironmentMapper environmentMapper;
private final PlatformBitanswerIdMappingMapper idMappingMapper; private final PlatformBitanswerIdMappingMapper idMappingMapper;
private final PlatformJsonTemplateMapper jsonTemplateMapper;
public IntegrationCatalogService( public IntegrationCatalogService(
PlatformProductLineMapper productLineMapper, PlatformProductLineMapper productLineMapper,
PlatformIntegrationEnvironmentMapper environmentMapper, PlatformIntegrationEnvironmentMapper environmentMapper,
PlatformBitanswerIdMappingMapper idMappingMapper) { PlatformBitanswerIdMappingMapper idMappingMapper,
PlatformJsonTemplateMapper jsonTemplateMapper) {
this.productLineMapper = productLineMapper; this.productLineMapper = productLineMapper;
this.environmentMapper = environmentMapper; this.environmentMapper = environmentMapper;
this.idMappingMapper = idMappingMapper; this.idMappingMapper = idMappingMapper;
this.jsonTemplateMapper = jsonTemplateMapper;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
@@ -174,6 +179,42 @@ public class IntegrationCatalogService {
idMappingMapper.deleteById(id); idMappingMapper.deleteById(id);
} }
@Transactional
public PlatformJsonTemplate createJsonTemplate(PlatformJsonTemplate template) {
template.setVersion(1);
template.setSchemaVersion(1);
template.setCreatedAt(java.time.OffsetDateTime.now());
template.setUpdatedAt(java.time.OffsetDateTime.now());
jsonTemplateMapper.insert(template);
return template;
}
public java.util.List<PlatformJsonTemplate> listJsonTemplates() {
return jsonTemplateMapper.selectList(
com.baomidou.mybatisplus.core.toolkit.Wrappers.lambdaQuery(PlatformJsonTemplate.class)
.orderByDesc(PlatformJsonTemplate::getCreatedAt));
}
public PlatformJsonTemplate getJsonTemplate(Long id) {
return jsonTemplateMapper.selectById(id);
}
@Transactional
public PlatformJsonTemplate updateJsonTemplate(Long id, PlatformJsonTemplate template) {
PlatformJsonTemplate existing = jsonTemplateMapper.selectById(id);
if (existing == null) return null;
template.setId(id);
template.setVersion(existing.getVersion() + 1);
template.setUpdatedAt(java.time.OffsetDateTime.now());
jsonTemplateMapper.updateById(template);
return jsonTemplateMapper.selectById(id);
}
@Transactional
public void deleteJsonTemplate(Long id) {
jsonTemplateMapper.deleteById(id);
}
private ProductLineResponse toProductLine(PlatformProductLine row) { private ProductLineResponse toProductLine(PlatformProductLine row) {
ProductLineResponse r = new ProductLineResponse(); ProductLineResponse r = new ProductLineResponse();
r.setId(row.getId()); r.setId(row.getId());
@@ -0,0 +1,12 @@
-- V14__m6_json_template.sql
CREATE TABLE platform_json_template (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(128) NOT NULL,
version INT NOT NULL DEFAULT 1,
template_content TEXT NOT NULL,
schema_version INT NOT NULL DEFAULT 1,
change_notes TEXT,
created_by VARCHAR(256),
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
@@ -401,3 +401,20 @@ export function updateIdMapping(id, body) {
export function deleteIdMapping(id) { export function deleteIdMapping(id) {
return axios.delete(`/api/v1/integration/id-mappings/${id}`); return axios.delete(`/api/v1/integration/id-mappings/${id}`);
} }
// —— I12-2 M6 JSON 模板 ——————————————————————
export function listJsonTemplates() {
return axios.get('/api/v1/integration/json-templates');
}
export function getJsonTemplate(id) {
return axios.get(`/api/v1/integration/json-templates/${id}`);
}
export function createJsonTemplate(body) {
return axios.post('/api/v1/integration/json-templates', body);
}
export function updateJsonTemplate(id, body) {
return axios.put(`/api/v1/integration/json-templates/${id}`, body);
}
export function deleteJsonTemplate(id) {
return axios.delete(`/api/v1/integration/json-templates/${id}`);
}
@@ -86,6 +86,12 @@ const routes = [
component: () => import("../views/IntegrationIdMappingView.vue"), component: () => import("../views/IntegrationIdMappingView.vue"),
meta: { roles: ["SYS_ADMIN", "DEVELOPER"], title: "ID 映射" }, meta: { roles: ["SYS_ADMIN", "DEVELOPER"], title: "ID 映射" },
}, },
{
path: "integration/json-templates",
name: "integration-json-templates",
component: () => import("../views/IntegrationJsonTemplateView.vue"),
meta: { roles: ["SYS_ADMIN"], title: "JSON 模板" },
},
{ {
path: "callbacks/:id", path: "callbacks/:id",
name: "callback-inbox-detail", name: "callback-inbox-detail",
@@ -0,0 +1,189 @@
<template>
<el-card shadow="never">
<template #header>
<div class="toolbar">
<span class="title">JSON 模板</span>
<div class="actions">
<el-button type="primary" :loading="loading" @click="load">刷新</el-button>
<el-button type="success" @click="openCreate">新建模板</el-button>
</div>
</div>
</template>
<el-table v-loading="loading" :data="rows" stripe style="width: 100%">
<el-table-column prop="name" label="名称" min-width="160" show-overflow-tooltip />
<el-table-column prop="version" label="版本" width="80" />
<el-table-column label="Schema 版本" width="120">
<template #default="{ row }">{{ row.schemaVersion ?? row.schema_version ?? 1 }}</template>
</el-table-column>
<el-table-column prop="changeNotes" label="变更说明" min-width="200" show-overflow-tooltip>
<template #default="{ row }">{{ row.changeNotes ?? row.change_notes ?? "—" }}</template>
</el-table-column>
<el-table-column label="创建时间" width="180" show-overflow-tooltip>
<template #default="{ row }">{{ row.createdAt ?? row.created_at ?? "—" }}</template>
</el-table-column>
<el-table-column label="操作" width="160" fixed="right">
<template #default="{ row }">
<el-button type="primary" link @click="openEdit(row)">编辑</el-button>
<el-button type="danger" link @click="onDelete(row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog v-model="dialogVisible" :title="dialogTitle" width="680px" destroy-on-close @closed="resetForm">
<el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
<el-form-item label="名称" prop="name">
<el-input v-model="form.name" maxlength="128" show-word-limit placeholder="请输入模板名称" />
</el-form-item>
<el-form-item label="模板内容" prop="templateContent">
<el-input v-model="form.templateContent" type="textarea" :rows="10" placeholder="请输入 JSON 模板内容" />
</el-form-item>
<el-form-item label="变更说明" prop="changeNotes">
<el-input v-model="form.changeNotes" type="textarea" :rows="3" maxlength="512" show-word-limit placeholder="选填" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" :loading="saving" @click="submit">保存</el-button>
</template>
</el-dialog>
</el-card>
</template>
<script setup>
import { ref, reactive, computed, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useAuthStore } from "../stores/auth";
import { listJsonTemplates, createJsonTemplate, updateJsonTemplate, deleteJsonTemplate } from "../api/platform";
import { apiErrorMessage } from "../utils/apiErrorMessage";
const auth = useAuthStore();
const loading = ref(false);
const saving = ref(false);
const rows = ref([]);
const dialogVisible = ref(false);
const editingId = ref(null);
const formRef = ref(null);
const form = reactive({
name: "",
templateContent: "",
changeNotes: "",
});
const rules = {
name: [{ required: true, message: "请输入模板名称", trigger: "blur" }],
templateContent: [{ required: true, message: "请输入模板内容", trigger: "blur" }],
};
const dialogTitle = computed(() => (editingId.value ? "编辑模板" : "新建模板"));
onMounted(async () => {
auth.restoreAxiosAuth();
await load();
});
async function load() {
loading.value = true;
try {
const { data } = await listJsonTemplates();
rows.value = Array.isArray(data) ? data : [];
} catch (e) {
ElMessage.error(apiErrorMessage(e, "加载 JSON 模板失败"));
rows.value = [];
} finally {
loading.value = false;
}
}
function openCreate() {
editingId.value = null;
resetForm();
dialogVisible.value = true;
}
function openEdit(row) {
editingId.value = row.id;
form.name = row.name ?? "";
form.templateContent = row.templateContent ?? row.template_content ?? "";
form.changeNotes = row.changeNotes ?? row.change_notes ?? "";
dialogVisible.value = true;
}
function resetForm() {
form.name = "";
form.templateContent = "";
form.changeNotes = "";
formRef.value?.resetFields?.();
}
async function submit() {
const f = formRef.value;
if (!f) return;
try {
await f.validate();
} catch {
return;
}
saving.value = true;
const payload = {
name: form.name.trim(),
templateContent: form.templateContent,
changeNotes: form.changeNotes?.trim() || undefined,
};
try {
if (editingId.value != null) {
await updateJsonTemplate(editingId.value, payload);
ElMessage.success("已保存");
} else {
await createJsonTemplate(payload);
ElMessage.success("已创建");
}
dialogVisible.value = false;
await load();
} catch (e) {
ElMessage.error(apiErrorMessage(e, "保存失败"));
} finally {
saving.value = false;
}
}
function onDelete(row) {
ElMessageBox.confirm(`确定删除模板「${row.name || row.id}」吗?`, "提示", {
type: "warning",
confirmButtonText: "删除",
cancelButtonText: "取消",
})
.then(async () => {
try {
await deleteJsonTemplate(row.id);
ElMessage.success("已删除");
await load();
} catch (e) {
ElMessage.error(apiErrorMessage(e, "删除失败"));
}
})
.catch(() => {});
}
</script>
<style scoped>
.toolbar {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.title {
font-weight: 600;
font-size: 16px;
}
.actions {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>