package cn.iocoder.yudao.server.service.crm;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.server.controller.admin.vo.crm.CrmNoticePageReqVO;
import cn.iocoder.yudao.server.controller.admin.vo.crm.CrmNoticeSaveReqVO;
import cn.iocoder.yudao.server.dal.dataobject.CrmNoticeDO;
import cn.iocoder.yudao.server.dal.mysql.CrmNoticeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * CRM消息通知 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class CrmNoticeServiceImpl implements CrmNoticeService {

    @Resource
    private CrmNoticeMapper crmNoticeMapper;

    @Override
    public Long createIotCrmNotice(CrmNoticeSaveReqVO createReqVO) {
        // 插入
        CrmNoticeDO iotCrmNotice = BeanUtils.toBean(createReqVO, CrmNoticeDO.class);
        crmNoticeMapper.insert(iotCrmNotice);
        // 返回
        return iotCrmNotice.getId();
    }

    @Override
    public void updateIotCrmNotice(CrmNoticeSaveReqVO updateReqVO) {
        // 校验存在
        validateIotCrmNoticeExists(updateReqVO.getId());
        // 更新
        CrmNoticeDO updateObj = BeanUtils.toBean(updateReqVO, CrmNoticeDO.class);
        crmNoticeMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotCrmNotice(Long id) {
        // 校验存在
        validateIotCrmNoticeExists(id);
        // 删除
        crmNoticeMapper.deleteById(id);
    }

    private void validateIotCrmNoticeExists(Long id) {
        if (crmNoticeMapper.selectById(id) == null) {
            throw exception(new ErrorCode(2,"不存咋"));
        }
    }

    @Override
    public CrmNoticeDO getIotCrmNotice(Long id) {
        return crmNoticeMapper.selectById(id);
    }

    @Override
    public PageResult<CrmNoticeDO> getIotCrmNoticePage(CrmNoticePageReqVO pageReqVO) {
        return crmNoticeMapper.selectPage(pageReqVO);
    }

}