refactor(service): 统一Service缓存为VO对象并优化关联实体处理

重构Service类实现,将QueryService泛型参数调整为VO类型,确保缓存VO对象而非实体。优化关联实体处理逻辑,减少重复代码。修改findById方法返回VO对象,新增getById方法获取实体。更新相关调用点以适配新接口。

调整WebSocket处理、控制器及Service实现,确保数据类型一致性。完善文档记录重构过程及发现的问题。为后续优化提供基础架构支持。
This commit is contained in:
2025-09-29 19:31:51 +08:00
parent 64471b46f8
commit 49413ad473
167 changed files with 6840 additions and 1811 deletions

View File

@@ -107,6 +107,11 @@ public class CloudRkService implements IEntityService<CloudRk>, VoableService<Cl
@Autowired
private CompanyBlackReasonRepository companyBlackReasonRepository;
@Override
public CloudRk getById(Integer id) {
return cloudRKRepository.findById(id).orElse(null);
}
@Cacheable(key = "#p0")
public CloudRk findById(Integer id) {
return cloudRKRepository.findById(id).orElse(null);
@@ -384,15 +389,15 @@ public class CloudRkService implements IEntityService<CloudRk>, VoableService<Cl
if (cloudRk == null || vo == null) {
throw new IllegalArgumentException("CloudRk and CloudRkVo cannot be null");
}
// 更新基本属性
if (vo.getCloudId() != null) {
cloudRk.setCloudId(vo.getCloudId());
}
cloudRk.setAutoUpdate(vo.isAutoUpdate());
cloudRk.setUpdateDays(vo.getUpdateDays());
if (vo.getCustomerGrade() != null) {
cloudRk.setCustomerGrade(vo.getCustomerGrade());
}
@@ -432,11 +437,11 @@ public class CloudRkService implements IEntityService<CloudRk>, VoableService<Cl
if (vo.getLatestUpdate() != null) {
cloudRk.setLatestUpdate(vo.getLatestUpdate());
}
// 更新关联的公司
if (vo.getCompanyId() != null) {
CompanyService companyService = SpringApp.getBean(CompanyService.class);
Company company = companyService.findById(vo.getCompanyId());
Company company = companyService.getById(vo.getCompanyId());
if (company != null) {
cloudRk.setCompany(company);
}

View File

@@ -53,6 +53,7 @@ public class CloudRkSyncTask extends Tasker<Object> {
AtomicInteger counter = new AtomicInteger(0);
holder.info("统计需要更新的 " + total + "");
var companyService = getCompanyService();
try {
// 每次获取100条记录
while (!isCancelled()) {
@@ -73,7 +74,7 @@ public class CloudRkSyncTask extends Tasker<Object> {
break;
}
if (!Hibernate.isInitialized(company)) {
company = getCompanyService().findById(company.getId());
company = companyService.getById(company.getId());
cloudRk.setCompany(company);
}
if (cloudRk.isAutoUpdate()) {

View File

@@ -46,6 +46,11 @@ public class CloudTycService implements IEntityService<CloudTyc>, VoableService<
@Autowired
private CloudTycRepository cloudTycRepository;
@Override
public CloudTyc getById(Integer id) {
return cloudTycRepository.findById(id).orElse(null);
}
public CloudTyc getOrCreateCloudTyc(CloudInfo info) {
Optional<CloudTyc> optional = cloudTycRepository.findById(info.getId());
return optional.orElseGet(() -> getOrCreateCloudTyc(info.getCompany()));
@@ -192,7 +197,7 @@ public class CloudTycService implements IEntityService<CloudTyc>, VoableService<
// 更新关联的公司
if (vo.getCompanyId() != null) {
CompanyService companyService = SpringApp.getBean(CompanyService.class);
Company company = companyService.findById(vo.getCompanyId());
Company company = companyService.getById(vo.getCompanyId());
if (company != null) {
cloudTyc.setCompany(company);
}

View File

@@ -12,7 +12,7 @@ import com.ecep.contract.ds.other.service.EmployeeService;
import com.ecep.contract.model.ContractGroup;
import com.ecep.contract.model.ContractKind;
import com.ecep.contract.model.ContractType;
import com.ecep.contract.model.Employee;
import com.ecep.contract.vo.EmployeeVo;
import lombok.Getter;
import lombok.Setter;
@@ -82,10 +82,10 @@ public class ContractSyncContext {
consumer.accept(message);
}
}
public Employee findEmployeeByCode(String personCode) {
public EmployeeVo findEmployeeByCode(String personCode) {
return getEmployeeService().findByCode(personCode);
}
public Employee findEmployeeByName(String personName) {
public EmployeeVo findEmployeeByName(String personName) {
if (personName == null) {
return null;
}

View File

@@ -1,6 +1,5 @@
package com.ecep.contract.cloud.u8;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;
@@ -100,7 +99,7 @@ public class CustomerSyncTask extends AbstContractRepairTasker {
return;
}
if (!Hibernate.isInitialized(customer)) {
customer = getCompanyCustomerService().findById(customer.getId());
customer = getCompanyCustomerService().getById(customer.getId());
}
Company company = customer.getCompany();
if (company == null) {
@@ -124,14 +123,14 @@ public class CustomerSyncTask extends AbstContractRepairTasker {
return;
}
if (!Hibernate.isInitialized(customer)) {
customer = getCompanyCustomerService().findById(customer.getId());
customer = getCompanyCustomerService().getById(customer.getId());
}
Company company = customer.getCompany();
if (company == null) {
return;
}
if (!Hibernate.isInitialized(company)) {
company = companyService.findById(company.getId());
company = companyService.getById(company.getId());
}
boolean modified = false;
CompanyCtx companyCtx = customerCtx.getCompanyCtx();

View File

@@ -13,9 +13,11 @@ import org.springframework.util.StringUtils;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.other.service.DepartmentService;
import com.ecep.contract.ds.other.service.EmployeeService;
import com.ecep.contract.model.Department;
import com.ecep.contract.model.Employee;
import com.ecep.contract.ui.Tasker;
import com.ecep.contract.vo.EmployeeVo;
/**
* 用友U8系统-同步员工信息
@@ -71,20 +73,21 @@ public class EmployeesSyncTask extends Tasker<Object> {
java.sql.Timestamp personInValidDate = (java.sql.Timestamp) rs.get("dPInValidDate");
boolean modified = false;
Employee employee = getEmployeeService().findByCode(personCode);
EmployeeService employeeService = getEmployeeService();
EmployeeVo employee = employeeService.findByCode(personCode);
// 按员工代码未匹配时,尝试使用名字去匹配
if (employee == null) {
employee = getEmployeeService().findByName(personName);
employee = employeeService.findByName(personName);
if (employee == null) {
employee = new Employee();
employee.setCode(personCode);
employee.setName(personName);
employee.setActive(false);
employee.setCreated(LocalDate.now());
Employee newInstance = new Employee();
newInstance.setCode(personCode);
newInstance.setName(personName);
newInstance.setActive(false);
newInstance.setCreated(LocalDate.now());
holder.info("创建员工:" + personCode + ", 姓名:" + personName + ".");
// consumer.accept("员工编号:" + personCode + ", 姓名:" + personName + ", 本地未创建");
// return;
getEmployeeService().save(newInstance);
employee = newInstance.toVo();
}
employee.setCode(personCode);
modified = true;
@@ -102,8 +105,8 @@ public class EmployeesSyncTask extends Tasker<Object> {
if (departmentByCode == null) {
subHolder.warn("部门代码:" + departmentCode + "未匹配到部门");
} else {
if (!Objects.equals(employee.getDepartment(), departmentByCode)) {
employee.setDepartment(departmentByCode);
if (!Objects.equals(employee.getDepartmentId(), departmentByCode.getId())) {
employee.setDepartmentId(departmentByCode.getId());
subHolder.info("更新部门:" + departmentByCode.getName());
modified = true;
}
@@ -145,7 +148,9 @@ public class EmployeesSyncTask extends Tasker<Object> {
}
if (modified) {
getEmployeeService().save(employee);
var v1 = employeeService.getById(employee.getId());
employeeService.updateByVo(v1, employee);
employeeService.save(v1);
subHolder.info("更新保存");
} else {
subHolder.debug("无更新");

View File

@@ -143,7 +143,7 @@ public class VendorSyncTask extends AbstContractRepairTasker {
return;
}
if (!Hibernate.isInitialized(company)) {
company = companyService.findById(company.getId());
company = companyService.getById(company.getId());
}
boolean modified = false;
CompanyCtx companyCtx = vendorCtx.getCompanyCtx();

View File

@@ -8,6 +8,10 @@ import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@@ -21,6 +25,7 @@ import com.ecep.contract.SpringApp;
import com.ecep.contract.cloud.CloudInfo;
import com.ecep.contract.cloud.CloudInfoRepository;
import com.ecep.contract.cloud.u8.ctx.AbstractYongYouU8Ctx;
import com.ecep.contract.constant.ServiceConstant;
import com.ecep.contract.ds.company.service.CompanyService;
import com.ecep.contract.model.CloudYu;
import com.ecep.contract.model.Company;
@@ -30,9 +35,9 @@ import com.fasterxml.jackson.databind.JsonNode;
@Lazy
@Service
// @ConditionalOnProperty(name = "cloud.u8.enabled", havingValue = "true")
@CacheConfig(cacheNames = "cloud-yu")
public class YongYouU8Service
implements IEntityService<CloudYu>, QueryService<CloudYu>, VoableService<CloudYu, CloudYuVo> {
implements IEntityService<CloudYu>, QueryService<CloudYuVo>, VoableService<CloudYu, CloudYuVo> {
private static final Logger logger = LoggerFactory.getLogger(YongYouU8Service.class);
public static final String KEY_HOST_IP = "u8.db.server.ip";
@@ -54,7 +59,14 @@ public class YongYouU8Service
}
public CloudYu findById(Integer id) {
@Cacheable(key = "#id")
public CloudYuVo findById(Integer id) {
Optional<CloudYu> optional = cloudYuRepository.findById(id);
return optional.map(CloudYu::toVo).orElse(null);
}
@Override
public CloudYu getById(Integer id) {
return cloudYuRepository.findById(id).orElse(null);
}
@@ -106,13 +118,19 @@ public class YongYouU8Service
* @param cloudYu Cloud Yu 对象
* @return 更新的 Cloud Yu
*/
@Caching(evict = { @CacheEvict(key = "#cloudYu.id") })
@Override
public CloudYu save(CloudYu cloudYu) {
return cloudYuRepository.save(cloudYu);
}
@Caching(evict = { @CacheEvict(key = "#cloudYu.id") })
@Override
public void delete(CloudYu entity) {
cloudYuRepository.delete(entity);
public void delete(CloudYu vo) {
CloudYu entity = cloudYuRepository.findById(vo.getId()).orElse(null);
if (entity != null) {
cloudYuRepository.delete(entity);
}
}
public void deleteByCompany(Company company) {
@@ -137,9 +155,13 @@ public class YongYouU8Service
}
@Override
public Page<CloudYu> findAll(JsonNode paramsNode, Pageable pageable) {
public Page<CloudYuVo> findAll(JsonNode paramsNode, Pageable pageable) {
Specification<CloudYu> spec = null;
return findAll(spec, pageable);
if (paramsNode.has(ServiceConstant.KEY_SEARCH_TEXT)) {
String searchText = paramsNode.get(ServiceConstant.KEY_SEARCH_TEXT).asText();
spec = getSpecification(searchText);
}
return findAll(spec, pageable).map(CloudYu::toVo);
}
@Override
@@ -225,11 +247,13 @@ public class YongYouU8Service
// 更新关联的公司
if (vo.getCompanyId() != null) {
CompanyService companyService = SpringApp.getBean(CompanyService.class);
Company company = companyService.findById(vo.getCompanyId());
if (company != null) {
if (cloudYu.getCompany() == null || !cloudYu.getCompany().getId().equals(vo.getCompanyId())) {
CompanyService companyService = SpringApp.getBean(CompanyService.class);
Company company = companyService.getById(vo.getCompanyId());
cloudYu.setCompany(company);
}
} else {
cloudYu.setCompany(null);
}
}
}

View File

@@ -12,17 +12,18 @@ import com.ecep.contract.MessageHolder;
import com.ecep.contract.cloud.AbstractCtx;
import com.ecep.contract.cloud.u8.YongYouU8Repository;
import com.ecep.contract.constant.CloudServiceConstant;
import com.ecep.contract.ds.company.service.CompanyService;
import com.ecep.contract.ds.customer.service.CompanyCustomerEntityService;
import com.ecep.contract.ds.customer.service.CompanyCustomerService;
import com.ecep.contract.ds.other.service.EmployeeService;
import com.ecep.contract.ds.vendor.service.VendorEntityService;
import com.ecep.contract.ds.vendor.service.VendorService;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyCustomer;
import com.ecep.contract.model.CompanyCustomerEntity;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.Vendor;
import com.ecep.contract.model.VendorEntity;
import com.ecep.contract.model.Employee;
import com.ecep.contract.vo.EmployeeVo;
import lombok.Getter;
import lombok.Setter;
@@ -66,13 +67,23 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
boolean updateEmployeeByCode(Supplier<Employee> getter, Consumer<Employee> setter, String code,
MessageHolder holder, String topic) {
if (StringUtils.hasText(code)) {
Employee employee = getEmployeeService().findByCode(code);
var employee = getEmployeeService().findByCode(code);
if (employee != null) {
if (!Objects.equals(getter.get(), employee)) {
setter.accept(employee);
holder.info(topic + "更新为 " + employee.getName());
return true;
}
return updateEmployee(getter, setter, employee, holder, topic);
}
}
return false;
}
boolean updateEmployee(Supplier<Employee> getter, Consumer<Employee> setter, EmployeeVo employee,
MessageHolder holder, String topic) {
var service = getEmployeeService();
if (employee != null) {
var v1 = getter.get();
if (v1 == null || !Objects.equals(v1.getId(), employee.getId())) {
setter.accept(service.getById(employee.getId()));
holder.info(topic + "更新为 " + employee.getName());
return true;
}
}
return false;
@@ -81,13 +92,9 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
boolean updateEmployeeByName(Supplier<Employee> getter, Consumer<Employee> setter, String name,
MessageHolder holder, String topic) {
if (StringUtils.hasText(name)) {
Employee employee = getEmployeeService().findByName(name);
EmployeeVo employee = getEmployeeService().findByName(name);
if (employee != null) {
if (!Objects.equals(getter.get(), employee)) {
setter.accept(employee);
holder.info(topic + "更新为 " + employee.getName());
return true;
}
return updateEmployee(getter, setter, employee, holder, topic);
}
}
return false;
@@ -108,7 +115,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
holder.warn("无效" + topic + "" + customerCode);
} else {
if (!Hibernate.isInitialized(customer)) {
customer = customerService.findById(customer.getId());
customer = customerService.getById(customer.getId());
}
company = customer.getCompany();
}
@@ -122,7 +129,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
if (!Objects.equals(getter.get(), company)) {
setter.accept(company);
if (!Hibernate.isInitialized(company)) {
company = getCompanyService().findById(company.getId());
company = getCompanyService().getById(company.getId());
}
holder.info(topic + "修改为: " + company.getName());
return true;
@@ -160,7 +167,7 @@ public class AbstractYongYouU8Ctx extends AbstractCtx {
if (!Objects.equals(getter.get(), company)) {
setter.accept(company);
if (!Hibernate.isInitialized(company)) {
company = getCompanyService().findById(company.getId());
company = getCompanyService().getById(company.getId());
}
holder.info(topic + "修改为: " + company.getName());
return true;

View File

@@ -35,7 +35,6 @@ public class CompanyCtx extends AbstractYongYouU8Ctx implements CompanyContext {
}
}
public boolean updateCompanyAbbNameIfAbsent(Company company, String abbName, MessageHolder holder) {
if (!StringUtils.hasText(abbName)) {
return false;
@@ -69,7 +68,8 @@ public class CompanyCtx extends AbstractYongYouU8Ctx implements CompanyContext {
return false;
}
public Company findOrCreateByNameOrAbbName(String name, String abbName, LocalDate developDate, MessageHolder holder) {
public Company findOrCreateByNameOrAbbName(String name, String abbName, LocalDate developDate,
MessageHolder holder) {
Company company = getCompanyService().findAndRemoveDuplicateCompanyByNameOrAbbName(name, abbName);
if (company != null) {
return company;
@@ -79,12 +79,12 @@ public class CompanyCtx extends AbstractYongYouU8Ctx implements CompanyContext {
holder.warn("企业库中找不到" + name + "" + abbName + " 的企业");
// 推测个人用户,归入散户
if (name.length() < 4 && !name.contains("公司")) {
company = getCompanyService().findByName("散户");
if (company == null) {
var v1 = getCompanyService().findByName("散户");
if (v1 == null) {
return null;
}
holder.info(name + " 个人用户归集到散户");
return company;
return getCompanyService().getById(v1.getId());
}
// 尝试创建公司

View File

@@ -17,6 +17,8 @@ import com.ecep.contract.model.*;
import com.ecep.contract.util.FileUtils;
import com.ecep.contract.util.NumberUtils;
import com.ecep.contract.util.TaxRateUtils;
import com.ecep.contract.vo.ContractFileTypeLocalVo;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.Hibernate;
@@ -272,7 +274,7 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
if (contract.getType() != null && contract.getPayWay() != null) {
ContractType type = contract.getType();
if (!Hibernate.isInitialized(type)) {
type = getCachedBean(ContractTypeService.class).findById(type.getId());
type = getCachedBean(ContractTypeService.class).getById(type.getId());
contract.setType(type);
}
if (!type.getDirection().equals(contract.getPayWay().getText())) {
@@ -283,12 +285,12 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
if (contract.getKind() != null && contract.getType() != null) {
ContractKind kind = contract.getKind();
if (!Hibernate.isInitialized(kind)) {
kind = getCachedBean(ContractKindService.class).findById(kind.getId());
kind = getCachedBean(ContractKindService.class).getById(kind.getId());
contract.setKind(kind);
}
ContractType type = contract.getType();
if (!Hibernate.isInitialized(type)) {
type = getCachedBean(ContractTypeService.class).findById(type.getId());
type = getCachedBean(ContractTypeService.class).getById(type.getId());
contract.setType(type);
}
if (!kind.getName().equals(type.getTitle())) {
@@ -375,7 +377,7 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
return false;
}
if (!Hibernate.isInitialized(company)) {
company = getCompanyService().findById(company.getId());
company = getCompanyService().getById(company.getId());
}
contract.setCompany(company);
holder.info("关联至 " + company.getName());
@@ -440,7 +442,7 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
}
} else {
if (!Hibernate.isInitialized(customer)) {
customer = customerService.findById(customer.getId());
customer = customerService.getById(customer.getId());
}
if (customer.getCompany() == null) {
customer.setCompany(company);
@@ -536,7 +538,7 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
holder.warn("* 合同业务员:" + personId + " 未导入");
} else {
if (!Hibernate.isInitialized(employee)) {
employee = getEmployeeService().findById(employee.getId());
employee = getEmployeeService().getById(employee.getId());
}
if (!employee.isActive()) {
holder.warn("业务员 " + employee.getName() + " 已经不可用,其离职日期:" + employee.getLeaveDate());
@@ -783,7 +785,7 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
String memo = (String) map.get("strMemo");
if (!Hibernate.isInitialized(contract)) {
contract = getContractService().findById(contract.getId());
contract = getContractService().getById(contract.getId());
item.setContract(contract);
}
boolean modified = false;
@@ -943,7 +945,7 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
ProjectSaleType saleType = project.getSaleType();
if (saleType != null) {
if (!Hibernate.isInitialized(saleType)) {
saleType = getSaleTypeService().findById(saleType.getId());
saleType = getSaleTypeService().getById(saleType.getId());
}
File dir = new File(saleType.getPath());
if (saleType.isStoreByYear()) {
@@ -1170,7 +1172,7 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
MyDateTimeUtils.pickLocalDate(file.getName()), holder, "生效日期");
}
List<ContractFileTypeLocal> matched = getCachedBean(ContractFileTypeService.class)
List<ContractFileTypeLocalVo> matched = getCachedBean(ContractFileTypeService.class)
.findAll(getLocale()).values().stream()
.filter(local -> {
ContractFileType type = local.getType();
@@ -1209,7 +1211,7 @@ public class ContractCtx extends AbstractYongYouU8Ctx {
contractFile.setType(matched.stream().max((o1, o2) -> maxLength(o1) - maxLength(o2)).get().getType());
}
private int maxLength(ContractFileTypeLocal o1) {
private int maxLength(ContractFileTypeLocalVo o1) {
String suggestFileName = o1.getSuggestFileName();
String value = o1.getValue();
if (!StringUtils.hasText(suggestFileName)) {

View File

@@ -156,7 +156,7 @@ public class CustomerCtx extends AbstractYongYouU8Ctx {
}
if (customer != null) {
if (!Hibernate.isInitialized(customer)) {
customer = getCompanyCustomerService().findById(customer.getId());
customer = getCompanyCustomerService().getById(customer.getId());
}
Company company = customer.getCompany();
if (company != null) {

View File

@@ -257,7 +257,7 @@ public class PurchaseOrderCtx extends AbstractYongYouU8Ctx {
return;
}
if (!Hibernate.isInitialized(contract)) {
contract = getContractService().findById(contract.getId());
contract = getContractService().getById(contract.getId());
}
getCompanyBankAccountCtx().updateBankAccount(contract.getCompany(), bank, bankAccount, holder);
}

View File

@@ -187,7 +187,7 @@ public class SalesBillVoucherCtx extends AbstractYongYouU8Ctx {
Contract contract = order.getContract();
if (!Hibernate.isInitialized(contract)) {
contract = getContractService().findById(contract.getId());
contract = getContractService().getById(contract.getId());
}
voucher.setCompany(contract.getCompany());
voucher.setRefId(sbvid);

View File

@@ -248,7 +248,7 @@ public class VendorCtx extends AbstractYongYouU8Ctx {
return false;
}
if (!Hibernate.isInitialized(company)) {
company = companyService.findById(company.getId());
company = companyService.getById(company.getId());
}
boolean modified = false;
CompanyCtx companyCtx = getCompanyCtx();