package cn.iocoder.yudao.module.employee.service.employeeinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.employee.controller.admin.employeeinfo.vo.EmployeeContractInfoPageReqVO;
import cn.iocoder.yudao.module.employee.controller.admin.employeeinfo.vo.EmployeeContractInfoSaveReqVO;
import cn.iocoder.yudao.module.employee.dal.dataobject.employeeinfo.EmployeeContractInfoDO;
import cn.iocoder.yudao.module.employee.dal.mysql.employeeinfo.EmployeeContractInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.employee.enums.ErrorCodeConstants.EMPLOYEE_CONTRACT_INFO_NOT_EXISTS;

/**
 * 员工合同信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class EmployeeContractInfoServiceImpl implements EmployeeContractInfoService {

    @Resource
    private EmployeeContractInfoMapper contractInfoMapper;

    @Override
    public Long createContractInfo(EmployeeContractInfoSaveReqVO createReqVO) {
        // 插入
        EmployeeContractInfoDO contractInfo = BeanUtils.toBean(createReqVO, EmployeeContractInfoDO.class);
        contractInfoMapper.insert(contractInfo);
        // 返回
        return contractInfo.getId();
    }

    @Override
    public void updateContractInfo(EmployeeContractInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateContractInfoExists(updateReqVO.getId());
        // 更新
        EmployeeContractInfoDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeContractInfoDO.class);
        contractInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteContractInfo(Long id) {
        // 校验存在
        validateContractInfoExists(id);
        // 删除
        contractInfoMapper.deleteById(id);
    }

    private void validateContractInfoExists(Long id) {
        if (contractInfoMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_CONTRACT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeContractInfoDO getContractInfo(Long id) {
        return contractInfoMapper.selectById(id);
    }

    @Override
    public PageResult<EmployeeContractInfoDO> getContractInfoPage(EmployeeContractInfoPageReqVO pageReqVO) {
        return contractInfoMapper.selectPage(pageReqVO);
    }

}