package com.yc.ship.module.trade.service.smsSendLog;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.trade.controller.admin.smsSendLog.vo.SmsSendLogPageReqVO;
import com.yc.ship.module.trade.controller.admin.smsSendLog.vo.SmsSendLogSaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.smsSendLog.SmsSendLogDO;
import com.yc.ship.module.trade.dal.mysql.smsSendLog.SmsSendLogMapper;
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.trade.enums.ErrorCodeConstants.SMS_SEND_LOG_NOT_EXISTS;


/**
 * 短信发送配置 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SmsSendLogServiceImpl implements SmsSendLogService {

    @Resource
    private SmsSendLogMapper smsSendLogMapper;

    @Override
    public Long createSmsSendLog(SmsSendLogSaveReqVO createReqVO) {
        // 插入
        SmsSendLogDO smsSendLog = BeanUtils.toBean(createReqVO, SmsSendLogDO.class);
        smsSendLogMapper.insert(smsSendLog);
        // 返回
        return smsSendLog.getId();
    }

    @Override
    public void updateSmsSendLog(SmsSendLogSaveReqVO updateReqVO) {
        // 校验存在
        validateSmsSendLogExists(updateReqVO.getId());
        // 更新
        SmsSendLogDO updateObj = BeanUtils.toBean(updateReqVO, SmsSendLogDO.class);
        smsSendLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteSmsSendLog(Long id) {
        // 校验存在
        validateSmsSendLogExists(id);
        // 删除
        smsSendLogMapper.deleteById(id);
    }

    private void validateSmsSendLogExists(Long id) {
        if (smsSendLogMapper.selectById(id) == null) {
            throw exception(SMS_SEND_LOG_NOT_EXISTS);
        }
    }

    @Override
    public SmsSendLogDO getSmsSendLog(Long id) {
        return smsSendLogMapper.selectById(id);
    }

    @Override
    public PageResult<SmsSendLogDO> getSmsSendLogPage(SmsSendLogPageReqVO pageReqVO) {
        return smsSendLogMapper.selectPage(pageReqVO);
    }

}