From abf325112276f36af2783011a674b6f2d53269f7 Mon Sep 17 00:00:00 2001 From: sin <2943460818@qq.com> Date: Thu, 28 Mar 2019 22:04:37 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E8=BF=94=E5=9B=9E=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/framework/util/CollectionUtil.java | 59 ++++- .../iocoder/mall/order/api/dto/OrderBO.java | 21 +- .../mall/order/api/dto/OrderItemBO.java | 207 ++++++++++++++++++ .../application/convert/OrderItemConvert.java | 6 + .../convert/OrderLogisticsConvert.java | 2 + .../mall/order/dao/OrderItemMapper.java | 13 ++ .../mall/order/service/OrderServiceImpl.java | 23 +- .../main/resources/mapper/OrderItemMapper.xml | 13 ++ 8 files changed, 337 insertions(+), 7 deletions(-) create mode 100644 order/order-service-api/src/main/java/cn/iocoder/mall/order/api/dto/OrderItemBO.java diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/CollectionUtil.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/CollectionUtil.java index 3df161759..76883dee3 100644 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/CollectionUtil.java +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/CollectionUtil.java @@ -1,9 +1,9 @@ package cn.iocoder.common.framework.util; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; +import org.springframework.util.ReflectionUtils; + +import java.lang.reflect.Field; +import java.util.*; public class CollectionUtil { @@ -12,7 +12,56 @@ public class CollectionUtil { } public static Set asSet(T... objs) { - return new HashSet<>(Arrays.asList(objs)); + return new HashSet<>(Arrays.asList(objs)); } + + /** + * 创建指定属性为KEY, objs的每个元素为值的Multimap的Map集合。 + * + * @param objs 数组 + * @param keyClazz 值类型,即{@code property}的类型 + * @param valClazz 值类型 + * @param property 属性名 + * @param 泛型 + * @param 泛型 + * @return 指定属性的Map集合 + */ + @SuppressWarnings("unchecked") + public static Map> buildMultimap(List objs, Class keyClazz, Class valClazz, + String property) { + if (objs.isEmpty()) { + return Collections.EMPTY_MAP; + } + Map> results = new HashMap<>(objs.size()); + try { + Field field = getField(objs, property); + for (V obj : objs) { + K key = (K) field.get(obj); + List value = results.get(key); + if (value == null) { + results.put(key, value = new ArrayList<>()); + } + value.add(obj); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + return results; + } + + /** + * 获取 object 属性 field + * + * @param objs 对象数组 + * @param property 属性 + * @return 对象元素里的指定属性Field, 并设置该field可以被访问 + */ + public static Field getField(List objs, String property) { + Field field = ReflectionUtils.findField(objs.get(0).getClass(), property); + if (!field.isAccessible()) { + field.setAccessible(true); + } + return field; + } } \ No newline at end of file diff --git a/order/order-service-api/src/main/java/cn/iocoder/mall/order/api/dto/OrderBO.java b/order/order-service-api/src/main/java/cn/iocoder/mall/order/api/dto/OrderBO.java index dfc2666b4..eef0fa82e 100644 --- a/order/order-service-api/src/main/java/cn/iocoder/mall/order/api/dto/OrderBO.java +++ b/order/order-service-api/src/main/java/cn/iocoder/mall/order/api/dto/OrderBO.java @@ -2,6 +2,7 @@ package cn.iocoder.mall.order.api.dto; import java.io.Serializable; import java.util.Date; +import java.util.List; /** * 订单 page @@ -79,9 +80,17 @@ public class OrderBO implements Serializable { */ private String remark; + /// + /// 关联信息 + + /** + * orderItem + */ + private List orderItems; + @Override public String toString() { - return "OrderPageBO{" + + return "OrderBO{" + "id=" + id + ", userId=" + userId + ", orderLogisticsId=" + orderLogisticsId + @@ -94,6 +103,7 @@ public class OrderBO implements Serializable { ", hasReturnExchange=" + hasReturnExchange + ", status=" + status + ", remark='" + remark + '\'' + + ", orderItems=" + orderItems + '}'; } @@ -204,4 +214,13 @@ public class OrderBO implements Serializable { this.remark = remark; return this; } + + public List getOrderItems() { + return orderItems; + } + + public OrderBO setOrderItems(List orderItems) { + this.orderItems = orderItems; + return this; + } } diff --git a/order/order-service-api/src/main/java/cn/iocoder/mall/order/api/dto/OrderItemBO.java b/order/order-service-api/src/main/java/cn/iocoder/mall/order/api/dto/OrderItemBO.java new file mode 100644 index 000000000..3852716d8 --- /dev/null +++ b/order/order-service-api/src/main/java/cn/iocoder/mall/order/api/dto/OrderItemBO.java @@ -0,0 +1,207 @@ +package cn.iocoder.mall.order.api.dto; + +import java.io.Serializable; +import java.util.Date; + +/** + * 订单 item + * + * @author Sin + * @time 2019-03-28 21:11 + */ +public class OrderItemBO implements Serializable { + + /** + * 编号 + */ + private Integer id; + /** + * 订单编号 + */ + private Integer orderId; + /** + * 订单号 + */ + private String orderNo; + /** + * 商品编号 + */ + private Integer skuId; + /** + * 数量 + */ + private Integer quantity; + /** + * 金额(分) + */ + private Integer price; + + /// + /// 时间信息 + + /** + * 付款时间 + */ + private Date paymentTime; + /** + * 发货时间 + */ + private Date deliveryTime; + /** + * 收货时间 + */ + private Date receiverTime; + /** + * 成交时间 + */ + private Date closingTime; + + /// + /// 其他 + + /** + * 是否退货 + * + * - 1、没有 + * - 2、换货 + * - 3、退货 + * - 4、换货 + 退货 + */ + private Integer hasReturnExchange; + /** + * 状态 + * + * - 1、待付款 + * - 2、待发货 + * - 3、已发货 + * - 4、已完成 + * - 5、已关闭 + */ + private Integer status; + + @Override + public String toString() { + return "OrderItemDO{" + + "id=" + id + + ", orderId=" + orderId + + ", orderNo='" + orderNo + '\'' + + ", skuId='" + skuId + '\'' + + ", quantity=" + quantity + + ", price=" + price + + ", paymentTime=" + paymentTime + + ", deliveryTime=" + deliveryTime + + ", receiverTime=" + receiverTime + + ", closingTime=" + closingTime + + ", hasReturnExchange=" + hasReturnExchange + + ", status=" + status + + '}'; + } + + public Integer getId() { + return id; + } + + public OrderItemBO setId(Integer id) { + this.id = id; + return this; + } + + public Integer getOrderId() { + return orderId; + } + + public OrderItemBO setOrderId(Integer orderId) { + this.orderId = orderId; + return this; + } + + public String getOrderNo() { + return orderNo; + } + + public OrderItemBO setOrderNo(String orderNo) { + this.orderNo = orderNo; + return this; + } + + public Integer getSkuId() { + return skuId; + } + + public OrderItemBO setSkuId(Integer skuId) { + this.skuId = skuId; + return this; + } + + public Integer getQuantity() { + return quantity; + } + + public OrderItemBO setQuantity(Integer quantity) { + this.quantity = quantity; + return this; + } + + public Integer getPrice() { + return price; + } + + public OrderItemBO setPrice(Integer price) { + this.price = price; + return this; + } + + public Date getPaymentTime() { + return paymentTime; + } + + public OrderItemBO setPaymentTime(Date paymentTime) { + this.paymentTime = paymentTime; + return this; + } + + public Date getDeliveryTime() { + return deliveryTime; + } + + public OrderItemBO setDeliveryTime(Date deliveryTime) { + this.deliveryTime = deliveryTime; + return this; + } + + public Date getReceiverTime() { + return receiverTime; + } + + public OrderItemBO setReceiverTime(Date receiverTime) { + this.receiverTime = receiverTime; + return this; + } + + public Date getClosingTime() { + return closingTime; + } + + public OrderItemBO setClosingTime(Date closingTime) { + this.closingTime = closingTime; + return this; + } + + public Integer getHasReturnExchange() { + return hasReturnExchange; + } + + public OrderItemBO setHasReturnExchange(Integer hasReturnExchange) { + this.hasReturnExchange = hasReturnExchange; + return this; + } + + public Integer getStatus() { + return status; + } + + public OrderItemBO setStatus(Integer status) { + this.status = status; + return this; + } +} diff --git a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/application/convert/OrderItemConvert.java b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/application/convert/OrderItemConvert.java index e06c50883..f3a01e0cf 100644 --- a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/application/convert/OrderItemConvert.java +++ b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/application/convert/OrderItemConvert.java @@ -1,8 +1,10 @@ package cn.iocoder.mall.order.application.convert; import cn.iocoder.mall.order.api.dto.OrderCreateItemDTO; +import cn.iocoder.mall.order.api.dto.OrderItemBO; import cn.iocoder.mall.order.api.dto.OrderItemUpdateDTO; import cn.iocoder.mall.order.dataobject.OrderItemDO; +import org.mapstruct.Mapper; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; @@ -14,6 +16,7 @@ import java.util.List; * @author Sin * @time 2019-03-23 14:34 */ +@Mapper public interface OrderItemConvert { OrderItemConvert INSTANCE = Mappers.getMapper(OrderItemConvert.class); @@ -23,4 +26,7 @@ public interface OrderItemConvert { @Mappings({}) List convert(List orderCreateItemDTOList); + + @Mappings({}) + List convertOrderItemDO(List orderItemDOList); } diff --git a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/application/convert/OrderLogisticsConvert.java b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/application/convert/OrderLogisticsConvert.java index 3143dc6ae..a61e61b03 100644 --- a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/application/convert/OrderLogisticsConvert.java +++ b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/application/convert/OrderLogisticsConvert.java @@ -3,6 +3,7 @@ package cn.iocoder.mall.order.application.convert; import cn.iocoder.mall.order.api.dto.OrderCreateDTO; import cn.iocoder.mall.order.api.dto.OrderLogisticsUpdateDTO; import cn.iocoder.mall.order.dataobject.OrderLogisticsDO; +import org.mapstruct.Mapper; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; @@ -12,6 +13,7 @@ import org.mapstruct.factory.Mappers; * @author Sin * @time 2019-03-23 14:39 */ +@Mapper public interface OrderLogisticsConvert { OrderLogisticsConvert INSTANCE = Mappers.getMapper(OrderLogisticsConvert.class); diff --git a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/dao/OrderItemMapper.java b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/dao/OrderItemMapper.java index 29bca50e3..016f8f1a9 100644 --- a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/dao/OrderItemMapper.java +++ b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/dao/OrderItemMapper.java @@ -5,6 +5,7 @@ import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import javax.validation.constraints.NotNull; +import java.util.Collection; import java.util.List; /** @@ -41,6 +42,18 @@ public interface OrderItemMapper { OrderItemDO orderItemDO ); + /** + * 查询 - 根据 orderIds 和 status + * + * @param orderIds + * @param status + * @return + */ + List selectByOrderIdsAndStatus( + @Param("orderIds") Collection orderIds, + @Param("status") Integer status + ); + /** * 查询 - 根据 orderId 下的 item * diff --git a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/service/OrderServiceImpl.java b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/service/OrderServiceImpl.java index fac3de9f2..f7cfac001 100644 --- a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/service/OrderServiceImpl.java +++ b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/service/OrderServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.mall.order.service; import cn.iocoder.common.framework.constant.DeletedStatusEnum; +import cn.iocoder.common.framework.util.CollectionUtil; import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.order.OrderCommon; @@ -53,13 +54,33 @@ public class OrderServiceImpl implements OrderService { return CommonResult.success(new OrderPageBO().setTotal(0)); } + // 获取订单数据 List orderDOList = orderMapper.selectPage(orderQueryDTO); + + // 获取订单 id + Set orderIds = orderDOList.stream().map(orderDO -> orderDO.getId()).collect(Collectors.toSet()); + + // 获取 订单的 items + List orderItemDOList = orderItemMapper + .selectByOrderIdsAndStatus(orderIds, DeletedStatusEnum.DELETED_NO.getValue()); + + List orderItemBOList = OrderItemConvert.INSTANCE.convertOrderItemDO(orderItemDOList); + Map> orderItemBOMultimap = CollectionUtil + .buildMultimap(orderItemBOList, Integer.class, OrderItemBO.class, "orderId"); + + // 转换 orderDO 为 OrderBO,并设置 item List orderPageBOList = OrderConvert.INSTANCE.convertPageBO(orderDOList); + List result = orderPageBOList.stream().map(orderBO -> { + if (orderItemBOMultimap.containsKey(orderBO.getId())) { + orderBO.setOrderItems(orderItemBOMultimap.get(orderBO.getId())); + } + return orderBO; + }).collect(Collectors.toList()); return CommonResult.success( new OrderPageBO() .setTotal(totalCount) - .setOrders(orderPageBOList) + .setOrders(result) ); } diff --git a/order/order-service-impl/src/main/resources/mapper/OrderItemMapper.xml b/order/order-service-impl/src/main/resources/mapper/OrderItemMapper.xml index 861e2c32e..aebee0fb1 100644 --- a/order/order-service-impl/src/main/resources/mapper/OrderItemMapper.xml +++ b/order/order-service-impl/src/main/resources/mapper/OrderItemMapper.xml @@ -110,4 +110,17 @@ AND order_id = #{orderId} + + +