package com.tashow.cloud.product.service.impl;

import com.tashow.cloud.product.dto.SkuDO;
import com.tashow.cloud.product.mapper.SkuMapper;
import com.tashow.cloud.product.service.SkuService;
import com.tashow.cloud.product.vo.sku.SkuPageReqVO;
import com.tashow.cloud.product.vo.sku.SkuSaveReqVO;
import com.tashow.cloud.productapi.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.pojo.PageParam;
import com.tashow.cloud.common.util.object.BeanUtils;


import static com.tashow.cloud.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 单品SKU Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SkuServiceImpl implements SkuService {

    @Resource
    private SkuMapper skuMapper;

    @Override
    public Long createSku(SkuSaveReqVO createReqVO) {
        // 插入
        SkuDO sku = BeanUtils.toBean(createReqVO, SkuDO.class);
        skuMapper.insert(sku);
        // 返回
        return sku.getSkuId();
    }

    @Override
    public void updateSku(SkuSaveReqVO updateReqVO) {
        // 校验存在
        validateSkuExists(updateReqVO.getSkuId());
        // 更新
        SkuDO updateObj = BeanUtils.toBean(updateReqVO, SkuDO.class);
        skuMapper.updateById(updateObj);
    }

    @Override
    public void deleteSku(Long id) {
        // 校验存在
        validateSkuExists(id);
        // 删除
        skuMapper.deleteById(id);
    }

    private void validateSkuExists(Long id) {
        if (skuMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.SKU_NOT_EXISTS);
        }
    }

    @Override
    public SkuDO getSku(Long id) {
        return skuMapper.selectById(id);
    }

    @Override
    public PageResult<SkuDO> getSkuPage(SkuPageReqVO pageReqVO) {
        return null;
    }

}