EDEN-MACE 1.5.0 更新,使用模板方法新增账务

 江峰工作室
发布于 2019年05月08日
收藏 4

EDEN-MACE 是一套灵活的管理佣金的分销管理系统,它涵盖并且总结了目前流行的分销模式,让分销更加简单。

开源地址:https://gitee.com/codingdb/distribution_management

原来的账务的管理,使用策略模式+工厂模式,导致每次新增一个账户之后就会复制大量的重复代码,这样会导致代码大量的冗余。

根据上面的问题,将策略模式换成了模板模式+工厂模式,每次新增一个账户的时候就可以只增加改动的部分即可。

下面演示新增一个交易账户需要做的事(只有最后一段需要新增,第一部分为静态工厂方法初始化,第二部分为模板方法调用,当然省略了部分部分代码,省略的代码为原来的公共代码)

 public AmountTemplateFactoryContext(String type) {
        switch (type){
            case "0":
                amountTemplate = new TradeAmountServiceImpl();
                break;
            case  "1":
                amountTemplate = new LevelAmountServiceImpl();
                break;
            case  "2":
                amountTemplate = new InviteAmountServiceImpl();
                break;
            default:
                break;
        }
    }
  public abstract void addMoney(DisMemberAmount memberAmount,BigDecimal amount,DisAmountSituation situation);
    @DataSource(name = DSEnum.DATA_SOURCE_BIZ)
    public final void addMoney(String userId, BigDecimal amount, String sourceName, String type) {
      ....
        addMoney(memberAmount,amount,situation);
       
      ...
    }
    public abstract void frozenAmount(DisMemberAmount memberAmount,BigDecimal amount);
    @DataSource(name = DSEnum.DATA_SOURCE_BIZ)
    public final void frozenAmount(String userId, BigDecimal amount) {
     ...

        frozenAmount(memberAmount,amount);
      ...
    }
    public abstract void reduceMoney(DisMemberAmount memberAmount,BigDecimal amount,DisAmountSituation situation);
    @DataSource(name = DSEnum.DATA_SOURCE_BIZ)
    public final void reduceMoney(String userId, BigDecimal amount) {
      ....
        reduceMoney(memberAmount,amount,situation);



      ...
    }


    public abstract void returnMoney(DisMemberAmount memberAmount,BigDecimal amount);
    @DataSource(name = DSEnum.DATA_SOURCE_BIZ)
    public final void returnMoney(String userId, BigDecimal amount) {
       ...
        returnMoney(memberAmount,amount);
     ...
    }
public class TradeAmountServiceImpl extends AmountTemplate{


    @Override
    public void addMoney(DisMemberAmount memberAmount, BigDecimal amount,DisAmountSituation situation) {
        BigDecimal afterThirdAmount=new BigDecimal(0);
        BigDecimal beforeThirdAmount=new BigDecimal(0);
        afterThirdAmount=memberAmount.getTradeTotalAmount().add(amount);
        beforeThirdAmount=memberAmount.getTradeTotalAmount();
        memberAmount.setTradeTotalAmount(memberAmount.getTradeTotalAmount().add(amount));
        memberAmount.setTradeAvaibleAmount(memberAmount.getTradeAvaibleAmount().add(amount));
        situation.setSpecificBeforeChangeAmount(beforeThirdAmount);
        situation.setSpecificAfterChangeAmount(afterThirdAmount);
    }

    @Override
    public void frozenAmount(DisMemberAmount memberAmount, BigDecimal amount) {

        BigDecimal  avaibleThirdAmount=new BigDecimal(0);
        avaibleThirdAmount=memberAmount.getTradeAvaibleAmount();
        if(avaibleThirdAmount.compareTo(amount)==-1){
            throw  new BussinessException(BizExceptionEnum.LOW_MONEY);
        }
        memberAmount.setTradeAvaibleAmount(memberAmount.getTradeAvaibleAmount().subtract(amount));
        memberAmount.setTradeFrozenAmount(memberAmount.getTradeFrozenAmount().add(amount));


    }

    @Override
    public void reduceMoney(DisMemberAmount memberAmount, BigDecimal amount,DisAmountSituation situation) {

        BigDecimal  avaibleThirdAmount=new BigDecimal(0);
        avaibleThirdAmount=memberAmount.getTradeFrozenAmount();

        if(avaibleThirdAmount.compareTo(amount)==-1){
            throw  new BussinessException(BizExceptionEnum.LOW_MONEY);
        }
        BigDecimal beforeThirdAmount=new BigDecimal(0);
        beforeThirdAmount=memberAmount.getTradeTotalAmount();


        memberAmount.setTradeFrozenAmount(memberAmount.getTradeFrozenAmount().subtract(amount));
        memberAmount.setTradeTotalAmount(beforeThirdAmount.subtract(amount));
        situation.setSpecificBeforeChangeAmount(beforeThirdAmount);
        situation.setSpecificAfterChangeAmount(beforeThirdAmount.subtract(amount));

    }

    @Override
    public void returnMoney(DisMemberAmount memberAmount, BigDecimal amount) {
        BigDecimal  avaibleThirdAmount=new BigDecimal(0);
        avaibleThirdAmount=memberAmount.getTradeFrozenAmount();
        if(avaibleThirdAmount.compareTo(amount)==-1){
            throw  new BussinessException(BizExceptionEnum.LOW_MONEY);
        }
        memberAmount.setTradeAvaibleAmount(memberAmount.getTradeAvaibleAmount().add(amount));
        memberAmount.setTradeFrozenAmount(memberAmount.getTradeFrozenAmount().subtract(amount));
    }
}

 

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创开源社区。
转载请注明:文章转载自 OSCHINA 社区 [http://www.oschina.net]
本文标题:EDEN-MACE 1.5.0 更新,使用模板方法新增账务
加载中
返回顶部
顶部