logger用{},或者用String.format("数字1:%s,和数字2:%s不一致",a,b);
例如:
private void validateRequest (LedgerCalculatorRequest request) {
List
BigDecimal sumReceived = ledgersInfo.stream().map(ledgersInfoBean -> new BigDecimal(ledgersInfoBean.getReceived())).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal received = new BigDecimal(request.getPaymentSubjectReceived()).add(sumReceived);
BigDecimal totalReceived = new BigDecimal(request.getTotalReceived());
if (received.compareTo(totalReceived) != 0) {
log.error("已收总金额不一致,计算所得值:{},参数值:{}", received, totalReceived);
throw new HbBusinessException("9999", String.format("已收总金额不一致,计算所得值:%s,参数值:%s", received, totalReceived));
}
BigDecimal totalReceivable = new BigDecimal(request.getTotalReceivable());
BigDecimal sumReceivable = ledgersInfo.stream().map(ledgersInfoBean -> new BigDecimal(ledgersInfoBean.getReceivable())).reduce(BigDecimal.ZERO, BigDecimal::add);
if (totalReceivable.compareTo(sumReceivable) < 0) {
log.error("已收总金额小于分账方已收金额,分账方已收金额:{},已收总金额:{}", sumReceivable, totalReceivable);
throw new HbBusinessException("9999", String.format("已收总金额小于分账方已收金额,分账方已收金额:%s,已收总金额:%s", sumReceivable, totalReceivable));
}
if (Objects.isNull(request.getPaymentSubjectReceivable())) {
request.setPaymentSubjectReceivable(totalReceivable.subtract(sumReceivable).longValueExact());
} else {
BigDecimal receivable = new BigDecimal(request.getPaymentSubjectReceivable()).add(sumReceivable);
if (receivable.compareTo(totalReceivable) != 0) {
log.error("应收总金额不一致,计算所得值:{},参数值:{}", receivable, totalReceivable);
throw new HbBusinessException("9999", String.format("应收总金额不一致,计算所得值:%s,参数值:%s", receivable, totalReceivable));
}
}
if (Objects.isNull(request.getPlannedWithhold()))
request.setPlannedWithhold(totalReceivable.subtract(totalReceived).longValueExact());
}
自定义异常类:
public class HbBusinessException extends AbstractException {
private static final long serialVersionUID = -1895606523417907168L;
@Setter
@Getter
private String code;
@Setter
@Getter
private String message;
public HbBusinessException(String code, String message) {
this.code = code;
this.message = message;
}
public HbBusinessException(ReturnCode returnCode) {
this.code = returnCode.getCode();
this.message = returnCode.getMessage();
}
public HbBusinessException(Exception e) {
super(e);
}
}
/**
* @author za-laijianbo2017年4月25日下午5:42:22
*
*/
public abstract class AbstractException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param e
*/
public AbstractException(Exception e) {
super(e);
}
/**
* @param e
*/
public AbstractException() {
}
public abstract String getCode();
public abstract String getMessage();
}