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

import com.tashow.cloud.product.dto.ProdDO;
import com.tashow.cloud.product.mapper.ProdMapper;
import com.tashow.cloud.product.service.ProdService;
import com.tashow.cloud.product.vo.prod.ProdPageReqVO;
import com.tashow.cloud.product.vo.prod.ProdSaveReqVO;
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;

/**
 * 商品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ProdServiceImpl implements ProdService {

    @Resource
    private ProdMapper prodMapper;

    @Override
    public Long createProd(ProdSaveReqVO createReqVO) {
        // 插入
        ProdDO prod = BeanUtils.toBean(createReqVO, ProdDO.class);
        prodMapper.insert(prod);
        // 返回
        return prod.getProdId();
    }

    @Override
    public void updateProd(ProdSaveReqVO updateReqVO) {
        // 校验存在
        validateProdExists(updateReqVO.getProdId());
        // 更新
        ProdDO updateObj = BeanUtils.toBean(updateReqVO, ProdDO.class);
        prodMapper.updateById(updateObj);
    }

    @Override
    public void deleteProd(Long id) {
        // 校验存在
        validateProdExists(id);
        // 删除
        prodMapper.deleteById(id);
    }

    private void validateProdExists(Long id) {
        if (prodMapper.selectById(id) == null) {
            //throw exception(100, "商品不存在");
        }
    }

    @Override
    public ProdDO getProd(Long id) {
        return prodMapper.selectById(id);
    }

    @Override
    public PageResult<ProdDO> getProdPage(ProdPageReqVO pageReqVO) {
        return null;// prodMapper.getProdPage1(pageReqVO);
    }

}