From 26d4bad0ff18f11829a8a1c33e053d32cb09d6cf Mon Sep 17 00:00:00 2001 From: lbw Date: Tue, 20 Feb 2024 13:42:30 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Introducing=20new=20features.=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E5=99=A8=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-visual/pigx-codegen/pom.xml | 17 +++ .../controller/GenCreateTableController.java | 141 ++++++++++++++++++ .../pigx/codegen/entity/GenCreateTable.java | 104 +++++++++++++ .../codegen/entity/GenTemplateEntity.java | 116 +++++++------- .../codegen/mapper/GenCreateTableMapper.java | 32 ++++ .../service/GenCreateTableService.java | 38 +++++ .../service/impl/CreateTableServiceImpl.java | 66 ++++++++ .../service/impl/GeneratorServiceImpl.java | 3 +- .../util/table/CreateTableHandler.java | 71 +++++++++ .../util/table/enums/TableModelEnum.java | 40 +++++ .../codegen/util/table/model/TableInfo.java | 44 ++++++ .../codegen/util/vo/GenCreateTableVO.java | 95 ++++++++++++ 12 files changed, 707 insertions(+), 60 deletions(-) create mode 100755 pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenCreateTableController.java create mode 100755 pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenCreateTable.java create mode 100755 pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenCreateTableMapper.java create mode 100755 pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenCreateTableService.java create mode 100755 pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/CreateTableServiceImpl.java create mode 100644 pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/CreateTableHandler.java create mode 100644 pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/enums/TableModelEnum.java create mode 100644 pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/model/TableInfo.java create mode 100755 pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/vo/GenCreateTableVO.java diff --git a/pigx-visual/pigx-codegen/pom.xml b/pigx-visual/pigx-codegen/pom.xml index dfba72846..9992960ea 100755 --- a/pigx-visual/pigx-codegen/pom.xml +++ b/pigx-visual/pigx-codegen/pom.xml @@ -18,6 +18,7 @@ 2.0.2 0.0.3 11.5.0.0 + 8.7.1-jdk17-20232101 1.10 @@ -87,6 +88,22 @@ com.alibaba druid-spring-boot-3-starter + + + org.anyline + anyline-data-jdbc-mysql + ${anyline.version} + + + org.anyline + anyline-data-jdbc-postgresql + ${anyline.version} + + + org.anyline + anyline-data-jdbc-dm + ${anyline.version} + com.pig4cloud diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenCreateTableController.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenCreateTableController.java new file mode 100755 index 000000000..1a06d9cef --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenCreateTableController.java @@ -0,0 +1,141 @@ +package com.pig4cloud.pigx.codegen.controller; +/* + * Copyright (c) 2018-2025, luolin All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: luolin (766488893@qq.com) + */ + +import cn.hutool.core.bean.BeanUtil; +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.codegen.entity.GenCreateTable; +import com.pig4cloud.pigx.codegen.service.GenCreateTableService; +import com.pig4cloud.pigx.codegen.util.vo.GenCreateTableVO; +import com.pig4cloud.pigx.common.core.util.R; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpHeaders; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * 自动创建表管理 + * + * @author luolin + * @date 2022-09-23 21:56:11 + */ +@RestController +@RequiredArgsConstructor +@RequestMapping("/create-table") +@Tag(description = "create-table", name = "自动创建表管理管理") +@SecurityRequirement(name = HttpHeaders.AUTHORIZATION) +public class GenCreateTableController { + + private final GenCreateTableService createTableService; + + /** + * 分页查询 + * @param page 分页对象 + * @param createTable 自动创建表管理 + * @return + */ + @Operation(summary = "分页查询", description = "分页查询") + @GetMapping("/page") + public R getPage(Page page, @ModelAttribute GenCreateTable createTable, + @RequestParam(required = false) String[] queryTime) { + QueryWrapper query = Wrappers.query(createTable); + return R.ok(createTableService.page(page, query)); + } + + /** + * 通过id查询自动创建表管理 + * @param id id + * @return R + */ + @Operation(summary = "通过id查询", description = "通过id查询") + @GetMapping("/{id}") + public R getById(@PathVariable("id") Long id) { + return R.ok(createTableService.getById(id)); + } + + /** + * 新增自动创建表管理 + * @param createTableVO 自动创建表管理 + * @return R + */ + @Operation(summary = "新增自动创建表管理", description = "新增自动创建表管理") + @PostMapping + @PreAuthorize("@pms.hasPermission('codegen_table_add')") + public R save(@RequestBody GenCreateTableVO createTableVO) { + // 切换目标数据源 + DynamicDataSourceContextHolder.push(createTableVO.getDsName()); + createTableService.createTable(createTableVO); + GenCreateTable createTable = new GenCreateTable(); + BeanUtil.copyProperties(createTableVO, createTable); + DynamicDataSourceContextHolder.clear(); + return R.ok(createTableService.saveOrUpdate(createTable)); + } + + /** + * 修改自动创建表管理 + * @param createTable 自动创建表管理 + * @return R + */ + @Operation(summary = "修改自动创建表管理", description = "修改自动创建表管理") + @PutMapping + @PreAuthorize("@pms.hasPermission('codegen_table_add')") + public R updateById(@RequestBody GenCreateTable createTable) { + return R.ok(createTableService.updateById(createTable)); + } + + /** + * 通过id删除自动创建表管理 + * @param id id + * @return R + */ + @Operation(summary = "通过id删除自动创建表管理", description = "通过id删除自动创建表管理") + @DeleteMapping("/{id}") + @PreAuthorize("@pms.hasPermission('codegen_table_add')") + public R removeById(@PathVariable Long id) { + return R.ok(createTableService.removeById(id)); + } + + /** + * 查询租户所有自动创建表管理 + * @return R + */ + @GetMapping("/list") + public R list() { + return R.ok(createTableService.list()); + } + + /** + * 通过ids删除 + * @param ids ids + * @return R + */ + @Operation(summary = "通过ids删除", description = "通过ids删除") + @DeleteMapping + @PreAuthorize("@pms.hasPermission('codegen_table_add')") + public R removeById(@RequestBody List ids) { + return R.ok(createTableService.removeByIds(ids)); + } + +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenCreateTable.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenCreateTable.java new file mode 100755 index 000000000..32c52a0ea --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenCreateTable.java @@ -0,0 +1,104 @@ +package com.pig4cloud.pigx.codegen.entity; +/* + * Copyright (c) 2018-2025, luolin All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: luolin (766488893@qq.com) + */ + +import com.baomidou.mybatisplus.annotation.*; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import com.pig4cloud.pigx.common.core.util.TenantTable; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.time.LocalDateTime; + +/** + * 自动创建表管理 + * + * @author luolin + * @date 2022-09-23 21:56:11 + */ +@Data +@TenantTable +@TableName("gen_create_table") +@EqualsAndHashCode(callSuper = true) +@Schema(description = "自动创建表管理") +public class GenCreateTable extends Model { + + private static final long serialVersionUID = 1L; + + /** + * 主键ID + */ + @TableId(type = IdType.ASSIGN_ID) + @Schema(description = "主键ID") + private Long id; + + /** + * 表名称 + */ + @Schema(description = "表名称") + private String tableName; + + /** + * 表注释 + */ + @Schema(description = "表注释") + private String comments; + + /** + * 字段信息 + */ + @Schema(description = "字段信息") + private String columnInfo; + + /** + * 创建人 + */ + @TableField(fill = FieldFill.INSERT) + @Schema(description = "创建人") + private String createBy; + + /** + * 修改人 + */ + @TableField(fill = FieldFill.UPDATE) + @Schema(description = "修改人") + private String updateBy; + + /** + * 创建时间 + */ + @Schema(description = "创建时间") + @TableField(fill = FieldFill.INSERT) + private LocalDateTime createTime; + + /** + * 修改时间 + */ + @Schema(description = "修改时间") + @TableField(fill = FieldFill.UPDATE) + private LocalDateTime updateTime; + + /** + * 删除标识(0-正常,1-删除) + */ + @TableLogic + @TableField(fill = FieldFill.INSERT) + @Schema(description = "删除标记,1:已删除,0:正常") + private String delFlag; + +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenTemplateEntity.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenTemplateEntity.java index f979a8b04..557f9181b 100644 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenTemplateEntity.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenTemplateEntity.java @@ -39,73 +39,73 @@ import java.time.LocalDateTime; @Schema(description = "模板") public class GenTemplateEntity extends Model { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - /** - * 主键 - */ - @TableId(type = IdType.ASSIGN_ID) - @Schema(description = "主键") - private Long id; + /** + * 主键 + */ + @TableId(type = IdType.ASSIGN_ID) + @Schema(description = "主键") + private Long id; - /** - * 模板名称 - */ - @Schema(description = "模板名称") - private String templateName; + /** + * 模板名称 + */ + @Schema(description = "模板名称") + private String templateName; - /** - * 模板路径 - */ - @Schema(description = "模板路径") - private String generatorPath; + /** + * 模板路径 + */ + @Schema(description = "模板路径") + private String generatorPath; - /** - * 模板描述 - */ - @Schema(description = "模板描述") - private String templateDesc; + /** + * 模板描述 + */ + @Schema(description = "模板描述") + private String templateDesc; - /** - * 模板代码 - */ - @Schema(description = "模板代码") - private String templateCode; + /** + * 模板代码 + */ + @Schema(description = "模板代码") + private String templateCode; - /** - * 创建人 - */ - @TableField(fill = FieldFill.INSERT) - @Schema(description = "创建人") - private String createBy; + /** + * 创建人 + */ + @TableField(fill = FieldFill.INSERT) + @Schema(description = "创建人") + private String createBy; - /** - * 修改人 - */ - @TableField(fill = FieldFill.UPDATE) - @Schema(description = "修改人") - private String updateBy; + /** + * 修改人 + */ + @TableField(fill = FieldFill.UPDATE) + @Schema(description = "修改人") + private String updateBy; - /** - * 创建时间 - */ - @Schema(description = "创建时间") - @TableField(fill = FieldFill.INSERT) - private LocalDateTime createTime; + /** + * 创建时间 + */ + @Schema(description = "创建时间") + @TableField(fill = FieldFill.INSERT) + private LocalDateTime createTime; - /** - * 修改时间 - */ - @Schema(description = "修改时间") - @TableField(fill = FieldFill.UPDATE) - private LocalDateTime updateTime; + /** + * 修改时间 + */ + @Schema(description = "修改时间") + @TableField(fill = FieldFill.UPDATE) + private LocalDateTime updateTime; - /** - * 删除标识(0-正常,1-删除) - */ - @TableLogic - @TableField(fill = FieldFill.INSERT) - @Schema(description = "删除标记,1:已删除,0:正常") - private String delFlag; + /** + * 删除标识(0-正常,1-删除) + */ + @TableLogic + @TableField(fill = FieldFill.INSERT) + @Schema(description = "删除标记,1:已删除,0:正常") + private String delFlag; } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenCreateTableMapper.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenCreateTableMapper.java new file mode 100755 index 000000000..0a8be2ef6 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenCreateTableMapper.java @@ -0,0 +1,32 @@ +package com.pig4cloud.pigx.codegen.mapper; +/* + * Copyright (c) 2018-2025, luolin All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: luolin (766488893@qq.com) + */ + +import com.pig4cloud.pigx.codegen.entity.GenCreateTable; +import com.pig4cloud.pigx.common.data.datascope.PigxBaseMapper; +import org.apache.ibatis.annotations.Mapper; + +/** + * 自动创建表管理 + * + * @author luolin + * @date 2022-09-23 21:56:11 + */ +@Mapper +public interface GenCreateTableMapper extends PigxBaseMapper { + +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenCreateTableService.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenCreateTableService.java new file mode 100755 index 000000000..3d99bc211 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenCreateTableService.java @@ -0,0 +1,38 @@ +package com.pig4cloud.pigx.codegen.service; + +/* + * Copyright (c) 2018-2025, luolin All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: luolin (766488893@qq.com) + */ + +import com.baomidou.mybatisplus.extension.service.IService; +import com.pig4cloud.pigx.codegen.entity.GenCreateTable; +import com.pig4cloud.pigx.codegen.util.vo.GenCreateTableVO; + +/** + * 自动创建表管理 + * + * @author luolin + * @date 2022-09-23 21:56:11 + */ +public interface GenCreateTableService extends IService { + + /** + * 自动创建表 + * @param createTableVO 表信息 + */ + Boolean createTable(GenCreateTableVO createTableVO); + +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/CreateTableServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/CreateTableServiceImpl.java new file mode 100755 index 000000000..3dade7a79 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/CreateTableServiceImpl.java @@ -0,0 +1,66 @@ +package com.pig4cloud.pigx.codegen.service.impl; +/* + * Copyright (c) 2018-2025, luolin All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: luolin (766488893@qq.com) + */ + +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.pig4cloud.pigx.codegen.entity.GenCreateTable; +import com.pig4cloud.pigx.codegen.mapper.GenCreateTableMapper; +import com.pig4cloud.pigx.codegen.service.GenCreateTableService; +import com.pig4cloud.pigx.codegen.util.table.CreateTableHandler; +import com.pig4cloud.pigx.codegen.util.table.model.TableInfo; +import com.pig4cloud.pigx.codegen.util.vo.GenCreateTableVO; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.anyline.metadata.Column; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.LinkedHashMap; + +/** + * 自动创建表管理 + * + * @author luolin + * @date 2022-09-23 21:56:11 + */ +@Slf4j +@Service +@RequiredArgsConstructor +@Transactional(rollbackFor = Exception.class) +public class CreateTableServiceImpl extends ServiceImpl + implements GenCreateTableService { + + private final CreateTableHandler createTableHandler; + + @Override + public Boolean createTable(GenCreateTableVO createTableVO) { + String columnsInfo = createTableVO.getColumnsInfo(); + TableInfo tableInfo = new TableInfo(); + tableInfo.setName(createTableVO.getTableName()); + LinkedHashMap columns = new LinkedHashMap<>(); + LinkedHashMap linkedHashMap = JSONUtil.toBean(columnsInfo, LinkedHashMap.class); + linkedHashMap.forEach((key, val) -> { + columns.put(key, JSONUtil.toBean(val, Column.class)); + }); + tableInfo.setColumns(columns); + tableInfo.setComment(createTableVO.getComments()); + return createTableHandler.createTable(tableInfo); + } + +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java index 38cfb6d2e..5937e93ee 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java @@ -309,8 +309,7 @@ public class GeneratorServiceImpl implements GeneratorService { // 按字段类型分组,使用 Map 存储不同类型的字段列表 Map> typeMap = table.getFieldList() .stream() - .collect(Collectors - .partitioningBy(columnEntity -> BooleanUtil.toBoolean(columnEntity.getPrimaryPk().toString()))); + .collect(Collectors.partitioningBy(columnEntity -> BooleanUtil.toBoolean(columnEntity.getPrimaryPk()))); // 从分组后的 Map 中获取不同类型的字段列表 List primaryList = typeMap.get(true); diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/CreateTableHandler.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/CreateTableHandler.java new file mode 100644 index 000000000..143dbd8ad --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/CreateTableHandler.java @@ -0,0 +1,71 @@ +package com.pig4cloud.pigx.codegen.util.table; +/* + * Copyright (c) 2018-2025, luolin All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: luolin (766488893@qq.com) + */ + +import com.pig4cloud.pigx.codegen.util.table.enums.TableModelEnum; +import com.pig4cloud.pigx.codegen.util.table.model.TableInfo; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.anyline.metadata.Table; +import org.anyline.proxy.ServiceProxy; +import org.anyline.service.AnylineService; +import org.springframework.stereotype.Component; + +/** + * 创建表核心逻辑类 + * + * @author luolin + * @date 2022-09-18 21:56:11 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class CreateTableHandler { + + /** + * 运行表单主键ID + */ + private final static String RUN_FORM_COLUMN_NAME = "run_form_id"; + + /** + * 自动创建表 + * @param tableInfo 表信息 + */ + public Boolean createTable(TableInfo tableInfo) { + if (tableInfo.getColumns() == null || tableInfo.getColumns().isEmpty()) { + throw new RuntimeException("自动创建表[" + tableInfo.getName() + "]中至少需要一个字段"); + } + if (!TableModelEnum.CREATE.name().toLowerCase().equals(tableInfo.getModel())) { + throw new RuntimeException("自动创建表[" + tableInfo.getModel() + "]模式的处理未找到,请检查"); + } + try { + AnylineService service = ServiceProxy.service(); + // 如果已存在,删除重键 + Table table = service.metadata().table(tableInfo.getName(), false); + if (null != table) + service.ddl().drop(table); + // 执行建表SQL + service.ddl().create(tableInfo); + log.info("自动创建表处理完成!"); + } + catch (Exception e) { + throw new RuntimeException("自动创建表异常", e); + } + return Boolean.TRUE; + } + +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/enums/TableModelEnum.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/enums/TableModelEnum.java new file mode 100644 index 000000000..0d32dbfb5 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/enums/TableModelEnum.java @@ -0,0 +1,40 @@ +package com.pig4cloud.pigx.codegen.util.table.enums; +/* + * Copyright (c) 2018-2025, luolin All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: luolin (766488893@qq.com) + */ + +/** + * 表生成模式 + * + * @author luolin + * @date 2022-09-18 21:56:11 + */ +public enum TableModelEnum { + + /** + * 创建模式: 将删除所有已存在的表,重新创建,会清除所有数据 + */ + CREATE, + /** + * 更新模式: 保留已存在的表以及字段,不针对字段或其结构进行修改,不影响数据 + */ + UPDATE, + /** + * 默认模式: 不做任何操作 + */ + NONE; + +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/model/TableInfo.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/model/TableInfo.java new file mode 100644 index 000000000..a0cc56cb6 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/table/model/TableInfo.java @@ -0,0 +1,44 @@ +package com.pig4cloud.pigx.codegen.util.table.model; +/* + * Copyright (c) 2018-2025, luolin All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: luolin (766488893@qq.com) + */ + +import com.pig4cloud.pigx.codegen.util.table.enums.TableModelEnum; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.anyline.metadata.Table; + +/** + * 表信息 + * + * @author luolin + * @date 2022-09-18 21:56:11 + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class TableInfo extends Table { + + /** + * 建表模式 + */ + private String model = TableModelEnum.CREATE.name().toLowerCase(); + + /** + * 表CRUD模式 + */ + private String crudModel; + +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/vo/GenCreateTableVO.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/vo/GenCreateTableVO.java new file mode 100755 index 000000000..4d513234b --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/vo/GenCreateTableVO.java @@ -0,0 +1,95 @@ +package com.pig4cloud.pigx.codegen.util.vo; +/* + * Copyright (c) 2018-2025, luolin All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: luolin (766488893@qq.com) + */ + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import lombok.Data; + +import java.time.LocalDateTime; + +/** + * 自动创建表管理 + * + * @author luolin + * @date 2022-09-23 21:56:11 + */ +@Data +@Schema(description = "自动创建表管理") +public class GenCreateTableVO { + + private static final long serialVersionUID = 1L; + + /** + * 主键ID + */ + @Schema(description = "主键ID") + private Long id; + + /** + * 表名称 + */ + @NotBlank(message = "表名称不能为空") + @Schema(description = "表名称") + private String tableName; + + /** + * 表注释 + */ + @NotBlank(message = "表注释不能为空") + @Schema(description = "表注释") + private String comments; + + /** + * 数据源名称 + */ + @NotBlank(message = "数据源名称不能为空") + @Schema(description = "数据源名称") + private String dsName; + + /** + * 主键策略 + */ + @NotBlank(message = "主键策略不能为空") + @Schema(description = "主键策略") + private String pkPolicy; + + /** + * 创建人 + */ + @Schema(description = "创建人") + private Long createUser; + + /** + * 创建时间 + */ + @Schema(description = "创建时间") + private LocalDateTime createTime; + + /** + * 表字段信息 + */ + @Schema(description = "表字段信息") + private String columnsInfo; + + /** + * 字段信息 + */ + @Schema(description = "字段信息") + private String columnInfo; + +}