package com.wzj.soopin.goods.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.soopin.goods.domain.bo.SkuBo;
import com.wzj.soopin.goods.domain.entity.Product;
import com.wzj.soopin.goods.domain.entity.Sku;
 import com.wzj.soopin.goods.domain.vo.SkuVO;
import com.wzj.soopin.goods.mapper.ProductMapper;
import com.wzj.soopin.goods.mapper.SkuMapper;
import com.wzj.soopin.goods.service.SkuService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;


/**
 * sku信息Service业务层处理
 *
 *
 * @author zcc
 */
@Service
@RequiredArgsConstructor
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

   private final SkuMapper skuMapper;

   private final ProductMapper productMapper;

    public IPage<SkuVO> getList(SkuBo query, Page<Sku> page) {
        Long tenantId = Long.valueOf(LoginHelper.getTenantId());
        return skuMapper.getlist(page,query,tenantId);
    }

    public SkuVO selectSkuByid(Long id) {
        return skuMapper.selectSkuByid(id);
    }

    @Override
    public void add(Sku sku) {
        Long productId = sku.getProductId();
        Product product = productMapper.selectById(productId);
        Assert.notNull(product, () -> new ServiceException("商品信息不存在"));
        sku.setTenantId(product.getTenantId());
        skuMapper.insert(sku);
    }
}
