package com.ruoyi.oa.utils;

import com.ruoyi.oa.domain.bo.OaSalaryDetailBo;
import com.ruoyi.oa.domain.bo.OaSalaryMasterBo;
import com.ruoyi.oa.domain.dto.BatchInsertSalaryStringDto;
import com.ruoyi.oa.domain.dto.SalaryDetailStringDto;
import cn.hutool.core.collection.CollUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 工资字符串数据转换工具类
 *
 * @author Joshi
 * @date 2025-09-08
 */
public class SalaryStringConverter {

    /**
     * 将字符串格式的请求DTO转换为后端BO对象
     *
     * @param requestDto 字符串格式的请求DTO
     * @return 后端BO对象
     */
    public static OaSalaryMasterBo convertToMasterBo(BatchInsertSalaryStringDto requestDto) {
        if (requestDto == null) {
            return null;
        }

        OaSalaryMasterBo masterBo = new OaSalaryMasterBo();
        masterBo.setUnitName(requestDto.getUnitName());
        masterBo.setSalaryPeriod(requestDto.getSalaryPeriod());
        masterBo.setGmApproval(requestDto.getGmApproval());
        masterBo.setFinanceAudit(requestDto.getFinanceAudit());
        masterBo.setCashier(requestDto.getCashier());
        masterBo.setDeptManager(requestDto.getDeptManager());
        masterBo.setOperator(requestDto.getOperator());
        masterBo.setSalaryTemplate(requestDto.getSalaryTemplate());
        masterBo.setRemark(requestDto.getRemark());

        // 转换明细列表
        if (CollUtil.isNotEmpty(requestDto.getSalaryDetailList())) {
            List<OaSalaryDetailBo> detailBoList = requestDto.getSalaryDetailList().stream()
                    .map(SalaryStringConverter::convertToDetailBo)
                    .collect(Collectors.toList());
            masterBo.setSalaryDetailList(detailBoList);
        }

        return masterBo;
    }

    /**
     * 将字符串格式的明细DTO转换为后端明细BO对象
     *
     * @param stringDto 字符串格式的明细DTO
     * @return 后端明细BO对象
     */
    public static OaSalaryDetailBo convertToDetailBo(SalaryDetailStringDto stringDto) {
        if (stringDto == null) {
            return null;
        }

        OaSalaryDetailBo detailBo = new OaSalaryDetailBo();
        
        // 基本信息
        detailBo.setDetailId(stringDto.getDetailId());
        detailBo.setMainId(stringDto.getMainId());
        detailBo.setSerialNumber(stringDto.getSerialNumberAsLong());
        detailBo.setDept(stringDto.getDept());
        detailBo.setName(stringDto.getName());
        detailBo.setRemark(stringDto.getRemark());

        // 工资项目 - 使用转换方法
        detailBo.setBasicSalary(stringDto.getBasicSalaryAsDecimal());
        detailBo.setPostSalary(stringDto.getPostSalaryAsDecimal());
        detailBo.setMealAllowance(stringDto.getMealAllowanceAsDecimal());
        detailBo.setHousingAllowance(stringDto.getHousingAllowanceAsDecimal());
        detailBo.setBusAllowance(stringDto.getBusAllowanceAsDecimal());
        detailBo.setBusinessDaysOther(stringDto.getBusinessDaysOtherAsLong());
        detailBo.setBusinessAllowance(stringDto.getBusinessAllowanceAsDecimal());
        detailBo.setSocialSecurityAllowance(stringDto.getSocialSecurityAllowanceAsDecimal());

        // 加班相关
        detailBo.setOvertimeHours(stringDto.getOvertimeHoursAsDecimal());
        detailBo.setOvertimeRate(stringDto.getOvertimeRateAsDecimal());
        detailBo.setOvertimeTotal(stringDto.getOvertimeTotalAsDecimal());
        detailBo.setBusinessDays(stringDto.getBusinessDaysAsLong());

        // 扣款项目
        detailBo.setLeaveDeduction(stringDto.getLeaveDeductionAsDecimal());
        detailBo.setOtherDeduction(stringDto.getOtherDeductionAsDecimal());
        detailBo.setGrossSalary(stringDto.getGrossSalaryAsDecimal());

        // 个人缴费
        detailBo.setPersonalPension(stringDto.getPersonalPensionAsDecimal());
        detailBo.setPersonalMedical(stringDto.getPersonalMedicalAsDecimal());
        detailBo.setPersonalUnemployment(stringDto.getPersonalUnemploymentAsDecimal());
        detailBo.setPersonalBigMedical(stringDto.getPersonalBigMedicalAsDecimal());
        detailBo.setPersonalHousingFund(stringDto.getPersonalHousingFundAsDecimal());
        detailBo.setPersonalTax(stringDto.getPersonalTaxAsDecimal());
        detailBo.setNetSalary(stringDto.getNetSalaryAsDecimal());

        // 企业缴费
        detailBo.setEnterprisePension(stringDto.getEnterprisePensionAsDecimal());
        detailBo.setEnterpriseMedical(stringDto.getEnterpriseMedicalAsDecimal());
        detailBo.setEnterpriseInjury(stringDto.getEnterpriseInjuryAsDecimal());
        detailBo.setEnterpriseUnemployment(stringDto.getEnterpriseUnemploymentAsDecimal());
        detailBo.setEnterpriseMaternity(stringDto.getEnterpriseMaternityAsDecimal());
        detailBo.setEnterpriseHousingFund(stringDto.getEnterpriseHousingFundAsDecimal());
        detailBo.setEnterpriseBigMedical(stringDto.getEnterpriseBigMedicalAsDecimal());
        detailBo.setUnitTotalExpense(stringDto.getUnitTotalExpenseAsDecimal());

        return detailBo;
    }
}