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.IotOaPersonPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.oa.IotOaPersonSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.oa.IotOaPersonDO;
import cn.iocoder.yudao.module.pms.dal.mysql.oa.IotOaPersonMapper;
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 IotOaPersonServiceImpl implements IotOaPersonService {

    @Resource
    private IotOaPersonMapper iotOaPersonMapper;

    @Override
    public Long createIotOaPerson(IotOaPersonSaveReqVO createReqVO) {
        // 插入
        IotOaPersonDO iotOaPerson = BeanUtils.toBean(createReqVO, IotOaPersonDO.class);
        iotOaPersonMapper.insert(iotOaPerson);
        // 返回
        return iotOaPerson.getId();
    }

    @Override
    public void updateIotOaPerson(IotOaPersonSaveReqVO updateReqVO) {
        // 校验存在
        validateIotOaPersonExists(updateReqVO.getId());
        // 更新
        IotOaPersonDO updateObj = BeanUtils.toBean(updateReqVO, IotOaPersonDO.class);
        iotOaPersonMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotOaPerson(Long id) {
        // 校验存在
        validateIotOaPersonExists(id);
        // 删除
        iotOaPersonMapper.deleteById(id);
    }

    private void validateIotOaPersonExists(Long id) {
        if (iotOaPersonMapper.selectById(id) == null) {
            throw exception(new ErrorCode(225,"oa人员信息不存在"));
        }
    }

    @Override
    public IotOaPersonDO getIotOaPerson(Long id) {
        return iotOaPersonMapper.selectById(id);
    }

    @Override
    public PageResult<IotOaPersonDO> getIotOaPersonPage(IotOaPersonPageReqVO pageReqVO) {
        return iotOaPersonMapper.selectPage(pageReqVO);
    }

}
