refactor: 重构服务依赖注入和上下文管理
移除硬编码的服务注入,改为使用缓存机制动态获取Bean 优化上下文类结构,统一服务获取方式 添加PageContent类支持分页数据封装 实现异步数据加载功能
This commit is contained in:
@@ -20,6 +20,7 @@ import org.springframework.data.domain.Sort;
|
||||
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
|
||||
import com.ecep.contract.controller.table.TableTabSkin;
|
||||
import com.ecep.contract.model.IdentityEntity;
|
||||
import com.ecep.contract.service.QueryService;
|
||||
import com.ecep.contract.service.ViewModelService;
|
||||
import com.ecep.contract.util.TableViewUtils;
|
||||
import com.ecep.contract.util.UITools;
|
||||
@@ -419,17 +420,14 @@ public abstract class AbstEntityManagerSkin<T extends IdentityEntity, TV extends
|
||||
dataSet.clear();
|
||||
runAsync(() -> {
|
||||
controller.setStatus("载入中...");
|
||||
// 异步加载数据
|
||||
if (getViewModelService() instanceof QueryService<T, TV> queryService) {
|
||||
asyncLoadTableData(queryService, future);
|
||||
return;
|
||||
}
|
||||
// 同步加载方法
|
||||
List<TV> models = loadTableData();
|
||||
Platform.runLater(() -> {
|
||||
try {
|
||||
updateTableDataSet(models);
|
||||
allowResize = true; // 恢复调整
|
||||
future.complete(null);
|
||||
} catch (Exception e) {
|
||||
allowResize = true; // 恢复调整
|
||||
future.completeExceptionally(e);
|
||||
}
|
||||
});
|
||||
_updateModels(models, future);
|
||||
}).exceptionally(ex -> {
|
||||
future.completeExceptionally(ex);
|
||||
return null;
|
||||
@@ -438,6 +436,20 @@ public abstract class AbstEntityManagerSkin<T extends IdentityEntity, TV extends
|
||||
return future;
|
||||
}
|
||||
|
||||
|
||||
private void _updateModels(List<TV> models, CompletableFuture<Void> future) {
|
||||
Platform.runLater(() -> {
|
||||
try {
|
||||
updateTableDataSet(models);
|
||||
allowResize = true; // 恢复调整
|
||||
future.complete(null);
|
||||
} catch (Exception e) {
|
||||
allowResize = true; // 恢复调整
|
||||
future.completeExceptionally(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新表格数据
|
||||
*
|
||||
@@ -489,6 +501,25 @@ public abstract class AbstEntityManagerSkin<T extends IdentityEntity, TV extends
|
||||
return page.map(service::from).toList();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 异步加载表格数据
|
||||
*
|
||||
* @param queryService
|
||||
* @param future
|
||||
*/
|
||||
private void asyncLoadTableData(QueryService<T, TV> queryService, CompletableFuture<Void> future) {
|
||||
queryService.asyncFindAll(getSpecification(), getPageable()).whenComplete((result, ex) -> {
|
||||
if (ex != null) {
|
||||
future.completeExceptionally(ex);
|
||||
return;
|
||||
}
|
||||
updateFooter(result);
|
||||
List<TV> models = result.map(getViewModelService()::from).toList();
|
||||
_updateModels(models, future);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ViewModelService
|
||||
*
|
||||
|
||||
@@ -51,18 +51,6 @@ public class CloudTycManagerSkin
|
||||
return companyService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<CloudTycInfoViewModel> loadTableData() {
|
||||
String searchText = controller.searchKeyField.getText();
|
||||
Map<String, Object> spec = new HashMap<>();
|
||||
if (StringUtils.hasText(searchText)) {
|
||||
spec.put("searchText", searchText);
|
||||
}
|
||||
Page<CloudTyc> page = getCloudTycService().findAll(spec, getPageable());
|
||||
updateFooter(page);
|
||||
return page.map(CloudTycInfoViewModel::from).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeTable() {
|
||||
controller.idColumn.setCellValueFactory(param -> param.getValue().getId());
|
||||
|
||||
@@ -41,18 +41,6 @@ public class YongYouU8ManagerSkin
|
||||
return getBean(CompanyService.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<CloudYuInfoViewModel> loadTableData() {
|
||||
String searchText = controller.searchKeyField.getText();
|
||||
Map<String, Object> params = ParamUtils.builder().build();
|
||||
if (StringUtils.hasText(searchText)) {
|
||||
params.put("searchText", searchText);
|
||||
}
|
||||
Page<CloudYu> page = getU8Service().findAll(params, getPageable());
|
||||
updateFooter(page);
|
||||
return page.map(CloudYuInfoViewModel::from).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeTable() {
|
||||
controller.idColumn.setCellValueFactory(param -> param.getValue().getId());
|
||||
|
||||
@@ -64,22 +64,7 @@ public class ContractManagerSkin
|
||||
public Map<String, Object> getSpecification() {
|
||||
Map<String, Object> params = super.getSpecification();
|
||||
if (controller.composeViewBtn.isSelected()) {
|
||||
|
||||
// TODO 完善查询条件
|
||||
params.put("payWay", ContractPayWay.RECEIVE);
|
||||
|
||||
// spec = SpecificationUtils.and(spec, (root, query, builder) -> {
|
||||
// Path<String> parentCode = root.get("parentCode");
|
||||
// Path<ContractPayWay> payWay = root.get("payWay");
|
||||
// return builder.or(
|
||||
// builder.equal(payWay, ContractPayWay.RECEIVE),
|
||||
// builder.and(
|
||||
// builder.equal(payWay, ContractPayWay.PAY),
|
||||
// builder.or(
|
||||
// builder.equal(parentCode, ""),
|
||||
// parentCode.isNull())));
|
||||
// });
|
||||
|
||||
}
|
||||
ContractGroup selectedGroup = controller.groupSelector.getValue();
|
||||
if (selectedGroup != null) {
|
||||
|
||||
@@ -9,11 +9,10 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import com.ecep.contract.PageArgument;
|
||||
import com.ecep.contract.PageContent;
|
||||
import com.ecep.contract.WebSocketService;
|
||||
import com.ecep.contract.model.IdentityEntity;
|
||||
import com.ecep.contract.msg.SimpleMessage;
|
||||
@@ -103,7 +102,6 @@ public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel
|
||||
return null;
|
||||
}
|
||||
if (response == null) {
|
||||
|
||||
return null;
|
||||
}
|
||||
T newEntity = createNewEntity();
|
||||
@@ -130,38 +128,59 @@ public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel
|
||||
return findAll(null, Pageable.unpaged()).getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> findAll(Map<String, Object> params, Pageable pageable) {
|
||||
public CompletableFuture<Page<T>> asyncFindAll(Map<String, Object> params, Pageable pageable) {
|
||||
SimpleMessage msg = new SimpleMessage();
|
||||
msg.setService(getBeanName());
|
||||
msg.setMethod("findAll");
|
||||
msg.setArguments(params, PageArgument.of(pageable));
|
||||
try {
|
||||
JsonNode response = webSocketService.send(msg).get(readTimeout, TimeUnit.MILLISECONDS);
|
||||
if (response != null) {
|
||||
return webSocketService.send(msg).orTimeout(readTimeout, TimeUnit.MILLISECONDS).handle((response, ex) -> {
|
||||
if (ex != null) {
|
||||
return null;
|
||||
}
|
||||
if (response == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
PageContent<T> pageContent = new PageContent<>();
|
||||
try {
|
||||
List<T> content = new ArrayList<>();
|
||||
JsonNode contentNode = response.get("content");
|
||||
if (contentNode != null && contentNode.isArray()) {
|
||||
for (JsonNode node : contentNode) {
|
||||
T newEntity = createNewEntity();
|
||||
objectMapper.updateValue(newEntity, node);
|
||||
content.add(newEntity);
|
||||
if (response.has("content")) {
|
||||
JsonNode contentNode = response.get("content");
|
||||
if (contentNode != null && contentNode.isArray()) {
|
||||
for (JsonNode node : contentNode) {
|
||||
T newEntity = createNewEntity();
|
||||
objectMapper.updateValue(newEntity, node);
|
||||
content.add(newEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode pageNode = response.get("page");
|
||||
|
||||
int total = pageNode.get("totalElements").asInt();
|
||||
int totalPages = pageNode.get("totalPages").asInt();
|
||||
int size = pageNode.get("size").asInt();
|
||||
int number = pageNode.get("number").asInt();
|
||||
|
||||
PageRequest newPageable = PageRequest.of(number, size);
|
||||
return new PageImpl<>(content, newPageable, total);
|
||||
pageContent.setContent(content);
|
||||
if (response.has("page")) {
|
||||
JsonNode pageNode = response.get("page");
|
||||
PageArgument pageArgument = objectMapper.treeToValue(pageNode, PageArgument.class);
|
||||
pageContent.setPage(pageArgument);
|
||||
}
|
||||
if (response.has("totalElements")) {
|
||||
int totalElements = response.get("totalElements").asInt();
|
||||
pageContent.setTotalElements(totalElements);
|
||||
}
|
||||
if (response.has("totalPages")) {
|
||||
int totalPages = response.get("totalPages").asInt();
|
||||
pageContent.setTotalPages(totalPages);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(response.toString(), e);
|
||||
}
|
||||
return pageContent.toPage();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> findAll(Map<String, Object> params, Pageable pageable) {
|
||||
try {
|
||||
return asyncFindAll(params, pageable).get();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user