PointRecCalculator { public PointRecCalcResult decreasePoint(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 used = rec.getUsed().add(delta); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(available).setUsed(used).setUsedCost(rec.getUsedCost().add(deltaCost)); } else { BigDecimal used = rec.getUsed().add(rec.getAvailable()); deltaCost = this.costCalculator.accountCost(rec, delta); rec.setAvailable(DecimalUtils.ZERO).setUsed(used).setUsedCost(rec.getUsedCost().add(deltaCost)); if (DecimalUtils.lte(rec.getFrozen(), DecimalUtils.ZERO)) { rec.setEnable(false); } } PointRecPo rec4Update = new PointRecPo(); rec4Update.setAvailable(rec.getAvailable()) .setUsed(rec.getUsed()) .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);  }
@Test public void test_decreasePoint_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.decreasePoint(rec, BigDecimal.valueOf(200)); }); }
@Test public void test_decreasePoint_all() { rec.setTotal(BigDecimal.valueOf(200L).setScale(DecimalUtils.SCALE_2)) .setAvailable(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2)) .setUsed(BigDecimal.valueOf(20L).setScale(DecimalUtils.SCALE_2)) .setFrozen(BigDecimal.valueOf(10L).setScale(DecimalUtils.SCALE_2)) .setTotalCost(BigDecimal.valueOf(100L).setScale(DecimalUtils.SCALE_2)) .setUsedCost(BigDecimal.valueOf(40L).setScale(DecimalUtils.SCALE_2)) .setFrozenCost(BigDecimal.valueOf(10L).setScale(DecimalUtils.SCALE_2)); PointRecCalcResult result = calculator.decreasePoint(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(50L).setScale(DecimalUtils.SCALE_2), rec.getUsed()); Assertions.assertEquals(BigDecimal.valueOf(50L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getUsed()); Assertions.assertEquals(BigDecimal.valueOf(90L).setScale(DecimalUtils.SCALE_2), rec.getUsedCost()); Assertions.assertEquals(BigDecimal.valueOf(90L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getUsedCost()); Assertions.assertEquals(BigDecimal.valueOf(50L).setScale(DecimalUtils.SCALE_2), result.getDeltaCost()); Assertions.assertTrue(rec.getEnable()); Assertions.assertTrue(result.getRec4Update().getEnable()); }
@Test public void test_decreasePoint_use_all() { rec.setTotal(BigDecimal.valueOf(200L).setScale(DecimalUtils.SCALE_2)) .setAvailable(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2)) .setUsed(BigDecimal.valueOf(20L).setScale(DecimalUtils.SCALE_2)) .setFrozen(DecimalUtils.ZERO) .setTotalCost(BigDecimal.valueOf(100L).setScale(DecimalUtils.SCALE_2)) .setUsedCost(BigDecimal.valueOf(40L).setScale(DecimalUtils.SCALE_2)) .setFrozenCost(BigDecimal.valueOf(10L).setScale(DecimalUtils.SCALE_2)); PointRecCalcResult result = calculator.decreasePoint(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(50L).setScale(DecimalUtils.SCALE_2), rec.getUsed()); Assertions.assertEquals(BigDecimal.valueOf(50L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getUsed()); Assertions.assertEquals(BigDecimal.valueOf(90L).setScale(DecimalUtils.SCALE_2), rec.getUsedCost()); Assertions.assertEquals(BigDecimal.valueOf(90L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getUsedCost()); Assertions.assertEquals(BigDecimal.valueOf(50L).setScale(DecimalUtils.SCALE_2), result.getDeltaCost()); Assertions.assertFalse(rec.getEnable()); Assertions.assertFalse(result.getRec4Update().getEnable()); }
@Test public void test_decreasePoint() { rec.setTotal(BigDecimal.valueOf(200L).setScale(DecimalUtils.SCALE_2)) .setAvailable(BigDecimal.valueOf(80L).setScale(DecimalUtils.SCALE_2)) .setUsed(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.decreasePoint(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.getUsed()); Assertions.assertEquals(BigDecimal.valueOf(30L).setScale(DecimalUtils.SCALE_2), result.getRec4Update().getUsed()); 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()); }