|
|
|
@ -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<Fieldx, IFieldxService> { |
|
|
|
|
@Autowired |
|
|
|
|
private IFieldxService fieldxService; |
|
|
|
|
@Autowired |
|
|
|
|
private ITablexService tablexService; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 分页列表查询 |
|
|
|
@ -82,9 +86,23 @@ public class FieldxController extends JeecgController<Fieldx, IFieldxService> { |
|
|
|
|
@ApiOperation(value="字段管理-添加", notes="字段管理-添加") |
|
|
|
|
@PostMapping(value = "/add") |
|
|
|
|
public Result<?> add(@RequestBody Fieldx fieldx) { |
|
|
|
|
|
|
|
|
|
fieldx.setVerison(1); |
|
|
|
|
if (fieldx.getTableId() != null){ |
|
|
|
|
List<Fieldx> fieldxList = fieldxService.list(new QueryWrapper<Fieldx>().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<Fieldx, IFieldxService> { |
|
|
|
|
@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<Fieldx, IFieldxService> { |
|
|
|
|
@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<Fieldx, IFieldxService> { |
|
|
|
|
@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<Fieldx, IFieldxService> { |
|
|
|
|
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<Fieldx> fieldxList = fieldxService.list(new QueryWrapper<Fieldx>().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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|