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.IotOaCompanyPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.oa.IotOaCompanySaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.oa.IotOaCompanyDO;
import cn.iocoder.yudao.module.pms.dal.mysql.oa.IotOaCompanyMapper;
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 IotOaCompanyServiceImpl implements IotOaCompanyService {

    @Resource
    private IotOaCompanyMapper iotOaCompanyMapper;

    @Override
    public Long createIotOaCompany(IotOaCompanySaveReqVO createReqVO) {
        // 插入
        IotOaCompanyDO iotOaCompany = BeanUtils.toBean(createReqVO, IotOaCompanyDO.class);
        iotOaCompanyMapper.insert(iotOaCompany);
        // 返回
        return iotOaCompany.getId();
    }

    @Override
    public void updateIotOaCompany(IotOaCompanySaveReqVO updateReqVO) {
        // 校验存在
        validateIotOaCompanyExists(updateReqVO.getId());
        // 更新
        IotOaCompanyDO updateObj = BeanUtils.toBean(updateReqVO, IotOaCompanyDO.class);
        iotOaCompanyMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotOaCompany(Long id) {
        // 校验存在
        validateIotOaCompanyExists(id);
        // 删除
        iotOaCompanyMapper.deleteById(id);
    }

    private void validateIotOaCompanyExists(Long id) {
        if (iotOaCompanyMapper.selectById(id) == null) {
            throw exception(new ErrorCode(222,"oa公司信息不存在"));
        }
    }

    @Override
    public IotOaCompanyDO getIotOaCompany(Long id) {
        return iotOaCompanyMapper.selectById(id);
    }

    @Override
    public PageResult<IotOaCompanyDO> getIotOaCompanyPage(IotOaCompanyPageReqVO pageReqVO) {
        return iotOaCompanyMapper.selectPage(pageReqVO);
    }

}
