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

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.server.controller.admin.vo.oa.IotOaNoticePageReqVO;
import cn.iocoder.yudao.server.controller.admin.vo.oa.IotOaNoticeSaveReqVO;
import cn.iocoder.yudao.server.dal.dataobject.OaNoticeDO;
import cn.iocoder.yudao.server.dal.mysql.OaNoticeMapper;
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;

/**
 * OA消息通知 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class OaNoticeServiceImpl implements OaNoticeService {

    @Resource
    private OaNoticeMapper iotOaNoticeMapper;

    @Override
    public Long createIotOaNotice(IotOaNoticeSaveReqVO createReqVO) {
        // 插入
        OaNoticeDO iotOaNotice = BeanUtils.toBean(createReqVO, OaNoticeDO.class);
        iotOaNoticeMapper.insert(iotOaNotice);
        // 返回
        return iotOaNotice.getId();
    }

    @Override
    public void updateIotOaNotice(IotOaNoticeSaveReqVO updateReqVO) {
        // 校验存在
        validateIotOaNoticeExists(updateReqVO.getId());
        // 更新
        OaNoticeDO updateObj = BeanUtils.toBean(updateReqVO, OaNoticeDO.class);
        iotOaNoticeMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotOaNotice(Long id) {
        // 校验存在
        validateIotOaNoticeExists(id);
        // 删除
        iotOaNoticeMapper.deleteById(id);
    }

    private void validateIotOaNoticeExists(Long id) {
        if (iotOaNoticeMapper.selectById(id) == null) {
            throw exception(new ErrorCode(2,"不存在"));
        }
    }

    @Override
    public OaNoticeDO getIotOaNotice(Long id) {
        return iotOaNoticeMapper.selectById(id);
    }

    @Override
    public PageResult<OaNoticeDO> getIotOaNoticePage(IotOaNoticePageReqVO pageReqVO) {
        return iotOaNoticeMapper.selectPage(pageReqVO);
    }

}