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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.employee.api.EmployeeApi;
import cn.iocoder.yudao.module.employee.api.dto.*;
import cn.iocoder.yudao.module.employee.controller.admin.info.vo.EmployeeInfoPageReqVO;
import cn.iocoder.yudao.module.employee.controller.admin.info.vo.EmployeeInfoQueryReqVO;
import cn.iocoder.yudao.module.employee.controller.admin.info.vo.EmployeeInfoSaveReqVO;
import cn.iocoder.yudao.module.employee.dal.dataobject.info.EmployeeInfoDO;
import cn.iocoder.yudao.module.employee.service.info.EmployeeInfoService;
import cn.iocoder.yudao.module.relations.api.contract.RelationsContractApi;
import cn.iocoder.yudao.module.relations.api.contract.dto.RelationsContractDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 员工 API 实现类
 *
 * @author zhaopq
 */
@Service
public class EmployeeApiImpl implements EmployeeApi {

    @Resource
    private EmployeeInfoService infoService;

    @Resource
    private RelationsContractApi relationsContractApi;

    @Override
    public EmployeeRespDTO getEmployeeById(Long id) {
        EmployeeInfoDO info = infoService.getInfo(id);
        return BeanUtils.toBean(info, EmployeeRespDTO.class);
    }

    @Override
    public List<EmployeeRespDTO> getEmployeeListByIds(Collection<Long> ids) {
        List<EmployeeInfoDO> employeeInfoDOList = infoService.getEmployeeListByIds(ids);
        return BeanUtils.toBean(employeeInfoDOList, EmployeeRespDTO.class);
    }

    @Override
    public EmployeeRespDTO getEmployeeByUserId(Long userId) {
        EmployeeInfoDO employeeInfoDO = infoService.getEmployeeByUserId(userId);
        if (Objects.isNull(employeeInfoDO)) {
            return null;
        }
        EmployeeRespDTO employeeRespDTO = BeanUtils.toBean(employeeInfoDO, EmployeeRespDTO.class);

        // 根据员工ID查询员工当前的合同信息
        RelationsContractDTO lastContract = relationsContractApi.getLastContract(employeeRespDTO.getId());
        // 如果员工的合同信息存在，则更新员工信息中的合同起始和结束日期
        if (lastContract != null) {
            employeeRespDTO.setContractStartDate(lastContract.getContractStartDate());
            employeeRespDTO.setContractEndDate(lastContract.getContractEndDate());
        }

        return employeeRespDTO;
    }

    @Override
    public List<EmployeeRespDTO> getEmployeeListByUserIds(Collection<Long> userIds) {
        List<EmployeeInfoDO> employeeInfoDOList = infoService.getInfoListByUserIds(userIds);
        return BeanUtils.toBean(employeeInfoDOList, EmployeeRespDTO.class);
    }

    @Override
    public Long createEmployee(EmployeeCreateReqDTO reqDTO) {
        EmployeeInfoSaveReqVO reqVO = BeanUtils.toBean(reqDTO, EmployeeInfoSaveReqVO.class);
        return infoService.createInfo(reqVO);
    }

    @Override
    public Integer updateEmployee(EmployeeSaveReqDTO reqDTO) {
        EmployeeInfoSaveReqVO reqVO = BeanUtils.toBean(reqDTO, EmployeeInfoSaveReqVO.class);
        return infoService.updateInfo(reqVO);
    }

    @Override
    public EmployeeRespDTO getEmployee(EmployeeQueryReqDTO reqDTO) {
        EmployeeInfoQueryReqVO reqVO = BeanUtils.toBean(reqDTO, EmployeeInfoQueryReqVO.class);
        EmployeeInfoDO employeeInfoDO = infoService.getInfo(reqVO);
        EmployeeRespDTO respDTO = BeanUtils.toBean(employeeInfoDO, EmployeeRespDTO.class);
        return respDTO;
    }

    @Override
    public PageResult<EmployeeRespDTO> getEmployeePage(EmployeePageReqDTO reqDTO) {
        EmployeeInfoPageReqVO pageReqVO = BeanUtils.toBean(reqDTO, EmployeeInfoPageReqVO.class);
        PageResult<EmployeeInfoDO> pageResult = infoService.getInfoPage(pageReqVO);
        PageResult<EmployeeRespDTO> respDTO = BeanUtils.toBean(pageResult, EmployeeRespDTO.class);
        return respDTO;
    }


}
