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

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.employee.controller.admin.info.vo.EmployeeInfoHistoryPageReqVO;
import cn.iocoder.yudao.module.employee.controller.admin.info.vo.EmployeeInfoHistorySaveReqVO;
import cn.iocoder.yudao.module.employee.dal.dataobject.info.EmployeeInfoHistoryDO;
import cn.iocoder.yudao.module.employee.dal.mysql.info.EmployeeInfoHistoryMapper;
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.*;

/**
 * 员工信息历史 Service 实现类
 *
 * @author zhaopq
 */
@Service
@Validated
public class EmployeeInfoHistoryServiceImpl implements EmployeeInfoHistoryService {

    @Resource
    private EmployeeInfoHistoryMapper infoHistoryMapper;

    @Override
    public Long createInfoHistory(EmployeeInfoHistorySaveReqVO createReqVO) {
        // 插入
        // 生成id
        createReqVO.setInfoId(IdUtil.fastSimpleUUID());
        EmployeeInfoHistoryDO infoHistory = BeanUtils.toBean(createReqVO, EmployeeInfoHistoryDO.class);
        infoHistoryMapper.insert(infoHistory);
        // 返回
        return infoHistory.getId();
    }

    @Override
    public void updateInfoHistory(EmployeeInfoHistorySaveReqVO updateReqVO) {
        // 校验存在
        validateInfoHistoryExists(updateReqVO.getId());
        // 更新
        EmployeeInfoHistoryDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeInfoHistoryDO.class);
        infoHistoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteInfoHistory(Long id) {
        // 校验存在
        validateInfoHistoryExists(id);
        // 删除
        infoHistoryMapper.deleteById(id);
    }

    private void validateInfoHistoryExists(Long id) {
        if (infoHistoryMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_INFO_HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeInfoHistoryDO getInfoHistory(Long id) {
        return infoHistoryMapper.selectById(id);
    }

    @Override
    public PageResult<EmployeeInfoHistoryDO> getInfoHistoryPage(EmployeeInfoHistoryPageReqVO pageReqVO) {
        return infoHistoryMapper.selectPage(pageReqVO);
    }

}