package cn.iocoder.yudao.module.server.service.smsrecord;

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

import java.util.*;
import cn.iocoder.yudao.module.server.controller.admin.smsrecord.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.smsrecord.SmsRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.server.dal.mysql.smsrecord.SmsRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.server.enums.ErrorCodeConstants.*;

/**
 * 短信发送记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class SmsRecordServiceImpl implements SmsRecordService {

    @Resource
    private SmsRecordMapper smsRecordMapper;

    @Override
    public Long createSmsRecord(SmsRecordSaveReqVO createReqVO) {
        // 插入
        SmsRecordDO smsRecord = BeanUtils.toBean(createReqVO, SmsRecordDO.class);
        smsRecordMapper.insert(smsRecord);
        // 返回
        return smsRecord.getId();
    }

    @Override
    public void updateSmsRecord(SmsRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateSmsRecordExists(updateReqVO.getId());
        // 更新
        SmsRecordDO updateObj = BeanUtils.toBean(updateReqVO, SmsRecordDO.class);
        smsRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteSmsRecord(Long id) {
        // 校验存在
        validateSmsRecordExists(id);
        // 删除
        smsRecordMapper.deleteById(id);
    }

    private void validateSmsRecordExists(Long id) {
        if (smsRecordMapper.selectById(id) == null) {
            throw exception(SMS_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public SmsRecordDO getSmsRecord(Long id) {
        return smsRecordMapper.selectById(id);
    }

    @Override
    public PageResult<SmsRecordDO> getSmsRecordPage(SmsRecordPageReqVO pageReqVO) {
        return smsRecordMapper.selectPage(pageReqVO);
    }

}