package com.ruoyi.credit.util;

import com.ruoyi.credit.domain.EmployeeInfo;
import com.ruoyi.credit.domain.EmployeeLibrary;
import com.ruoyi.credit.dto.EmployeeRequestDTO;
import com.ruoyi.credit.service.IEmployeeManagerService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;

/**
 * 员工信息转换工具类
 * 处理前端DTO与后端实体类之间的数据转换
 * 
 * @author ruoyi
 * @date 2025-12-22
 */
public class EmployeeConverter {

    /**
     * 将员工请求DTO转换为员工实体类
     * 
     * @param dto 员工请求DTO
     * @return 员工实体类
     */
    public static EmployeeInfo convertToEntity(EmployeeRequestDTO dto) {
        if (dto == null) {
            return null;
        }

        EmployeeInfo entity = new EmployeeInfo();
        
        // 基本字段映射
        entity.setEmployeeId(dto.getEmployeeId());
        entity.setEmployeeNumber(dto.getEmployeeNumber());
        entity.setEmployeeName(dto.getName()); // name -> employeeName
        entity.setIdCardNumber(dto.getIdCard()); // idCard -> idCardNumber
        entity.setContactPhone(dto.getPhoneNumber()); // phoneNumber -> contactPhone
        entity.setPosition(dto.getPosition());
        entity.setDepartment(dto.getDepartment());
        entity.setEmployeeStatus(dto.getStatus()); // status -> employeeStatus
        entity.setHireDate(dto.getHireDate());
        entity.setLibraryId(dto.getLibraryId());
        
        // 处理工资单位 - 将数组转换为逗号分隔的字符串
        if (dto.getSalaryUnits() != null && !dto.getSalaryUnits().isEmpty()) {
            String salaryUnitStr = String.join(",", dto.getSalaryUnits());
            entity.setSalaryUnit(salaryUnitStr);
        }
        
        // 处理薪资字段 - 直接设置具体的薪资字段
        entity.setMonthlySalary(dto.getMonthlySalary());
        entity.setDailySalary(dto.getDailySalary());
        entity.setHourlySalary(dto.getHourlySalary());
        
        // 设置默认值
        if (dto.getIsTemporary() != null) {
            entity.setIsTemporary(dto.getIsTemporary());
        } else {
            entity.setIsTemporary(false); // 默认不是临时员工
        }
        
        return entity;
    }

    /**
     * 将员工实体类转换为响应DTO（用于返回给前端）
     * 
     * @param entity 员工实体类
     * @return 员工响应DTO
     */
    public static EmployeeRequestDTO convertToDTO(EmployeeInfo entity) {
        if (entity == null) {
            return null;
        }

        EmployeeRequestDTO dto = new EmployeeRequestDTO();
        
        // 基本字段映射
        dto.setEmployeeId(entity.getEmployeeId());
        dto.setEmployeeNumber(entity.getEmployeeNumber());
        dto.setName(entity.getEmployeeName()); // employeeName -> name
        dto.setIdCard(entity.getIdCardNumber()); // idCardNumber -> idCard
        dto.setPhoneNumber(entity.getContactPhone()); // contactPhone -> phoneNumber
        dto.setPosition(entity.getPosition());
        dto.setDepartment(entity.getDepartment());
        dto.setStatus(entity.getEmployeeStatus()); // employeeStatus -> status
        dto.setHireDate(entity.getHireDate());
        dto.setLibraryId(entity.getLibraryId());
        dto.setCreateTime(entity.getCreateTime());
        dto.setUpdateTime(entity.getUpdateTime());
        dto.setIsTemporary(entity.getIsTemporary());
        
        // 处理薪资字段
        dto.setMonthlySalary(entity.getMonthlySalary());
        dto.setDailySalary(entity.getDailySalary());
        dto.setHourlySalary(entity.getHourlySalary());
        
        // 获取员工库名称
        if (entity.getLibraryId() != null) {
            try {
                IEmployeeManagerService employeeManagerService = SpringUtils.getBean(IEmployeeManagerService.class);
                EmployeeLibrary library = employeeManagerService.selectEmployeeLibraryByLibraryId(entity.getLibraryId());
                if (library != null) {
                    dto.setLibraryName(library.getLibraryName());
                }
            } catch (Exception e) {
                // 如果获取员工库名称失败，保持为null
                dto.setLibraryName(null);
            }
        }
        
        // 处理工资单位 - 将逗号分隔的字符串转换为数组
        if (StringUtils.isNotEmpty(entity.getSalaryUnit())) {
            String[] salaryUnits = entity.getSalaryUnit().split(",");
            dto.setSalaryUnits(java.util.Arrays.asList(salaryUnits));
        } else {
            dto.setSalaryUnits(new java.util.ArrayList<>());
        }
        
        return dto;
    }
}