package cn.newfeifan.mall.module.trade.service.jpushmessagelog;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.newfeifan.mall.module.trade.controller.admin.jpushmessagelog.vo.*;
import cn.newfeifan.mall.module.trade.dal.dataobject.jpushmessagelog.JPushMessageLogDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.trade.dal.mysql.jpushmessagelog.JPushMessageLogMapper;

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

/**
 * 极光推送记录 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class JPushMessageLogServiceImpl implements JPushMessageLogService {

    @Resource
    private JPushMessageLogMapper jPushMessageLogMapper;

    @Override
    public Long createJPushMessageLog(JPushMessageLogSaveReqVO createReqVO) {
        // 插入
        JPushMessageLogDO jPushMessageLog = BeanUtils.toBean(createReqVO, JPushMessageLogDO.class);
        jPushMessageLogMapper.insert(jPushMessageLog);
        // 返回
        return jPushMessageLog.getId();
    }

    @Override
    public void updateJPushMessageLog(JPushMessageLogSaveReqVO updateReqVO) {
        // 校验存在
        validateJPushMessageLogExists(updateReqVO.getId());
        // 更新
        JPushMessageLogDO updateObj = BeanUtils.toBean(updateReqVO, JPushMessageLogDO.class);
        jPushMessageLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteJPushMessageLog(Long id) {
        // 校验存在
        validateJPushMessageLogExists(id);
        // 删除
        jPushMessageLogMapper.deleteById(id);
    }

    private void validateJPushMessageLogExists(Long id) {
        if (jPushMessageLogMapper.selectById(id) == null) {
            throw exception(J_PUSH_MESSAGE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public JPushMessageLogDO getJPushMessageLog(Long id) {
        return jPushMessageLogMapper.selectById(id);
    }

    @Override
    public PageResult<JPushMessageLogDO> getJPushMessageLogPage(JPushMessageLogPageReqVO pageReqVO) {
        return jPushMessageLogMapper.selectPage(pageReqVO);
    }

}