mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 21:49:18 +08:00
fix: 修复mongo写入订单统计时间时区问题
This commit is contained in:
+45
-2
@@ -9,19 +9,34 @@ import org.springframework.data.convert.WritingConverter;
|
|||||||
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
|
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BigDecimal 存 Decimal128,避免默认写成字符串导致 $inc / 数值运算失败。
|
* Mongo 自定义转换:
|
||||||
|
* <ul>
|
||||||
|
* <li>BigDecimal ↔ Decimal128</li>
|
||||||
|
* <li>LocalDate ↔ BSON Date:按<strong>日历日 UTC 0 点</strong>落库(仍是时间类型,可排序/范围查询;
|
||||||
|
* Compass 显示为 {@code yyyy-MM-ddT00:00:00.000Z},避免东八区把「上海 0 点」看成前一天 16:00Z)</li>
|
||||||
|
* </ul>
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class BiMongoConversionsConfig {
|
public class BiMongoConversionsConfig {
|
||||||
|
|
||||||
|
private static final ZoneId BIZ_ZONE = ZoneId.of("Asia/Shanghai");
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public MongoCustomConversions mongoCustomConversions() {
|
public MongoCustomConversions mongoCustomConversions() {
|
||||||
return new MongoCustomConversions(List.of(
|
return new MongoCustomConversions(List.of(
|
||||||
new BigDecimalToDecimal128Converter(),
|
new BigDecimalToDecimal128Converter(),
|
||||||
new Decimal128ToBigDecimalConverter()));
|
new Decimal128ToBigDecimalConverter(),
|
||||||
|
new LocalDateToUtcDateConverter(),
|
||||||
|
new DateToLocalDateConverter()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@WritingConverter
|
@WritingConverter
|
||||||
@@ -39,4 +54,32 @@ public class BiMongoConversionsConfig {
|
|||||||
return source.bigDecimalValue();
|
return source.bigDecimalValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 日历日 → UTC 午夜 Date,例如 2026-08-10 → 2026-08-10T00:00:00.000Z */
|
||||||
|
@WritingConverter
|
||||||
|
static class LocalDateToUtcDateConverter implements Converter<LocalDate, Date> {
|
||||||
|
@Override
|
||||||
|
public Date convert(LocalDate source) {
|
||||||
|
return Date.from(source.atStartOfDay(ZoneOffset.UTC).toInstant());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date → 日历日。
|
||||||
|
* 新数据:UTC 午夜 → 按 UTC 取日期;
|
||||||
|
* 旧数据:曾按 JVM/上海 0 点写入(常见为前一天 16:00Z)→ 按 Asia/Shanghai 取日期。
|
||||||
|
*/
|
||||||
|
@ReadingConverter
|
||||||
|
static class DateToLocalDateConverter implements Converter<Date, LocalDate> {
|
||||||
|
@Override
|
||||||
|
public LocalDate convert(Date source) {
|
||||||
|
Instant instant = source.toInstant();
|
||||||
|
ZonedDateTime utc = instant.atZone(ZoneOffset.UTC);
|
||||||
|
if (utc.getHour() == 0 && utc.getMinute() == 0 && utc.getSecond() == 0
|
||||||
|
&& utc.getNano() == 0) {
|
||||||
|
return utc.toLocalDate();
|
||||||
|
}
|
||||||
|
return instant.atZone(BIZ_ZONE).toLocalDate();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -351,13 +351,13 @@ public class PddOrderConvertService {
|
|||||||
item.setLineAmount(ci.getGoodsPrice().multiply(BigDecimal.valueOf(item.getQuantity())));
|
item.setLineAmount(ci.getGoodsPrice().multiply(BigDecimal.valueOf(item.getQuantity())));
|
||||||
}
|
}
|
||||||
|
|
||||||
Long productId = 0L;
|
Long productId = null;
|
||||||
Long skuId = 0L;
|
Long skuId = null;
|
||||||
String skuCode = null;
|
String skuCode = null;
|
||||||
if (StringUtils.hasText(ci.getOuterId())) {
|
if (StringUtils.hasText(ci.getOuterId())) {
|
||||||
ProductSkuPO sku = productSkuService.getByCompanyIdAndSkuCode(order.getCompanyId(), ci.getOuterId().trim());
|
ProductSkuPO sku = productSkuService.getByCompanyIdAndSkuCode(order.getCompanyId(), ci.getOuterId().trim());
|
||||||
if (sku != null) {
|
if (sku != null) {
|
||||||
productId = sku.getProductId() != null ? sku.getProductId() : 0L;
|
productId = sku.getProductId();
|
||||||
skuId = sku.getId();
|
skuId = sku.getId();
|
||||||
skuCode = sku.getSkuCode();
|
skuCode = sku.getSkuCode();
|
||||||
ProductSkuCostPO cost = productSkuCostService.getCurrentCost(sku.getId());
|
ProductSkuCostPO cost = productSkuCostService.getCurrentCost(sku.getId());
|
||||||
|
|||||||
+2
-1
@@ -34,7 +34,8 @@ public class PddOrderFullSyncTaskPO {
|
|||||||
@TableField("date_to")
|
@TableField("date_to")
|
||||||
private LocalDate dateTo;
|
private LocalDate dateTo;
|
||||||
|
|
||||||
@TableField("current_date")
|
/** MySQL 保留字 CURRENT_DATE,必须反引号转义 */
|
||||||
|
@TableField("`current_date`")
|
||||||
private LocalDate currentDate;
|
private LocalDate currentDate;
|
||||||
|
|
||||||
@TableField("total_days")
|
@TableField("total_days")
|
||||||
|
|||||||
Reference in New Issue
Block a user