1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
// 实体类
@Entity
@Data
public class Order {
private Long id;
private String orderNumber;
private LocalDateTime orderTime;
private OrderStatus status;
private BigDecimal totalAmount;
private User customer;
private List<OrderItem> items;
private Address shippingAddress;
}
@Entity
@Data
public class OrderItem {
private Long id;
private Product product;
private Integer quantity;
private BigDecimal unitPrice;
private BigDecimal subtotal;
}
// DTO类
@Data
public class OrderDTO {
private Long orderId;
private String orderNumber;
private String orderTimeStr;
private String statusName;
private BigDecimal totalAmount;
private String customerName;
private List<OrderItemDTO> items;
private String shippingAddressStr;
private Integer totalItems;
private BigDecimal averageItemPrice;
}
@Data
public class OrderItemDTO {
private String productName;
private String productCode;
private Integer quantity;
private BigDecimal unitPrice;
private BigDecimal subtotal;
}
// Mapper实现
@Mapper(
componentModel = "spring",
uses = {OrderItemMapper.class},
imports = {DateTimeFormatter.class, Collectors.class}
)
public interface OrderMapper {
@Mapping(source = "id", target = "orderId")
@Mapping(source = "orderTime", target = "orderTimeStr",
dateFormat = "yyyy-MM-dd HH:mm:ss")
@Mapping(source = "status", target = "statusName",
qualifiedByName = "mapOrderStatus")
@Mapping(source = "customer.firstName", target = "customerName",
qualifiedByName = "buildCustomerName")
@Mapping(source = "shippingAddress", target = "shippingAddressStr",
qualifiedByName = "formatAddress")
@Mapping(target = "totalItems",
expression = "java(calculateTotalItems(order.getItems()))")
@Mapping(target = "averageItemPrice",
expression = "java(calculateAveragePrice(order.getItems()))")
OrderDTO toDTO(Order order);
@Named("mapOrderStatus")
default String mapOrderStatus(OrderStatus status) {
return switch (status) {
case PENDING -> "待处理";
case CONFIRMED -> "已确认";
case SHIPPED -> "已发货";
case DELIVERED -> "已送达";
case CANCELLED -> "已取消";
};
}
@Named("buildCustomerName")
default String buildCustomerName(String firstName, String lastName) {
return (firstName != null ? firstName : "") +
(lastName != null ? " " + lastName : "");
}
@Named("formatAddress")
default String formatAddress(Address address) {
if (address == null) return null;
return String.format("%s %s, %s, %s",
address.getStreet(),
address.getCity(),
address.getState(),
address.getZipCode());
}
default Integer calculateTotalItems(List<OrderItem> items) {
return items.stream()
.mapToInt(OrderItem::getQuantity)
.sum();
}
default BigDecimal calculateAveragePrice(List<OrderItem> items) {
if (items.isEmpty()) return BigDecimal.ZERO;
BigDecimal total = items.stream()
.map(OrderItem::getUnitPrice)
.reduce(BigDecimal.ZERO, BigDecimal::add);
return total.divide(new BigDecimal(items.size()), 2, RoundingMode.HALF_UP);
}
}
@Mapper(componentModel = "spring")
public interface OrderItemMapper {
@Mapping(source = "product.name", target = "productName")
@Mapping(source = "product.code", target = "productCode")
OrderItemDTO toDTO(OrderItem item);
}
|