帮我优化这段代码结构 //不提示顺序 if (accountExt.getRemainEcpm() <= (amountCpm + withdrawOrderCpmCfg.getIntStart())) { //提现顺序 if (WithdrawOrderSwitchEnum.GLOBAL_LIMIT.getCode() == withdrawOrderSwitch) { Integer withdrawOrderCpm = withdrawOrderCpmCfg.getIntVal(); if (Integer.parseInt(accountExt.getFirstEcpm()) >= withdrawOrderCpm) { //针对1元以上 if (cfgWithdraw.getAmount().compareTo(new BigDecimal("1")) >= 0) { throw new ApiBusinessException(ErrorCodeEnum.ERROR_DEFAULT.getCode(), "请按顺序提现哦~"); } }else { //有价值用户 if (accountExt.getRemainEcpm() <= amountCpm && cfgWithdraw.getAmount().compareTo(new BigDecimal("0.3")) > 0) { //是否有其他挡位 if (checkOtherTap(accountExt, cfgWithdraw)) { throw new ApiBusinessException(ErrorCodeEnum.ERROR_DEFAULT.getCode(), "请按顺序提现哦~"); } } } }else if (WithdrawOrderSwitchEnum.ORDER_LIMIT.getCode() == withdrawOrderSwitch) { if (checkOtherTap(accountExt, cfgWithdraw)) { throw new ApiBusinessException(ErrorCodeEnum.ERROR_DEFAULT.getCode(), "请按顺序提现哦~"); } } }
时间: 2024-02-10 10:13:22 浏览: 67
顺序线性表的初始化、插入与输出
以下是对代码结构的优化建议:
1. 抽取方法:将 if 块内的代码块抽取成一个独立的方法,提高代码的可读性和可维护性。
2. 提取变量:将常量字符串和数字提取成变量,方便后续修改和维护。
3. 优化条件判断顺序:将条件判断中易先满足的条件放在前面,提高代码执行效率。
4. 减少嵌套:使用 continue 或者 return 来减少 if 嵌套,提高代码的可读性。
下面是经过优化后的代码结构:
```
// 抽取方法
private void checkWithdrawOrder(AccountExt accountExt, WithdrawCfg cfgWithdraw, BigDecimal amountCpm) {
WithdrawOrderCpmCfg withdrawOrderCpmCfg = withdrawOrderCpmCfgMapper.selectByAppId(accountExt.getAppId());
Integer withdrawOrderSwitch = withdrawOrderCpmCfg.getWithdrawOrderSwitch();
BigDecimal minWithdrawAmount = new BigDecimal("0.3");
int startWithdrawCpm = withdrawOrderCpmCfg.getIntStart();
int withdrawCpm = withdrawOrderCpmCfg.getIntVal();
if (accountExt.getRemainEcpm() <= (amountCpm + startWithdrawCpm)) {
if (WithdrawOrderSwitchEnum.GLOBAL_LIMIT.getCode() == withdrawOrderSwitch) {
if (Integer.parseInt(accountExt.getFirstEcpm()) >= withdrawCpm) {
if (cfgWithdraw.getAmount().compareTo(new BigDecimal("1")) >= 0) {
throw new ApiBusinessException(ErrorCodeEnum.ERROR_DEFAULT.getCode(), "请按顺序提现哦~");
}
return;
}
if (accountExt.getRemainEcpm() <= amountCpm && cfgWithdraw.getAmount().compareTo(minWithdrawAmount) > 0) {
if (checkOtherTap(accountExt, cfgWithdraw)) {
throw new ApiBusinessException(ErrorCodeEnum.ERROR_DEFAULT.getCode(), "请按顺序提现哦~");
}
}
} else if (WithdrawOrderSwitchEnum.ORDER_LIMIT.getCode() == withdrawOrderSwitch) {
if (checkOtherTap(accountExt, cfgWithdraw)) {
throw new ApiBusinessException(ErrorCodeEnum.ERROR_DEFAULT.getCode(), "请按顺序提现哦~");
}
}
}
}
```
在实际使用时,你只需要调用新的方法 checkWithdrawOrder(),并传入对应的参数即可。
阅读全文