refactor(controller): 重构控制器类名和路径,优化代码结构

feat(service): 新增QueryService接口实现,支持通用查询功能

docs(util): 完善ProxyUtils工具类的注释说明

fix(model): 修复CustomerCatalog实现IdentityEntity接口

style: 优化代码格式和导入顺序

perf(util): 提升FileUtils工具类功能,新增文件处理方法

chore: 更新README.md文件,补充UI资源路径说明

build: 更新pom.xml文件中的mainClass配置

test: 调整测试类命名和路径

ci: 更新CI配置文件中的类引用

refactor(controller): 重构表格单元格异步更新逻辑

docs(constant): 新增常量定义和注释

style: 统一代码风格和命名规范

refactor(service): 重构服务类继承关系

perf(controller): 优化表格数据加载性能

fix(service): 修复文件类型服务缓存问题

docs(model): 完善视图模型类注释

refactor(util): 重构文件工具类方法

style: 清理无用导入和代码

chore: 更新.gitignore文件

build: 调整项目依赖配置

refactor(controller): 重构控制器基类

perf(service): 优化查询服务性能

fix(controller): 修复表格数据加载异常

docs: 更新代码注释和文档

style: 统一代码缩进和格式
This commit is contained in:
2025-09-13 01:02:43 +08:00
parent 5edb44f619
commit ada539bebf
83 changed files with 2092 additions and 960 deletions

View File

@@ -1,26 +1,28 @@
package com.ecep.contract.service;
import java.io.File;
import org.springframework.stereotype.Service;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyCustomer;
import com.ecep.contract.model.CustomerCatalog;
import com.ecep.contract.vm.CompanyCustomerViewModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@Service
public class CompanyCustomerService extends QueryService<CompanyCustomer, CompanyCustomerViewModel> {
public CompanyCustomer findByCompany(Company company) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findByCompany'");
}
public CustomerCatalog findCatalogById(Integer id) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findCatalogById'");
Map<String, Object> params = new HashMap<>();
params.put("company", company.getId());
Page<CompanyCustomer> page = findAll(params, Pageable.ofSize(1));
if (page.isEmpty()) {
return null;
}
return page.getContent().getFirst();
}
public boolean reBuildingFiles(CompanyCustomer companyCustomer, MessageHolder holder) {

View File

@@ -1,27 +1,237 @@
package com.ecep.contract.service;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.springframework.stereotype.Service;
import com.ecep.contract.CompanyFileType;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.SpringApp;
import com.ecep.contract.constant.CloudServiceConstant;
import com.ecep.contract.constant.CompanyConstant;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyFile;
import com.ecep.contract.model.CompanyFileTypeLocal;
import com.ecep.contract.model.CompanyOldName;
import com.ecep.contract.util.FileUtils;
import com.ecep.contract.vm.CompanyFileViewModel;
import javafx.collections.ObservableList;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.File;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
public class CompanyFileService extends QueryService<CompanyFile, CompanyFileViewModel> {
public List<CompanyFile> findByCompany(Company company) {
Map<String, Object> params = new HashMap<>();
params.put("company", company.getId());
return findAll(params, Pageable.unpaged()).getContent();
}
public List<CompanyFile> findByCompanyAndPath(Company company, String absolutePath) {
Map<String, Object> params = new HashMap<>();
params.put("company", company.getId());
params.put("filePath", absolutePath);
return findAll(params, Pageable.unpaged()).getContent();
}
public boolean reBuildingFiles(Company company, MessageHolder holder) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'reBuildingFiles'");
List<CompanyFile> dbFiles = findByCompany(company);
List<CompanyFile> retrieveFiles = new ArrayList<>();
boolean modfied = false;
Map<String, CompanyFile> map = new HashMap<>();
// 排除掉数据库中重复的
for (CompanyFile dbFile : dbFiles) {
String filePath = dbFile.getFilePath();
// 没有文件信息,无效记录,删除
if (!StringUtils.hasText(filePath)) {
delete(dbFile);
modfied = true;
continue;
}
// 目录不存在,删除
File dir = new File(filePath);
if (!dir.exists()) {
delete(dbFile);
modfied = true;
continue;
}
CompanyFile old = map.put(filePath, dbFile);
// 目录有重复删除
if (old != null) {
delete(old);
modfied = true;
}
}
Map<String, File> directoryMap = new HashMap<>();
// 公司目录
if (StringUtils.hasText(company.getPath())) {
File dir = new File(company.getPath());
directoryMap.put(company.getName(), dir);
}
// 获取所有曾用名
List<CompanyOldName> oldNames = SpringApp.getBean(CompanyOldNameService.class).findAllByCompany(company);
for (CompanyOldName companyOldName : oldNames) {
String path = companyOldName.getPath();
if (StringUtils.hasText(path)) {
File dir = new File(path);
directoryMap.put(companyOldName.getName(), dir);
}
}
for (Map.Entry<String, File> entry : directoryMap.entrySet()) {
String companyName = entry.getKey();
File dir = entry.getValue();
if (!StringUtils.hasText(companyName)) {
continue;
}
if (dir == null || !dir.exists()) {
continue;
}
File[] files = dir.listFiles();
if (files == null) {
// 文件系统出错或者没有相关文件
continue;
}
for (File file : files) {
// 只处理文件
if (!file.isFile() || FileUtils.isHiddenFile(file)) {
continue;
}
String filePath = file.getAbsolutePath();
if (!map.containsKey(filePath)) {
// 未记录
CompanyFile filled = fillFileType(file, holder);
retrieveFiles.add(filled);
}
}
}
holder.info("导入 " + retrieveFiles.size() + " 个文件");
if (retrieveFiles.isEmpty()) {
return modfied;
}
// update db
retrieveFiles.forEach(v -> {
v.setCompany(company);
save(v);
});
return true;
}
/**
* 从文件名生成公司文件对象,文件已经存在公司对应的存储目录下
*
* @param file 文件
* @param holder 状态输出
* @return 公司文件对象
*/
private CompanyFile fillFileType(File file, MessageHolder holder) {
String fileName = file.getName();
CompanyFile companyFile = new CompanyFile();
companyFile.setType(CompanyFileType.General);
companyFile.setFilePath(file.getAbsolutePath());
fillApplyDateAndExpiringDateAbsent(file, companyFile);
// 天眼查 基础版企业信用报告
if (fileName.contains(CloudServiceConstant.TYC_ENTERPRISE_BASIC_REPORT)
|| fileName.contains(CloudServiceConstant.TYC_ENTERPRISE_MAJOR_REPORT)
|| fileName.contains(CloudServiceConstant.TYC_ENTERPRISE_ANALYSIS_REPORT)) {
companyFile.setType(CompanyFileType.CreditReport);
fillExpiringDateAbsent(companyFile);
return companyFile;
}
// 天眼查 企业信用信息公示报告
if (fileName.contains(CloudServiceConstant.TYC_ENTERPRISE_CREDIT_REPORT)) {
companyFile.setType(CompanyFileType.CreditInfoPublicityReport);
return companyFile;
}
// 集团相关方平台 元素征信 企业征信报告
if (fileName.contains(CloudServiceConstant.RK_VENDOR_NAME)
&& fileName.contains(CloudServiceConstant.RK_ENTERPRISE_CREDIT_REPORT)) {
companyFile.setType(CompanyFileType.CreditReport);
fillExpiringDateAbsent(companyFile);
return companyFile;
}
// 营业执照
if (fileName.contains(CompanyConstant.BUSINESS_LICENSE)) {
companyFile.setType(CompanyFileType.BusinessLicense);
return companyFile;
}
// 其他企业信用报告
if (fileName.contains(CompanyConstant.ENTERPRISE_REPORT)) {
companyFile.setType(CompanyFileType.CreditReport);
fillExpiringDateAbsent(companyFile);
return companyFile;
}
return companyFile;
}
/**
* 补齐有效期
*/
private void fillExpiringDateAbsent(CompanyFile file) {
LocalDate expiringDate = file.getExpiringDate();
if (expiringDate == null) {
LocalDate applyDate = file.getApplyDate();
if (applyDate != null) {
expiringDate = applyDate.plusYears(1);
file.setExpiringDate(expiringDate);
}
}
}
private static void fillApplyDateAndExpiringDateAbsent(File file, CompanyFile companyFile) {
LocalDate applyDate = companyFile.getApplyDate();
if (applyDate != null) {
return;
}
String fileName = file.getName();
Pattern pattern = Pattern.compile(MyDateTimeUtils.REGEX_DATE);
Matcher matcher = pattern.matcher(fileName);
while (matcher.find()) {
// 找到第一个日期,记作起始日期
String date = matcher.group();
try {
LocalDate n = LocalDate.parse(date);
companyFile.setApplyDate(n);
// 如果 截至日期未设置,则第二个日期记作截至日期(如有)
LocalDate expiringDate = companyFile.getExpiringDate();
if (expiringDate == null) {
while (matcher.find()) {
date = matcher.group();
try {
n = LocalDate.parse(date);
companyFile.setExpiringDate(n);
break;
} catch (Exception ignored) {
}
}
}
break;
} catch (Exception ignored) {
}
}
}
public void verify(Company company, LocalDate setupDate, MessageHolder holder) {
@@ -39,19 +249,205 @@ public class CompanyFileService extends QueryService<CompanyFile, CompanyFileVie
throw new UnsupportedOperationException("Unimplemented method 'copyAsMatched'");
}
public Map<? extends CompanyFileType, ? extends CompanyFileTypeLocal> findAllFileTypes(String languageTag) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findAllFileTypes'");
}
public List<CompanyFile> findByCompany(Company company) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findByCompany'");
}
public void copyAsMatchedByContract(Company parent, ObservableList<String> list) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'copyAsMatchedByContract'");
}
/**
* 移动文件到企业目录下
*
* @param company 企业对象
* @param files 要被移动的文件集合,需要从中选择需要的
* @param holder 状态输出
*/
public boolean retrieveFromDownloadFiles(Company company, File[] files, MessageHolder holder) {
Map<String, File> map = new HashMap<>();
File home = new File(company.getPath());
map.put(company.getName(), home);
List<CompanyFile> retrieveFiles = new ArrayList<>();
List<CompanyOldName> oldNames = SpringApp.getBean(CompanyOldNameService.class).findAllByCompany(company);
// 获取所有曾用名
for (CompanyOldName companyOldName : oldNames) {
String name = companyOldName.getName();
if (!StringUtils.hasText(name)) {
continue;
}
File dir = null;
String path = companyOldName.getPath();
if (StringUtils.hasText(path)) {
dir = new File(path);
}
map.put(name, dir);
}
// 对所有文件进行遍历
for (int i = 0; i < files.length; i++) {
File file = files[i];
// 只处理文件
if (!file.isFile()) {
continue;
}
MessageHolder sub = holder.sub("[" + (i + 1) + "/" + files.length + "]");
String fileName = file.getName();
sub.info(fileName);
for (Map.Entry<String, File> entry : map.entrySet()) {
String companyName = entry.getKey();
// 必须要包含公司名称否则无法区分
if (!fileName.contains(companyName)) {
continue;
}
// 文件存储的目的地目录
File dir = entry.getValue();
if (dir == null) {
dir = home;
}
CompanyFile filled = fillDownloadFileType(company, file, companyName, dir, sub);
if (filled != null) {
retrieveFiles.add(filled);
}
}
}
holder.info("导入 " + retrieveFiles.size() + " 个文件");
if (retrieveFiles.isEmpty()) {
return false;
}
// update db
retrieveFiles.forEach(v -> {
v.setCompany(company);
save(v);
});
return true;
}
/**
* 从文件名生成公司文件对象
* 文件从下载目录中导入
*
* @param company 公司对象
* @param file 导入的文件对象
* @param companyName 公司名称
* @param destDir 目标目录
* @param holder 状态输出
* @return 生成的公司文件对象如果无法转换则返回null
*/
private CompanyFile fillDownloadFileType(Company company, File file, String companyName, File destDir,
MessageHolder holder) {
String fileName = file.getName();
// 天眼查的报告
// 目前只有 基础版企业信用报告, 企业信用信息公示报告下载保存时的文件名中没有天眼查
if (CloudTycService.isTycReport(fileName)) {
CompanyFile companyFile = new CompanyFile();
companyFile.setType(CompanyFileType.CreditReport);
fillApplyDateAbsent(file, companyFile);
String destFileName = fileName;
// 重命名 基础版企业信用报告
for (String report : Arrays.asList(
CloudServiceConstant.TYC_ENTERPRISE_ANALYSIS_REPORT,
CloudServiceConstant.TYC_ENTERPRISE_BASIC_REPORT,
CloudServiceConstant.TYC_ENTERPRISE_MAJOR_REPORT)) {
if (fileName.contains(report)) {
LocalDate applyDate = companyFile.getApplyDate();
if (applyDate == null) {
applyDate = LocalDate.now();
companyFile.setApplyDate(applyDate);
}
String formatted = MyDateTimeUtils.format(applyDate);
String extension = StringUtils.getFilenameExtension(fileName);
destFileName = String.format("%s_%s_%s_%s.%s",
companyName, CloudServiceConstant.TYC_NAME, report, formatted, extension);
}
}
// 重新设置 企业分析报告 未普通文件
// if (fileName.contains(CloudTycService.TYC_ENTERPRISE_ANALYSIS_REPORT)) {
// companyFile.setType(General);
// }
File dest = new File(destDir, destFileName);
// 移动文件
if (!file.renameTo(dest)) {
// 移动失败时
holder.warn(fileName + " 无法移动到 " + dest.getAbsolutePath());
return null;
}
holder.info(fileName + " 移动到 " + dest.getAbsolutePath());
companyFile.setFilePath(dest.getAbsolutePath());
//
if (companyFile.getExpiringDate() == null) {
if (companyFile.getApplyDate() != null) {
companyFile.setExpiringDate(companyFile.getApplyDate().plusYears(1));
}
}
return companyFile;
}
// 企业信用信息公示报告
if (fileName.contains(CloudServiceConstant.TYC_ENTERPRISE_CREDIT_REPORT)) {
CompanyFile companyFile = new CompanyFile();
companyFile.setType(CompanyFileType.CreditInfoPublicityReport);
fillApplyDateAbsent(file, companyFile);
File dest = new File(destDir, fileName);
// 移动文件
if (!file.renameTo(dest)) {
if (dest.exists()) {
// 尝试删除已经存在的文件
if (!dest.delete()) {
holder.warn("覆盖时,无法删除已存在的文件 " + dest.getAbsolutePath());
return null;
}
if (file.renameTo(dest)) {
List<CompanyFile> files = findByCompanyAndPath(company, dest.getAbsolutePath());
if (!files.isEmpty()) {
companyFile = files.getFirst();
}
} else {
holder.error(fileName + " 无法覆盖到 " + dest.getAbsolutePath());
return null;
}
} else {
holder.error(fileName + " 无法移动到 " + dest.getAbsolutePath());
return null;
}
}
holder.info(fileName + " 移动到 " + dest.getAbsolutePath());
companyFile.setFilePath(dest.getAbsolutePath());
return companyFile;
}
return null;
}
/**
* 当 ApplyDate 未设置时,尝试使用文件名中包含的日期
*/
private static void fillApplyDateAbsent(File file, CompanyFile companyFile) {
LocalDate applyDate = companyFile.getApplyDate();
if (applyDate != null) {
return;
}
String fileName = file.getName();
// 从文件名中提取日期
LocalDate picked = MyDateTimeUtils.pickLocalDate(fileName);
if (picked != null) {
companyFile.setApplyDate(picked);
}
}
}

View File

@@ -1,4 +1,29 @@
package com.ecep.contract.service;
public class CompanyFileTypeService {
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.ecep.contract.CompanyFileType;
import com.ecep.contract.model.CompanyFileTypeLocal;
import com.ecep.contract.vm.CompanyFileTypeLocalViewModel;
@Service
@CacheConfig(cacheNames = "company-file-type")
public class CompanyFileTypeService extends QueryService<CompanyFileTypeLocal, CompanyFileTypeLocalViewModel> {
@Cacheable
public Map<CompanyFileType, CompanyFileTypeLocal> findAll(Locale locale) {
Map<String, Object> params = new HashMap<>();
params.put("lang", locale.toLanguageTag());
return findAll(params, Pageable.unpaged()).stream()
.collect(Collectors.toMap(CompanyFileTypeLocal::getType, Function.identity()));
}
}

View File

@@ -1,13 +1,14 @@
package com.ecep.contract.service;
import java.time.LocalDate;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyOldName;
import com.ecep.contract.vm.CompanyOldNameViewModel;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
@Service
public class CompanyOldNameService extends QueryService<CompanyOldName, CompanyOldNameViewModel> {
@@ -26,4 +27,10 @@ public class CompanyOldNameService extends QueryService<CompanyOldName, CompanyO
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findAllByCompanyAndName'");
}
public List<CompanyOldName> findAllByCompany(Company company) {
HashMap<String, Object> params = new HashMap<>();
params.put("company", company.getId());
return findAll(params, Pageable.unpaged()).getContent();
}
}

View File

@@ -1,31 +1,54 @@
package com.ecep.contract.service;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.SpringApp;
import com.ecep.contract.constant.CompanyConstant;
import com.ecep.contract.model.Company;
import com.ecep.contract.util.FileUtils;
import com.ecep.contract.vm.CompanyViewModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.File;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.model.Company;
import com.ecep.contract.vm.CompanyViewModel;
import java.util.Map;
@Service
public class CompanyService extends QueryService<Company, CompanyViewModel> {
@Autowired
private SysConfService confService;
private File basePath;
@Override
public String getBeanName() {
return "companyService";
}
public File getBasePath() {
if (basePath == null) {
basePath = new File(confService.getString(CompanyConstant.COMPANY_BASE_PATH));
}
return basePath;
}
public Company findByName(String name) {
// return companyRepository.findByName(name);
throw new UnsupportedOperationException("Unimplemented method 'findByName'");
List<Company> list = findAllByName(name);
if (list.isEmpty()) {
return null;
}
return list.getFirst();
}
public List<Company> findAllByName(String name) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findAllByName'");
Map<String, Object> params = new HashMap<>();
params.put("name", name);
return findAll(params, Pageable.unpaged()).getContent();
}
public void merge(Company company, Company updater) {
@@ -34,8 +57,10 @@ public class CompanyService extends QueryService<Company, CompanyViewModel> {
}
public Company createNewCompany(String newCompanyName) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'createNewCompany'");
Company company = new Company();
company.setName(newCompanyName);
company.setCreated(LocalDate.now());
return company;
}
public boolean existsCompanyPath(Company company) {
@@ -54,13 +79,97 @@ public class CompanyService extends QueryService<Company, CompanyViewModel> {
}
public boolean makePathAbsent(Company company) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'makePathAbsent'");
String path = company.getPath();
if (StringUtils.hasText(path)) {
File file = new File(path);
if (file.exists()) {
return false;
}
}
File dir = makePath(company);
if (dir == null) {
return false;
}
if (!dir.exists()) {
return false;
}
company.setPath(dir.getAbsolutePath());
company.setPathExist(true);
return true;
}
/**
* 创建企业目录
*
* @param company 要创建的企业对象
* @return 目录
*/
public File makePath(Company company) {
File basePath = getBasePath();
if (!basePath.exists()) {
return null;
}
String companyName = company.getName();
String district = company.getDistrict();
if (StringUtils.hasText(district)) {
String parentPrefix = FileUtils.getParentPrefixByDistrict(district);
if (parentPrefix != null) {
File parent = new File(basePath, parentPrefix);
if (!parent.exists()) {
if (!parent.mkdir()) {
return null;
}
}
String fileName = FileUtils.escapeFileName(companyName);
File dir = new File(parent, fileName);
if (!dir.exists()) {
if (!dir.mkdir()) {
return null;
}
}
return dir;
}
}
return null;
}
/**
* 移动文件到企业目录下
*
* @param company 企业对象
* @param files 要被移动的文件集合,需要从中选择需要的
* @param holder 状态输出
*/
public boolean retrieveFromDownloadFiles(Company company, File[] files, MessageHolder holder) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'retrieveFromDownloadFiles'");
//
boolean companyChanged = makePathAbsent(company);
if (!StringUtils.hasText(company.getPath())) {
// fixed 要退出,需要保存
if (companyChanged) {
save(company);
}
holder.error("存储目录未设置,请检查");
return false;
}
File home = new File(company.getPath());
if (!home.exists()) {
// fixed 要退出,需要保存
if (companyChanged) {
company = save(company);
}
holder.error(company.getPath() + " 不存在,无法访问,请检查或者修改");
return false;
}
CompanyFileService fileService = SpringApp.getBean(CompanyFileService.class);
boolean retrieved = fileService.retrieveFromDownloadFiles(company, files, holder);
if (companyChanged) {
save(company);
}
return retrieved;
}
}

View File

@@ -1,18 +1,14 @@
package com.ecep.contract.service;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.*;
import com.ecep.contract.model.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyVendor;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.VendorCatalog;
import com.ecep.contract.model.VendorTypeLocal;
import com.ecep.contract.vm.CompanyVendorViewModel;
@Service
@@ -24,8 +20,13 @@ public class CompanyVendorService extends QueryService<CompanyVendor, CompanyVen
}
public CompanyVendor findByCompany(Company company) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findByCompany'");
Map<String, Object> params = new HashMap<>();
params.put("company", company.getId());
Page<CompanyVendor> page = findAll(params, Pageable.ofSize(1));
if (page.isEmpty()) {
return null;
}
return page.getContent().getFirst();
}
public boolean reBuildingFiles(CompanyVendor companyVendor, MessageHolder messageHolder) {

View File

@@ -1,4 +1,10 @@
package com.ecep.contract.service;
public class CustomerCatalogService {
import com.ecep.contract.model.CustomerCatalog;
import com.ecep.contract.vm.CustomerCatalogViewModel;
import org.springframework.stereotype.Service;
@Service
public class CustomerCatalogService extends QueryService<CustomerCatalog, CustomerCatalogViewModel>{
}

View File

@@ -1,25 +1,26 @@
package com.ecep.contract.service;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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;
import com.ecep.contract.vm.IdentityViewModel;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel<T>>
implements ViewModelService<T, TV> {
@@ -27,7 +28,6 @@ public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel
protected WebSocketService webSocketService;
@Autowired
protected ObjectMapper objectMapper;
private long readTimeout = 30000;
@SuppressWarnings("unchecked")
public TV createNewViewModel() {
@@ -66,7 +66,7 @@ public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel
msg.setMethod("save");
msg.setArguments(entity);
try {
Object response = webSocketService.send(msg).get(readTimeout, TimeUnit.MILLISECONDS);
Object response = webSocketService.send(msg).get(webSocketService.getReadTimeout(), TimeUnit.MILLISECONDS);
if (response != null) {
objectMapper.updateValue(entity, response);
}
@@ -83,7 +83,7 @@ public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel
msg.setMethod("delete");
msg.setArguments(entity);
try {
JsonNode response = webSocketService.send(msg).get(readTimeout, TimeUnit.MILLISECONDS);
JsonNode response = webSocketService.send(msg).get(webSocketService.getReadTimeout(), TimeUnit.MILLISECONDS);
if (response != null) {
objectMapper.updateValue(entity, response);
}
@@ -97,7 +97,7 @@ public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel
msg.setService(getBeanName());
msg.setMethod("findById");
msg.setArguments(id);
return webSocketService.send(msg).orTimeout(readTimeout, TimeUnit.MILLISECONDS).handle((response, ex) -> {
return webSocketService.send(msg).orTimeout(webSocketService.getReadTimeout(), TimeUnit.MILLISECONDS).handle((response, ex) -> {
if (ex != null) {
return null;
}
@@ -133,7 +133,7 @@ public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel
msg.setService(getBeanName());
msg.setMethod("findAll");
msg.setArguments(params, PageArgument.of(pageable));
return webSocketService.send(msg).orTimeout(readTimeout, TimeUnit.MILLISECONDS).handle((response, ex) -> {
return webSocketService.send(msg).orTimeout(webSocketService.getReadTimeout(), TimeUnit.MILLISECONDS).handle((response, ex) -> {
if (ex != null) {
return null;
}
@@ -141,37 +141,12 @@ public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel
return null;
}
PageContent<T> pageContent = new PageContent<>();
try {
List<T> content = new ArrayList<>();
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);
}
}
}
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);
}
PageContent<T> pageContent = of(response, objectMapper, this::createNewEntity);
return pageContent.toPage();
} catch (Exception e) {
throw new RuntimeException(response.toString(), e);
}
return pageContent.toPage();
});
}
@@ -190,4 +165,38 @@ public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel
List<T> list = findAll(params, Pageable.ofSize(10)).getContent();
return list;
}
public static <T> PageContent<T> of(JsonNode response, ObjectMapper objectMapper, Supplier<T> createNewEntity)
throws JsonProcessingException {
PageContent<T> pageContent = new PageContent<>();
List<T> content = new ArrayList<>();
if (response.has("content")) {
JsonNode contentNode = response.get("content");
if (contentNode != null && contentNode.isArray()) {
for (JsonNode node : contentNode) {
T newEntity = createNewEntity.get();
objectMapper.updateValue(newEntity, node);
content.add(newEntity);
}
}
}
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);
}
return pageContent;
}
}

View File

@@ -1,60 +1,186 @@
package com.ecep.contract.service;
import java.util.List;
import java.util.Map;
import com.ecep.contract.PageArgument;
import com.ecep.contract.PageContent;
import com.ecep.contract.WebSocketService;
import com.ecep.contract.model.SysConf;
import com.ecep.contract.msg.SimpleMessage;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.ecep.contract.model.SysConf;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@Service
public class SysConfService {
@CacheConfig(cacheNames = "sys_conf")
public class SysConfService {
@Autowired
protected WebSocketService webSocketService;
@Autowired
protected ObjectMapper objectMapper;
public SysConf findById(String id) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findById'");
public CompletableFuture<SysConf> asyncFindById(String id) {
SimpleMessage msg = new SimpleMessage();
msg.setService(getBeanName());
msg.setMethod("findById");
msg.setArguments(id);
return webSocketService.send(msg).orTimeout(webSocketService.getReadTimeout(), TimeUnit.MILLISECONDS).handle((response, ex) -> {
if (ex != null) {
return null;
}
if (response == null) {
return null;
}
SysConf newEntity = new SysConf();
try {
objectMapper.updateValue(newEntity, response);
} catch (JsonMappingException e) {
throw new RuntimeException(response.toString(), e);
}
return newEntity;
});
}
public SysConf save(SysConf entity) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'save'");
public SysConf findById(String key) {
try {
return asyncFindById(key).get();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void delete(SysConf entity) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'delete'");
}
public List<SysConf> findAll() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findAll'");
public CompletableFuture<Page<SysConf>> asyncFindAll(Map<String, Object> params, Pageable pageable) {
SimpleMessage msg = new SimpleMessage();
msg.setService(getBeanName());
msg.setMethod("findAll");
msg.setArguments(params, PageArgument.of(pageable));
return webSocketService.send(msg).orTimeout(webSocketService.getReadTimeout(), TimeUnit.MILLISECONDS).handle((response, ex) -> {
if (ex != null) {
return null;
}
if (response == null) {
return null;
}
try {
PageContent<SysConf> pageContent = QueryService.of(response, objectMapper, SysConf::new);
return pageContent.toPage();
} catch (Exception e) {
throw new RuntimeException(response.toString(), e);
}
});
}
public Page<SysConf> findAll(Map<String, Object> params, Pageable pageable) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findAll'");
try {
return asyncFindAll(params, pageable).get();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Cacheable(key = "#p0")
public boolean getBoolean(String key) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getBoolean'");
return get(key, false);
}
public void set(String key, String string) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'set'");
public boolean get(String key, boolean defaultValue) {
SysConf conf = findById(key);
if (conf == null) {
return defaultValue;
} else {
if (Boolean.parseBoolean(conf.getValue())) {
return true;
}
return "1".equalsIgnoreCase(conf.getValue());
}
}
public String getString(String string) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getString'");
@CacheEvict(key = "#p0")
public SysConf set(String key, String value) {
SysConf conf = findById(key);
if (conf == null) {
conf = new SysConf();
conf.setId(key);
conf.setCreated(LocalDateTime.now());
}
conf.setValue(value);
conf.setModified(LocalDateTime.now());
return save(conf);
}
@Cacheable(key = "#p0")
public String getString(String key) {
return get(key, null);
}
public String get(String key, String defaultValue) {
SysConf conf = findById(key);
if (conf == null) {
return defaultValue;
} else {
return conf.getValue();
}
}
@Cacheable(key = "#p0")
public long getLong(String key) {
return get(key, 0);
}
public long get(String key, long defaultValue) {
SysConf conf = findById(key);
if (conf == null) {
return defaultValue;
} else {
return Long.parseLong(conf.getValue());
}
}
@Cacheable(key = "#p0")
public int getInt(String key) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getInt'");
return get(key, 0);
}
public int get(String key, int defaultValue) {
SysConf conf = findById(key);
if (conf == null) {
return defaultValue;
} else {
return Integer.parseInt(conf.getValue());
}
}
@CacheEvict(key = "#p0.id")
public SysConf save(SysConf entity) {
SimpleMessage msg = new SimpleMessage();
msg.setService(getBeanName());
msg.setMethod("save");
msg.setArguments(entity);
try {
Object response = webSocketService.send(msg).get(webSocketService.getReadTimeout(), TimeUnit.MILLISECONDS);
if (response != null) {
objectMapper.updateValue(entity, response);
}
} catch (Exception e) {
e.printStackTrace();
}
return entity;
}
public String getBeanName() {
return "sysConfService";
}
}