package cn.iocoder.yudao.module.member.service.combo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.combo.vo.ComboPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.combo.vo.ComboSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.combo.ComboDO;
import cn.iocoder.yudao.module.member.dal.mysql.combo.ComboMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.COMBO_NOT_EXISTS;

/**
 * 套餐 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ComboServiceImpl implements ComboService {

    @Resource
    private ComboMapper comboMapper;

    @Override
    public Long createCombo(ComboSaveReqVO createReqVO) {
        // 插入
        ComboDO combo = BeanUtils.toBean(createReqVO, ComboDO.class);
        comboMapper.insert(combo);
        // 返回
        return combo.getId();
    }

    @Override
    public void updateCombo(ComboSaveReqVO updateReqVO) {
        // 校验存在
        validateComboExists(updateReqVO.getId());
        // 更新
        ComboDO updateObj = BeanUtils.toBean(updateReqVO, ComboDO.class);
        comboMapper.updateById(updateObj);
    }

    @Override
    public void deleteCombo(Long id) {
        // 校验存在
        validateComboExists(id);
        // 删除
        comboMapper.deleteById(id);
    }

    private void validateComboExists(Long id) {
        if (comboMapper.selectById(id) == null) {
            throw exception(COMBO_NOT_EXISTS);
        }
    }

    @Override
    public ComboDO getCombo(Long id) {
        return comboMapper.selectById(id);
    }

    @Override
    public PageResult<ComboDO> getComboPage(ComboPageReqVO pageReqVO) {
        return comboMapper.selectPage(pageReqVO);
    }

}