@Test public void test_cancelPoint() { rec.setTotal(BigDecimal.valueOf(200L).setScale(DecimalUtils.SCALE_2)) .setAvailable(BigDecimal.valueOf(80L).setScale(DecimalUtils.SCALE_2)) .setCancelled(BigDecimal.valueOf(10L).setScale(DecimalUtils.SCALE_2)) .setFrozen(BigDecimal.valueOf(10L).setScale(DecimalUtils.SCALE_2)) .setTotalCost(BigDecimal.valueOf(100L).setScale(DecimalUtils.SCALE_2)) .setFrozenCost(BigDecimal.valueOf(5L).setScale(DecimalUtils.SCALE_2)); PointRecCalcResult result = calculator.cancelPoint(rec, BigDecimal.valueOf(20).setScale(DecimalUtils.SCALE_2)); log.info("result = {}", result); Assertions.assertEquals(rec.getId(), result.getRec4Update().getId()); Assertions.assertEquals(BigDecimal.valueOf(60L).setScale(DecimalUtils.SCALE_2), rec.getAvailable()); Assertions.assertEquals(BigDecimal.valueOf(60L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getAvailable()); Assertions.assertEquals(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2), rec.getCancelled()); Assertions.assertEquals(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getCancelled()); Assertions.assertEquals(BigDecimal.valueOf(10L).setScale(DecimalUtils.SCALE_2), rec.getUsedCost()); Assertions.assertEquals(BigDecimal.valueOf(10L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getUsedCost()); Assertions.assertEquals(BigDecimal.valueOf(10L).setScale(DecimalUtils.SCALE_2), result.getDeltaCost()); }
public PointRecCalcResult cancelPoint(PointRecPo rec, BigDecimal delta) { BigDecimal deltaCost; if (DecimalUtils.lt(rec.getAvailable(), delta)) { log.warn("no enough available point. rec = {}, delta = {}", rec, delta); throw new HyenaNoPointException("no enough available point", Level.WARN); } else if (DecimalUtils.gt(rec.getAvailable(), delta)) { BigDecimal available = rec.getAvailable().subtract(delta); BigDecimal canceled = rec.getCancelled().add(delta); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(available).setCancelled(canceled) .setUsedCost(rec.getUsedCost().add(deltaCost)); } else { BigDecimal canceled = rec.getCancelled().add(rec.getAvailable()); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(DecimalUtils.ZERO).setCancelled(canceled) .setUsedCost(rec.getUsedCost().add(deltaCost)); if (DecimalUtils.lte(rec.getFrozen(), DecimalUtils.ZERO)) { rec.setEnable(false); } } PointRecPo rec4Update = new PointRecPo(); rec4Update.setAvailable(rec.getAvailable()) .setCancelled(rec.getCancelled()) .setUsedCost(rec.getUsedCost()) .setEnable(rec.getEnable()) .setId(rec.getId()); return new PointRecCalcResult(rec4Update, deltaCost); }
PointRecCalculator { public PointRecCalcResult cancelPoint(PointRecPo rec, BigDecimal delta) { BigDecimal deltaCost; if (DecimalUtils.lt(rec.getAvailable(), delta)) { log.warn("no enough available point. rec = {}, delta = {}", rec, delta); throw new HyenaNoPointException("no enough available point", Level.WARN); } else if (DecimalUtils.gt(rec.getAvailable(), delta)) { BigDecimal available = rec.getAvailable().subtract(delta); BigDecimal canceled = rec.getCancelled().add(delta); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(available).setCancelled(canceled) .setUsedCost(rec.getUsedCost().add(deltaCost)); } else { BigDecimal canceled = rec.getCancelled().add(rec.getAvailable()); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(DecimalUtils.ZERO).setCancelled(canceled) .setUsedCost(rec.getUsedCost().add(deltaCost)); if (DecimalUtils.lte(rec.getFrozen(), DecimalUtils.ZERO)) { rec.setEnable(false); } } PointRecPo rec4Update = new PointRecPo(); rec4Update.setAvailable(rec.getAvailable()) .setCancelled(rec.getCancelled()) .setUsedCost(rec.getUsedCost()) .setEnable(rec.getEnable()) .setId(rec.getId()); return new PointRecCalcResult(rec4Update, deltaCost); } }
PointRecCalculator { public PointRecCalcResult cancelPoint(PointRecPo rec, BigDecimal delta) { BigDecimal deltaCost; if (DecimalUtils.lt(rec.getAvailable(), delta)) { log.warn("no enough available point. rec = {}, delta = {}", rec, delta); throw new HyenaNoPointException("no enough available point", Level.WARN); } else if (DecimalUtils.gt(rec.getAvailable(), delta)) { BigDecimal available = rec.getAvailable().subtract(delta); BigDecimal canceled = rec.getCancelled().add(delta); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(available).setCancelled(canceled) .setUsedCost(rec.getUsedCost().add(deltaCost)); } else { BigDecimal canceled = rec.getCancelled().add(rec.getAvailable()); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(DecimalUtils.ZERO).setCancelled(canceled) .setUsedCost(rec.getUsedCost().add(deltaCost)); if (DecimalUtils.lte(rec.getFrozen(), DecimalUtils.ZERO)) { rec.setEnable(false); } } PointRecPo rec4Update = new PointRecPo(); rec4Update.setAvailable(rec.getAvailable()) .setCancelled(rec.getCancelled()) .setUsedCost(rec.getUsedCost()) .setEnable(rec.getEnable()) .setId(rec.getId()); return new PointRecCalcResult(rec4Update, deltaCost); }  }
PointRecCalculator { public PointRecCalcResult cancelPoint(PointRecPo rec, BigDecimal delta) { BigDecimal deltaCost; if (DecimalUtils.lt(rec.getAvailable(), delta)) { log.warn("no enough available point. rec = {}, delta = {}", rec, delta); throw new HyenaNoPointException("no enough available point", Level.WARN); } else if (DecimalUtils.gt(rec.getAvailable(), delta)) { BigDecimal available = rec.getAvailable().subtract(delta); BigDecimal canceled = rec.getCancelled().add(delta); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(available).setCancelled(canceled) .setUsedCost(rec.getUsedCost().add(deltaCost)); } else { BigDecimal canceled = rec.getCancelled().add(rec.getAvailable()); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(DecimalUtils.ZERO).setCancelled(canceled) .setUsedCost(rec.getUsedCost().add(deltaCost)); if (DecimalUtils.lte(rec.getFrozen(), DecimalUtils.ZERO)) { rec.setEnable(false); } } PointRecPo rec4Update = new PointRecPo(); rec4Update.setAvailable(rec.getAvailable()) .setCancelled(rec.getCancelled()) .setUsedCost(rec.getUsedCost()) .setEnable(rec.getEnable()) .setId(rec.getId()); return new PointRecCalcResult(rec4Update, deltaCost); }  PointRecCalcResult freezePoint(PointRecPo rec, BigDecimal delta); PointRecCalcResult unfreezePoint(PointRecPo rec, BigDecimal delta, BigDecimal deltaCost); PointRecCalcResult decreasePoint(PointRecPo rec, BigDecimal delta); PointRecPo refundPoint(PointRecPo rec, BigDecimal delta, BigDecimal cost); PointRecCalcResult cancelPoint(PointRecPo rec, BigDecimal delta); }
PointRecCalculator { public PointRecCalcResult cancelPoint(PointRecPo rec, BigDecimal delta) { BigDecimal deltaCost; if (DecimalUtils.lt(rec.getAvailable(), delta)) { log.warn("no enough available point. rec = {}, delta = {}", rec, delta); throw new HyenaNoPointException("no enough available point", Level.WARN); } else if (DecimalUtils.gt(rec.getAvailable(), delta)) { BigDecimal available = rec.getAvailable().subtract(delta); BigDecimal canceled = rec.getCancelled().add(delta); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(available).setCancelled(canceled) .setUsedCost(rec.getUsedCost().add(deltaCost)); } else { BigDecimal canceled = rec.getCancelled().add(rec.getAvailable()); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(DecimalUtils.ZERO).setCancelled(canceled) .setUsedCost(rec.getUsedCost().add(deltaCost)); if (DecimalUtils.lte(rec.getFrozen(), DecimalUtils.ZERO)) { rec.setEnable(false); } } PointRecPo rec4Update = new PointRecPo(); rec4Update.setAvailable(rec.getAvailable()) .setCancelled(rec.getCancelled()) .setUsedCost(rec.getUsedCost()) .setEnable(rec.getEnable()) .setId(rec.getId()); return new PointRecCalcResult(rec4Update, deltaCost); }  PointRecCalcResult freezePoint(PointRecPo rec, BigDecimal delta); PointRecCalcResult unfreezePoint(PointRecPo rec, BigDecimal delta, BigDecimal deltaCost); PointRecCalcResult decreasePoint(PointRecPo rec, BigDecimal delta); PointRecPo refundPoint(PointRecPo rec, BigDecimal delta, BigDecimal cost); PointRecCalcResult cancelPoint(PointRecPo rec, BigDecimal delta);  }