package cn.iocoder.yudao.module.order.api;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.boot.module.order.api.OrderApi;
import cn.iocoder.boot.module.order.api.dto.OrderConsigneeApiDO;
import cn.iocoder.boot.module.order.api.dto.OrderItemRespDTO;
import cn.iocoder.boot.module.order.api.dto.OrderObjectiveApiDO;
import cn.iocoder.boot.module.order.api.dto.OrderRespDTO;
import cn.iocoder.yudao.module.order.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderConsignee.OrderConsigneeDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderItem.OrderItemDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderObjective.OrderObjectiveDO;
import cn.iocoder.yudao.module.order.service.order.OrderConsigneeService;
import cn.iocoder.yudao.module.order.service.order.OrderObjectiveService;
import cn.iocoder.yudao.module.order.service.order.OrderService;
import cn.iocoder.yudao.module.order.service.order.impl.OrderItemServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Validated
public class OrderApiImpl implements OrderApi {
    @Resource
    private OrderService orderService;
    @Resource
    private OrderObjectiveService objectiveService;
    @Resource
    private OrderConsigneeService orderConsigneeService;
    @Resource
    private OrderItemServiceImpl orderItemService;

    @Override
    public OrderRespDTO getOrder(Long id, String orderNo) {
        OrderDO orderDO;
        if (id != null) {
            orderDO = orderService.getById(id);
        } else {
            orderDO = orderService.selectOne(new LambdaQueryWrapper<OrderDO>().eq(OrderDO::getOrderNo, orderNo).last("limit 1"));
        }
        if (orderDO == null) {
            return null;
        }
        OrderRespDTO orderRespDTO = new OrderRespDTO();
        BeanUtils.copyProperties(orderDO, orderRespDTO);

        // 设置目的地信息
        OrderObjectiveDO orderObjectiveDO = objectiveService.getByOrderId(orderDO.getOrderId());
        if (orderObjectiveDO != null) {
            OrderObjectiveApiDO orderObjectiveApiDO = new OrderObjectiveApiDO();
            BeanUtils.copyProperties(orderObjectiveDO, orderObjectiveApiDO);
            orderRespDTO.setOrderObjectiveDO(orderObjectiveApiDO);
        }

        // 设置收货人信息
        OrderConsigneeDO orderConsigneeByOrderId = orderConsigneeService.getOrderConsigneeByOrderId(orderDO.getOrderId());
        if (orderConsigneeByOrderId != null) {
            OrderConsigneeApiDO orderConsigneeApiDO = new OrderConsigneeApiDO();
            BeanUtils.copyProperties(orderConsigneeByOrderId, orderConsigneeApiDO);
            orderRespDTO.setOrderConsigneeDO(orderConsigneeApiDO);
        }

        return orderRespDTO;
    }

    @Override
    public boolean isFirstOrder(Long userId) {
        Long count = orderService.selectCount(new LambdaQueryWrapper<OrderDO>().eq(OrderDO::getUserId, userId));
        return count <= 0;
    }

    @Override
    public List<OrderItemRespDTO> getOrderItemsByOrderId(Long orderId) {
        List<OrderItemDO> orderItemDOS = orderItemService.selectList("order_id", orderId);
        if (CollectionUtil.isNotEmpty(orderItemDOS)) {
            return orderItemDOS.stream().map(orderItemDO -> new OrderItemRespDTO(orderItemDO.getChargeWeight(), orderItemDO.getChargeVolume())).collect(Collectors.toList());
        }
        return null;
    }
}
