diff --git a/ant-design-vue-jeecg/src/views/fieldx/FieldxList.vue b/ant-design-vue-jeecg/src/views/fieldx/FieldxList.vue index acae4e2..1288f30 100644 --- a/ant-design-vue-jeecg/src/views/fieldx/FieldxList.vue +++ b/ant-design-vue-jeecg/src/views/fieldx/FieldxList.vue @@ -4,6 +4,27 @@
+ + + + + + + + + + + + + + + + + + 查询 + 重置 + +
@@ -71,19 +92,11 @@ 编辑 - - 更多 - - - 详情 - - - - 删除 - - - - + 详情 + + + 删除 + diff --git a/ant-design-vue-jeecg/src/views/tablex/TablexList.vue b/ant-design-vue-jeecg/src/views/tablex/TablexList.vue index 1bb52dc..d8cba3f 100644 --- a/ant-design-vue-jeecg/src/views/tablex/TablexList.vue +++ b/ant-design-vue-jeecg/src/views/tablex/TablexList.vue @@ -4,6 +4,27 @@
+ + + + + + + + + + + + + + + + + + 查询 + 重置 + +
@@ -71,19 +92,11 @@ 编辑 - - 更多 - - - 详情 - - - - 删除 - - - - + 详情 + + + 删除 + @@ -100,14 +113,17 @@ import { JeecgListMixin } from '@/mixins/JeecgListMixin' import TablexModal from './modules/TablexModal' import {filterMultiDictText} from '@/components/dict/JDictSelectUtil' + import JEllipsis from '@/components/jeecg/JEllipsis' export default { name: 'TablexList', mixins:[JeecgListMixin, mixinDevice], components: { - TablexModal + TablexModal, + JEllipsis, }, data () { + let ellipsis = (v, l = 20) => () return { description: '实体表管理页面', // 表头 @@ -140,7 +156,8 @@ { title:'表结构SQL', align:"center", - dataIndex: 'tableStructure' + dataIndex: 'tableStructure', + customRender: (t)=>ellipsis(t) }, { title:'结构图', diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/fieldx/controller/FieldxController.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/fieldx/controller/FieldxController.java index 358e077..01e1f93 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/fieldx/controller/FieldxController.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/fieldx/controller/FieldxController.java @@ -20,6 +20,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; +import org.jeecg.modules.tablex.entity.Tablex; +import org.jeecg.modules.tablex.service.ITablexService; import org.jeecgframework.poi.excel.ExcelImportUtil; import org.jeecgframework.poi.excel.def.NormalExcelConstants; import org.jeecgframework.poi.excel.entity.ExportParams; @@ -49,6 +51,8 @@ import org.jeecg.common.aspect.annotation.AutoLog; public class FieldxController extends JeecgController { @Autowired private IFieldxService fieldxService; + @Autowired + private ITablexService tablexService; /** * 分页列表查询 @@ -82,9 +86,23 @@ public class FieldxController extends JeecgController { @ApiOperation(value="字段管理-添加", notes="字段管理-添加") @PostMapping(value = "/add") public Result add(@RequestBody Fieldx fieldx) { - fieldx.setVerison(1); + if (fieldx.getTableId() != null){ + List fieldxList = fieldxService.list(new QueryWrapper().eq("table_id", fieldx.getTableId())); + for (Fieldx fieldx1 : fieldxList){ + if (fieldx1.getFieldName().equals(fieldx.getFieldName())){ + return Result.error("添加失败,所属实体已有该中文名称"); + } + if (fieldx1.getFieldEnName().equals(fieldx.getFieldEnName())){ + return Result.error("添加失败,所属实体已有该英文名称"); + } + if (fieldx.getIsPk() == 1 && fieldx1.getIsPk() == 1){ + return Result.error("该实体已经有主键,添加失败"); + } + } + } fieldxService.save(fieldx); + tablexService.updateSql(fieldx.getTableId(),createSql(fieldx.getTableId())); return Result.OK("添加成功!"); } @@ -99,6 +117,7 @@ public class FieldxController extends JeecgController { @PutMapping(value = "/edit") public Result edit(@RequestBody Fieldx fieldx) { fieldxService.updateById(fieldx); + tablexService.updateSql(fieldx.getTableId(),createSql(fieldx.getTableId())); return Result.OK("编辑成功!"); } @@ -113,6 +132,8 @@ public class FieldxController extends JeecgController { @DeleteMapping(value = "/delete") public Result delete(@RequestParam(name="id",required=true) String id) { fieldxService.removeById(id); + Fieldx fieldx = fieldxService.getById(id); + tablexService.updateSql(fieldx.getTableId(),createSql(fieldx.getTableId())); return Result.OK("删除成功!"); } @@ -127,6 +148,8 @@ public class FieldxController extends JeecgController { @DeleteMapping(value = "/deleteBatch") public Result deleteBatch(@RequestParam(name="ids",required=true) String ids) { this.fieldxService.removeByIds(Arrays.asList(ids.split(","))); + Fieldx fieldx = fieldxService.getById(ids.split(",")[0]); + tablexService.updateSql(fieldx.getTableId(),createSql(fieldx.getTableId())); return Result.OK("批量删除成功!"); } @@ -170,4 +193,184 @@ public class FieldxController extends JeecgController { return super.importExcel(request, response, Fieldx.class); } + +// 生成sql + public String createSql(String tableId){ + Tablex tablex = tablexService.getById(tableId); + String tableEnName = tablex.getTableEnName(); + String sql = "DROP TABLE IF EXISTS "+"'"+tableEnName+"';"+" CREATE TABLE '"+tableEnName+"' ("; + String primary = ""; + String unique = ""; + String foreign = ""; + String pre_foreign =""; + List fieldxList = fieldxService.list(new QueryWrapper().eq("table_id", tableId)); + for (Fieldx fieldx : fieldxList){ + String fieldEnName = fieldx.getFieldEnName(); + String fieldName = fieldx.getFieldName(); + String dateType = dateType(fieldx.getType(), fieldx.getLength(), fieldx.getAccuracy()); + String isnull = ""; +// 判断是否为空 + if (fieldx.getIsNull() == 1){ + isnull = " NULL"; + }else { + isnull = " NOT NULL"; + } +// 判断是否是主键 + if (fieldx.getIsPk() == 1){ + if (primary != ""){ + primary = primary+" ,'"+fieldEnName+"'"; + }else { + primary ="'"+fieldEnName+"'"; + } + } +// 判断是否是唯一 并且不是主键的 + if (fieldx.getIsUniqueness() == 0 && fieldx.getIsPk() != 1){ + if (unique != ""){ + unique = unique+" UNIQUE INDEX('"+fieldEnName+"'),"; + }else { + unique = " UNIQUE INDEX('"+fieldEnName+"'),"; + } + } +// 判断是否是外键 + Fieldx fieldx_foreign = fieldxService.getById(fieldx.getAssociateField()); + Tablex tablex_foreign = tablexService.getById(fieldx.getAssociateTable()); + if (fieldx.getIsFk() == 1 && fieldx.getAssociateTable() != null && fieldx.getAssociateField() != null){ + if (foreign != ""){ + foreign = foreign+" FOREIGN KEY ('"+fieldEnName+"') REFERENCES '"+tablex_foreign.getTableEnName()+"' ('"+fieldx_foreign.getFieldEnName()+"'),"; + }else { + foreign = " FOREIGN KEY ('"+fieldEnName+"') REFERENCES '"+tablex_foreign.getTableEnName()+"' ('"+fieldx_foreign.getFieldEnName()+"'),"; + } +// 判断外键关联的字段是否是主键 + if (fieldx_foreign.getIsPk() ==0){ + pre_foreign = pre_foreign+"ALTER TABLE '"+tablex_foreign.getTableEnName()+"' ADD INDEX('"+fieldx_foreign.getFieldEnName()+"');"; + } + } +// 拼装sql + sql = sql+"'"+fieldEnName+"' "+dateType+isnull+" COMMENT '"+fieldName+"',"; + } + if (pre_foreign != ""){ + sql = pre_foreign+sql; + } + if (primary != ""){ + sql = sql+" PRIMARY KEY ("+primary+"), "; + } + if (unique != ""){ + sql = sql+unique; + } + if (foreign != ""){ + sql = sql+foreign; + } +// 移除逗号 + String a = sql.substring(0, sql.length() - 1); + sql = a+");"; + return sql; + } + + public String dateType(Integer type,Integer length,Integer accuracy) { + /** + * 数据类型对应编号: + * 1 tinyint,2 smallint,3 mediumint,4 int,5 bigint,6 float,7 double,8 decimal,9 year,10 time + * 11 date,12 datetime,13 timestamp,14 char,15 varchar,16 binary,17 varbinary,18 blob,19 text,20 enum + * 21 set, 22 bit,23 tinyblob,24 blob,25 mediumblob,26 longblob + */ + if (type == 1) { + if (length != null) { + return "tinyint" + "(" + length + ")"; + } else return "tinyint"; + } else if (type == 2) { + if (length != null) { + return "smallint" + "(" + length + ")"; + } else return "smallint" + "(" + length + ")"; + } else if (type == 3) { + if (length != null) { + return "mediumint" + "(" + length + ")"; + } else return "mediumint" + "(" + length + ")"; + } else if (type == 4) { + if (length != null) { + return "int" + "(" + length + ")"; + } else return "int" + "(" + length + ")"; + } else if (type == 5) { + if (length != null) { + return "bigint" + "(" + length + ")"; + } else return "bigint" + "(" + length + ")"; + } else if (type == 6) { + if (length != null) { + if (accuracy != null) { + return "float" + "(" + length + " ," + accuracy + ")"; + } else return "float" + "(" + length + ")"; + } else { + return "float"; + } + } else if (type == 7) { + if (length != null) { + if (accuracy != null) { + return "double" + "(" + length + " ," + accuracy + ")"; + } else return "double" + "(" + length + ")"; + } else { + return "double"; + } + } else if (type == 8) { + if (length != null) { + if (accuracy != null) { + return "decimal" + "(" + length + " ," + accuracy + ")"; + } else return "decimal" + "(" + length + ")"; + } else { + return "decimal"; + } + } else if (type == 9) { + return "year"; + } else if (type == 10) { + if (length != null) { + return "time" + "(" + length + ")"; + } else return "time"; + } else if (type == 11) { + return "date"; + } else if (type == 12) { + if (length != null) { + return "datetime" + "(" + length + ")"; + } else return "datetime"; + } else if (type == 13) { + if (length != null) { + return "timestamp" + "(" + length + ")"; + } else return "timestamp"; + } else if (type == 14) { + if (length != null) { + return "char" + "(" + length + ")"; + } else return "char"; + } else if (type == 15) { + if (length != null) { + return "varchar" + "(" + length + ")"; + } else return "varchar"; + } else if (type == 16) { + if (length != null) { + return "binary" + "(" + length + ")"; + } else return "binary"; + } else if (type == 17) { + if (length != null) { + return "varbinary" + "(" + length + ")"; + } else return "varbinary"; + } else if (type == 18) { + return "blob"; + } else if (type == 19) { + return "text"; + } else if (type == 20) { + return "enum"; + } else if (type == 21) { + return "set"; + } else if (type == 22) { + if (length != null) { + return "bit" + "(" + length + ")"; + } else return "bit"; + } else if (type == 23) { + return "tinyblob"; + } else if (type == 24) { + return "blob"; + } else if (type == 25) { + return "mediumblob"; + } else if (type == 26) { + return "longblob"; + } else return null; + } + + } diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/controller/TablexController.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/controller/TablexController.java index e3c2096..b682662 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/controller/TablexController.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/controller/TablexController.java @@ -9,9 +9,14 @@ import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + +import io.swagger.models.auth.In; import org.jeecg.common.api.vo.Result; import org.jeecg.common.system.query.QueryGenerator; import org.jeecg.common.util.oConvertUtils; +import org.jeecg.modules.fieldx.controller.FieldxController; +import org.jeecg.modules.fieldx.entity.Fieldx; +import org.jeecg.modules.fieldx.service.IFieldxService; import org.jeecg.modules.modulex.entity.Modulex; import org.jeecg.modules.modulex.service.IModulexService; import org.jeecg.modules.tablex.entity.Tablex; @@ -54,6 +59,11 @@ public class TablexController extends JeecgController { @Autowired private IModulexService modulexService; + @Autowired + private IFieldxService fieldxService; + + @Autowired + private FieldxController fieldxController; /** * 分页列表查询 @@ -93,6 +103,7 @@ public class TablexController extends JeecgController { tablex.setTableEnName(modulex.getModuleEnName()); } tablex.setVerison(1); + tablex.setTableStructure("CREATE TABLE '"+tablex.getTableEnName()+"' ();"); tablexService.save(tablex); return Result.OK("添加成功!"); } @@ -107,7 +118,13 @@ public class TablexController extends JeecgController { @ApiOperation(value="实体表-编辑", notes="实体表-编辑") @PutMapping(value = "/edit") public Result edit(@RequestBody Tablex tablex) { + if (tablex.getModuleId() != null){ + Modulex modulex = modulexService.getById(tablex.getModuleId()); + tablex.setTableName(modulex.getModuleName()); + tablex.setTableEnName(modulex.getModuleEnName()); + } tablexService.updateById(tablex); + tablexService.updateSql(tablex.getId(),fieldxController.createSql(tablex.getId())); return Result.OK("编辑成功!"); } diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/mapper/TablexMapper.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/mapper/TablexMapper.java index cc716b3..23a2e58 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/mapper/TablexMapper.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/mapper/TablexMapper.java @@ -3,6 +3,8 @@ package org.jeecg.modules.tablex.mapper; import java.util.List; import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Update; +import org.checkerframework.checker.guieffect.qual.UI; import org.jeecg.modules.tablex.entity.Tablex; import com.baomidou.mybatisplus.core.mapper.BaseMapper; @@ -14,4 +16,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; */ public interface TablexMapper extends BaseMapper { + @Update("update tablex set table_structure = #{sql} where id = #{tableId}") + void updateSql(String tableId, String sql); } diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/service/ITablexService.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/service/ITablexService.java index d29852a..fba23b7 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/service/ITablexService.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/service/ITablexService.java @@ -11,4 +11,6 @@ import com.baomidou.mybatisplus.extension.service.IService; */ public interface ITablexService extends IService { + void updateSql(String tableId, String sql); + } diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/service/impl/TablexServiceImpl.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/service/impl/TablexServiceImpl.java index e105a94..62fc0fe 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/service/impl/TablexServiceImpl.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/tablex/service/impl/TablexServiceImpl.java @@ -3,6 +3,7 @@ package org.jeecg.modules.tablex.service.impl; import org.jeecg.modules.tablex.entity.Tablex; import org.jeecg.modules.tablex.mapper.TablexMapper; import org.jeecg.modules.tablex.service.ITablexService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -16,4 +17,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @Service public class TablexServiceImpl extends ServiceImpl implements ITablexService { + @Autowired + private TablexMapper tablexMapper; + + @Override + public void updateSql(String tableId, String sql) { + tablexMapper.updateSql(tableId,sql); + } }