package com.yiqi.order.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.GoodsInfo;
import com.yiqi.core.service.IGoodsInfoService;
import com.yiqi.order.domain.OrderPurchaseGoods;
import com.yiqi.order.domain.OrderPurchaseGoodsItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.order.mapper.OrderPurchaseGoodsSkuMapper;
import com.yiqi.order.domain.OrderPurchaseGoodsSku;
import com.yiqi.order.service.IOrderPurchaseGoodsSkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 商品订单物料明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-20
 */
@Service
public class OrderPurchaseGoodsSkuServiceImpl extends ServiceImpl<OrderPurchaseGoodsSkuMapper,OrderPurchaseGoodsSku> implements IOrderPurchaseGoodsSkuService {

    @Autowired
    private IGoodsInfoService goodsInfoService;

    /**
     * 查询商品订单物料明细
     *
     * @param id 商品订单物料明细主键
     * @return 商品订单物料明细
     */
    @Override
    public OrderPurchaseGoodsSku selectOrderPurchaseGoodsSkuById(Long id) {
        return baseMapper.selectOrderPurchaseGoodsSkuById(id);
    }

    /**
     * 查询商品订单物料明细列表
     *
     * @param orderPurchaseGoodsSku 商品订单物料明细
     * @return 商品订单物料明细
     */
    @Override
    public List<OrderPurchaseGoodsSku> selectOrderPurchaseGoodsSkuList(OrderPurchaseGoodsSku orderPurchaseGoodsSku) {
        return baseMapper.selectOrderPurchaseGoodsSkuList(orderPurchaseGoodsSku);
    }

    private List<GoodsInfo> getGoodsInfoListByOrderPurchase(OrderPurchaseGoods orderPurchaseGoods) {
        List<OrderPurchaseGoodsSku> purchaseGoodsItems = list(new QueryWrapper<OrderPurchaseGoodsSku>().lambda()
                .eq(OrderPurchaseGoodsSku::getOrderId, orderPurchaseGoods.getId()));
        List<Long> goodIdList = new ArrayList<>(purchaseGoodsItems.stream().collect(Collectors.groupingBy(OrderPurchaseGoodsSku::getGoodsId)).keySet());
        return goodsInfoService.list(new QueryWrapper<GoodsInfo>().lambda().in(GoodsInfo::getId, goodIdList));
    }

    /**
     * 新增商品订单物料明细
     *
     * @param orderPurchaseGoodsSku 商品订单物料明细
     * @return 结果
     */
    @Override
    public int insertOrderPurchaseGoodsSku(OrderPurchaseGoodsSku orderPurchaseGoodsSku) {
        orderPurchaseGoodsSku.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertOrderPurchaseGoodsSku(orderPurchaseGoodsSku);
    }

    /**
     * 修改商品订单物料明细
     *
     * @param orderPurchaseGoodsSku 商品订单物料明细
     * @return 结果
     */
    @Override
    public int updateOrderPurchaseGoodsSku(OrderPurchaseGoodsSku orderPurchaseGoodsSku) {
        orderPurchaseGoodsSku.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateOrderPurchaseGoodsSku(orderPurchaseGoodsSku);
    }

    /**
     * 批量删除商品订单物料明细
     *
     * @param ids 需要删除的商品订单物料明细主键
     * @return 结果
     */
    @Override
    public int deleteOrderPurchaseGoodsSkuByIds(Long[] ids) {
        return baseMapper.deleteOrderPurchaseGoodsSkuByIds(ids);
    }

    /**
     * 删除商品订单物料明细信息
     *
     * @param id 商品订单物料明细主键
     * @return 结果
     */
    @Override
    public int deleteOrderPurchaseGoodsSkuById(Long id) {
        return baseMapper.deleteOrderPurchaseGoodsSkuById(id);
    }



}
