Commit d4994eaa by zhangxingmin

push

parent 80ee4e93
......@@ -181,38 +181,50 @@ public class ApiExpectedCommissionRatioServiceImpl implements ApiExpectedCommiss
return Result.success();
}
/**
* 校验入参-来佣比率规格明细列表(年限区间和有效时间区间均不能重叠)
* @param ratioBatchSaveDtoList
* @return
* 校验入参-来佣比率规格明细列表(年限区间和有效时间区间同时重叠时才禁止)
* 规则:
* - 分组条件:费用名称、是否受汇率影响、结算币种、对账公司、销售组织 完全一致
* - 同一分组内,年限重叠 且 有效时间重叠 → 报错
* - 年限不重叠则无论如何都通过
*/
public Result checkBatchSaveRequestPram(List<ApiExpectedCommissionRatioBatchSaveDto> ratioBatchSaveDtoList) {
// 1. 验证并准备数据(增加有效时间字段)
// 1. 准备解析后的数据
List<DtoWithParsedData> preparedDataList = prepareData(ratioBatchSaveDtoList);
// 2. 检查所有数据对的年限重叠情况(原有逻辑,保持不变)
List<OverlapError> yearErrors = checkYearOverlap(preparedDataList);
// 3. 检查所有数据对的有效时间重叠情况(新增逻辑,使用排除时间字段的业务分组)
List<OverlapError> effectiveErrors = checkEffectiveTimeOverlap(preparedDataList);
// 2. 检查同一分组内年限重叠且有效时间重叠的冲突
List<OverlapError> errors = new ArrayList<>();
int n = preparedDataList.size();
// 4. 合并错误
List<OverlapError> allErrors = new ArrayList<>();
allErrors.addAll(yearErrors);
allErrors.addAll(effectiveErrors);
for (int i = 0; i < n - 1; i++) {
DtoWithParsedData data1 = preparedDataList.get(i);
for (int j = i + 1; j < n; j++) {
DtoWithParsedData data2 = preparedDataList.get(j);
if (isSameBusinessGroup(data1.dto, data2.dto)) {
boolean yearOverlap = ProductCommonUtils.isYearRangeOverlap(data1.startYear, data1.endYear,
data2.startYear, data2.endYear);
boolean timeOverlap = isTimeRangeOverlap(data1.effectiveStart, data1.effectiveEnd,
data2.effectiveStart, data2.effectiveEnd);
if (yearOverlap && timeOverlap) {
if (!isErrorAlreadyExists(errors, data1.originalIndex, data2.originalIndex, "CONFLICT")) {
errors.add(new OverlapError(data1.originalIndex, data2.originalIndex, "CONFLICT"));
}
}
}
}
}
// 5. 如果有错误,返回错误信息
if (!allErrors.isEmpty()) {
String errorMsg = buildErrorMessage(allErrors, ratioBatchSaveDtoList);
// 3. 有冲突则抛出异常
if (!errors.isEmpty()) {
String errorMsg = buildConflictErrorMessage(errors, ratioBatchSaveDtoList);
throw new BusinessException(1004, errorMsg);
}
return Result.success();
}
/**
* 准备数据:解析年限、存储有效时间,并校验基本合法性
* @param dtoList
* @return
* 准备数据:解析年限、有效时间,并校验基本合法性
*/
private List<DtoWithParsedData> prepareData(List<ApiExpectedCommissionRatioBatchSaveDto> dtoList) {
List<DtoWithParsedData> preparedList = new ArrayList<>();
......@@ -220,17 +232,15 @@ public class ApiExpectedCommissionRatioServiceImpl implements ApiExpectedCommiss
for (int i = 0; i < dtoList.size(); i++) {
ApiExpectedCommissionRatioBatchSaveDto dto = dtoList.get(i);
// 解析年限
Integer startYear = ProductCommonUtils.parseYearToInt(dto.getStartPeriod());
Integer endYear = ProductCommonUtils.parseYearToInt(dto.getEndPeriod());
if (startYear > endYear) {
throw new BusinessException("第" + (i+1) + "条数据的起始年限[" + startYear + "]不能大于结束年限[" + endYear + "]");
throw new BusinessException("第" + (i + 1) + "条数据的起始年限[" + startYear + "]不能大于结束年限[" + endYear + "]");
}
// 校验有效时间起止顺序(新增)
if (dto.getEffectiveStart().isAfter(dto.getEffectiveEnd())) {
throw new BusinessException("第" + (i+1) + "条数据的有效开始时间不能晚于有效结束时间");
throw new BusinessException("第" + (i + 1) + "条数据的有效开始时间不能晚于有效结束时间");
}
preparedList.add(new DtoWithParsedData(i, dto, startYear, endYear,
......@@ -241,56 +251,6 @@ public class ApiExpectedCommissionRatioServiceImpl implements ApiExpectedCommiss
}
/**
* 检查年限重叠(原有逻辑,完全不变)
*/
private List<OverlapError> checkYearOverlap(List<DtoWithParsedData> preparedDataList) {
List<OverlapError> errors = new ArrayList<>();
int n = preparedDataList.size();
for (int i = 0; i < n - 1; i++) {
DtoWithParsedData data1 = preparedDataList.get(i);
for (int j = i + 1; j < n; j++) {
DtoWithParsedData data2 = preparedDataList.get(j);
// 使用原有的分组条件(包含 effectiveStart/end)
if (isSameGroupForYear(data1, data2)) {
if (ProductCommonUtils.isYearRangeOverlap(data1.startYear, data1.endYear,
data2.startYear, data2.endYear)) {
if (!isErrorAlreadyExists(errors, data1.originalIndex, data2.originalIndex, "YEAR")) {
errors.add(new OverlapError(data1.originalIndex, data2.originalIndex, "YEAR"));
}
}
}
}
}
return errors;
}
/**
* 检查有效时间重叠(新增逻辑,使用排除时间字段的业务分组)
*/
private List<OverlapError> checkEffectiveTimeOverlap(List<DtoWithParsedData> preparedDataList) {
List<OverlapError> errors = new ArrayList<>();
int n = preparedDataList.size();
for (int i = 0; i < n - 1; i++) {
DtoWithParsedData data1 = preparedDataList.get(i);
for (int j = i + 1; j < n; j++) {
DtoWithParsedData data2 = preparedDataList.get(j);
// 使用排除时间字段的分组条件(expenseName, isExchangeRate, currency, reconciliationCompany)
if (isSameGroupForEffectiveTime(data1.dto, data2.dto)) {
if (isTimeRangeOverlap(data1.effectiveStart, data1.effectiveEnd,
data2.effectiveStart, data2.effectiveEnd)) {
if (!isErrorAlreadyExists(errors, data1.originalIndex, data2.originalIndex, "EFFECTIVE")) {
errors.add(new OverlapError(data1.originalIndex, data2.originalIndex, "EFFECTIVE"));
}
}
}
}
}
return errors;
}
/**
* 判断两个有效时间区间是否重叠
*/
private boolean isTimeRangeOverlap(LocalDateTime start1, LocalDateTime end1,
......@@ -299,38 +259,19 @@ public class ApiExpectedCommissionRatioServiceImpl implements ApiExpectedCommiss
}
/**
* 原有的分组条件(包含有效开始/结束时间),用于年限重叠校验,保持不变
*/
private boolean isSameGroupForYear(DtoWithParsedData data1, DtoWithParsedData data2) {
return isSameGroupExcept(data1.dto, data2.dto);
}
/**
* 原有的 isSameGroupExcept 方法,完全不变
* 同一业务分组判断:费用名称、是否受汇率影响、结算币种、对账公司、销售组织完全一致
*/
private boolean isSameGroupExcept(ApiExpectedCommissionRatioBatchSaveDto dto1,
ApiExpectedCommissionRatioBatchSaveDto dto2) {
private boolean isSameBusinessGroup(ApiExpectedCommissionRatioBatchSaveDto dto1,
ApiExpectedCommissionRatioBatchSaveDto dto2) {
return Objects.equals(dto1.getExpenseName(), dto2.getExpenseName())
&& Objects.equals(dto1.getEffectiveStart(), dto2.getEffectiveStart())
&& Objects.equals(dto1.getEffectiveEnd(), dto2.getEffectiveEnd())
&& Objects.equals(dto1.getIsExchangeRate(), dto2.getIsExchangeRate())
&& Objects.equals(dto1.getCurrency(), dto2.getCurrency())
&& Objects.equals(dto1.getReconciliationCompany(), dto2.getReconciliationCompany());
&& Objects.equals(dto1.getReconciliationCompany(), dto2.getReconciliationCompany())
&& Objects.equals(dto1.getSalesOrg(), dto2.getSalesOrg());
}
/**
* 新增:用于有效时间重叠校验的分组条件(排除 effectiveStart/end)
*/
private boolean isSameGroupForEffectiveTime(ApiExpectedCommissionRatioBatchSaveDto dto1,
ApiExpectedCommissionRatioBatchSaveDto dto2) {
return Objects.equals(dto1.getExpenseName(), dto2.getExpenseName())
&& Objects.equals(dto1.getIsExchangeRate(), dto2.getIsExchangeRate())
&& Objects.equals(dto1.getCurrency(), dto2.getCurrency())
&& Objects.equals(dto1.getReconciliationCompany(), dto2.getReconciliationCompany());
}
/**
* 检查错误是否已存在(区分类型)
* 检查冲突是否已记录(去重)
*/
private boolean isErrorAlreadyExists(List<OverlapError> errors, int index1, int index2, String type) {
for (OverlapError error : errors) {
......@@ -344,64 +285,37 @@ public class ApiExpectedCommissionRatioServiceImpl implements ApiExpectedCommiss
}
/**
* 构建错误消息(区分年限重叠和有效时间重叠)
* 构建冲突错误信息
*/
private String buildErrorMessage(List<OverlapError> errors,
List<ApiExpectedCommissionRatioBatchSaveDto> originalList) {
private String buildConflictErrorMessage(List<OverlapError> errors,
List<ApiExpectedCommissionRatioBatchSaveDto> originalList) {
if (errors.isEmpty()) {
return "未发现重叠数据";
return "未发现冲突数据";
}
// 按重叠类型分组
Map<String, Map<Integer, List<Integer>>> typeGroupMap = new LinkedHashMap<>();
StringBuilder sb = new StringBuilder("发现年限区间重叠且有效时间区间重叠的数据:\n");
for (OverlapError error : errors) {
typeGroupMap.computeIfAbsent(error.type, k -> new TreeMap<>())
.computeIfAbsent(error.index1, k -> new ArrayList<>()).add(error.index2);
typeGroupMap.get(error.type)
.computeIfAbsent(error.index2, k -> new ArrayList<>()).add(error.index1);
}
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Map<Integer, List<Integer>>> typeEntry : typeGroupMap.entrySet()) {
String overlapType = "YEAR".equals(typeEntry.getKey()) ? "佣金年限区间" : "有效时间区间";
sb.append("发现").append(overlapType).append("重叠的数据:\n");
Map<Integer, List<Integer>> overlapMap = typeEntry.getValue();
for (Map.Entry<Integer, List<Integer>> entry : overlapMap.entrySet()) {
int dataIndex = entry.getKey();
List<Integer> overlapIndices = entry.getValue();
ApiExpectedCommissionRatioBatchSaveDto dto = originalList.get(dataIndex);
if ("YEAR".equals(typeEntry.getKey())) {
sb.append("第").append(dataIndex + 1).append("行数据年限区间 ")
.append(dto.getStartPeriod()).append("~").append(dto.getEndPeriod())
.append(" 与以下数据年限区间重叠:");
} else {
// 格式化有效时间
String startStr = dto.getEffectiveStart().format(DATE_FORMATTER);
String endStr = dto.getEffectiveEnd().format(DATE_FORMATTER);
sb.append("第").append(dataIndex + 1).append("行数据有效时间 ")
.append(startStr).append("~").append(endStr)
.append(" 与以下数据有效时间重叠:");
}
for (int overlapIndex : overlapIndices) {
sb.append(" 第").append(overlapIndex + 1).append("行");
}
sb.append("\n");
}
sb.append("\n");
int i = error.index1;
int j = error.index2;
ApiExpectedCommissionRatioBatchSaveDto dto1 = originalList.get(i);
ApiExpectedCommissionRatioBatchSaveDto dto2 = originalList.get(j);
sb.append("第").append(i + 1).append("行(年限 ").append(dto1.getStartPeriod()).append("~").append(dto1.getEndPeriod())
.append(",有效 ").append(dto1.getEffectiveStart().format(DATE_FORMATTER)).append("~").append(dto1.getEffectiveEnd().format(DATE_FORMATTER))
.append(")与第").append(j + 1).append("行(年限 ").append(dto2.getStartPeriod()).append("~").append(dto2.getEndPeriod())
.append(",有效 ").append(dto2.getEffectiveStart().format(DATE_FORMATTER)).append("~").append(dto2.getEffectiveEnd().format(DATE_FORMATTER))
.append(")冲突\n");
}
return sb.toString();
}
/**
* 内部类:包含解析后数据的DTO(增加有效时间字段)
*/
private static class DtoWithParsedData {
final int originalIndex; // 在原始列表中的索引
final ApiExpectedCommissionRatioBatchSaveDto dto; // 原始DTO
final int startYear; // 解析后的起始年份
final int endYear; // 解析后的结束年份
final LocalDateTime effectiveStart; // 有效开始时间
final LocalDateTime effectiveEnd; // 有效结束时间
// ------------------ 内部类 ------------------
public static class DtoWithParsedData {
final int originalIndex;
final ApiExpectedCommissionRatioBatchSaveDto dto;
final int startYear;
final int endYear;
final LocalDateTime effectiveStart;
final LocalDateTime effectiveEnd;
public DtoWithParsedData(int originalIndex, ApiExpectedCommissionRatioBatchSaveDto dto,
int startYear, int endYear,
......@@ -415,13 +329,10 @@ public class ApiExpectedCommissionRatioServiceImpl implements ApiExpectedCommiss
}
}
/**
* 内部类:重叠错误(增加类型字段)
*/
private static class OverlapError {
public static class OverlapError {
private final int index1;
private final int index2;
private final String type; // "YEAR" 或 "EFFECTIVE"
private final String type;
public OverlapError(int index1, int index2, String type) {
this.index1 = index1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment