package cn.iocoder.yudao.module.pms.service.oa;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.oa.IotOaDepartPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.oa.IotOaDepartSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.oa.IotOaDepartDO;
import cn.iocoder.yudao.module.pms.dal.mysql.oa.IotOaDepartMapper;
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.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


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

/**
 * oa部门 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotOaDepartServiceImpl implements IotOaDepartService {

    @Resource
    private IotOaDepartMapper iotOaDepartMapper;

    @Override
    public Long createIotOaDepart(IotOaDepartSaveReqVO createReqVO) {
        // 插入
        IotOaDepartDO iotOaDepart = BeanUtils.toBean(createReqVO, IotOaDepartDO.class);
        iotOaDepartMapper.insert(iotOaDepart);
        // 返回
        return iotOaDepart.getId();
    }

    @Override
    public void updateIotOaDepart(IotOaDepartSaveReqVO updateReqVO) {
        // 校验存在
        validateIotOaDepartExists(updateReqVO.getId());
        // 更新
        IotOaDepartDO updateObj = BeanUtils.toBean(updateReqVO, IotOaDepartDO.class);
        iotOaDepartMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotOaDepart(Long id) {
        // 校验存在
        validateIotOaDepartExists(id);
        // 删除
        iotOaDepartMapper.deleteById(id);
    }

    private void validateIotOaDepartExists(Long id) {
        if (iotOaDepartMapper.selectById(id) == null) {
            throw exception(new ErrorCode(224, "oa部门信息不存咋"));
        }
    }

    @Override
    public IotOaDepartDO getIotOaDepart(Long id) {
        return iotOaDepartMapper.selectById(id);
    }

    @Override
    public PageResult<IotOaDepartDO> getIotOaDepartPage(IotOaDepartPageReqVO pageReqVO) {
        return iotOaDepartMapper.selectPage(pageReqVO);
    }

}
