package com.haha.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.common.vo.OrderItemVO;
import com.haha.entity.OrderGoods;
import com.haha.mapper.OrderGoodsMapper;
import com.haha.service.OrderGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单商品服务实现
 */
@Slf4j
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements OrderGoodsService {

    @Override
    public List<OrderGoods> getByOrderId(Long orderId) {
        if (orderId == null) {
            return new ArrayList<>();
        }
        return lambdaQuery()
                .eq(OrderGoods::getOrderId, orderId)
                .list();
    }

    @Override
    public List<OrderGoods> getByOrderNo(String orderNo) {
        if (orderNo == null || orderNo.isEmpty()) {
            return new ArrayList<>();
        }
        return lambdaQuery()
                .eq(OrderGoods::getOrderNo, orderNo)
                .list();
    }

    @Override
    public List<OrderGoods> getByActivityId(String activityId) {
        if (activityId == null || activityId.isEmpty()) {
            return new ArrayList<>();
        }
        return lambdaQuery()
                .eq(OrderGoods::getActivityId, activityId)
                .list();
    }

    @Override
    public List<OrderItemVO> getVOByOrderId(Long orderId) {
        List<OrderGoods> goodsList = getByOrderId(orderId);
        List<OrderItemVO> voList = new ArrayList<>();
        
        for (OrderGoods goods : goodsList) {
            OrderItemVO vo = new OrderItemVO();
            vo.setId(goods.getId());
            vo.setProductId(goods.getProductId());
            vo.setCode(goods.getCode());
            vo.setBarCode(goods.getBarCode());
            vo.setProductName(goods.getProductName());
            vo.setPic(goods.getPic());
            vo.setProductNum(goods.getProductNum());
            vo.setMoney(goods.getMoney());
            vo.setOriginPrice(goods.getOriginPrice());
            vo.setPrice(goods.getPrice());
            voList.add(vo);
        }
        
        return voList;
    }

    @Override
    public Map<Long, List<OrderItemVO>> getVOByOrderIds(List<Long> orderIds) {
        Map<Long, List<OrderItemVO>> result = new HashMap<>();
        if (orderIds == null || orderIds.isEmpty()) {
            return result;
        }
        List<OrderGoods> allGoods = lambdaQuery()
                .in(OrderGoods::getOrderId, orderIds)
                .list();
        if (allGoods == null || allGoods.isEmpty()) {
            return result;
        }
        for (OrderGoods goods : allGoods) {
            result.computeIfAbsent(goods.getOrderId(), k -> new ArrayList<>());
        }
        // 按 orderId 分组后，对每组做相同的数据转换（与 getVOByOrderId 逻辑一致）
        Map<Long, List<OrderGoods>> grouped = allGoods.stream()
                .collect(Collectors.groupingBy(OrderGoods::getOrderId));
        for (Map.Entry<Long, List<OrderGoods>> entry : grouped.entrySet()) {
            List<OrderItemVO> voList = entry.getValue().stream().map(goods -> {
                OrderItemVO vo = new OrderItemVO();
                vo.setId(goods.getId());
                vo.setProductId(goods.getProductId());
                vo.setCode(goods.getCode());
                vo.setBarCode(goods.getBarCode());
                vo.setProductName(goods.getProductName());
                vo.setPic(goods.getPic());
                vo.setProductNum(goods.getProductNum());
                vo.setMoney(goods.getMoney());
                vo.setOriginPrice(goods.getOriginPrice());
                vo.setPrice(goods.getPrice());
                return vo;
            }).collect(Collectors.toList());
            result.put(entry.getKey(), voList);
        }
        return result;
    }
}
