楼盘历史价格管理导入功能优化
备注:红色为重点关注的内容,绿色表示修改的内容
1.原始代码
ToolHistoricalPriceControl.java
@RequestMapping("fileImport")
public String fileImport(@RequestParam("file") MultipartFile file, Model model) {
Map<String, Object> results = toolHistoricalPriceService.fileImport(file);
model.addAllAttributes(results);
return "tool/tool_add_fileImport";
}
ToolHistoricalPriceServiceImpl.java
@Transactional
@Override
@SuppressWarnings("unchecked")
public Map<String, Object> fileImport(MultipartFile file) {
List<String> msgs = new ArrayList<>();
Map<String, Object> results = new HashMap<>();
String name = null;
try {
name = FileUploadUtil.uploadFileReturnName(file, tempFilePath);
} catch (IOException e) {
e.printStackTrace();
}
if (name == null) {
msgs.add("文件上传失败");
results.put("msgs", msgs);
return results;
}
ExcelReader excelReader = new ExcelReader();
Map<String, Object> fileContent = excelReader.readExcelContent(name);
// 获取标题(楼盘名称)
Map<String, Integer> titleMap = (Map<String, Integer>) fileContent.get("title");
List<Map<Integer, String>> datas = (List<Map<Integer, String>>) fileContent.get("data");
// 用于计算导入数据的条数
int counts = 0;
// 用于判断获取标题栏
int count = 0;
// 创建线程池
// ExecutorService exs = Executors.newFixedThreadPool(5);
// List<SaveTask> tasks = new ArrayList<>();
// List<Future<ErrorObject>> futures = new ArrayList<>();
UserObject user = UserObjectHelper.currentUserObject();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
long datastart = System.currentTimeMillis();
List<ToolHistoricalPrice> prices = new ArrayList<>();
for (Map<Integer, String> data : datas) {
List<HbDictionary> list = dictionaryService.queryByKeyword(data.get(titleMap.get("楼盘名")));
if (list.isEmpty()) {
// throw new ErpErrorException(data.get(titleMap.get("楼盘名")).toString()
// + "不存在,无法导入");
msgs.add("楼盘[" + data.get(titleMap.get("楼盘名")) + "]不存在");
continue;
}
HbArea hbarea = hbAreaService.findParentAreaName(list.get(0).getCityName());
ToolHistoricalPrice toolHistoricalPrice = new ToolHistoricalPrice();
if (hbarea != null) {
toolHistoricalPrice.setCity(hbarea.getAreaName());
} else {
toolHistoricalPrice.setCity("");
}
// 设置楼盘名称
toolHistoricalPrice.setBuildingId(list.get(0).getBillNo());
if (StringUtils.isBlank(data.get(titleMap.get("楼盘名")))) {
// throw new ErpErrorException(data.get(titleMap.get("楼盘名")).toString()
// + "不存在,无法导入");
msgs.add("楼盘名为空!");
continue;
}
toolHistoricalPrice.setBuildingName(data.get(titleMap.get("楼盘名")));
toolHistoricalPrice.setArea(list.get(0).getAreaName());
toolHistoricalPrice.setHouseType(
data.get(titleMap.get("室(个)")) + "房" + StringUtils.trimToEmpty(data.get(titleMap.get("厅数(个)"))));
toolHistoricalPrice.setDeptId(user.getDeptId());
toolHistoricalPrice.setDeptName(user.getDeptName());
toolHistoricalPrice.setUserName(user.getName());
toolHistoricalPrice.setPrice(
new BigDecimal(StringUtils.isBlank(data.get(titleMap.get("租金(元)"))) ? "0" : data.get(titleMap.get("租金(元)"))));
toolHistoricalPrice.setPriceSource("链家");
toolHistoricalPrice.setProportion(new BigDecimal(0));
if (StringUtils.isNotBlank(list.get(0).getCityName())) {
toolHistoricalPrice.setRegion(list.get(0).getCityName());
} else {
toolHistoricalPrice.setRegion("");
}
try {
toolHistoricalPrice.setCheckDate(StringUtils.trimToEmpty(data.get(titleMap.get("日期"))).toString() == null
|| StringUtils.trimToEmpty(data.get(titleMap.get("日期"))).toString() == "" ? sdf.parse("0000.00.00")
: sdf.parse(data.get(titleMap.get("日期"))));
} catch (ParseException e) {
e.printStackTrace();
}
// 统计无法导入的数据的原因
prices.add(toolHistoricalPrice);
counts++;
}
toolHistoricalPriceRepository.save(prices);
// 保存数据
long dataend = System.currentTimeMillis();
results.put("msgs", msgs);
results.put("total", counts);
// results.put("title", dictName);
log.debug("数据存储耗时:" + (dataend - datastart));
return results;
}
2.优化代码
ToolHistoricalPriceServiceImpl.java
@Transactional
@Override
@SuppressWarnings("unchecked")
public Map<String, Object> fileImport(MultipartFile file) {
List<String> msgs = new ArrayList<>();
Map<String, Object> results = new HashMap<>();
String name = null;
try {
name = FileUploadUtil.uploadFileReturnName(file, tempFilePath);
} catch (IOException e) {
e.printStackTrace();
}
if (name == null) {
msgs.add("文件上传失败");
results.put("msgs", msgs);
return results;
}
ExcelReader excelReader = new ExcelReader();
Map<String, Object> fileContent = excelReader.readExcelContent(name);
// 获取标题(楼盘名称)
Map<String, Integer> titleMap = (Map<String, Integer>) fileContent.get("title");
List<Map<Integer, String>> datas = (List<Map<Integer, String>>) fileContent.get("data");
// 用于计算导入数据的条数
int counts = 0;
// 用于判断获取标题栏
int count = 0;
// 创建线程池
// ExecutorService exs = Executors.newFixedThreadPool(5);
// List<SaveTask> tasks = new ArrayList<>();
// List<Future<ErrorObject>> futures = new ArrayList<>();
UserObject user = UserObjectHelper.currentUserObject();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
long datastart = System.currentTimeMillis();
List<ToolHistoricalPrice> prices = new ArrayList<>();
Map<String, HbDictionary> map = DataUtil.findHbDictionaryMap();
Map<String, HbArea> pMap = DataUtil.findPathMap();
Map<String, HbArea> bMap = DataUtil.findBillNoMap();
for (Map<Integer, String> data : datas) {
HbDictionary hbDictionary = map.get(data.get(titleMap.get("楼盘名")));
// List<HbDictionary> list =
// dictionaryService.queryByKeyword(data.get(titleMap.get("楼盘名")));
if (hbDictionary == null) {
// throw new ErpErrorException(data.get(titleMap.get("楼盘名")).toString()
// + "不存在,无法导入");
msgs.add("楼盘[" + data.get(titleMap.get("楼盘名")) + "]不存在");
continue;
}
// HbArea hbarea =
// hbAreaService.findParentAreaName(hbDictionary.getCityName());
ToolHistoricalPrice toolHistoricalPrice = new ToolHistoricalPrice();
String code = hbDictionary.getPath().substring(0, 2);
if (code.equalsIgnoreCase("SZ")) {
toolHistoricalPrice.setCity("深圳市");
}
if (code.equalsIgnoreCase("GZ")) {
toolHistoricalPrice.setCity("广州市");
}
// 设置楼盘名称
toolHistoricalPrice.setBuildingId(hbDictionary.getBillNo());
if (StringUtils.isBlank(data.get(titleMap.get("楼盘名")))) {
// throw new ErpErrorException(data.get(titleMap.get("楼盘名")).toString()
// + "不存在,无法导入");
msgs.add("楼盘名为空!");
continue;
}
toolHistoricalPrice.setBuildingName(data.get(titleMap.get("楼盘名")));
toolHistoricalPrice.setArea(hbDictionary.getAreaName());
toolHistoricalPrice.setHouseType(
data.get(titleMap.get("室(个)")) + "房" + StringUtils.trimToEmpty(data.get(titleMap.get("厅数(个)"))));
toolHistoricalPrice.setDeptId(user.getDeptId());
toolHistoricalPrice.setDeptName(user.getDeptName());
toolHistoricalPrice.setUserName(user.getName());
toolHistoricalPrice.setPrice(
new BigDecimal(StringUtils.isBlank(data.get(titleMap.get("租金(元)"))) ? "0" : data.get(titleMap.get("租金(元)"))));
toolHistoricalPrice.setPriceSource("链家");
toolHistoricalPrice.setProportion(new BigDecimal(0));
HbArea hbArea = pMap.get(hbDictionary.getPath());
if (hbArea != null) {
String billNo = pMap.get(hbDictionary.getPath()).getParentBillNo();
if (billNo.equals("0")) {// 顶级
toolHistoricalPrice.setRegion(toolHistoricalPrice.getCity());
} else {
String regionName = bMap.get(billNo).getAreaName();
if (StringUtils.isNotBlank(regionName)) {
toolHistoricalPrice.setRegion(regionName);
} else {
toolHistoricalPrice.setRegion("");
}
}
} else {
toolHistoricalPrice.setRegion("");
}
try {
toolHistoricalPrice.setCheckDate(StringUtils.trimToEmpty(data.get(titleMap.get("日期"))).toString() == null
|| StringUtils.trimToEmpty(data.get(titleMap.get("日期"))).toString() == "" ? sdf.parse("0000.00.00")
: sdf.parse(data.get(titleMap.get("日期"))));
} catch (ParseException e) {
e.printStackTrace();
}
// 统计无法导入的数据的原因
prices.add(toolHistoricalPrice);
counts++;
}
toolHistoricalPriceRepository.save(prices);
// 保存数据
long dataend = System.currentTimeMillis();
results.put("msgs", msgs);
results.put("total", counts);
// results.put("title", dictName);
log.debug("数据存储耗时:" + (dataend - datastart));
return results;
}
DataUtil.java
public static Map<String, HbDictionary> findHbDictionaryMap() {
HbDictionaryService dictionaryService = SpringContextHolder.getBean(HbDictionaryService.class);
return dictionaryService.findMap();
}
public static Map<String, HbArea> findPathMap() {
HbAreaService hbAreaService = SpringContextHolder.getBean(HbAreaService.class);
return hbAreaService.findPathMap();
}
public static Map<String, HbArea> findBillNoMap() {
HbAreaService hbAreaService = SpringContextHolder.getBean(HbAreaService.class);
return hbAreaService.findBillNoMap();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY