package com.kxs.product.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.product.api.model.KxsShopGoodsPic;
import com.kxs.product.api.model.KxsShopGoodsSku;
import com.kxs.product.api.vo.kxsapp.shop.ShopGoodsDetailVO;
import com.kxs.product.api.vo.kxsapp.shop.ShopGoodsListVO;
import com.kxs.product.api.vo.kxsapp.shop.ShopGoodsSkuVO;
import com.kxs.product.api.vo.kxsapp.shop.ShopGoodsSpecVO;
import com.kxs.product.biz.mapper.KxsShopGoodsMapper;
import com.kxs.product.api.model.KxsShopGoods;
import com.kxs.product.biz.mapper.KxsShopGoodsPicMapper;
import com.kxs.product.biz.mapper.KxsShopGoodsSkuMapper;
import com.kxs.product.biz.mapper.KxsShopGoodsSpecMapper;
import com.kxs.product.biz.service.KxsShopGoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商城商品表(KxsShopGoods)表服务实现类
 *
 * @author 系统
 * @since 2023-12-20 10:09:35
 */
@Service("kxsShopGoodsService")
@RequiredArgsConstructor
public class KxsShopGoodsServiceImpl extends ServiceImpl<KxsShopGoodsMapper, KxsShopGoods> implements KxsShopGoodsService {

    private final KxsShopGoodsPicMapper kxsShopGoodsPicMapper;

    private final KxsShopGoodsSkuMapper kxsShopGoodsSkuMapper;

    private final KxsShopGoodsSpecMapper kxsShopGoodsSpecMapper;

    @Override
    public IPage<ShopGoodsListVO> getByPage(Page<ShopGoodsListVO> page, Integer colId) {

        return baseMapper.getByPage(page, colId);
    }

    @Override
    public ShopGoodsDetailVO getByGoodsDetail(Integer id) {

        ShopGoodsDetailVO goodsDetailVO = new ShopGoodsDetailVO();

        //查询商品
        KxsShopGoods kxsShopGoods = baseMapper.selectById(id);
        if(kxsShopGoods == null){
            return null;
        }
        BeanUtils.copyProperties(kxsShopGoods, goodsDetailVO);
        //查询商品图片
        List<KxsShopGoodsPic> kxsShopGoodsPics = kxsShopGoodsPicMapper
                .selectList(Wrappers.<KxsShopGoodsPic>lambdaQuery()
                        .eq(KxsShopGoodsPic::getGoodsId, id));
        for (KxsShopGoodsPic kxsShopGoodsPic : kxsShopGoodsPics) {
            goodsDetailVO.getPicList().add(kxsShopGoodsPic.getUrl());
        }

        //查询商品sku列表
        List<ShopGoodsSkuVO> skus = kxsShopGoodsSkuMapper.getByGoodsSkuList(id);
        goodsDetailVO.setSkuList(skus);

        //查询商品规格
        List<ShopGoodsSpecVO> specs = kxsShopGoodsSpecMapper.getGoodsSpecList(id);
        goodsDetailVO.setSpecList(specs);

        return goodsDetailVO;
    }











}

