bug修复

This commit is contained in:
xiaoshi 2025-06-23 18:38:33 +08:00
parent 2786d65392
commit 5bc5287ee7
1 changed files with 60 additions and 16 deletions

View File

@ -115,7 +115,7 @@ public class EquipmentCardTaskPlugin extends AbstractTask {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM"); //设置格式
String ym =sd.format(date);
DynamicObjectCollection entrys = eceq_equipment.getDynamicObjectCollection("zcgj_entryentity");
QFilter qf = new QFilter("realcard.number", QCP.in, number);
QFilter qf = new QFilter("realcard.number", QCP.equals, number);
if (entrys.size()>0){//若分录行数大于0 则取上月的
qf.and(new QFilter("billno", QCP.in, ym));
for (int i = 0; i < entrys.size(); i++) {
@ -126,32 +126,44 @@ public class EquipmentCardTaskPlugin extends AbstractTask {
}
DynamicObject[] depresplitdetails = BusinessDataServiceHelper.load("fa_depresplitdetail", "billno,period,splitdept,assentry.costcentrer,assentry.splitamount", new QFilter[]{qf});
// === 新增按期间编码年月格式排序 ===
Arrays.sort(depresplitdetails, (o1, o2) -> {
// 获取期间对象
DynamicObject p1 = o1.getDynamicObject("period");
DynamicObject p2 = o2.getDynamicObject("period");
// === 修复1正确的空值处理 ===
// 将空值放在最后无论升降序
if (p1 == null && p2 == null) return 0;
if (p1 == null) return 1;
if (p2 == null) return -1;
if (p1 == null) return 1; // o1空则排在后面
if (p2 == null) return -1; // o2空则排在后面
String c1 = p1.getString("number");
String c2 = p2.getString("number");
String code1 = p1.getString("number");
String code2 = p2.getString("number");
if (c1 == null) c1 = "";
if (c2 == null) c2 = "";
// 处理空字符串
if (code1 == null) code1 = "";
if (code2 == null) code2 = "";
// 创建年月格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
// === 修复2自动检测日期格式 ===
DateTimeFormatter formatter = detectFormat(code1, code2);
try {
YearMonth ym1 = YearMonth.parse(c1, formatter);
YearMonth ym2 = YearMonth.parse(c2, formatter);
return ym2.compareTo(ym1); // 升序
YearMonth ym1 = parseYearMonth(code1, formatter);
YearMonth ym2 = parseYearMonth(code2, formatter);
// === 修复3正确的降序比较 ===
return ym2.compareTo(ym1); // 注意这是ym2在前
} catch (DateTimeParseException e) {
// 记录解析失败的编码
log.error("期间编码解析失败: " + c1 + "" + c2, e);
return c1.compareTo(c2);
// === 修复4数值化比较作为备选 ===
try {
int num1 = Integer.parseInt(code1.replaceAll("\\D", ""));
int num2 = Integer.parseInt(code2.replaceAll("\\D", ""));
return Integer.compare(num2, num1); // 降序数值比较
} catch (NumberFormatException ex) {
// 最终回退到字符串降序
return code2.compareTo(code1);
}
}
});
for (DynamicObject depresplitdetail : depresplitdetails) {
@ -201,4 +213,36 @@ public class EquipmentCardTaskPlugin extends AbstractTask {
return false;
}
}
// 安全解析年月
private YearMonth parseYearMonth(String code, DateTimeFormatter fmt) {
if (code == null || code.isEmpty()) {
return YearMonth.of(1900, 1); // 返回最小值
}
return YearMonth.parse(code, fmt);
}
// 自动检测日期格式的辅助方法
private DateTimeFormatter detectFormat(String code1, String code2) {
// 常见格式检测
String[] patterns = {
"yyyyMM", "yyyy-MM", "yyyy/MM",
"yyyy年MM月", "MM-yyyy", "MM/yyyy"
};
for (String pattern : patterns) {
try {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
// 用两个值测试格式
if (!code1.isEmpty()) YearMonth.parse(code1, fmt);
if (!code2.isEmpty()) YearMonth.parse(code2, fmt);
return fmt; // 找到匹配格式
} catch (Exception ignored) {}
}
// 默认返回标准格式
return DateTimeFormatter.ofPattern("yyyyMM");
}
}