在工作中,碰到如下代码,一段记录价格修改记录日志的逻辑
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的时候, 可能下面的每个判断都可能进, 最多会进四个判断,然后每个判断都会去保存日志, 而这个日志保存是需要OpenFeign调用日志服务去保存,增加了额外的开销。 如果服务不稳定,还有可能导致数据错乱的问题
- 以上代码出现了大量重复的代码。
现在根据以上问题,做出第一次优化
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;
}
个人的优化思路
- 单个调用日志服务保存的操作,改成批量操作。
- 将重复的代码抽成公用的方法调用