字段类型问题
This commit is contained in:
@@ -78,19 +78,21 @@ public class ShopProdStatistic implements Serializable {
|
||||
* @return true=有效,false=无效
|
||||
*/
|
||||
public boolean isValid() {
|
||||
// 1. 校验所有字段是否为 null
|
||||
if (saleCount == null || saleAmount == null || refundCount == null || refundAmount == null) {
|
||||
return false;
|
||||
}
|
||||
// 2. 校验所有字段是否大于 0(BigDecimal 需用 compareTo 比较,不能直接用 >)
|
||||
if (saleCount.compareTo(BigDecimal.ZERO) <= 0 ||
|
||||
saleAmount.compareTo(BigDecimal.ZERO) <= 0 ||
|
||||
refundCount.compareTo(BigDecimal.ZERO) <= 0 ||
|
||||
refundAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
||||
return false;
|
||||
}
|
||||
// 3. 所有条件满足,返回 true
|
||||
return true;
|
||||
// 定义一个方法,统一校验“字段非空且大于0”
|
||||
// 避免重复代码,提高可读性
|
||||
return isPositive(saleCount) ||
|
||||
isPositive(saleAmount) ||
|
||||
isPositive(refundCount) ||
|
||||
isPositive(refundAmount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验 BigDecimal 字段是否非空且大于0
|
||||
* @param value 待校验字段
|
||||
* @return true=非空且大于0,false=否则
|
||||
*/
|
||||
private boolean isPositive(BigDecimal value) {
|
||||
// 先判断是否非空,再用 compareTo 比较是否大于0(compareTo 返回 >0 表示当前值大)
|
||||
return value != null && value.compareTo(BigDecimal.ZERO) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -42,8 +43,8 @@ public class CountPayTypeVo {
|
||||
/**
|
||||
* 合并实时数据和历史统计数据
|
||||
*/
|
||||
public static List<CountPayTypeVo> mergePayTypeData(Map<String, Integer> realTimeData,
|
||||
Map<String, Integer> historyData) {
|
||||
public static List<CountPayTypeVo> mergePayTypeData(Map<String, BigDecimal> realTimeData,
|
||||
Map<String, BigDecimal> historyData) {
|
||||
|
||||
List<CountPayTypeVo> result = new ArrayList<>();
|
||||
|
||||
@@ -51,13 +52,13 @@ public class CountPayTypeVo {
|
||||
String payCode = entry.getKey();
|
||||
String payName = entry.getValue();
|
||||
|
||||
Integer realTimeCount = getSafeValue(realTimeData, payCode);
|
||||
Integer historyCount = getSafeValue(historyData, payCode);
|
||||
BigDecimal realTimeCount = getSafeValue(realTimeData, payCode);
|
||||
BigDecimal historyCount = getSafeValue(historyData, payCode);
|
||||
|
||||
int totalCount = (realTimeCount != null ? realTimeCount : 0)
|
||||
+ (historyCount != null ? historyCount : 0);
|
||||
BigDecimal totalCount = (realTimeCount != null ? realTimeCount : BigDecimal.ZERO)
|
||||
.add(historyCount != null ? historyCount : BigDecimal.ZERO);
|
||||
|
||||
result.add(new CountPayTypeVo(totalCount, payName));
|
||||
result.add(new CountPayTypeVo(totalCount.intValue(), payName));
|
||||
|
||||
}
|
||||
|
||||
@@ -67,7 +68,7 @@ public class CountPayTypeVo {
|
||||
/**
|
||||
* 安全获取Map中的值,处理null情况
|
||||
*/
|
||||
private static Integer getSafeValue(Map<String, Integer> data, String key) {
|
||||
private static BigDecimal getSafeValue(Map<String, BigDecimal> data, String key) {
|
||||
if (data == null || data.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user