楼盘历史价格管理导入功能优化

备注:红色为重点关注的内容,绿色表示修改的内容
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();
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值