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.*;
import cn.iocoder.yudao.module.order.convert.orderConsignor.OrderConsignorConvert;
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.orderConsignor.OrderConsignorDO;
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.OrderConsignorService;
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.Objects;
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;
    @Resource
    private OrderConsignorService orderConsignorService;

    @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);
        }
        // 设置发货人信息
        OrderConsignorDO orderConsignorDO =
                orderConsignorService.getOne(new LambdaQueryWrapper<OrderConsignorDO>().eq(OrderConsignorDO::getOrderId, orderDO.getOrderId()).orderByDesc(OrderConsignorDO::getId).last("limit 1"));
        if (Objects.nonNull(orderConsignorDO)) {
            OrderConsignorApiDO orderConsignorApiDO = new OrderConsignorApiDO();
            BeanUtils.copyProperties(orderConsignorDO, orderConsignorApiDO);
            orderRespDTO.setOrderConsignorDO(orderConsignorApiDO);
        }
        // 设置收货人信息
        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;
    }
}
