PointRecCalculator { public PointRecCalcResult unfreezePoint(PointRecPo rec, BigDecimal delta, BigDecimal deltaCost) { PointRecPo rec4Update = new PointRecPo(); rec4Update.setId(rec.getId()); if (DecimalUtils.lt(rec.getFrozen(), delta)) { log.warn("no enough frozen point. rec = {}, delta = {}", rec, delta); throw new HyenaNoPointException("no enough frozen point", Level.WARN); } else if (DecimalUtils.gt(rec.getFrozen(), delta)) { BigDecimal frozen = rec.getFrozen().subtract(delta); BigDecimal available = rec.getAvailable().add(delta); if (deltaCost == null) { deltaCost = this.costCalculator.accountCost4Unfreeze(rec, delta); } rec.setAvailable(available).setFrozen(frozen) .setFrozenCost(rec.getFrozenCost().subtract(deltaCost)); } else { BigDecimal available = rec.getAvailable().add(rec.getFrozen()); if (deltaCost == null) { deltaCost = rec.getFrozenCost(); } rec.setFrozen(DecimalUtils.ZERO).setAvailable(available) .setFrozenCost(DecimalUtils.ZERO); } rec4Update.setFrozen(rec.getFrozen()) .setAvailable(rec.getAvailable()) .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_unfreezePoint_no_enough_point() { Assertions.assertThrows(HyenaNoPointException.class, () -> { rec.setTotal(BigDecimal.valueOf(200L)) .setAvailable(BigDecimal.valueOf(100L)).setFrozen(BigDecimal.valueOf(30L)) .setTotalCost(BigDecimal.valueOf(100L)) .setFrozenCost(BigDecimal.valueOf(15L)); calculator.unfreezePoint(rec, BigDecimal.valueOf(40), null); }); }
@Test public void test_unfreezePoint_all() { 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)); PointRecCalcResult result = calculator.unfreezePoint(rec, BigDecimal.valueOf(30L), null); log.info("result = {}", result); Assertions.assertEquals(rec.getId(), result.getRec4Update().getId()); Assertions.assertEquals(BigDecimal.valueOf(130L).setScale(DecimalUtils.SCALE_2), rec.getAvailable() ); Assertions.assertEquals(BigDecimal.valueOf(130L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getAvailable() ); Assertions.assertEquals(DecimalUtils.ZERO, rec.getFrozen()); Assertions.assertEquals(DecimalUtils.ZERO, result.getRec4Update().getFrozen()); Assertions.assertEquals(DecimalUtils.ZERO, rec.getFrozenCost()); Assertions.assertEquals(DecimalUtils.ZERO, result.getRec4Update().getFrozenCost()); Assertions.assertEquals(BigDecimal.valueOf(15L).setScale(DecimalUtils.SCALE_2), result.getDeltaCost()); }
@Test public void test_unfreezePoint() { rec.setTotal(BigDecimal.valueOf(200L).setScale(DecimalUtils.SCALE_2)) .setAvailable(BigDecimal.valueOf(100L).setScale(DecimalUtils.SCALE_2)) .setFrozen(BigDecimal.valueOf(80L).setScale(DecimalUtils.SCALE_2)) .setTotalCost(BigDecimal.valueOf(100L).setScale(DecimalUtils.SCALE_2)) .setFrozenCost(BigDecimal.valueOf(40L).setScale(DecimalUtils.SCALE_2)); PointRecCalcResult result = calculator.unfreezePoint(rec, BigDecimal.valueOf(20L).setScale(DecimalUtils.SCALE_2), null); log.info("result = {}", result); Assertions.assertEquals(rec.getId(), result.getRec4Update().getId()); Assertions.assertEquals(BigDecimal.valueOf(120L).setScale(DecimalUtils.SCALE_2), rec.getAvailable() ); Assertions.assertEquals(BigDecimal.valueOf(120L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getAvailable() ); Assertions.assertEquals(BigDecimal.valueOf(60L).setScale(DecimalUtils.SCALE_2), rec.getFrozen() ); Assertions.assertEquals(BigDecimal.valueOf(60L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getFrozen() ); Assertions.assertEquals(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2), rec.getFrozenCost() ); Assertions.assertEquals(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getFrozenCost()); Assertions.assertEquals(BigDecimal.valueOf(10L).setScale(DecimalUtils.SCALE_2), result.getDeltaCost()); }