package com.yc.ship.module.product.service.productSaleChannel;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.product.controller.admin.productSaleChannel.vo.ProductSaleChannelPageReqVO;
import com.yc.ship.module.product.controller.admin.productSaleChannel.vo.ProductSaleChannelSaveReqVO;
import com.yc.ship.module.product.dal.dataobject.productSaleChannel.ProductSaleChannelDO;
import com.yc.ship.module.product.dal.mysql.productSaleChannel.ProductSaleChannelMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.product.enums.ErrorCodeConstants.SALE_CHANNEL_NOT_EXISTS;


/**
 * 产品销售渠道 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ProductSaleChannelServiceImpl implements ProductSaleChannelService {

    @Resource
    private ProductSaleChannelMapper saleChannelMapper;

    @Override
    public Long createSaleChannel(ProductSaleChannelSaveReqVO createReqVO) {
        // 插入
        ProductSaleChannelDO saleChannel = BeanUtils.toBean(createReqVO, ProductSaleChannelDO.class);
        saleChannelMapper.insert(saleChannel);
        // 返回
        return saleChannel.getId();
    }

    @Override
    public void updateSaleChannel(ProductSaleChannelSaveReqVO updateReqVO) {
        // 校验存在
        validateSaleChannelExists(updateReqVO.getId());
        // 更新
        ProductSaleChannelDO updateObj = BeanUtils.toBean(updateReqVO, ProductSaleChannelDO.class);
        saleChannelMapper.updateById(updateObj);
    }

    @Override
    public void deleteSaleChannel(Long id) {
        // 校验存在
        validateSaleChannelExists(id);
        // 删除
        saleChannelMapper.deleteById(id);
    }

    private void validateSaleChannelExists(Long id) {
        if (saleChannelMapper.selectById(id) == null) {
            throw exception(SALE_CHANNEL_NOT_EXISTS);
        }
    }

    @Override
    public ProductSaleChannelDO getSaleChannel(Long id) {
        return saleChannelMapper.selectById(id);
    }

    @Override
    public PageResult<ProductSaleChannelDO> getSaleChannelPage(ProductSaleChannelPageReqVO pageReqVO) {
        return saleChannelMapper.selectPage(pageReqVO);
    }

}