- 优化订单列表返回结构

This commit is contained in:
sin 2019-03-28 22:04:37 +08:00
parent 1900ccafe2
commit abf3251122
8 changed files with 337 additions and 7 deletions

View File

@ -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 {
@ -15,4 +15,53 @@ public class CollectionUtil {
return new HashSet<>(Arrays.asList(objs));
}
/**
* 创建指定属性为KEY, objs的每个元素为值的Multimap的Map集合
*
* @param objs 数组
* @param keyClazz 值类型{@code property}的类型
* @param valClazz 值类型
* @param property 属性名
* @param <K> 泛型
* @param <V> 泛型
* @return 指定属性的Map集合
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, List<V>> buildMultimap(List<V> objs, Class<K> keyClazz, Class<V> valClazz,
String property) {
if (objs.isEmpty()) {
return Collections.EMPTY_MAP;
}
Map<K, List<V>> results = new HashMap<>(objs.size());
try {
Field field = getField(objs, property);
for (V obj : objs) {
K key = (K) field.get(obj);
List<V> 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;
}
}

View File

@ -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<OrderItemBO> 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<OrderItemBO> getOrderItems() {
return orderItems;
}
public OrderBO setOrderItems(List<OrderItemBO> orderItems) {
this.orderItems = orderItems;
return this;
}
}

View File

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

View File

@ -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<OrderItemDO> convert(List<OrderCreateItemDTO> orderCreateItemDTOList);
@Mappings({})
List<OrderItemBO> convertOrderItemDO(List<OrderItemDO> orderItemDOList);
}

View File

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

View File

@ -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<OrderItemDO> selectByOrderIdsAndStatus(
@Param("orderIds") Collection<Integer> orderIds,
@Param("status") Integer status
);
/**
* 查询 - 根据 orderId 下的 item
*

View File

@ -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<OrderDO> orderDOList = orderMapper.selectPage(orderQueryDTO);
// 获取订单 id
Set<Integer> orderIds = orderDOList.stream().map(orderDO -> orderDO.getId()).collect(Collectors.toSet());
// 获取 订单的 items
List<OrderItemDO> orderItemDOList = orderItemMapper
.selectByOrderIdsAndStatus(orderIds, DeletedStatusEnum.DELETED_NO.getValue());
List<OrderItemBO> orderItemBOList = OrderItemConvert.INSTANCE.convertOrderItemDO(orderItemDOList);
Map<Integer, List<OrderItemBO>> orderItemBOMultimap = CollectionUtil
.buildMultimap(orderItemBOList, Integer.class, OrderItemBO.class, "orderId");
// 转换 orderDO OrderBO并设置 item
List<OrderBO> orderPageBOList = OrderConvert.INSTANCE.convertPageBO(orderDOList);
List<OrderBO> 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)
);
}

View File

@ -110,4 +110,17 @@
AND order_id = #{orderId}
</if>
</select>
<!--
查询 - 根据 orderIds 和 status
-->
<select id="selectByOrderIdsAndStatus" resultType="cn.iocoder.mall.order.dataobject.OrderItemDO">
SELECT * FROM `order_item`
WHERE `status` = #{status}
AND `order_id`
IN
<foreach collection="orderIds" index="orderId" open="(" close=")" separator=",">
#{orderId}
</foreach>
</select>
</mapper>