修复删除选中功能的类型转换问题

问题原因:
- 后端deleteBatch方法直接强制类型转换导致错误
- 前端传递的ID数组可能是Integer或Long类型
- 后端接收时类型不匹配

解决方案:
- 优化deleteBatch方法的类型转换逻辑
- 支持多种ID类型(Number、String、Object)
- 添加空列表检查和错误处理
- 新增ArrayList导入

修复内容:
- 使用List<?>接收ID列表
- 遍历列表进行安全的类型转换
- 支持Number、String等多种类型
- 添加空列表验证
This commit is contained in:
H1d3rOne committed 2026-03-08 21:55:44 +08:00
1 parent 9845f0f369
commit a0dc5d4602
1 file changed
+19 -1
@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.*;
import xuan.entity.DeductRecord;
import xuan.service.DeductRecordService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -130,7 +131,24 @@ public class RecordController {
public Map<String, Object> deleteBatch(@RequestBody Map<String, Object> params) {
Map<String, Object> result = new HashMap<>();
try {
List<Long> ids = (List<Long>) params.get("ids");
List<?> idList = (List<?>) params.get("ids");
if (idList == null || idList.isEmpty()) {
result.put("success", false);
result.put("error", "ID列表为空");
return result;
}
List<Long> ids = new ArrayList<>();
for (Object idObj : idList) {
if (idObj instanceof Number) {
ids.add(((Number) idObj).longValue());
} else if (idObj instanceof String) {
ids.add(Long.parseLong((String) idObj));
} else {
ids.add(Long.parseLong(idObj.toString()));
}
}
recordService.deleteByIds(ids);
result.put("success", true);
result.put("message", "批量删除成功");