PointRecCalculator { public PointRecCalcResult freezePoint(PointRecPo rec, BigDecimal delta) { PointRecPo rec4Update = new PointRecPo(); rec4Update.setId(rec.getId()); 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); } if (DecimalUtils.gt(rec.getAvailable(), delta)) { BigDecimal available = rec.getAvailable().subtract(delta); BigDecimal frozen = rec.getFrozen().add(delta); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(available) .setFrozen(frozen) .setFrozenCost(rec.getFrozenCost().add(deltaCost)); } else { BigDecimal frozen = rec.getFrozen().add(rec.getAvailable()); deltaCost = costCalculator.getAvailableCost(rec); rec.setAvailable(DecimalUtils.ZERO) .setFrozen(frozen) .setFrozenCost(rec.getFrozenCost().add(deltaCost)); } rec4Update.setAvailable(rec.getAvailable()) .setFrozen(rec.getFrozen()) .setFrozenCost(rec.getFrozenCost()); 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);  }
@Test public void test_freezePoint_no_enough_point() { Assertions.assertThrows(HyenaNoPointException.class, () -> { rec.setTotal(BigDecimal.valueOf(200L).setScale(DecimalUtils.SCALE_2)) .setAvailable(BigDecimal.valueOf(100L).setScale(DecimalUtils.SCALE_2)) .setFrozen(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2)) .setTotalCost(BigDecimal.valueOf(100L).setScale(DecimalUtils.SCALE_2)) .setFrozenCost(BigDecimal.valueOf(15L).setScale(DecimalUtils.SCALE_2)); calculator.freezePoint(rec, BigDecimal.valueOf(200).setScale(DecimalUtils.SCALE_2)); }); }
@Test public void test_freezePoint_all() { rec.setTotal(BigDecimal.valueOf(200L).setScale(DecimalUtils.SCALE_2)) .setAvailable(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2)) .setFrozen(DecimalUtils.ZERO) .setTotalCost(BigDecimal.valueOf(100L).setScale(DecimalUtils.SCALE_2)) .setUsedCost(BigDecimal.valueOf(40L).setScale(DecimalUtils.SCALE_2)) .setFrozenCost(DecimalUtils.ZERO); PointRecCalcResult result = calculator.freezePoint(rec, BigDecimal.valueOf(30L)); log.info("result = {}", result); Assertions.assertEquals(rec.getId(), result.getRec4Update().getId()); Assertions.assertEquals(DecimalUtils.ZERO, rec.getAvailable() ); Assertions.assertEquals(DecimalUtils.ZERO, result.getRec4Update().getAvailable() ); Assertions.assertEquals(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2), rec.getFrozen() ); Assertions.assertEquals(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getFrozen() ); Assertions.assertEquals(BigDecimal.valueOf(60L).setScale(DecimalUtils.SCALE_2), rec.getFrozenCost() ); Assertions.assertEquals(BigDecimal.valueOf(60L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getFrozenCost() ); Assertions.assertEquals(BigDecimal.valueOf(60L).setScale(DecimalUtils.SCALE_2), result.getDeltaCost() ); }
@Test public void test_freezePoint() { rec.setTotal(BigDecimal.valueOf(200L).setScale(DecimalUtils.SCALE_2)) .setAvailable(BigDecimal.valueOf(80L).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.freezePoint(rec, BigDecimal.valueOf(20L).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.getFrozen() ); Assertions.assertEquals(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getFrozen() ); Assertions.assertEquals(BigDecimal.valueOf(15L).setScale(DecimalUtils.SCALE_2), rec.getFrozenCost() ); Assertions.assertEquals(BigDecimal.valueOf(15L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getFrozenCost() ); Assertions.assertEquals(BigDecimal.valueOf(10L).setScale(DecimalUtils.SCALE_2), result.getDeltaCost() ); }