代码优化之路

 在工作中,碰到如下代码,一段记录价格修改记录日志的逻辑

private void addGoodsPriceRecord(UpdateGoodsItemDTO goodsItem, GoodsItemBO oldGoodsItem) {
      
        JSONObject logJson = new JSONObject();
        SysSendMsgDTO sendMsgDTO = new SysSendMsgDTO();
        sendMsgDTO.setProjectNo(ProjectNoConsts.PROJECT_XDXT);
        sendMsgDTO.setBusId(goodsItem.getId());
        sendMsgDTO.setLogNo(goodsItem.getLogNo());
        sendMsgDTO.setStaffId(goodsItem.getStaffId());
        
        if (goodsItem.getEditType() == 2) {
            if (goodsItem.getMinPrice().compareTo(oldGoodsItem.getMinPrice()) != 0) {
                sendMsgDTO.setType(SkuPriceOptRecordEnum.getTypeByValue(-1).getSysOptCode());
                
                BigDecimal oldPrice = oldGoodsItem.getMinPrice();
            
                BigDecimal newPrice = goodsItem.getMinPrice();
                logJson.put("oldValue", oldPrice);
                logJson.put("newValue", newPrice);
                sendMsgDTO.setLogJson(logJson.toJSONString());
                this.addOptLog(sendMsgDTO);
            }
        } else if (goodsItem.getEditType() == 1) {
           
            if (goodsItem.getVip0Price().compareTo(oldGoodsItem.getVip0Price()) != 0) {
                sendMsgDTO.setType(SkuPriceOptRecordEnum.getTypeByValue(0).getSysOptCode());
                
                BigDecimal oldPrice = oldGoodsItem.getVip0Price();
                
                BigDecimal newPrice = goodsItem.getVip0Price();
                logJson.put("oldValue", oldPrice);
                logJson.put("newValue", newPrice);
                sendMsgDTO.setLogJson(logJson.toJSONString());
                this.addOptLog(sendMsgDTO);
            }
            if (goodsItem.getVip1Price().compareTo(oldGoodsItem.getVip1Price()) != 0) {
                sendMsgDTO.setType(SkuPriceOptRecordEnum.getTypeByValue(1).getSysOptCode());
           
                BigDecimal oldPrice = oldGoodsItem.getVip1Price();
               
                BigDecimal newPrice = goodsItem.getVip1Price();
                logJson.put("oldValue", oldPrice);
                logJson.put("newValue", newPrice);
                sendMsgDTO.setLogJson(logJson.toJSONString());
                this.addOptLog(sendMsgDTO);
            }
            if (goodsItem.getVip2Price().compareTo(oldGoodsItem.getVip2Price()) != 0) {
                sendMsgDTO.setType(SkuPriceOptRecordEnum.getTypeByValue(2).getSysOptCode());
                
                BigDecimal oldPrice = oldGoodsItem.getVip2Price();
           
                BigDecimal newPrice = goodsItem.getVip2Price();
                logJson.put("oldValue", oldPrice);
                logJson.put("newValue", newPrice);
                sendMsgDTO.setLogJson(logJson.toJSONString());
                this.addOptLog(sendMsgDTO);
            }
            if (goodsItem.getVip3Price().compareTo(oldGoodsItem.getVip3Price()) != 0) {
                sendMsgDTO.setType(SkuPriceOptRecordEnum.getTypeByValue(3).getSysOptCode());
                
                BigDecimal oldPrice = oldGoodsItem.getVip3Price();
                BigDecimal newPrice = goodsItem.getVip3Price();
                logJson.put("oldValue", oldPrice);
                logJson.put("newValue", newPrice);
                sendMsgDTO.setLogJson(logJson.toJSONString());
                this.addOptLog(sendMsgDTO);
            }
            if (goodsItem.getVip4Price().compareTo(oldGoodsItem.getVip4Price()) != 0) {
                sendMsgDTO.setType(SkuPriceOptRecordEnum.getTypeByValue(4).getSysOptCode());
                
                BigDecimal oldPrice = oldGoodsItem.getVip4Price();
                
                BigDecimal newPrice = goodsItem.getVip4Price();
                logJson.put("oldValue", oldPrice);
                logJson.put("newValue", newPrice);
                sendMsgDTO.setLogJson(logJson.toJSONString());
                this.addOptLog(sendMsgDTO);
            }
        }
    }
以上代码,个人觉得有如下鸡哥问题
  1. 当修改类型等于1的时候, 可能下面的每个判断都可能进, 最多会进四个判断,然后每个判断都会去保存日志, 而这个日志保存是需要OpenFeign调用日志服务去保存,增加了额外的开销。 如果服务不稳定,还有可能导致数据错乱的问题
  2. 以上代码出现了大量重复的代码。

现在根据以上问题,做出第一次优化

private void addGoodsPriceRecord(UpdateGoodsItemDTO updateGoodsItemDTO, GoodsItemBO oldGoodsItem) {
        List<InsertOptLogDTO> insertOptLogDTOList = new ArrayList<>();
        String optName = getOptName(updateGoodsItemDTO.getStaffId());
        if (updateGoodsItemDTO.getEditType() == 2) {
            if (updateGoodsItemDTO.getMinPrice().compareTo(oldGoodsItem.getMinPrice()) != 0) {
                BigDecimal oldPrice = oldGoodsItem.getMinPrice();
                BigDecimal newPrice = updateGoodsItemDTO.getMinPrice();
                insertOptLogDTOList.add(builderInsertOptLog(updateGoodsItemDTO, -1, oldPrice, newPrice, optName));
            }
        } else if (updateGoodsItemDTO.getEditType() == 1) {
            if (updateGoodsItemDTO.getVip0Price().compareTo(oldGoodsItem.getVip0Price()) != 0) {
                BigDecimal oldPrice = oldGoodsItem.getVip0Price();
                BigDecimal newPrice = updateGoodsItemDTO.getVip0Price();
                insertOptLogDTOList.add(builderInsertOptLog(updateGoodsItemDTO, 0, oldPrice, newPrice, optName));
            }
            if (updateGoodsItemDTO.getVip1Price().compareTo(oldGoodsItem.getVip1Price()) != 0) {
                BigDecimal oldPrice = oldGoodsItem.getVip1Price();
                BigDecimal newPrice = updateGoodsItemDTO.getVip1Price();
                insertOptLogDTOList.add(builderInsertOptLog(updateGoodsItemDTO, 1, oldPrice, newPrice, optName));
            }
            if (updateGoodsItemDTO.getVip2Price().compareTo(oldGoodsItem.getVip2Price()) != 0) {
                BigDecimal oldPrice = oldGoodsItem.getVip2Price();
                BigDecimal newPrice = updateGoodsItemDTO.getVip2Price();
                insertOptLogDTOList.add(builderInsertOptLog(updateGoodsItemDTO, 2, oldPrice, newPrice, optName));
            }
            if (updateGoodsItemDTO.getVip3Price().compareTo(oldGoodsItem.getVip3Price()) != 0) {
                BigDecimal oldPrice = oldGoodsItem.getVip3Price();
                BigDecimal newPrice = updateGoodsItemDTO.getVip3Price();
                insertOptLogDTOList.add(builderInsertOptLog(updateGoodsItemDTO, 3, oldPrice, newPrice, optName));
            }
            if (updateGoodsItemDTO.getVip4Price().compareTo(oldGoodsItem.getVip4Price()) != 0) {
                BigDecimal oldPrice = oldGoodsItem.getVip4Price();
                BigDecimal newPrice = updateGoodsItemDTO.getVip4Price();
                insertOptLogDTOList.add(builderInsertOptLog(updateGoodsItemDTO, 4, oldPrice, newPrice, optName));
            }
        }
        if (CollUtil.isNotEmpty(insertOptLogDTOList)) {
            optLogFeignClient.batchInsertOptLog(insertOptLogDTOList).onError("保存价格变动日志失败");
        }
    }

    private InsertOptLogDTO builderInsertOptLog(UpdateGoodsItemDTO updateGoodsItemDTO, Integer level, BigDecimal oldPrice, BigDecimal newPrice, String optName) {
        InsertOptLogDTO insertOptLogDTO = new InsertOptLogDTO();
        insertOptLogDTO.setProjectNo(ProjectNoConsts.PROJECT_XDXT);
        insertOptLogDTO.setBusId(updateGoodsItemDTO.getId());
        insertOptLogDTO.setLogNo(updateGoodsItemDTO.getLogNo());
        insertOptLogDTO.setStaffId(updateGoodsItemDTO.getStaffId());
        insertOptLogDTO.setStaffName(optName);
        insertOptLogDTO.setType(SkuPriceOptRecordEnum.getTypeByValue(level).getSysOptCode());
        JSONObject logJson = new JSONObject();
        logJson.put("oldValue", oldPrice);
        logJson.put("newValue", newPrice);
        insertOptLogDTO.setLogJson(logJson.toJSONString());
        return insertOptLogDTO;
    }
个人的优化思路
  1. 单个调用日志服务保存的操作,改成批量操作。
  2. 将重复的代码抽成公用的方法调用
        个人觉得以上代码还有空间,比如每个等级的价格,根据等级去获取。但思虑再三,决定暂时先保留现在的版本。
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值