字段类型问题

This commit is contained in:
2025-11-24 14:23:52 +08:00
parent 97b03a8495
commit c8b9a82d33
4 changed files with 28 additions and 23 deletions

View File

@@ -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. 校验所有字段是否大于 0BigDecimal 需用 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=非空且大于0false=否则
*/
private boolean isPositive(BigDecimal value) {
// 先判断是否非空,再用 compareTo 比较是否大于0compareTo 返回 >0 表示当前值大)
return value != null && value.compareTo(BigDecimal.ZERO) > 0;
}
}

View File

@@ -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;
}

View File

@@ -119,7 +119,7 @@ public interface ShopOrderStatisticMapper extends BaseMapper<ShopOrderStatistic>
" shop_id = #{shopId} " +
"and trade_day = #{tradeDay} " +
"and paid_time is not null ")
Map<String, Integer> getOnlinePayTypeDate(Long shopId, LocalDate tradeDay);
Map<String, BigDecimal> getOnlinePayTypeDate(Long shopId, LocalDate tradeDay);
/**
* 订单支付方式统计 按日期范围查询
@@ -136,7 +136,7 @@ public interface ShopOrderStatisticMapper extends BaseMapper<ShopOrderStatistic>
" WHERE shop_id = #{shopId} " +
" AND statistic_date >= #{start} " +
" AND statistic_date <= #{end} ")
Map<String, Integer> getPayTypeDateRangeRaw(Long shopId, LocalDate start, LocalDate end);
Map<String, BigDecimal> getPayTypeDateRangeRaw(Long shopId, LocalDate start, LocalDate end);
//*********************以下为日常统计********************************************************************************

View File

@@ -6,6 +6,8 @@
<select id="selectProStatByDay" resultType="com.czg.order.entity.ShopProdStatistic">
SELECT
#{shopId} as shopId,
#{day} as createDay,
CASE WHEN detail.is_temporary = 1 THEN -1 ELSE detail.product_id END AS prodId,
CASE WHEN detail.is_temporary = 1 THEN '临时菜' ELSE prod.name END AS productName,
sum(detail.num-detail.return_num) as saleCount,