package com.seer.teach.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.exception.CommonException;
import com.seer.teach.mall.entity.OrderGoodsEntity;
import com.seer.teach.mall.mapper.OrderGoodsMapper;
import com.seer.teach.mall.service.IOrderGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Captain
 * @since 2025-06-10
 */
@Slf4j
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoodsEntity> implements IOrderGoodsService {

    @Override
    public List<OrderGoodsEntity> listByOrderId(Integer orderId) {
        return super.list(new LambdaQueryWrapper<>(OrderGoodsEntity.class).eq(OrderGoodsEntity::getOrderId, orderId));
    }

    @Override
    public List<OrderGoodsEntity> listByChildOrderId(Integer orderChildId) {
        return super.list(new LambdaQueryWrapper<>(OrderGoodsEntity.class).eq(OrderGoodsEntity::getChildOrderId, orderChildId));
    }

    @Override
    public List<OrderGoodsEntity> getOrderGoodsByOrderIds(Set<Integer> orderIds) {
        return super.list(new LambdaQueryWrapper<OrderGoodsEntity>().in(OrderGoodsEntity::getOrderId, orderIds));
    }

    @Override
    public List<OrderGoodsEntity> getOrderGoodsByOrderChildIds(Set<Integer> orderChildrenIds) {
        return super.list(new LambdaQueryWrapper<OrderGoodsEntity>().in(OrderGoodsEntity::getChildOrderId, orderChildrenIds));
    }

    @Override
    public boolean deleteOrderGoodsByOrderId(Integer orderId) {
        List<OrderGoodsEntity> orderGoodsList = this.listByOrderId(orderId);
        Set<Integer> orderGoodIds = orderGoodsList.stream().map(OrderGoodsEntity::getId).collect(Collectors.toSet());
        boolean removeGoodItemResult = super.removeByIds(orderGoodIds);
        if (!removeGoodItemResult) {
            log.error("订单商品删除失败，订单ID: {}", orderId);
            throw new CommonException(ResultCodeEnum.ORDER_NON_DELETE);
        }
        return true;
    }

    @Override
    public boolean deleteOrderGoodsByOrderChildId(Integer orderChildId) {
        List<OrderGoodsEntity> orderGoodsList = this.listByChildOrderId(orderChildId);
        Set<Integer> orderGoodIds = orderGoodsList.stream().map(OrderGoodsEntity::getId).collect(Collectors.toSet());
        boolean removeGoodItemResult = super.removeByIds(orderGoodIds);
        if (!removeGoodItemResult) {
            log.warn("订单商品删除失败，子订单ID：{}",  orderChildId);
            throw new CommonException(ResultCodeEnum.ORDER_NON_DELETE);
        }
        return true;
    }

    @Override
    public OrderGoodsEntity getOneByChildOrderIdAndSkuId(Integer childOrderId, Integer skuId) {
        return super.getOne(new LambdaQueryWrapper<>(OrderGoodsEntity.class).eq(OrderGoodsEntity::getChildOrderId, childOrderId).eq(OrderGoodsEntity::getSkuId, skuId));
    }
}
