提交 27034f8e authored 作者: 黄承天's avatar 黄承天

[模型模块]分类复杂查询改动

上级 b51c53a2
...@@ -47,8 +47,6 @@ public class ModelController { ...@@ -47,8 +47,6 @@ public class ModelController {
private VersionService versionService; private VersionService versionService;
/** /**
* @param * @param
* @return java.util.List<com.tykj.workflowcore.model_layer.model.TableInfo> * @return java.util.List<com.tykj.workflowcore.model_layer.model.TableInfo>
...@@ -78,7 +76,7 @@ public class ModelController { ...@@ -78,7 +76,7 @@ public class ModelController {
List<ColumnInfo> columnInfos = modelService.showModelFields(searchColumnInfoVo); List<ColumnInfo> columnInfos = modelService.showModelFields(searchColumnInfoVo);
for (ColumnInfo columnInfo : columnInfos) { for (ColumnInfo columnInfo : columnInfos) {
List<Quote> allByColumnId = quoteDao.findAllByColumnId(columnInfo.getId()); List<Quote> allByColumnId = quoteDao.findAllByColumnId(columnInfo.getId());
columnInfo.setQuoteList(allByColumnId); columnInfo.setQuoteList(allByColumnId);
} }
return ResultUtil.success(columnInfos, "数据查询成功!"); return ResultUtil.success(columnInfos, "数据查询成功!");
} }
...@@ -183,7 +181,7 @@ public class ModelController { ...@@ -183,7 +181,7 @@ public class ModelController {
@ApiOperation("复杂查询(分类)") @ApiOperation("复杂查询(分类)")
@PostMapping("/complexQuery/group") @PostMapping("/complexQuery/group")
public ResponseEntity complexQueryWithGroup(@RequestBody complexQueryVo complexQueryVo) { public ResponseEntity complexQueryWithGroup(@RequestBody complexQueryVo complexQueryVo) {
Map<Object, List<Map<String, Object>>> list = modelService.complexQueryWithGroup(complexQueryVo.getTableName(), complexQueryVo.getColumnNames(), complexQueryVo.getQueryConditions(), complexQueryVo.getGroupByColumn()); Map<Object, List<Map<String, Object>>> list = modelService.complexQueryWithGroup(complexQueryVo.getTableName(), complexQueryVo.getGroupConditions(), complexQueryVo.getColumnNames(), complexQueryVo.getQueryConditions(), complexQueryVo.getGroupByColumn());
if (list != null) { if (list != null) {
return ResultUtil.success(list, "查询成功!"); return ResultUtil.success(list, "查询成功!");
} }
......
package com.tykj.model_layer.entity.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class GroupCondition {
private String name;
private String value;
}
...@@ -27,6 +27,9 @@ public class complexQueryVo { ...@@ -27,6 +27,9 @@ public class complexQueryVo {
@ApiModelProperty("筛选字段") @ApiModelProperty("筛选字段")
List<String> columnNames; List<String> columnNames;
@ApiModelProperty("分类条件信息")
List<GroupCondition> groupConditions;
@ApiModelProperty("查询条件") @ApiModelProperty("查询条件")
List<QueryCondition> queryConditions; List<QueryCondition> queryConditions;
......
...@@ -22,32 +22,33 @@ public interface ModelService { ...@@ -22,32 +22,33 @@ public interface ModelService {
/** /**
* 分页查询 * 分页查询
*
* @param searchTableInfoVo * @param searchTableInfoVo
* @return * @return
*/ */
Page<TableInfo> pageAllEntities(SearchTableInfoVo searchTableInfoVo); Page<TableInfo> pageAllEntities(SearchTableInfoVo searchTableInfoVo);
/** /**
* 方法重载不分页查询 * 方法重载不分页查询
*
* @return * @return
*/ */
List<TableInfo> listAllEntities(SearchTableInfoVo searchTableInfoVo) ; List<TableInfo> listAllEntities(SearchTableInfoVo searchTableInfoVo);
/** /**
* 根据表名查询所有字段信息 * 根据表名查询所有字段信息
*
* @param searchColumnInfoVo * @param searchColumnInfoVo
* @return * @return
*/ */
List<ColumnInfo> showModelFields(SearchColumnInfoVo searchColumnInfoVo); List<ColumnInfo> showModelFields(SearchColumnInfoVo searchColumnInfoVo);
/** /**
* 新建模型 * 新建模型
*
* @param tableVO * @param tableVO
* @return * @return
*/ */
...@@ -56,6 +57,7 @@ public interface ModelService { ...@@ -56,6 +57,7 @@ public interface ModelService {
/** /**
* 根据表名插入数据 * 根据表名插入数据
*
* @param map * @param map
* @return * @return
*/ */
...@@ -63,6 +65,7 @@ public interface ModelService { ...@@ -63,6 +65,7 @@ public interface ModelService {
/** /**
* 插入一组数据 * 插入一组数据
*
* @param mapList * @param mapList
* @return * @return
*/ */
...@@ -70,33 +73,37 @@ public interface ModelService { ...@@ -70,33 +73,37 @@ public interface ModelService {
/** /**
* 根据表名查询所有 * 根据表名查询所有
*
* @param name * @param name
* @return * @return
* @throws SQLException * @throws SQLException
*/ */
List<Map<String, Object>> findAllByName(String name) throws SQLException; List<Map<String, Object>> findAllByName(String name) throws SQLException;
/** /**
* 复杂查询 * 复杂查询
*
* @param tableName * @param tableName
* @param queryConditions * @param queryConditions
* @return * @return
*/ */
List<Map<String, Object>> complexQuery(String tableName,List<String> columnNames, List<QueryCondition> queryConditions); List<Map<String, Object>> complexQuery(String tableName, List<String> columnNames, List<QueryCondition> queryConditions);
/** /**
* 复杂查询(带分类) * 复杂查询(带分类)
*
* @param tableName * @param tableName
* @param queryConditions * @param queryConditions
* @return * @return
*/ */
Map<Object,List<Map<String, Object>>> complexQueryWithGroup(String tableName,List<String> columnNames, List<QueryCondition> queryConditions,String groupByColumn); Map<Object, List<Map<String, Object>>> complexQueryWithGroup(String tableName, List<GroupCondition> groupConditions, List<String>columnNames, List<QueryCondition> queryConditions, String groupByColumn);
/** /**
* 修改表 * 修改表
* @param updateTableInfoVO *
* @param updateTableInfoVO
* @return * @return
*/ */
int updateTable(UpdateTableInfoVO updateTableInfoVO); int updateTable(UpdateTableInfoVO updateTableInfoVO);
...@@ -104,6 +111,7 @@ public interface ModelService { ...@@ -104,6 +111,7 @@ public interface ModelService {
/** /**
* 删除表 * 删除表
*
* @param delTableVO * @param delTableVO
* @return * @return
*/ */
...@@ -112,6 +120,7 @@ public interface ModelService { ...@@ -112,6 +120,7 @@ public interface ModelService {
/** /**
* 根据id批量查询所有tableInfo和ColumnInfo * 根据id批量查询所有tableInfo和ColumnInfo
*
* @param ids * @param ids
* @return * @return
*/ */
......
...@@ -11,6 +11,7 @@ import com.tykj.model_layer.dao.TableInfoDao; ...@@ -11,6 +11,7 @@ import com.tykj.model_layer.dao.TableInfoDao;
import com.tykj.model_layer.entity.ColumnInfo; import com.tykj.model_layer.entity.ColumnInfo;
import com.tykj.model_layer.entity.Quote; import com.tykj.model_layer.entity.Quote;
import com.tykj.model_layer.entity.TableInfo; import com.tykj.model_layer.entity.TableInfo;
import com.tykj.model_layer.entity.customEnums.ConnectionType;
import com.tykj.model_layer.entity.vo.*; import com.tykj.model_layer.entity.vo.*;
import com.tykj.model_layer.myEnum.ModelType; import com.tykj.model_layer.myEnum.ModelType;
import com.tykj.model_layer.service.ModelService; import com.tykj.model_layer.service.ModelService;
...@@ -157,7 +158,7 @@ public class ModelImpl implements ModelService { ...@@ -157,7 +158,7 @@ public class ModelImpl implements ModelService {
tableInfo.setModelName(tableVO.getModelName()); tableInfo.setModelName(tableVO.getModelName());
tableInfo.setModelTitle(tableVO.getModelTitle()); tableInfo.setModelTitle(tableVO.getModelTitle());
tableInfo.setXml(xmlMapping); tableInfo.setXml(xmlMapping);
tableInfo.setBackName(tableVO.getModelName()+"_back"); tableInfo.setBackName(tableVO.getModelName() + "_back");
tableInfo.setBackXml(xmlBack); tableInfo.setBackXml(xmlBack);
tableInfo.setModelType(tableVO.getModelType()); tableInfo.setModelType(tableVO.getModelType());
tableInfo.setDescription(tableVO.getDescription()); tableInfo.setDescription(tableVO.getDescription());
...@@ -179,9 +180,9 @@ public class ModelImpl implements ModelService { ...@@ -179,9 +180,9 @@ public class ModelImpl implements ModelService {
columnInfo.setPrimaryKey(columnVO.getPrimaryKey()); columnInfo.setPrimaryKey(columnVO.getPrimaryKey());
ColumnInfo save = columnInfoDao.save(columnInfo); ColumnInfo save = columnInfoDao.save(columnInfo);
List<Quote> quoteList = columnVO.getQuoteList(); List<Quote> quoteList = columnVO.getQuoteList();
if (quoteList!=null){ if (quoteList != null) {
for (Quote quote : quoteList) { for (Quote quote : quoteList) {
if (StringUtils.isNotEmpty(quote.getValue())){ if (StringUtils.isNotEmpty(quote.getValue())) {
quote.setColumnId(save.getId()); quote.setColumnId(save.getId());
quoteDao.save(quote); quoteDao.save(quote);
} }
...@@ -218,22 +219,22 @@ public class ModelImpl implements ModelService { ...@@ -218,22 +219,22 @@ public class ModelImpl implements ModelService {
} }
Integer modelType = tableInfo.getModelType(); Integer modelType = tableInfo.getModelType();
SessionImpl session = (SessionImpl)sessionUtil.getSession(); SessionImpl session = (SessionImpl) sessionUtil.getSession();
session.getTransaction().begin(); session.getTransaction().begin();
Object values = map.get(tableName); Object values = map.get(tableName);
if (values instanceof Map) { if (values instanceof Map) {
//插入数据 //插入数据
insertValue(tableInfo.getModelName(), (Map) values,session,true,false); insertValue(tableInfo.getModelName(), (Map) values, session, true, false);
} else { } else {
//循环插入数据 //循环插入数据
List valuesList = (List) values; List valuesList = (List) values;
for (int i = 0; i < valuesList.size(); i++) { for (int i = 0; i < valuesList.size(); i++) {
insertValue(tableInfo.getModelName(), (Map) valuesList.get(i),session,true,false); insertValue(tableInfo.getModelName(), (Map) valuesList.get(i), session, true, false);
}
} }
session.getTransaction().commit(); }
session.close(); session.getTransaction().commit();
session.close();
} }
...@@ -249,7 +250,7 @@ public class ModelImpl implements ModelService { ...@@ -249,7 +250,7 @@ public class ModelImpl implements ModelService {
**/ **/
@Override @Override
public int putValueByEntityNameList(List<Map<String, Object>> mapList) { public int putValueByEntityNameList(List<Map<String, Object>> mapList) {
SessionImpl session = (SessionImpl)sessionUtil.getSession(); SessionImpl session = (SessionImpl) sessionUtil.getSession();
session.getTransaction().begin(); session.getTransaction().begin();
for (Map<String, Object> map : mapList) { for (Map<String, Object> map : mapList) {
for (String tableName : for (String tableName :
...@@ -267,17 +268,17 @@ public class ModelImpl implements ModelService { ...@@ -267,17 +268,17 @@ public class ModelImpl implements ModelService {
tableInfo = (TableInfo) one.get(); tableInfo = (TableInfo) one.get();
} }
Integer modelType = tableInfo.getModelType(); Integer modelType = tableInfo.getModelType();
Object values = map.get(tableName); Object values = map.get(tableName);
if (values instanceof Map) { if (values instanceof Map) {
//插入数据 //插入数据
insertValue(tableInfo.getModelName(), (Map) values,session,true,false); insertValue(tableInfo.getModelName(), (Map) values, session, true, false);
} else { } else {
//循环插入数据 //循环插入数据
List valuesList = (List) values; List valuesList = (List) values;
for (int i = 0; i < valuesList.size(); i++) { for (int i = 0; i < valuesList.size(); i++) {
insertValue(tableInfo.getModelName(), (Map) valuesList.get(i),session,true,false); insertValue(tableInfo.getModelName(), (Map) valuesList.get(i), session, true, false);
}
} }
}
} }
} }
...@@ -295,7 +296,7 @@ public class ModelImpl implements ModelService { ...@@ -295,7 +296,7 @@ public class ModelImpl implements ModelService {
* @Description 新增参数的方法 * @Description 新增参数的方法
* @Date 16:17 2021/3/5 * @Date 16:17 2021/3/5
**/ **/
public void insertValue(String tableName, Map map,SessionImpl session,boolean back,boolean saveOrSaveAndUpdate) { public void insertValue(String tableName, Map map, SessionImpl session, boolean back, boolean saveOrSaveAndUpdate) {
EntityPersister entityPersister = session.getEntityPersister(tableName, map); EntityPersister entityPersister = session.getEntityPersister(tableName, map);
Type[] propertyTypes = entityPersister.getPropertyTypes(); Type[] propertyTypes = entityPersister.getPropertyTypes();
String[] propertyNames = entityPersister.getEntityPersister().getPropertyNames(); String[] propertyNames = entityPersister.getEntityPersister().getPropertyNames();
...@@ -306,8 +307,8 @@ public class ModelImpl implements ModelService { ...@@ -306,8 +307,8 @@ public class ModelImpl implements ModelService {
//先将Type转为java类 //先将Type转为java类
if (propertyType instanceof TimestampType) { if (propertyType instanceof TimestampType) {
try { try {
if (value instanceof String){ if (value instanceof String) {
if (StringUtils.isNotEmpty((String)value)){ if (StringUtils.isNotEmpty((String) value)) {
Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) value); Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) value);
map.put(propertyNames[i], parse); map.put(propertyNames[i], parse);
} }
...@@ -329,17 +330,17 @@ public class ModelImpl implements ModelService { ...@@ -329,17 +330,17 @@ public class ModelImpl implements ModelService {
} }
HashMap hashMap = new HashMap(); HashMap hashMap = new HashMap();
hashMap.putAll(map); hashMap.putAll(map);
if (saveOrSaveAndUpdate){ if (saveOrSaveAndUpdate) {
session.save(tableName, map); session.save(tableName, map);
}else { } else {
session.saveOrUpdate(tableName, map); session.saveOrUpdate(tableName, map);
} }
if (back){ if (back) {
if (saveOrSaveAndUpdate){ if (saveOrSaveAndUpdate) {
session.save(tableName+"_back", map); session.save(tableName + "_back", map);
}else { } else {
session.saveOrUpdate(tableName+"_back", map); session.saveOrUpdate(tableName + "_back", map);
} }
} }
} }
...@@ -395,27 +396,36 @@ public class ModelImpl implements ModelService { ...@@ -395,27 +396,36 @@ public class ModelImpl implements ModelService {
} }
@Override @Override
public Map<Object, List<Map<String, Object>>> complexQueryWithGroup(String tableName, List<String> columnNames, List<QueryCondition> queryConditions, String groupByColumn) { public Map<Object, List<Map<String, Object>>> complexQueryWithGroup(String tableName, List<GroupCondition> groupConditions, List<String> columnNames, List<QueryCondition> queryConditions, String groupByColumn) {
Map<Object, List<Map<String, Object>>> result = new HashMap<>(); Map<Object, List<Map<String, Object>>> result = new HashMap<>();
if (Strings.isNotEmpty(tableName)) { if (Strings.isNotEmpty(tableName) && Strings.isNotEmpty(groupByColumn) && !groupConditions.isEmpty()) {
String query = createQuery(tableName, new ArrayList<>(), queryConditions, Strings.EMPTY); for (GroupCondition groupCondition : groupConditions) {
Session session = sessionUtil.getSession(); queryConditions.add(new QueryCondition(
Query query1 = session.createQuery(query); groupByColumn,
List<Map<String, Object>> list = query1.list(); "like",
if (Strings.isNotEmpty(groupByColumn)) { groupCondition.getValue(),
Set<Object> groupValues = list.stream().map(obj -> obj.get(groupByColumn)).collect(Collectors.toSet()); ConnectionType.AND
for (Object groupValue : groupValues) { ));
List<Map<String, Object>> entityList = list.stream().filter(obj -> Objects.equals(obj.get(groupByColumn), groupValue)).collect(Collectors.toList()); String query = createQuery(tableName, columnNames, queryConditions, Strings.EMPTY);
result.put(groupValue, entityList); Session session = sessionUtil.getSession();
} Query query1 = session.createQuery(query);
} else { List<Map<String, Object>> list = query1.list();
result.put("all", list); result.put(groupCondition.getName(), list);
} }
return result; return result;
} }
return null; return null;
} }
private QueryCondition queryCondition(String groupByColumn, GroupCondition groupCondition) {
return new QueryCondition(
groupByColumn,
"like",
groupCondition.getValue(),
ConnectionType.AND
);
}
@Override @Override
public int updateTable(UpdateTableInfoVO updateTableInfoVO) { public int updateTable(UpdateTableInfoVO updateTableInfoVO) {
//tableInfo和columnInfo变化 //tableInfo和columnInfo变化
...@@ -423,49 +433,49 @@ public class ModelImpl implements ModelService { ...@@ -423,49 +433,49 @@ public class ModelImpl implements ModelService {
TableVO tableVO = updateTableInfoVO.getTableVO(); TableVO tableVO = updateTableInfoVO.getTableVO();
Integer dbId = updateTableInfoVO.getDbId(); Integer dbId = updateTableInfoVO.getDbId();
TableInfo tableInfo = tableInfoDao.findById(dbId).orElseThrow(() -> new RuntimeException("未找到该id的表信息")); TableInfo tableInfo = tableInfoDao.findById(dbId).orElseThrow(() -> new RuntimeException("未找到该id的表信息"));
tableInfo.setUpdatedTime(new Date()); tableInfo.setUpdatedTime(new Date());
tableInfo.setDescription(tableVO.getDescription()); tableInfo.setDescription(tableVO.getDescription());
tableInfo.setModelTitle(tableVO.getModelTitle()); tableInfo.setModelTitle(tableVO.getModelTitle());
tableInfo.setModelType(tableVO.getModelType()); tableInfo.setModelType(tableVO.getModelType());
String xml = createTable(tableVO); String xml = createTable(tableVO);
//重新存xml //重新存xml
tableInfo.setXml(xml); tableInfo.setXml(xml);
tableInfoDao.save(tableInfo); tableInfoDao.save(tableInfo);
//原来的字段信息
Specification spec = (Specification) (root, criteriaQuery, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.equal(root.get("dbId"), dbId);
return predicate;
};
List<ColumnInfo> originalColumnInfos = columnInfoDao.findAll(spec);
//新的字段信息
List<ColumnInfo> currentColumnInfos = tableVO.getDataList().stream()
.map(columnVO -> columnInfo(tableInfo.getId(), tableInfo.getModelName(), columnVO))
.collect(Collectors.toList());
//原来的字段信息
Specification spec = (Specification) (root, criteriaQuery, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.equal(root.get("dbId"), dbId);
return predicate;
};
List<ColumnInfo> originalColumnInfos = columnInfoDao.findAll(spec);
//新的字段信息
List<ColumnInfo> currentColumnInfos = tableVO.getDataList().stream()
.map(columnVO -> columnInfo(tableInfo.getId(), tableInfo.getModelName(), columnVO))
.collect(Collectors.toList());
//根据ColumnInfo集合得出表实际改动的sql语句集合
List<String> sqls = getTableSqls(tableInfo.getModelName(), originalColumnInfos, currentColumnInfos);
//执行sql语句
for (String sql : sqls) {
System.out.println(sql);
jdbcTemplate.execute(sql);
} //根据ColumnInfo集合得出表实际改动的sql语句集合
List<String> sqls = getTableSqls(tableInfo.getModelName(), originalColumnInfos, currentColumnInfos);
//执行sql语句
for (String sql : sqls) {
System.out.println(sql);
jdbcTemplate.execute(sql);
//重新保存字段信息 }
List<ColumnInfo> columnsForAdd = getColumnsForAdd(originalColumnInfos, currentColumnInfos);
for (ColumnInfo columnInfo : columnsForAdd) { //重新保存字段信息
columnInfoDao.save(columnInfo); List<ColumnInfo> columnsForAdd = getColumnsForAdd(originalColumnInfos, currentColumnInfos);
} for (ColumnInfo columnInfo : columnsForAdd) {
List<ColumnInfo> columnsForUpdate = getColumnsForUpdate(originalColumnInfos, currentColumnInfos); columnInfoDao.save(columnInfo);
for (ColumnInfo columnInfo : columnsForUpdate) { }
columnInfoDao.save(columnInfo); List<ColumnInfo> columnsForUpdate = getColumnsForUpdate(originalColumnInfos, currentColumnInfos);
} for (ColumnInfo columnInfo : columnsForUpdate) {
List<ColumnInfo> columnsFordDelete = getColumnsFordDelete(originalColumnInfos, currentColumnInfos); columnInfoDao.save(columnInfo);
for (ColumnInfo originColumnInfo : columnsFordDelete) { }
columnInfoDao.delete(originColumnInfo); List<ColumnInfo> columnsFordDelete = getColumnsFordDelete(originalColumnInfos, currentColumnInfos);
} for (ColumnInfo originColumnInfo : columnsFordDelete) {
columnInfoDao.delete(originColumnInfo);
}
// columnInfoDao.deleteAllByDbId(updateTableInfoVO.getDbId()); // columnInfoDao.deleteAllByDbId(updateTableInfoVO.getDbId());
// //
// List<ColumnInfo> dataList = updateTableInfoVO.getTableVO().getDataList().stream() // List<ColumnInfo> dataList = updateTableInfoVO.getTableVO().getDataList().stream()
...@@ -482,7 +492,7 @@ public class ModelImpl implements ModelService { ...@@ -482,7 +492,7 @@ public class ModelImpl implements ModelService {
//// 重新存xml //// 重新存xml
// tableInfo.setXml(xml2); // tableInfo.setXml(xml2);
// tableInfoDao.save(tableInfo); // tableInfoDao.save(tableInfo);
return 1; return 1;
} }
...@@ -525,7 +535,7 @@ public class ModelImpl implements ModelService { ...@@ -525,7 +535,7 @@ public class ModelImpl implements ModelService {
} }
} }
for (ColumnInfo originColumnInfo : origin) { for (ColumnInfo originColumnInfo : origin) {
if (!originColumnInfo.getFieldName().equals("id")){ if (!originColumnInfo.getFieldName().equals("id")) {
boolean noneMatch = current.stream() boolean noneMatch = current.stream()
.noneMatch(columnInfo1 -> Objects.equals(originColumnInfo.getId(), columnInfo1.getId())); .noneMatch(columnInfo1 -> Objects.equals(originColumnInfo.getId(), columnInfo1.getId()));
if (noneMatch) { if (noneMatch) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论