refactor(modules): 拆分多模块工程并收口common基础模块
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package com.bruce.common.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.bruce.common.domain.entity.SysAttachment;
|
||||
import com.bruce.common.dto.request.SysAttachmentUploadRequest;
|
||||
|
||||
public interface ISysAttachmentService extends IService<SysAttachment> {
|
||||
|
||||
SysAttachment upload(SysAttachmentUploadRequest request);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.bruce.common.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.bruce.common.domain.entity.SysEnum;
|
||||
import com.bruce.common.dto.request.SysEnumBatchSaveRequest;
|
||||
import com.bruce.common.dto.request.SysEnumManageQueryRequest;
|
||||
import com.bruce.common.dto.request.SysEnumQueryRequest;
|
||||
import com.bruce.common.dto.request.SysEnumSaveRequest;
|
||||
import com.bruce.common.dto.response.SysEnumResponse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ISysEnumService extends IService<SysEnum> {
|
||||
|
||||
List<SysEnum> listByCatalogAndType(SysEnumQueryRequest request);
|
||||
|
||||
List<SysEnumResponse> listResponses();
|
||||
|
||||
List<SysEnumResponse> listByCatalogAndTypeResponses(SysEnumQueryRequest request);
|
||||
|
||||
List<SysEnumResponse> listForManagement(SysEnumManageQueryRequest request);
|
||||
|
||||
SysEnumResponse getResponseById(Long id);
|
||||
|
||||
boolean saveOrUpdate(SysEnumSaveRequest request);
|
||||
|
||||
boolean batchSave(SysEnumBatchSaveRequest request);
|
||||
|
||||
boolean replaceByCatalogAndType(String catalog, String type, List<SysEnum> items);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.bruce.common.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.bruce.common.config.AttachmentProperties;
|
||||
import com.bruce.common.constant.CommonConsts;
|
||||
import com.bruce.common.domain.entity.SysAttachment;
|
||||
import com.bruce.common.dto.request.SysAttachmentUploadRequest;
|
||||
import com.bruce.common.mapper.SysAttachmentMapper;
|
||||
import com.bruce.common.service.ISysAttachmentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class SysAttachmentServiceImpl extends ServiceImpl<SysAttachmentMapper, SysAttachment> implements ISysAttachmentService {
|
||||
|
||||
@Autowired
|
||||
private AttachmentProperties attachmentProperties;
|
||||
|
||||
@Override
|
||||
public SysAttachment upload(SysAttachmentUploadRequest request) {
|
||||
// 这里先完成上传校验和本地落盘,后续如接入对象存储也只需替换该流程内部实现。
|
||||
if (request == null) {
|
||||
throw new IllegalArgumentException("上传请求不能为空");
|
||||
}
|
||||
MultipartFile file = request.getFile();
|
||||
if (file == null || file.isEmpty()) {
|
||||
throw new IllegalArgumentException("上传文件不能为空");
|
||||
}
|
||||
String sourceType = request.getSourceType();
|
||||
if (!StringUtils.hasText(sourceType)) {
|
||||
throw new IllegalArgumentException("sourceType不能为空");
|
||||
}
|
||||
|
||||
String originalName = file.getOriginalFilename();
|
||||
String suffix = StringUtils.getFilenameExtension(originalName);
|
||||
String storedFileName = UUID.randomUUID() + (StringUtils.hasText(suffix) ? "." + suffix : "");
|
||||
String dateDirectory = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
|
||||
Path rootPath = Paths.get(attachmentProperties.getBasePath()).toAbsolutePath().normalize();
|
||||
Path targetDirectory = rootPath.resolve(dateDirectory);
|
||||
Path targetPath = targetDirectory.resolve(storedFileName);
|
||||
|
||||
try {
|
||||
Files.createDirectories(targetDirectory);
|
||||
file.transferTo(targetPath);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("文件上传失败", e);
|
||||
}
|
||||
|
||||
SysAttachment attachment = new SysAttachment();
|
||||
attachment.setSourceType(sourceType);
|
||||
attachment.setSourceId(request.getSourceId());
|
||||
attachment.setOriginalName(originalName);
|
||||
attachment.setFileName(storedFileName);
|
||||
attachment.setFileSuffix(suffix);
|
||||
attachment.setContentType(file.getContentType());
|
||||
attachment.setFileSize(file.getSize());
|
||||
attachment.setStorageType(CommonConsts.STORAGE_TYPE_LOCAL);
|
||||
attachment.setFilePath(dateDirectory + "/" + storedFileName);
|
||||
attachment.setFileUrl(null);
|
||||
attachment.setVersion(1);
|
||||
save(attachment);
|
||||
return attachment;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
package com.bruce.common.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.bruce.common.domain.entity.SysEnum;
|
||||
import com.bruce.common.dto.request.SysEnumBatchSaveRequest;
|
||||
import com.bruce.common.dto.request.SysEnumManageQueryRequest;
|
||||
import com.bruce.common.dto.request.SysEnumQueryRequest;
|
||||
import com.bruce.common.dto.request.SysEnumSaveRequest;
|
||||
import com.bruce.common.dto.response.SysEnumResponse;
|
||||
import com.bruce.common.factory.SysEnumFactory;
|
||||
import com.bruce.common.mapper.SysEnumMapper;
|
||||
import com.bruce.common.service.ISysEnumService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysEnumServiceImpl extends ServiceImpl<SysEnumMapper, SysEnum> implements ISysEnumService {
|
||||
|
||||
private final SysEnumFactory sysEnumFactory;
|
||||
|
||||
@Override
|
||||
public List<SysEnum> listByCatalogAndType(SysEnumQueryRequest request) {
|
||||
log.info("SysEnumServiceImpl.listByCatalogAndType start, request={}", request);
|
||||
if (request == null) {
|
||||
throw new IllegalArgumentException("查询请求不能为空");
|
||||
}
|
||||
List<SysEnum> result = lambdaQuery()
|
||||
.eq(StringUtils.hasText(request.getCatalog()), SysEnum::getCatalog, request.getCatalog())
|
||||
.eq(StringUtils.hasText(request.getType()), SysEnum::getType, request.getType())
|
||||
.orderByAsc(SysEnum::getSort)
|
||||
.list();
|
||||
log.info("SysEnumServiceImpl.listByCatalogAndType success, count={}", result.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysEnumResponse> listResponses() {
|
||||
log.info("查询系统枚举列表开始");
|
||||
List<SysEnumResponse> responses = sysEnumFactory.toResponses(list());
|
||||
log.info("查询系统枚举列表结束,count={}", responses.size());
|
||||
return responses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysEnumResponse> listByCatalogAndTypeResponses(SysEnumQueryRequest request) {
|
||||
log.info("按模块和类型查询系统枚举开始,catalog={}, type={}",
|
||||
request == null ? null : request.getCatalog(),
|
||||
request == null ? null : request.getType());
|
||||
List<SysEnumResponse> responses = sysEnumFactory.toResponses(listByCatalogAndType(request));
|
||||
log.info("按模块和类型查询系统枚举结束,count={}", responses.size());
|
||||
return responses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysEnumResponse> listForManagement(SysEnumManageQueryRequest request) {
|
||||
log.info("SysEnumServiceImpl.listForManagement start, request={}", request);
|
||||
SysEnumManageQueryRequest queryRequest = request == null ? new SysEnumManageQueryRequest() : request;
|
||||
String keyword = queryRequest.getKeyword();
|
||||
List<SysEnumResponse> responses = sysEnumFactory.toResponses(lambdaQuery()
|
||||
.eq(StringUtils.hasText(queryRequest.getCatalog()), SysEnum::getCatalog, queryRequest.getCatalog())
|
||||
.eq(StringUtils.hasText(queryRequest.getType()), SysEnum::getType, queryRequest.getType())
|
||||
.and(StringUtils.hasText(keyword), wrapper -> wrapper
|
||||
.like(SysEnum::getCatalog, keyword)
|
||||
.or()
|
||||
.like(SysEnum::getType, keyword)
|
||||
.or()
|
||||
.like(SysEnum::getName, keyword)
|
||||
.or()
|
||||
.like(SysEnum::getStrvalue, keyword)
|
||||
.or()
|
||||
.like(SysEnum::getRemark, keyword))
|
||||
.orderByAsc(SysEnum::getCatalog)
|
||||
.orderByAsc(SysEnum::getType)
|
||||
.orderByAsc(SysEnum::getSort)
|
||||
.orderByAsc(SysEnum::getId)
|
||||
.list());
|
||||
log.info("SysEnumServiceImpl.listForManagement success, count={}", responses.size());
|
||||
return responses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysEnumResponse getResponseById(Long id) {
|
||||
log.info("查询系统枚举详情开始,id={}", id);
|
||||
SysEnumResponse response = sysEnumFactory.toResponse(getById(id));
|
||||
log.info("查询系统枚举详情结束,id={}, found={}", id, response != null);
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveOrUpdate(SysEnumSaveRequest request) {
|
||||
log.info("保存系统枚举开始,id={}, catalog={}, type={}, value={}",
|
||||
request == null ? null : request.getId(),
|
||||
request == null ? null : request.getCatalog(),
|
||||
request == null ? null : request.getType(),
|
||||
request == null ? null : request.getValue());
|
||||
if (request == null) {
|
||||
throw new IllegalArgumentException("保存请求不能为空");
|
||||
}
|
||||
|
||||
SysEnum sysEnum = sysEnumFactory.toEntity(request);
|
||||
boolean result = super.saveOrUpdate(sysEnum);
|
||||
log.info("保存系统枚举结束,id={}, catalog={}, type={}, value={}, result={}",
|
||||
request.getId(), request.getCatalog(), request.getType(), request.getValue(), result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean batchSave(SysEnumBatchSaveRequest request) {
|
||||
log.info("批量保存系统枚举开始,catalog={}, type={}",
|
||||
request == null ? null : request.getCatalog(),
|
||||
request == null ? null : request.getType());
|
||||
List<SysEnum> existingEnums = lambdaQuery()
|
||||
.eq(request != null && StringUtils.hasText(request.getCatalog()), SysEnum::getCatalog, request == null ? null : request.getCatalog())
|
||||
.eq(request != null && StringUtils.hasText(request.getType()), SysEnum::getType, request == null ? null : request.getType())
|
||||
.list();
|
||||
validateBatchSaveRequest(request, existingEnums);
|
||||
|
||||
List<SysEnum> enums = request.getItems().stream()
|
||||
.map(item -> sysEnumFactory.toEntity(request.getCatalog(), request.getType(), item))
|
||||
.toList();
|
||||
boolean result = saveBatch(enums);
|
||||
log.info("批量保存系统枚举结束,catalog={}, type={}, itemCount={}, result={}",
|
||||
request.getCatalog(), request.getType(), enums.size(), result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean replaceByCatalogAndType(String catalog, String type, List<SysEnum> items) {
|
||||
log.info("SysEnumServiceImpl.replaceByCatalogAndType start, catalog={}, type={}, itemCount={}",
|
||||
catalog, type, items == null ? 0 : items.size());
|
||||
validateReplaceByCatalogAndTypeRequest(catalog, type, items);
|
||||
|
||||
boolean removed = lambdaUpdate()
|
||||
.eq(SysEnum::getCatalog, catalog)
|
||||
.eq(SysEnum::getType, type)
|
||||
.remove();
|
||||
boolean saved = saveBatch(items);
|
||||
boolean result = removed || saved;
|
||||
log.info("SysEnumServiceImpl.replaceByCatalogAndType success, catalog={}, type={}, removed={}, saved={}, result={}",
|
||||
catalog, type, removed, saved, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void validateBatchSaveRequest(SysEnumBatchSaveRequest request, List<SysEnum> existingEnums) {
|
||||
log.info("SysEnumServiceImpl.validateBatchSaveRequest start");
|
||||
if (request == null) {
|
||||
throw new IllegalArgumentException("批量保存请求不能为空");
|
||||
}
|
||||
if (!StringUtils.hasText(request.getCatalog())) {
|
||||
throw new IllegalArgumentException("模块目录不能为空");
|
||||
}
|
||||
if (!StringUtils.hasText(request.getType())) {
|
||||
throw new IllegalArgumentException("枚举类型不能为空");
|
||||
}
|
||||
if (request.getItems() == null || request.getItems().isEmpty()) {
|
||||
throw new IllegalArgumentException("枚举项不能为空");
|
||||
}
|
||||
|
||||
Set<Integer> requestValues = new HashSet<>();
|
||||
for (SysEnumBatchSaveRequest.Item item : request.getItems()) {
|
||||
if (item == null) {
|
||||
throw new IllegalArgumentException("枚举项不能为空");
|
||||
}
|
||||
if (!StringUtils.hasText(item.getName())) {
|
||||
throw new IllegalArgumentException("枚举名称不能为空");
|
||||
}
|
||||
if (item.getValue() == null) {
|
||||
throw new IllegalArgumentException("枚举整型值不能为空");
|
||||
}
|
||||
if (!requestValues.add(item.getValue())) {
|
||||
throw new IllegalArgumentException("批量保存请求中存在重复枚举值: " + item.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
Set<Integer> existingValues = new HashSet<>();
|
||||
if (existingEnums != null) {
|
||||
existingEnums.stream()
|
||||
.map(SysEnum::getValue)
|
||||
.forEach(existingValues::add);
|
||||
}
|
||||
for (Integer value : requestValues) {
|
||||
if (existingValues.contains(value)) {
|
||||
throw new IllegalArgumentException("枚举值已存在: " + value);
|
||||
}
|
||||
}
|
||||
log.info("SysEnumServiceImpl.validateBatchSaveRequest success, catalog={}, type={}, requestValueCount={}, existingValueCount={}",
|
||||
request.getCatalog(), request.getType(), requestValues.size(), existingValues.size());
|
||||
}
|
||||
|
||||
private void validateReplaceByCatalogAndTypeRequest(String catalog, String type, List<SysEnum> items) {
|
||||
if (!StringUtils.hasText(catalog)) {
|
||||
throw new IllegalArgumentException("模块目录不能为空");
|
||||
}
|
||||
if (!StringUtils.hasText(type)) {
|
||||
throw new IllegalArgumentException("枚举类型不能为空");
|
||||
}
|
||||
if (items == null || items.isEmpty()) {
|
||||
throw new IllegalArgumentException("枚举项不能为空");
|
||||
}
|
||||
|
||||
Set<Integer> values = new HashSet<>();
|
||||
Set<Integer> sorts = new HashSet<>();
|
||||
for (SysEnum item : items) {
|
||||
if (item == null) {
|
||||
throw new IllegalArgumentException("枚举项不能为空");
|
||||
}
|
||||
if (!catalog.equals(item.getCatalog()) || !type.equals(item.getType())) {
|
||||
throw new IllegalArgumentException("替换的枚举项 catalog/type 必须与目标分组一致");
|
||||
}
|
||||
if (!StringUtils.hasText(item.getName())) {
|
||||
throw new IllegalArgumentException("枚举名称不能为空");
|
||||
}
|
||||
if (item.getValue() == null) {
|
||||
throw new IllegalArgumentException("枚举整型值不能为空");
|
||||
}
|
||||
if (!values.add(item.getValue())) {
|
||||
throw new IllegalArgumentException("枚举值重复: " + item.getValue());
|
||||
}
|
||||
if (item.getSort() == null) {
|
||||
throw new IllegalArgumentException("枚举排序不能为空");
|
||||
}
|
||||
if (!sorts.add(item.getSort())) {
|
||||
throw new IllegalArgumentException("枚举排序重复: " + item.getSort());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user