如果可以的话,重复逻辑不要多次判断,代码会非常的冗余。
这段代码没有修改之前,大概300行,优化之后只有100行左右。
/**
* 判断是否可以执行退还保证金操作
* 1.判断该条保证金缴纳记录是否可以退还
* 2.如果可以退还,退还的金额是多少?
*
* 根据时间做判断?
* 不用凑整数,可以退就直接退掉
*/
@Override
public Map<String, Object> returnCheck(String bailPayId,Long customerId) {
logger.debug("returnCheck,bailPayId {},customerId{}",bailPayId,customerId);
Map<String,Object> map = null;
BigDecimal returnBailPay = new BigDecimal(0);
BailPay bailPay = bailPayRepository.findOne(bailPayId);
if (bailPay != null) {
int bailClass = bailPay.bailClass();//该条记录的保证金缴纳的类别
String agencyId = bailPay.agencyId();//缴纳该条保证金的机构id值
BigDecimal maxCommonBailLock;
BigDecimal bailLockSpecialSum;
if (bailClass == 1) {//通用保证金
//如果当前竞买人存在保证金冻结记录状态为"锁定"的状态,不能够执行保证金退还操作
int bailLockCount = this.bailLockCommonQuery(customerId);
if (bailLockCount != 0) {
map = new HashMap<String,Object>();
map.put("flag", 0);//不可以返还
map.put("returnBailPay", 0);//返还的金额为 0
return map;
}
maxCommonBailLock = this.checkMaxCommonFrozenBailPay(customerId);
bailLockSpecialSum = new BigDecimal(0);
} else {//专项保证金
maxCommonBailLock = this.bailLockSpecialQuery(customerId,agencyId);
bailLockSpecialSum = this.bailLockSpecialQuery(customerId,agencyId);
}
BigDecimal canUseBailLockSum = new BigDecimal(0);
//查询出当前该笔保证金缴纳记录之前缴纳的所有的缴纳记录的总和
Specification<BailPay> spec = (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<Predicate>();
Predicate predicate1 = cb.equal(root.get(BailPay_.customerId), customerId);//当前登录用户的id
predicates.add(predicate1);
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Predicate predicate2 = cb.lessThan(root.<Date>get(BailPay_.crtTime), sdf.parse(bailPay.crtTime().toString()));//
predicates.add(predicate2);
} catch (ParseException e) {
e.printStackTrace();
}
Predicate predicate3 = cb.equal(root.get(BailPay_.bailClass), BailPay.BAIL_CLASS_TONGYONG);//通用保证金
predicates.add(predicate3);
//判断缴纳时间早于当前该条记录的缴纳记录
if (!predicates.isEmpty()) {
return cb.and(predicates.toArray(new Predicate[0]));
} else {
return null;
}
};
List<BailPay> bailPayList = bailPayRepository.findAll(spec);
if (bailPayList != null && bailPayList.size()>0) {
for (BailPay bailPay2 : bailPayList) {
if (bailPay2 != null) {
canUseBailLockSum = canUseBailLockSum.add(bailPay2.remainBailPay());//每一条缴纳记录的剩余金额的总和
}
}
}
if(canUseBailLockSum.compareTo(maxCommonBailLock.add(bailLockSpecialSum)) != -1){
//可以退还该条保证金缴纳记录的金额
map = new HashMap<String,Object>();
map.put("flag", 1);//可以返还
map.put("returnBailPay", returnBailPay);//返还的金额
return map;
}
}
map = new HashMap<String,Object>();
map.put("flag", 0);//不可以返还
map.put("returnBailPay", 0);//返还的金额
return map;
}