package cn.newfeifan.mall.module.product.service.skuapply;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.newfeifan.mall.module.product.controller.admin.skuapply.vo.*;
import cn.newfeifan.mall.module.product.dal.dataobject.skuapply.SkuApplyDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.product.dal.mysql.skuapply.SkuApplyMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.product.enums.ErrorCodeConstants.*;

/**
 * 商品sku申请 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class SkuApplyServiceImpl implements SkuApplyService {

    @Resource
    private SkuApplyMapper skuApplyMapper;

    @Override
    public Long createSkuApply(SkuApplySaveReqVO createReqVO) {
        // 插入
        SkuApplyDO skuApply = BeanUtils.toBean(createReqVO, SkuApplyDO.class);
        skuApplyMapper.insert(skuApply);
        // 返回
        return skuApply.getId();
    }

    @Override
    public void updateSkuApply(SkuApplySaveReqVO updateReqVO) {
        // 校验存在
        validateSkuApplyExists(updateReqVO.getId());
        // 更新
        SkuApplyDO updateObj = BeanUtils.toBean(updateReqVO, SkuApplyDO.class);
        skuApplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteSkuApply(Long id) {
        // 校验存在
        validateSkuApplyExists(id);
        // 删除
        skuApplyMapper.deleteById(id);
    }

    private void validateSkuApplyExists(Long id) {
        if (skuApplyMapper.selectById(id) == null) {
            throw exception(SKU_APPLY_NOT_EXISTS);
        }
    }

    @Override
    public SkuApplyDO getSkuApply(Long id) {
        return skuApplyMapper.selectById(id);
    }

    @Override
    public PageResult<SkuApplyDO> getSkuApplyPage(SkuApplyPageReqVO pageReqVO) {
        return skuApplyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SkuApplyDO> getSkuListBySpuId(Long spuApplyId) {
        return skuApplyMapper.selectListBySpuId(spuApplyId);
    }

}