package com.qs.mp.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qs.mp.user.domain.UserDeliverOrderItem;
import com.qs.mp.user.domain.vo.OrderDeliverItemVO;
import com.qs.mp.user.domain.vo.UserDeliverOrderItemVO;
import com.qs.mp.user.mapper.UserDeliverOrderItemMapper;
import com.qs.mp.user.service.IUserDeliverOrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户提货订单明细 服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-03-07
 */
@Service
public class UserDeliverOrderItemServiceImpl extends ServiceImpl<UserDeliverOrderItemMapper, UserDeliverOrderItem> implements IUserDeliverOrderItemService {

	@Override
	public List<OrderDeliverItemVO> selectOrderDeliverItemVOList(String orderId) {
		return getBaseMapper().selectOrderDeliverItemVOList(orderId);
	}

	@Override
	public List<UserDeliverOrderItemVO> selectUserDeliverOrderItemVOList(String orderId) {
		return getBaseMapper().selectUserDeliverOrderItemVOList(orderId);
	}

	@Override
	public List<UserDeliverOrderItemVO> selectUserDeliverOrderItemVOListByQueryWrapper(QueryWrapper<UserDeliverOrderItemVO> queryWrapper) {
		return getBaseMapper().selectUserDeliverOrderItemVOListByQueryWrapper(queryWrapper);
	}

	@Override
	public List<OrderDeliverItemVO> getDeliverItemList(String orderId, List<UserDeliverOrderItemVO> orderItems) {
		List<OrderDeliverItemVO> itemList = new ArrayList<OrderDeliverItemVO>();


		// 1.查询物流公司信息
		itemList = selectOrderDeliverItemVOList(orderId);

		for (OrderDeliverItemVO itemVO : itemList) {
			if (StringUtils.isBlank(itemVO.getDeliveryFlowId()) || itemVO.getDeliveryId() == null) {
				itemVO.setDeliveryFlowId("");
				itemVO.setDeliveryId(-1L);
			}

			for (UserDeliverOrderItemVO item : orderItems) {

				if (item.getDeliveryTime() == null) {
					continue;
				}

				if (StringUtils.isBlank(item.getDeliveryFlowId()) || item.getDeliveryId() == null) {
					item.setDeliveryFlowId("");
					item.setDeliveryId(-1L);
				}


				if (itemVO.getDeliveryFlowId().equals(item.getDeliveryFlowId()) &&
						itemVO.getDeliveryId().equals(item.getDeliveryId()) &&
						itemVO.getDeliveryTime().equals(item.getDeliveryTime())) {
					UserDeliverOrderItemVO flowItem = new UserDeliverOrderItemVO();
					BeanUtils.copyProperties(item, flowItem);
					itemVO.getItems().add(flowItem);
				}

			}
		}
		return itemList;
	}
}
