refactor(util): 重构BeanContext接口及相关实现
将ContextUtils重命名为BeanContext,并统一客户端和服务端的实现 添加DefaultBeanContext作为默认实现 优化Inventory同步任务逻辑,支持WebSocket远程调用 更新tasker_mapper.json添加新的任务映射 移除未使用的syncInventory方法
This commit is contained in:
@@ -9,7 +9,7 @@ import java.util.Locale;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.ecep.contract.util.ContextUtils;
|
||||
import com.ecep.contract.util.BeanContext;
|
||||
import com.ecep.contract.util.FxmlPath;
|
||||
import com.ecep.contract.util.FxmlUtils;
|
||||
import com.ecep.contract.vo.EmployeeVo;
|
||||
@@ -42,7 +42,7 @@ import javafx.stage.Window;
|
||||
import javafx.stage.WindowEvent;
|
||||
import lombok.Getter;
|
||||
|
||||
public class BaseController implements ContextUtils {
|
||||
public class BaseController implements BeanContext {
|
||||
private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
|
||||
public static HashMap<String, Stage> stages = new HashMap<>();
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import com.ecep.contract.controller.AbstManagerWindowController;
|
||||
import com.ecep.contract.service.InventoryService;
|
||||
import com.ecep.contract.task.InventoryAllSyncTask;
|
||||
import com.ecep.contract.task.InventorySyncTask;
|
||||
import com.ecep.contract.util.FxmlPath;
|
||||
import com.ecep.contract.util.UITools;
|
||||
@@ -79,7 +80,7 @@ public class InventoryManagerWindowController
|
||||
}
|
||||
|
||||
public void onSyncAction(ActionEvent event) {
|
||||
InventorySyncTask task = new InventorySyncTask();
|
||||
InventoryAllSyncTask task = new InventoryAllSyncTask();
|
||||
UITools.showTaskDialogAndWait("同步数据", task, null);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.text.NumberFormat;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.ecep.contract.task.InventorySyncTask;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
@@ -157,19 +158,9 @@ public class InventoryTabSkinBase
|
||||
|
||||
private void onSyncAction(ActionEvent event) {
|
||||
InventoryVo inventory = getEntity();
|
||||
setStatus("开始同步数据...");
|
||||
if (inventory == null) {
|
||||
setStatus("请选择要同步的数据.");
|
||||
return;
|
||||
}
|
||||
if (!StringUtils.hasText(inventory.getCode())) {
|
||||
setStatus("请填写商品编码.");
|
||||
return;
|
||||
}
|
||||
setStatus("正在同步数据...");
|
||||
MessageHolder holder = (lv, msg) -> setStatus(msg);
|
||||
getService().syncInventory(inventory, holder);
|
||||
|
||||
InventorySyncTask task = new InventorySyncTask();
|
||||
task.setInventory(inventory);
|
||||
UITools.showTaskDialogAndWait("同步数据", task, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -33,11 +33,6 @@ public class InventoryService extends QueryService<InventoryVo, InventoryViewMod
|
||||
return inventory;
|
||||
}
|
||||
|
||||
public void syncInventory(InventoryVo inventory, MessageHolder holder) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'syncInventory'");
|
||||
}
|
||||
|
||||
@Cacheable(key = "#p0")
|
||||
@Override
|
||||
public InventoryVo findById(Integer id) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.ecep.contract.*;
|
||||
import com.ecep.contract.controller.project.cost.ProjectCostImportItemsFromContractsTasker;
|
||||
import com.ecep.contract.model.ContractFileTypeLocal;
|
||||
import com.ecep.contract.service.*;
|
||||
import com.ecep.contract.util.ContextUtils;
|
||||
import com.ecep.contract.util.BeanContext;
|
||||
import com.ecep.contract.vo.*;
|
||||
import javafx.beans.property.SimpleBooleanProperty;
|
||||
import javafx.collections.ObservableMap;
|
||||
@@ -22,10 +22,10 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Data
|
||||
public class ContractVerifyComm implements ContextUtils {
|
||||
ContextUtils parent;
|
||||
public class ContractVerifyComm implements BeanContext {
|
||||
BeanContext parent;
|
||||
|
||||
public ContractVerifyComm(ContextUtils parent) {
|
||||
public ContractVerifyComm(BeanContext parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.ecep.contract.task;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.WebSocketClientTasker;
|
||||
|
||||
/**
|
||||
* 库存全量同步任务客户端实现
|
||||
* 用于通过WebSocket与服务器通信,执行库存数据的全量同步
|
||||
*/
|
||||
public class InventoryAllSyncTask extends Tasker<Object> implements WebSocketClientTasker {
|
||||
private static final Logger logger = LoggerFactory.getLogger(InventoryAllSyncTask.class);
|
||||
|
||||
@Override
|
||||
public String getTaskName() {
|
||||
return "InventoryAllSyncTask";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProgress(long current, long total) {
|
||||
super.updateProgress(current, total);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
// 设置任务标题
|
||||
updateTitle("全量库存同步任务");
|
||||
|
||||
// 更新任务消息
|
||||
updateMessage("开始执行全量库存同步...");
|
||||
|
||||
// 调用远程WebSocket任务
|
||||
return callRemoteTask(holder, getLocale());
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,55 @@
|
||||
package com.ecep.contract.task;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class InventorySyncTask extends Tasker<Object> {
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.WebSocketClientTasker;
|
||||
import com.ecep.contract.vo.InventoryVo;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class InventorySyncTask extends Tasker<Object> implements WebSocketClientTasker {
|
||||
private static final Logger logger = LoggerFactory.getLogger(InventorySyncTask.class);
|
||||
|
||||
@Setter
|
||||
private InventoryVo inventory;
|
||||
|
||||
@Override
|
||||
public String getTaskName() {
|
||||
return "InventorySyncTask";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProgress(long current, long total) {
|
||||
super.updateProgress(current, total);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'execute'");
|
||||
}
|
||||
try {
|
||||
// 设置任务标题
|
||||
updateTitle("库存同步任务");
|
||||
|
||||
// 更新任务消息
|
||||
holder.info("开始同步库存数据...");
|
||||
|
||||
if (inventory == null) {
|
||||
holder.warn("请选择要同步的数据.");
|
||||
return null;
|
||||
}
|
||||
if (!StringUtils.hasText(inventory.getCode())) {
|
||||
holder.warn("请填写商品编码.");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 调用远程WebSocket任务
|
||||
return callRemoteTask(holder, getLocale(), inventory.getId());
|
||||
} catch (Exception e) {
|
||||
logger.error("库存同步任务执行失败", e);
|
||||
updateMessage("库存同步任务执行失败:" + e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.ecep.contract.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.SpringApp;
|
||||
|
||||
/**
|
||||
* 默认的Bean上下文实现类
|
||||
*/
|
||||
public class DefaultBeanContext implements BeanContext {
|
||||
private Map<Class<?>, Object> cachedBeans = new HashMap<>();
|
||||
|
||||
public <T> T getBean(Class<T> requiredType) throws BeansException {
|
||||
return getCachedBean(requiredType);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getCachedBean(Class<T> requiredType) throws BeansException {
|
||||
Object object = cachedBeans.get(requiredType);
|
||||
if (object == null) {
|
||||
object = SpringApp.getBean(requiredType);
|
||||
cachedBeans.put(requiredType, object);
|
||||
}
|
||||
return (T) object;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package com.ecep.contract.util;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
public interface ContextUtils {
|
||||
public interface BeanContext {
|
||||
<T> T getBean(Class<T> requiredType) throws BeansException;
|
||||
|
||||
<T> T getCachedBean(Class<T> requiredType) throws BeansException;
|
||||
@@ -6,9 +6,7 @@ import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
@@ -17,35 +15,38 @@ import org.springframework.beans.BeansException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.ds.other.service.EmployeeService;
|
||||
import com.ecep.contract.ds.other.service.SysConfService;
|
||||
import com.ecep.contract.util.ContextUtils;
|
||||
import com.ecep.contract.util.BeanContext;
|
||||
import com.ecep.contract.util.DefaultBeanContext;
|
||||
import com.ecep.contract.util.MyStringUtils;
|
||||
import com.ecep.contract.util.NumberUtils;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class AbstractCtx implements ContextUtils {
|
||||
public class AbstractCtx implements BeanContext {
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
Locale locale = Locale.getDefault();
|
||||
private Map<Class<?>, Object> cachedBeans = new HashMap<>();
|
||||
private BeanContext beanContext;
|
||||
|
||||
public <T> T getBean(Class<T> requiredType) throws BeansException {
|
||||
return getCachedBean(requiredType);
|
||||
public AbstractCtx() {
|
||||
this(new DefaultBeanContext());
|
||||
}
|
||||
|
||||
public AbstractCtx(BeanContext ctx) {
|
||||
beanContext = ctx;
|
||||
}
|
||||
|
||||
public <T> T getBean(Class<T> requiredType) throws BeansException {
|
||||
return beanContext.getBean(requiredType);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getCachedBean(Class<T> requiredType) throws BeansException {
|
||||
Object object = cachedBeans.get(requiredType);
|
||||
if (object == null) {
|
||||
object = SpringApp.getBean(requiredType);
|
||||
cachedBeans.put(requiredType, object);
|
||||
}
|
||||
return (T) object;
|
||||
return beanContext.getCachedBean(requiredType);
|
||||
}
|
||||
|
||||
public SysConfService getConfService() {
|
||||
|
||||
@@ -5,6 +5,9 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.ecep.contract.vo.*;
|
||||
|
||||
import ch.qos.logback.core.util.ContextUtil;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -19,6 +22,7 @@ import com.ecep.contract.ds.vendor.service.VendorEntityService;
|
||||
import com.ecep.contract.ds.vendor.service.VendorService;
|
||||
import com.ecep.contract.ds.company.model.Company;
|
||||
import com.ecep.contract.model.Employee;
|
||||
import com.ecep.contract.util.BeanContext;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -28,6 +32,14 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
|
||||
@Setter
|
||||
YongYouU8Repository repository;
|
||||
|
||||
public AbstractYongYouU8Ctx() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AbstractYongYouU8Ctx(BeanContext ctx) {
|
||||
super(ctx);
|
||||
}
|
||||
|
||||
public void from(AbstractYongYouU8Ctx parent) {
|
||||
repository = parent.repository;
|
||||
}
|
||||
@@ -60,7 +72,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
|
||||
}
|
||||
|
||||
boolean updateEmployeeByCode(Supplier<Employee> getter, Consumer<Employee> setter, String code,
|
||||
MessageHolder holder, String topic) {
|
||||
MessageHolder holder, String topic) {
|
||||
if (StringUtils.hasText(code)) {
|
||||
var employee = getEmployeeService().findByCode(code);
|
||||
if (employee != null) {
|
||||
@@ -71,7 +83,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
|
||||
}
|
||||
|
||||
boolean updateEmployeeId(Supplier<Integer> getter, Consumer<Integer> setter, EmployeeVo employee,
|
||||
MessageHolder holder, String topic) {
|
||||
MessageHolder holder, String topic) {
|
||||
if (employee == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -90,7 +102,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
|
||||
}
|
||||
|
||||
boolean updateEmployeeIdByCode(Supplier<Integer> getter, Consumer<Integer> setter, String code,
|
||||
MessageHolder holder, String topic) {
|
||||
MessageHolder holder, String topic) {
|
||||
if (StringUtils.hasText(code)) {
|
||||
var employee = getEmployeeService().findByCode(code);
|
||||
if (employee != null) {
|
||||
@@ -101,7 +113,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
|
||||
}
|
||||
|
||||
boolean updateEmployee(Supplier<Employee> getter, Consumer<Employee> setter, EmployeeVo employee,
|
||||
MessageHolder holder, String topic) {
|
||||
MessageHolder holder, String topic) {
|
||||
if (employee == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -124,7 +136,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
|
||||
}
|
||||
|
||||
boolean updateEmployeeByName(Supplier<Employee> getter, Consumer<Employee> setter, String name,
|
||||
MessageHolder holder, String topic) {
|
||||
MessageHolder holder, String topic) {
|
||||
if (StringUtils.hasText(name)) {
|
||||
EmployeeVo employee = getEmployeeService().findByName(name);
|
||||
if (employee != null) {
|
||||
@@ -135,7 +147,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
|
||||
}
|
||||
|
||||
boolean updateEmployeeIdByName(Supplier<Integer> getter, Consumer<Integer> setter, String name,
|
||||
MessageHolder holder, String topic) {
|
||||
MessageHolder holder, String topic) {
|
||||
if (StringUtils.hasText(name)) {
|
||||
EmployeeVo employee = getEmployeeService().findByName(name);
|
||||
if (employee != null) {
|
||||
@@ -146,7 +158,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
|
||||
}
|
||||
|
||||
boolean updateCompanyByCustomerCode(Supplier<Company> getter, Consumer<Company> setter, String customerCode,
|
||||
MessageHolder holder, String topic) {
|
||||
MessageHolder holder, String topic) {
|
||||
Company company = null;
|
||||
if (StringUtils.hasText(customerCode)) {
|
||||
CompanyCustomerEntityService customerEntityService = getCompanyCustomerEntityService();
|
||||
@@ -181,7 +193,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
|
||||
}
|
||||
|
||||
boolean updateCompanyByVendorCode(Supplier<Company> getter, Consumer<Company> setter, String vendorCode,
|
||||
MessageHolder holder, String topic) {
|
||||
MessageHolder holder, String topic) {
|
||||
Company company = null;
|
||||
if (StringUtils.hasText(vendorCode)) {
|
||||
VendorEntityService vendorEntityService = getCompanyVendorEntityService();
|
||||
|
||||
@@ -7,41 +7,34 @@ import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.ecep.contract.vo.InventoryCatalogVo;
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.other.service.InventoryCatalogService;
|
||||
import com.ecep.contract.ds.other.service.InventoryService;
|
||||
import com.ecep.contract.model.Inventory;
|
||||
import com.ecep.contract.model.InventoryCatalog;
|
||||
import com.ecep.contract.model.Price;
|
||||
import com.ecep.contract.util.BeanContext;
|
||||
import com.ecep.contract.vo.InventoryCatalogVo;
|
||||
import com.ecep.contract.vo.InventoryVo;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class InventoryCtx extends AbstractYongYouU8Ctx {
|
||||
@Setter
|
||||
private InventoryService inventoryService;
|
||||
@Setter
|
||||
private InventoryCatalogService inventoryCatalogService;
|
||||
public InventoryCtx() {
|
||||
super();
|
||||
}
|
||||
|
||||
public InventoryCtx(BeanContext ctx) {
|
||||
super(ctx);
|
||||
}
|
||||
|
||||
public InventoryService getInventoryService() {
|
||||
if (inventoryService == null) {
|
||||
inventoryService = getBean(InventoryService.class);
|
||||
}
|
||||
return inventoryService;
|
||||
return getCachedBean(InventoryService.class);
|
||||
}
|
||||
|
||||
public InventoryCatalogService getInventoryCatalogService() {
|
||||
if (inventoryCatalogService == null) {
|
||||
inventoryCatalogService = getBean(InventoryCatalogService.class);
|
||||
}
|
||||
return inventoryCatalogService;
|
||||
return getCachedBean(InventoryCatalogService.class);
|
||||
}
|
||||
|
||||
private boolean applyInventoryDetail(Inventory item, Map<String, Object> map, MessageHolder holder) {
|
||||
@@ -127,6 +120,21 @@ public class InventoryCtx extends AbstractYongYouU8Ctx {
|
||||
return modified;
|
||||
}
|
||||
|
||||
public boolean syncInventory(Inventory inventory, MessageHolder holder) {
|
||||
Map<String, Object> map = null;
|
||||
try {
|
||||
map = repository.queryInventoryDetail(inventory.getCode());
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
holder.error("无此存货:" + inventory.getCode());
|
||||
return false;
|
||||
}
|
||||
if (applyInventoryDetail(inventory, map, holder)) {
|
||||
inventory.setUpdateDate(LocalDateTime.now());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean syncInventoryDetailByCode(Inventory inventory, String inventoryCode, MessageHolder holder) {
|
||||
if (repository == null) {
|
||||
return false;
|
||||
@@ -134,9 +142,6 @@ public class InventoryCtx extends AbstractYongYouU8Ctx {
|
||||
Map<String, Object> map = null;
|
||||
try {
|
||||
map = repository.queryInventoryDetail(inventoryCode);
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
holder.warn("未启用 " + CloudServiceConstant.U8_NAME + " 服务");
|
||||
return false;
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
holder.error("无此存货:" + inventoryCode);
|
||||
return false;
|
||||
@@ -159,7 +164,7 @@ public class InventoryCtx extends AbstractYongYouU8Ctx {
|
||||
* @return 是否更新
|
||||
*/
|
||||
private boolean updateInventoryCatalog(Supplier<InventoryCatalog> getter, Consumer<InventoryCatalog> setter,
|
||||
String catalogCode, MessageHolder holder, String topic) {
|
||||
String catalogCode, MessageHolder holder, String topic) {
|
||||
InventoryCatalogVo catalog = null;
|
||||
if (StringUtils.hasText(catalogCode)) {
|
||||
catalog = getInventoryCatalogService().findByCode(catalogCode);
|
||||
@@ -195,7 +200,7 @@ public class InventoryCtx extends AbstractYongYouU8Ctx {
|
||||
* @return 是否更新
|
||||
*/
|
||||
public boolean syncInventoryDetailByCode(Supplier<Inventory> getter, Consumer<Inventory> setter,
|
||||
String inventoryCode, MessageHolder holder, String topic) {
|
||||
String inventoryCode, MessageHolder holder, String topic) {
|
||||
if (!StringUtils.hasText(inventoryCode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.ecep.contract.ds.other.controller;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.cloud.u8.ctx.InventoryCtx;
|
||||
import com.ecep.contract.ds.other.service.InventoryService;
|
||||
import com.ecep.contract.model.Inventory;
|
||||
import com.ecep.contract.service.tasker.WebSocketServerTasker;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
/**
|
||||
* 全量同步库存
|
||||
*/
|
||||
public class InventoryAllSyncTask extends Tasker<Object> implements WebSocketServerTasker {
|
||||
private InventoryCtx inventoryCtx;
|
||||
|
||||
public InventoryService getInventoryService() {
|
||||
return getCachedBean(InventoryService.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(JsonNode argsNode) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
updateProgress(0, 1000);
|
||||
|
||||
inventoryCtx = new InventoryCtx(this);
|
||||
inventoryCtx.initializeRepository(holder);
|
||||
|
||||
InventoryService service = getInventoryService();
|
||||
Pageable pageRequest = PageRequest.ofSize(200);
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
Specification<Inventory> spec = null;
|
||||
long total = service.count(spec);
|
||||
holder.info("共需同步 " + total + " 条库存数据");
|
||||
|
||||
while (true) {
|
||||
if (isCancelled()) {
|
||||
holder.warn("库存同步任务已取消");
|
||||
break;
|
||||
}
|
||||
|
||||
Page<Inventory> page = service.findAll(spec, pageRequest);
|
||||
if (page.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (Inventory inventory : page) {
|
||||
if (isCancelled()) {
|
||||
holder.warn("库存同步任务已取消");
|
||||
break;
|
||||
}
|
||||
|
||||
String prefix = counter.get() + " / " + total + "> #" + inventory.getId() + "> ";
|
||||
MessageHolder subHolder = holder.sub(prefix);
|
||||
|
||||
syncInventory(inventory, subHolder);
|
||||
|
||||
int progress = (int) ((counter.incrementAndGet() * 1000L) / total);
|
||||
updateProgress(progress, 1000);
|
||||
}
|
||||
if (!page.hasNext()) {
|
||||
break;
|
||||
}
|
||||
pageRequest = page.nextPageable();
|
||||
}
|
||||
|
||||
updateProgress(1000, 1000);
|
||||
holder.info("库存同步任务完成!");
|
||||
return null;
|
||||
}
|
||||
|
||||
private void syncInventory(Inventory inventory, MessageHolder holder) {
|
||||
holder.info("正在同步库存数据:" + inventory.getCode() + " - " + inventory.getName());
|
||||
try {
|
||||
if (inventoryCtx.syncInventoryDetailByCode(inventory, inventory.getCode(), holder)) {
|
||||
inventoryCtx.getInventoryService().save(inventory);
|
||||
holder.info("库存数据同步成功:" + inventory.getCode());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
holder.error("库存同步失败:" + inventory.getCode() + " - " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,54 +11,47 @@ import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.cloud.u8.ctx.InventoryCtx;
|
||||
import com.ecep.contract.ds.other.service.InventoryService;
|
||||
import com.ecep.contract.model.Inventory;
|
||||
import com.ecep.contract.service.tasker.WebSocketServerTasker;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
import com.ecep.contract.vo.InventoryVo;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class InventorySyncTask extends Tasker<Object> {
|
||||
import lombok.Setter;
|
||||
|
||||
public class InventorySyncTask extends Tasker<Object> implements WebSocketServerTasker {
|
||||
private InventoryCtx inventoryCtx;
|
||||
@Setter
|
||||
private Inventory inventory;
|
||||
|
||||
public InventoryService getInventoryService() {
|
||||
return getCachedBean(InventoryService.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(JsonNode argsNode) {
|
||||
int inventoryId = argsNode.get(0).asInt();
|
||||
inventory = getInventoryService().getById(inventoryId);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
inventoryCtx = new InventoryCtx();
|
||||
if (inventory == null) {
|
||||
holder.error("库存不存在");
|
||||
return null;
|
||||
}
|
||||
updateProgress(0, 1000);
|
||||
|
||||
inventoryCtx = new InventoryCtx(this);
|
||||
inventoryCtx.initializeRepository(holder);
|
||||
|
||||
InventoryService service = inventoryCtx.getInventoryService();
|
||||
Pageable pageRequest = PageRequest.ofSize(200);
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
Specification<Inventory> spec = null;
|
||||
long total = service.count(spec);
|
||||
while (true) {
|
||||
if (isCancelled()) {
|
||||
break;
|
||||
}
|
||||
|
||||
Page<Inventory> page = service.findAll(spec, pageRequest);
|
||||
if (page.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (Inventory inventory : page) {
|
||||
if (isCancelled()) {
|
||||
break;
|
||||
}
|
||||
|
||||
String prefix = counter.get() + " / " + total + "> #" + inventory.getId() + "> ";
|
||||
MessageHolder subHolder = holder.sub(prefix);
|
||||
syncInventory(inventory, subHolder);
|
||||
|
||||
updateProgress(counter.incrementAndGet(), total);
|
||||
}
|
||||
if (!page.hasNext()) {
|
||||
break;
|
||||
}
|
||||
pageRequest = page.nextPageable();
|
||||
if (inventoryCtx.syncInventory(inventory, holder)) {
|
||||
getInventoryService().save(inventory);
|
||||
holder.info("库存数据同步成功:" + inventory.getCode());
|
||||
}
|
||||
updateProgress(1, 1);
|
||||
return super.call();
|
||||
|
||||
updateProgress(1000, 1000);
|
||||
holder.info("库存同步任务完成!");
|
||||
return null;
|
||||
}
|
||||
|
||||
private void syncInventory(Inventory inventory, MessageHolder holder) {
|
||||
if (inventoryCtx.syncInventoryDetailByCode(inventory, inventory.getCode(), holder)) {
|
||||
inventoryCtx.getInventoryService().save(inventory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,13 @@ import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.ds.other.service.EmployeeService;
|
||||
import com.ecep.contract.ds.other.service.SysConfService;
|
||||
import com.ecep.contract.model.Employee;
|
||||
import com.ecep.contract.util.BeanContext;
|
||||
import com.ecep.contract.util.DefaultBeanContext;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public abstract class Tasker<T> implements java.util.concurrent.Callable<T> {
|
||||
public abstract class Tasker<T> implements java.util.concurrent.Callable<T>, BeanContext {
|
||||
private static final Logger logger = LoggerFactory.getLogger(Tasker.class);
|
||||
@Setter
|
||||
protected java.util.function.Predicate<Message> messageHandler;
|
||||
@@ -33,13 +35,16 @@ public abstract class Tasker<T> implements java.util.concurrent.Callable<T> {
|
||||
@Getter
|
||||
@Setter
|
||||
private Locale locale = Locale.getDefault();
|
||||
private HashMap<Class<?>, Object> cachedMap = new HashMap<>();
|
||||
private BeanContext beanContext;
|
||||
@Setter
|
||||
private Employee currentUser;
|
||||
@Setter
|
||||
@Getter
|
||||
private boolean cancelled = false;
|
||||
|
||||
public Tasker() {
|
||||
beanContext = new DefaultBeanContext();
|
||||
}
|
||||
|
||||
public SysConfService getConfService() {
|
||||
return getCachedBean(SysConfService.class);
|
||||
@@ -53,18 +58,12 @@ public abstract class Tasker<T> implements java.util.concurrent.Callable<T> {
|
||||
return getCachedBean(EmployeeService.class);
|
||||
}
|
||||
|
||||
protected <K> K getBean(Class<K> requiredType) throws BeansException {
|
||||
return SpringApp.getBean(requiredType);
|
||||
public <K> K getBean(Class<K> requiredType) throws BeansException {
|
||||
return beanContext.getBean(requiredType);
|
||||
}
|
||||
|
||||
protected <K> K getCachedBean(Class<K> requiredType) {
|
||||
@SuppressWarnings("unchecked")
|
||||
K bean = (K) cachedMap.get(requiredType);
|
||||
if (bean == null) {
|
||||
bean = getBean(requiredType);
|
||||
cachedMap.put(requiredType, bean);
|
||||
}
|
||||
return bean;
|
||||
public <K> K getCachedBean(Class<K> requiredType) {
|
||||
return beanContext.getCachedBean(requiredType);
|
||||
}
|
||||
|
||||
public Employee getCurrentUser() {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.ecep.contract.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.SpringApp;
|
||||
|
||||
/**
|
||||
* 默认的Bean上下文实现类
|
||||
*/
|
||||
public class DefaultBeanContext implements BeanContext {
|
||||
private Map<Class<?>, Object> cachedBeans = new HashMap<>();
|
||||
|
||||
public <T> T getBean(Class<T> requiredType) throws BeansException {
|
||||
return getCachedBean(requiredType);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getCachedBean(Class<T> requiredType) throws BeansException {
|
||||
Object object = cachedBeans.get(requiredType);
|
||||
if (object == null) {
|
||||
object = SpringApp.getBean(requiredType);
|
||||
cachedBeans.put(requiredType, object);
|
||||
}
|
||||
return (T) object;
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,9 @@
|
||||
"CloudRkSyncTask": "com.ecep.contract.cloud.rk.CloudRkSyncTask",
|
||||
"VendorReBuildFilesTasker": "com.ecep.contract.ds.vendor.task.VendorReBuildFilesTasker",
|
||||
"CompanyVendorEvaluationFormUpdateTask": "com.ecep.contract.service.tasker.CompanyVendorEvaluationFormUpdateTask",
|
||||
"VendorNextSignDateTask": "com.ecep.contract.service.tasker.VendorNextSignDateTask"
|
||||
"VendorNextSignDateTask": "com.ecep.contract.service.tasker.VendorNextSignDateTask",
|
||||
"InventorySyncTask": "com.ecep.contract.ds.other.controller.InventorySyncTask",
|
||||
"InventoryAllSyncTask": "com.ecep.contract.ds.other.controller.InventoryAllSyncTask"
|
||||
},
|
||||
"descriptions": "任务注册信息, 客户端的任务可以通过 WebSocket 调用"
|
||||
}
|
||||
Reference in New Issue
Block a user