@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)); }); }
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); }
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); } }
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); }  }
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); }
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);  }