diff --git a/app/src/main/resources/sql/h2/create_model_ddl_2025_0717.sql b/app/src/main/resources/sql/h2/create_model_ddl_2025_0717.sql new file mode 100644 index 00000000..b0c8400f --- /dev/null +++ b/app/src/main/resources/sql/h2/create_model_ddl_2025_0717.sql @@ -0,0 +1,22 @@ +drop table if exists `t_model`; + +create table `t_model` +( + `id` int not null auto_increment comment '主键id', + `name_cn` varchar(255) not null comment '中文名称', + `name_en` varchar(255) not null comment '英文名称', + `version` varchar(255) comment '版本', + `model_url` varchar(255) comment '模型url', + `parameters` varchar(2000) not null comment '字段参数', + `method` longtext comment '方法', + `description` varchar(2000) comment '描述', + `created_by` varchar(60) not null comment '创建人', + `created_time` timestamp not null default current_timestamp comment '创建时间', + `last_updated_by` varchar(60) not null comment '最后修改人', + `last_updated_time` timestamp not null default current_timestamp comment '更新时间', + `tenant_id` varchar(60) comment '租户id', + `renter_id` varchar(60) comment '业务租户id', + `site_id` varchar(60) comment '站点id,设计预留字段', + primary key (`id`) using btree, + unique index `u_idx_model` (`name_en`,`version`) using btree +) engine = innodb comment = '模型表'; diff --git a/app/src/main/resources/sql/h2/update_all_tables_ddl.sql b/app/src/main/resources/sql/h2/update_all_tables_ddl.sql index 879a44c7..7a944faf 100644 --- a/app/src/main/resources/sql/h2/update_all_tables_ddl.sql +++ b/app/src/main/resources/sql/h2/update_all_tables_ddl.sql @@ -27,6 +27,3 @@ ALTER TABLE t_platform MODIFY tenant_id varchar(60) NULL; ALTER TABLE t_platform_history MODIFY tenant_id varchar(60) NULL; ALTER TABLE t_task_record MODIFY tenant_id varchar(60) NULL; ALTER TABLE t_user MODIFY tenant_id varchar(60) NULL; - -ALTER TABLE t_component_library ADD app_id int NULL; - diff --git a/app/src/main/resources/sql/mysql/create_model_ddl_2025_0717.sql b/app/src/main/resources/sql/mysql/create_model_ddl_2025_0717.sql new file mode 100644 index 00000000..b0c8400f --- /dev/null +++ b/app/src/main/resources/sql/mysql/create_model_ddl_2025_0717.sql @@ -0,0 +1,22 @@ +drop table if exists `t_model`; + +create table `t_model` +( + `id` int not null auto_increment comment '主键id', + `name_cn` varchar(255) not null comment '中文名称', + `name_en` varchar(255) not null comment '英文名称', + `version` varchar(255) comment '版本', + `model_url` varchar(255) comment '模型url', + `parameters` varchar(2000) not null comment '字段参数', + `method` longtext comment '方法', + `description` varchar(2000) comment '描述', + `created_by` varchar(60) not null comment '创建人', + `created_time` timestamp not null default current_timestamp comment '创建时间', + `last_updated_by` varchar(60) not null comment '最后修改人', + `last_updated_time` timestamp not null default current_timestamp comment '更新时间', + `tenant_id` varchar(60) comment '租户id', + `renter_id` varchar(60) comment '业务租户id', + `site_id` varchar(60) comment '站点id,设计预留字段', + primary key (`id`) using btree, + unique index `u_idx_model` (`name_en`,`version`) using btree +) engine = innodb comment = '模型表'; diff --git a/base/src/main/java/com/tinyengine/it/common/enums/Enums.java b/base/src/main/java/com/tinyengine/it/common/enums/Enums.java index c534de21..fe3b2ea6 100644 --- a/base/src/main/java/com/tinyengine/it/common/enums/Enums.java +++ b/base/src/main/java/com/tinyengine/it/common/enums/Enums.java @@ -1003,4 +1003,168 @@ public String getValue() { return value; } } + + public enum methodName { + + /** + * CREATED name. + */ + CREATED("新增方法"), + + /** + * UPDATE name. + */ + UPDATE("修改方法"), + + /** + * DELETE name. + */ + DELETE("删除方法"), + + /** + * QUERY name. + */ + QUERY("查询方法"), + + /** + * INSERTAPI name. + */ + INSERTAPI("insertApi"), + + /** + * UPDATEAPI name. + */ + UPDATEAPI("updateApi"), + + /** + * QUERYAPI name. + */ + QUERYAPI("queryApi"), + + /** + * INSERTAPI name. + */ + DELETEAPI("deleteApi"); + + private final String value; + + methodName(String value) { + this.value = value; + } + + /** + * Gets value. + * + * @return the value + */ + public String getValue() { + return value; + } + } + + public enum paramType { + + /** + * TYPE name. + */ + OBJECT("Object"), + + /** + * TYPE name. + */ + NUMBER("Number"), + + /** + * TYPE name. + */ + STRING("String"), + + /** + * TYPE name. + */ + ENUM("Enum"); + + private final String value; + + paramType(String value) { + this.value = value; + } + + /** + * Gets value. + * + * @return the value + */ + public String getValue() { + return value; + } + } + + public enum methodParam { + + /** + * ID + */ + ID("id"), + /** + * CURRENTPAGE + */ + CURRENTPAGE("currentPage"), + + /** + * PAGESIZE + */ + PAGESIZE("pageSize"), + + /** + * NAMECN + */ + NAMECN("nameCn"), + + /** + * NAMEEN + */ + NAMEEN("nameEn"), + + /** + * PARAMS + */ + PARAMS("params"), + + /** + * CODE + */ + CODE("code"), + + /** + * MESSAGE + */ + MESSAGE("message"), + + /** + * DATA + */ + DATA("data"), + + /** + * TOTAL + */ + TOTAL("total"); + + + private final String value; + + methodParam(String value) { + this.value = value; + } + + /** + * Gets value. + * + * @return the value + */ + public String getValue() { + return value; + } + } } diff --git a/base/src/main/java/com/tinyengine/it/controller/ModelController.java b/base/src/main/java/com/tinyengine/it/controller/ModelController.java new file mode 100644 index 00000000..394575a4 --- /dev/null +++ b/base/src/main/java/com/tinyengine/it/controller/ModelController.java @@ -0,0 +1,218 @@ +/** + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ + +package com.tinyengine.it.controller; + +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.tinyengine.it.common.base.Result; +import com.tinyengine.it.common.log.SystemControllerLog; +import com.tinyengine.it.model.entity.Model; +import com.tinyengine.it.service.material.ModelService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * 模型 + * + * @since 2025-07-17 + */ +@Validated +@RestController +@RequestMapping("/material-center/api") +@Tag(name = "模型") +public class ModelController { + /** + * The Model service. + */ + @Autowired + private ModelService modelService; + + /** + * 查询表Model信息 + * + * @return all Model信息 + */ + @Operation(summary = "查询表Model信息列表", description = "查询表Model信息列表", parameters = { + @Parameter(name = "currentPage", description = "当前页"), + @Parameter(name = "pageSize", description = "页数"), + @Parameter(name = "nameCn", description = "模型中文名称"), + @Parameter(name = "nameEn", description = "模型英文名称") + }, responses = { + @ApiResponse(responseCode = "200", description = "返回信息", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = Model.class))), + @ApiResponse(responseCode = "400", description = "请求失败") + }) + @SystemControllerLog(description = "查询表Model信息列表") + @GetMapping("/model/list") + public Result> getAllModel(@RequestParam(value = "currentPage", required = false) Integer currentPage, + @RequestParam(value = "pageSize", required = false) Integer pageSize, + @RequestParam(value = "nameCn", required = false) String nameCn, + @RequestParam(value = "nameEn", required = false) String nameEn) { + Page modelPage = modelService.pageQuery(currentPage, pageSize, nameCn, nameEn); + return Result.success(modelPage); + } + + /** + * 根据name查询表Model信息 + * + * @return Model信息 + */ + @Operation(summary = "根据nameCn查询表Model信息", description = "根据nameCn查询表Model信息", parameters = { + @Parameter(name = "nameCn", description = "名称"), + }, responses = { + @ApiResponse(responseCode = "200", description = "返回信息", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = Model.class))), + @ApiResponse(responseCode = "400", description = "请求失败") + }) + @SystemControllerLog(description = "根据nameCn查询表Model信息") + @GetMapping("/model/find") + public Result> getModelByName(@RequestParam(value = "nameCn", required = false) String nameCn) { + List modelPage = modelService.getModelByName(nameCn); + return Result.success(modelPage); + } + + /** + * 创建Model + * + * @param model the model + * @return Model信息 result + */ + @Operation(summary = "创建Model", description = "创建Model", parameters = { + @Parameter(name = "Model", description = "Model入参对象") + }, responses = { + @ApiResponse(responseCode = "200", description = "返回信息", content = @Content(mediaType = "application/json", + schema = @Schema(implementation = Model.class))), + @ApiResponse(responseCode = "400", description = "请求失败") + }) + @SystemControllerLog(description = "创建Model") + @PostMapping("/model/create") + public Result createModel(@Valid @RequestBody Model model) { + Model result = modelService.createModel(model); + return Result.success(result); + } + + /** + * 修改Model信息 + * + * @param id the id + * @param model the model + * @return Model信息 result + */ + @Operation(summary = "修改单个Model信息", description = "修改单个Model信息", parameters = { + @Parameter(name = "id", description = "模型id"), + @Parameter(name = "Model", description = "入参对象") + }, responses = { + @ApiResponse(responseCode = "200", description = "返回信息", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = Model.class))), + @ApiResponse(responseCode = "400", description = "请求失败") + }) + @SystemControllerLog(description = "修改单个Model信息") + @PutMapping("/model/update/{id}") + public Result updateModel(@PathVariable Integer id, @RequestBody Model model) { + model.setId(id); + Model result = modelService.updateModelById(model); + return Result.success(result); + } + + /** + * 删除Model信息 + * + * @param id the id + * @return app信息 result + */ + @Operation(summary = "删除Model信息", description = "删除Model信息", parameters = { + @Parameter(name = "id", description = "Model主键id") + }, responses = { + @ApiResponse(responseCode = "200", description = "返回信息", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = Model.class))), + @ApiResponse(responseCode = "400", description = "请求失败") + }) + @SystemControllerLog(description = "删除Model信息") + @DeleteMapping("/model/delete/{id}") + public Result deleteModel(@PathVariable Integer id) { + Model result = modelService.deleteModelById(id); + return Result.success(result); + } + + /** + * 获取Model信息详情 + * + * @param id the id + * @return the result + */ + @Operation(summary = "获取Model信息详情", description = "获取Model信息详情", parameters = { + @Parameter(name = "id", description = "模型id") + }, responses = { + @ApiResponse(responseCode = "200", description = "返回信息", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = Model.class))), + @ApiResponse(responseCode = "400", description = "请求失败")}) + @SystemControllerLog(description = "获取Model信息详情") + @GetMapping("/model/detail/{id}") + public Result detail(@PathVariable Integer id) { + Model result = modelService.queryModelById(id); + return Result.success(result); + } + + /** + * 获取Model建表sql + * + * @param id the id + * @return the result + */ + @Operation(summary = "获取Model建表sql", description = "获取Model建表sql", parameters = { + @Parameter(name = "id", description = "模型id") + }, responses = { + @ApiResponse(responseCode = "200", description = "返回信息", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = String.class))), + @ApiResponse(responseCode = "400", description = "请求失败")}) + @SystemControllerLog(description = "获取Model建表sql") + @GetMapping("/model/table/{id}") + public Result getTable(@PathVariable Integer id) { + String result = modelService.getTableById(id); + return Result.success(result); + } + + /** + * 获取所有Model建表sql + * + * @return the result + */ + @Operation(summary = "获取所有Model建表sql", description = "获取所有Model建表sql", parameters = { + }, responses = { + @ApiResponse(responseCode = "200", description = "返回信息", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = String.class))), + @ApiResponse(responseCode = "400", description = "请求失败")}) + @SystemControllerLog(description = "获取所有Model建表sql") + @GetMapping("/model/table/list") + public Result getAllTable() { + String result = modelService.getAllTable(); + return Result.success(result); + } +} diff --git a/base/src/main/java/com/tinyengine/it/mapper/ModelMapper.java b/base/src/main/java/com/tinyengine/it/mapper/ModelMapper.java new file mode 100644 index 00000000..1ecbbf2a --- /dev/null +++ b/base/src/main/java/com/tinyengine/it/mapper/ModelMapper.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ + +package com.tinyengine.it.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.tinyengine.it.model.entity.Model; + +/** + * The interface Model mapper. + * + * @since 2025-07-17 + */ +public interface ModelMapper extends BaseMapper { + /** + * 新增表t_model数据 + * + * @param model the model + * @return the int + */ + int createModel(Model model); + + /** + * 修改表t_model数据 + * + * @param model the model + * @return the int + */ + int updateModelById(Model model); + +} diff --git a/base/src/main/java/com/tinyengine/it/model/dto/MethodDto.java b/base/src/main/java/com/tinyengine/it/model/dto/MethodDto.java new file mode 100644 index 00000000..bcf06a56 --- /dev/null +++ b/base/src/main/java/com/tinyengine/it/model/dto/MethodDto.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ + +package com.tinyengine.it.model.dto; + +import lombok.Data; + +import java.util.List; + +/** + * The type Method dto. + * + * @since 2025-07-17 + */ +@Data +public class MethodDto { + private String name; + private String nameEn; + private List requestParameters; + private List responseParameters; +} diff --git a/base/src/main/java/com/tinyengine/it/model/dto/ParametersDto.java b/base/src/main/java/com/tinyengine/it/model/dto/ParametersDto.java new file mode 100644 index 00000000..680d3212 --- /dev/null +++ b/base/src/main/java/com/tinyengine/it/model/dto/ParametersDto.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ + +package com.tinyengine.it.model.dto; + +import lombok.Data; + +/** + * The type Parameters dto. + * + * @since 2025-07-17 + */ +@Data +public class ParametersDto { + private String prop; + private String label; + private String defaultValue; + private Boolean isModel; + private String type; + private String options; + private Boolean required; + private String description; +} diff --git a/base/src/main/java/com/tinyengine/it/model/dto/RequestParameter.java b/base/src/main/java/com/tinyengine/it/model/dto/RequestParameter.java new file mode 100644 index 00000000..109f2086 --- /dev/null +++ b/base/src/main/java/com/tinyengine/it/model/dto/RequestParameter.java @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ + +package com.tinyengine.it.model.dto; + +import lombok.Data; + +import java.util.List; + +/** + * The type responseParameter dto. + * + * @since 2025-07-17 + */ +@Data +public class RequestParameter { + private String prop; + private String type; + private List children; +} diff --git a/base/src/main/java/com/tinyengine/it/model/dto/ResponseParameter.java b/base/src/main/java/com/tinyengine/it/model/dto/ResponseParameter.java new file mode 100644 index 00000000..2f42152c --- /dev/null +++ b/base/src/main/java/com/tinyengine/it/model/dto/ResponseParameter.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ + +package com.tinyengine.it.model.dto; + +import lombok.Data; + +/** + * The type responseParameter dto. + * + * @since 2025-07-17 + */ +@Data +public class ResponseParameter { + private String prop; + private String type; +} diff --git a/base/src/main/java/com/tinyengine/it/model/entity/Model.java b/base/src/main/java/com/tinyengine/it/model/entity/Model.java new file mode 100644 index 00000000..1bd7f835 --- /dev/null +++ b/base/src/main/java/com/tinyengine/it/model/entity/Model.java @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ + + +package com.tinyengine.it.model.entity; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableName; +import com.tinyengine.it.common.base.BaseEntity; +import com.tinyengine.it.common.handler.ListTypeHandler; +import com.tinyengine.it.model.dto.MethodDto; +import com.tinyengine.it.model.dto.ParametersDto; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +/** + * 模型表 + * + * @since 2025-07-17 + */ +@Getter +@Setter +@TableName("t_model") +@Schema(name = "Model", description = "模型表") +public class Model extends BaseEntity { + + @Schema(name = "name_cn", description = "中文名称") + private String nameCn; + + @Schema(name = "name_En", description = "英文名称") + private String nameEn; + + @Schema(name = "version", description = "版本") + private String version; + + @Schema(name = "model_url", description = "模型Url") + private String modelUrl; + + @Schema(name = "parameters", description = "字段参数") + @TableField(typeHandler = ListTypeHandler.class) + private List parameters; + + @Schema(name = "method", description = "方法") + @TableField(typeHandler = ListTypeHandler.class) + private List method; + + @Schema(name = "description", description = "描述") + private String description; + +} diff --git a/base/src/main/java/com/tinyengine/it/service/material/ModelService.java b/base/src/main/java/com/tinyengine/it/service/material/ModelService.java new file mode 100644 index 00000000..8f88eaf1 --- /dev/null +++ b/base/src/main/java/com/tinyengine/it/service/material/ModelService.java @@ -0,0 +1,82 @@ +/** + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ + +package com.tinyengine.it.service.material; + +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.IService; +import com.tinyengine.it.model.entity.Model; + +import java.io.IOException; +import java.util.List; + +public interface ModelService extends IService{ + /** + * 查询表t_model信息 + * + * @return the Model + */ + Model queryModelById(Integer id); + + /** + * 根据name查询表t_model信息 + * + * @return the model list + */ + List getModelByName(String nameCn); + + /** + * 分页查询表t_model + * + * @return the Page + */ + Page pageQuery(int currentPage, int pageSize, String nameCn, String nameEn); + + /** + * 创建t_model + * + * @return the model + * @ param the model + */ + Model createModel(Model model); + + /** + * 删除t_model + * + * @return the Model + * @ param the id + */ + Model deleteModelById(Integer id); + + /** + * 修改t_model + * + * @return the model + * @ param the model + */ + Model updateModelById(Model model); + + /** + * 获取Model建表sql + * + * @ param the id + * @return the String + * @throws IOException + */ + String getTableById(Integer id); + + /** + * 获取所有模型的建表SQL语句 + * @return 拼接好的SQL语句字符串,每个表的SQL用分号分隔并换行 + */ + String getAllTable(); +} diff --git a/base/src/main/java/com/tinyengine/it/service/material/impl/ModelServiceImpl.java b/base/src/main/java/com/tinyengine/it/service/material/impl/ModelServiceImpl.java new file mode 100644 index 00000000..96bf4252 --- /dev/null +++ b/base/src/main/java/com/tinyengine/it/service/material/impl/ModelServiceImpl.java @@ -0,0 +1,337 @@ +/** + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ + +package com.tinyengine.it.service.material.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.tinyengine.it.common.enums.Enums; +import com.tinyengine.it.common.exception.ExceptionEnum; +import com.tinyengine.it.common.exception.ServiceException; +import com.tinyengine.it.common.log.SystemServiceLog; +import com.tinyengine.it.common.utils.JsonUtils; +import com.tinyengine.it.mapper.ModelMapper; +import com.tinyengine.it.model.dto.MethodDto; +import com.tinyengine.it.model.dto.ParametersDto; +import com.tinyengine.it.model.dto.RequestParameter; +import com.tinyengine.it.model.dto.ResponseParameter; +import com.tinyengine.it.model.entity.Model; +import com.tinyengine.it.service.material.ModelService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.StringJoiner; +import java.util.stream.Collectors; + +@Service +@Slf4j +public class ModelServiceImpl extends ServiceImpl implements ModelService { + /** + * 查询表t_model信息 + * + * @param id + * @return the Model + */ + @Override + @SystemServiceLog(description = "根据id查询model实现方法") + public Model queryModelById(Integer id) { + return this.baseMapper.selectById(id); + } + + /** + * 根据name查询表t_model信息 + * + * @param nameCn + * @return the model list + */ + @Override + @SystemServiceLog(description = "根据名称查询model实现方法") + public List getModelByName(String nameCn) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.like("name_cn", nameCn); + return this.baseMapper.selectList(queryWrapper); + } + + /** + * 分页查询表t_model信息 + * + * @return the list + */ + @Override + @SystemServiceLog(description = "分页查询model实现方法") + public Page pageQuery(int currentPage, int pageSize, String nameCn, String nameEn) { + Page page = new Page<>(currentPage, pageSize); + QueryWrapper queryWrapper = new QueryWrapper<>(); + + // 判断 nameCn 是否存在 + if (nameCn != null && !nameCn.isEmpty()) { + queryWrapper.like("name_cn", nameCn); + } + + // 判断 nameEn 是否存在 + if (nameEn != null && !nameEn.isEmpty()) { + if (nameCn != null && !nameCn.isEmpty()) { + queryWrapper.or().like("name_en", nameEn); + } else { + queryWrapper.like("name_en", nameEn); + } + } + + page(page, queryWrapper); + return page; + } + + /** + * 创建t_material + * + * @param model + * @return the model + * @ param the model + */ + @Override + @SystemServiceLog(description = "创建model实现方法") + public Model createModel(Model model) { + List methodDtos = new ArrayList<>(); + methodDtos.add(getMethodDto(Enums.methodName.CREATED.getValue(), Enums.methodName.INSERTAPI.getValue(), model)); + methodDtos.add(getMethodDto(Enums.methodName.UPDATE.getValue(), Enums.methodName.UPDATEAPI.getValue(), model)); + methodDtos.add(getMethodDto(Enums.methodName.QUERY.getValue(), Enums.methodName.QUERYAPI.getValue(), model)); + methodDtos.add(getMethodDto(Enums.methodName.DELETE.getValue(), Enums.methodName.DELETEAPI.getValue(), model)); + model.setMethod(methodDtos); + int result = this.baseMapper.createModel(model); + if (result != 1) { + throw new ServiceException(ExceptionEnum.CM001.getResultCode(), ExceptionEnum.CM001.getResultCode()); + } + return model; + } + + /** + * 删除t_model + * + * @param id + * @return the Model + * @ param the id + */ + @Override + @SystemServiceLog(description = "根据id删除model实现方法") + public Model deleteModelById(Integer id) { + Model model = this.baseMapper.selectById(id); + int result = this.baseMapper.deleteById(id); + if (result != 1) { + throw new ServiceException(ExceptionEnum.CM001.getResultCode(), ExceptionEnum.CM001.getResultCode()); + } + return model; + } + + /** + * 修改t_model + * + * @param model + * @return the model + * @ param the model + */ + @Override + @SystemServiceLog(description = "根据id修改model实现方法") + public Model updateModelById(Model model) { + List methodDtos = new ArrayList<>(); + methodDtos.add(getMethodDto(Enums.methodName.CREATED.getValue(), Enums.methodName.INSERTAPI.getValue(), model)); + methodDtos.add(getMethodDto(Enums.methodName.UPDATE.getValue(), Enums.methodName.UPDATEAPI.getValue(), model)); + methodDtos.add(getMethodDto(Enums.methodName.QUERY.getValue(), Enums.methodName.QUERYAPI.getValue(), model)); + methodDtos.add(getMethodDto(Enums.methodName.DELETE.getValue(), Enums.methodName.DELETEAPI.getValue(), model)); + model.setMethod(methodDtos); + if (model.getId() == null) { + throw new ServiceException(ExceptionEnum.CM002.getResultCode(), ExceptionEnum.CM002.getResultCode()); + } + int result = this.baseMapper.updateModelById(model); + if (result != 1) { + throw new ServiceException(ExceptionEnum.CM001.getResultCode(), ExceptionEnum.CM001.getResultCode()); + } + Model modelResult = this.baseMapper.selectById(model.getId()); + return modelResult; + } + + /** + * 获取Model建表sql + * + * @param id + * @return the String + * @ param the id + */ + @Override + public String getTableById(Integer id) { + Model model = this.baseMapper.selectById(id); + StringBuilder sql = new StringBuilder(getTableByModle(model)); + List rawList = model.getParameters(); + List fields = rawList.stream() + .map(item -> JsonUtils.MAPPER.convertValue(item, ParametersDto.class)) + .collect(Collectors.toList()); + fields.forEach(item -> { + if(item.getIsModel()) { + Model result = this.baseMapper.selectById(item.getDefaultValue()); + sql.append(getTableByModle(result)); + } + }); + return sql.toString(); + } + + /** + * 获取所有模型的建表SQL语句 + * @return 拼接好的SQL语句字符串,每个表的SQL用分号分隔并换行 + * @throws IOException 如果JSON解析失败 + */ + @Override + public String getAllTable() { + // 查询所有模型 + List modelList = this.baseMapper.selectList(null); + if (CollectionUtils.isEmpty(modelList)) { + return ""; + } + + StringJoiner sqlJoiner = new StringJoiner(" "); + + modelList.stream() + .map(this::getTableByModle) + .forEach(sqlJoiner::add); + + return sqlJoiner.toString(); + } + + private String getTableByModle(Model model) { + List rawList = model.getParameters(); + List fields = rawList.stream() + .map(item -> JsonUtils.MAPPER.convertValue(item, ParametersDto.class)) + .collect(Collectors.toList()); + + StringBuilder sql = new StringBuilder("CREATE TABLE " + model.getNameEn() + " ("); + + for (int i = 0; i < fields.size(); i++) { + ParametersDto field = fields.get(i); + + String prop = field.getProp(); + String type = field.getType(); + String defaultValue = field.getDefaultValue(); + + // 根据字段类型映射为 SQL 数据类型 + String sqlType = mapJavaTypeToSQL(type); + + sql.append(prop).append(" ").append(sqlType); + + if (defaultValue != null && !defaultValue.isEmpty()) { + sql.append(" DEFAULT ").append(defaultValue); + } + + // 如果不是最后一个字段,添加逗号 + if (i != fields.size() - 1) { + sql.append(", "); + } + } + + sql.append(");"); + return sql.toString(); + } + + private static String mapJavaTypeToSQL(String javaType) { + if (javaType == null) { + return "VARCHAR(255)"; // 默认处理 + } + switch (javaType) { + case "String": + return "VARCHAR(500)"; + case "Number": + return "INT"; + case "Boolean": + return "BOOLEAN"; + case "Date": + return "TIMESTAMP"; + case "Enum": + return "LONGTEXT"; + default: + return "LONGTEXT"; // 默认处理 + } + } + + private MethodDto getMethodDto(String name, String nameEn, Model model) { + MethodDto methodDto = new MethodDto(); + methodDto.setName(name); + methodDto.setNameEn(nameEn); + List responseParameterList = getResponseParameters(name); + RequestParameter requestParameter = new RequestParameter(); + requestParameter.setProp(Enums.methodParam.ID.getValue()); + requestParameter.setType(Enums.paramType.NUMBER.getValue()); + List parameterList = new ArrayList<>(); + if (name.equals(Enums.methodName.QUERY.getValue())) { + RequestParameter currentPage = new RequestParameter(); + currentPage.setProp(Enums.methodParam.CURRENTPAGE.getValue()); + currentPage.setType(Enums.paramType.NUMBER.getValue()); + RequestParameter pageSize = new RequestParameter(); + pageSize.setProp(Enums.methodParam.PAGESIZE.getValue()); + pageSize.setType(Enums.paramType.NUMBER.getValue()); + RequestParameter nameCn = new RequestParameter(); + nameCn.setProp(Enums.methodParam.NAMECN.getValue()); + nameCn.setType(Enums.paramType.STRING.getValue()); + RequestParameter requestNameEn = new RequestParameter(); + requestNameEn.setProp(Enums.methodParam.NAMEEN.getValue()); + requestNameEn.setType(Enums.paramType.STRING.getValue()); + parameterList.add(currentPage); + parameterList.add(pageSize); + parameterList.add(nameCn); + parameterList.add(requestNameEn); + + } + if (!name.equals(Enums.methodName.DELETE.getValue())) { + requestParameter.setProp(Enums.methodParam.PARAMS.getValue()); + requestParameter.setType(Enums.paramType.OBJECT.getValue()); + requestParameter.setChildren(model.getParameters()); + parameterList.add(requestParameter); + + methodDto.setRequestParameters(parameterList); + methodDto.setResponseParameters(responseParameterList); + return methodDto; + } + + parameterList.add(requestParameter); + methodDto.setRequestParameters(parameterList); + methodDto.setResponseParameters(responseParameterList); + return methodDto; + } + + private static List getResponseParameters(String name) { + ResponseParameter code = new ResponseParameter(); + code.setProp(Enums.methodParam.CODE.getValue()); + code.setType(Enums.paramType.NUMBER.getValue()); + ResponseParameter message = new ResponseParameter(); + message.setProp(Enums.methodParam.MESSAGE.getValue()); + message.setType(Enums.paramType.STRING.getValue()); + ResponseParameter data = new ResponseParameter(); + data.setProp(Enums.methodParam.DATA.getValue()); + data.setType(Enums.paramType.ENUM.getValue()); + + List responseParameterList = new ArrayList<>(); + if (name.equals(Enums.methodName.QUERY.getValue())) { + ResponseParameter total = new ResponseParameter(); + total.setProp(Enums.methodParam.TOTAL.getValue()); + total.setType(Enums.paramType.NUMBER.getValue()); + responseParameterList.add(total); + } + + responseParameterList.add(code); + responseParameterList.add(message); + responseParameterList.add(data); + return responseParameterList; + } + + +} diff --git a/base/src/main/resources/mappers/ModelMapper.xml b/base/src/main/resources/mappers/ModelMapper.xml new file mode 100644 index 00000000..cad1c2b1 --- /dev/null +++ b/base/src/main/resources/mappers/ModelMapper.xml @@ -0,0 +1,201 @@ + + + + + + + + + id + , name_cn, name_en, version, model_url, parameters, `method`, description, created_by, last_updated_by, created_time, last_updated_time, tenant_id, renter_id, site_id + + + + + + AND name_cn = #{nameCn} + + + AND name_en = #{nameEn} + + + AND version = #{version} + + + AND model_url = #{modelUrl} + + + AND parameters = #{parameters} + + + AND `method` = #{method} + + + AND description = #{description} + + + AND created_by = #{createdBy} + + + AND last_updated_by = #{lastUpdatedBy} + + + AND created_time = #{createdTime} + + + AND last_updated_time = #{lastUpdatedTime} + + + AND tenant_id = #{tenantId} + + + AND renter_id = #{renterId} + + + AND site_id = #{siteId} + + + + + + + + name_cn = #{nameCn}, + + + name_en = #{nameEn}, + + + version = #{version}, + + + model_url = #{modelUrl}, + + + parameters = #{parameters}, + + + `method` = #{method}, + + + description = #{description}, + + + created_by = #{createdBy}, + + + last_updated_by = #{lastUpdatedBy}, + + + created_time = #{createdTime}, + + + last_updated_time = #{lastUpdatedTime}, + + + tenant_id = #{tenantId}, + + + renter_id = #{renterId}, + + + site_id = #{siteId}, + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + DELETE + FROM t_model + WHERE id = #{id} + + + + + UPDATE t_model + + + + WHERE + id=#{id} + + + + + INSERT INTO t_model ( id + , name_cn + , name_en + , version + , model_url + , parameters + , `method` + , description + , created_by + , last_updated_by + , created_time + , last_updated_time + , tenant_id + , renter_id + , site_id) + VALUES ( #{id} + , #{nameCn} + , #{nameEn} + , #{version} + , #{modelUrl} + , #{parameters} + , #{method} + , #{description} + , #{createdBy} + , #{lastUpdatedBy} + , #{createdTime} + , #{lastUpdatedTime} + , #{tenantId} + , #{renterId} + , #{siteId}) + + diff --git a/base/src/main/resources/mappers/TenantMapper.xml b/base/src/main/resources/mappers/TenantMapper.xml index 7486be7f..dbf43841 100644 --- a/base/src/main/resources/mappers/TenantMapper.xml +++ b/base/src/main/resources/mappers/TenantMapper.xml @@ -142,6 +142,6 @@ , #{lastUpdatedBy} , #{createdTime} , #{lastUpdatedTime} - , #{orgCode} + , #{orgCode}) diff --git a/docker-deploy-data/mysql/init/create_model_ddl_2025_0717.sql b/docker-deploy-data/mysql/init/create_model_ddl_2025_0717.sql new file mode 100644 index 00000000..dbbe651e --- /dev/null +++ b/docker-deploy-data/mysql/init/create_model_ddl_2025_0717.sql @@ -0,0 +1,20 @@ +drop table if exists `t_model`; + +create table `t_model` +( + `id` int not null auto_increment comment '主键id', + `name` varchar(255) comment '名称', + `version` varchar(255) comment '版本', + `parameters` varchar(2000) not null comment '字段参数', + `method` longtext comment '方法', + `description` varchar(2000) comment '描述', + `created_by` varchar(60) not null comment '创建人', + `created_time` timestamp not null default current_timestamp comment '创建时间', + `last_updated_by` varchar(60) not null comment '最后修改人', + `last_updated_time` timestamp not null default current_timestamp comment '更新时间', + `tenant_id` varchar(60) comment '租户id', + `renter_id` varchar(60) comment '业务租户id', + `site_id` varchar(60) comment '站点id,设计预留字段', + primary key (`id`) using btree, + unique index `u_idx_model` (`name`,`version`) using btree +) engine = innodb comment = '模型表';