package com.engine.salary.entity.salarysob.bo;

import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.salaryformula.ExpressFormula;
import com.engine.salary.entity.salarysob.dto.SalarySobCheckRuleFormDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobCheckRuleListDTO;
import com.engine.salary.entity.salarysob.param.SalarySobCheckRuleSaveParam;
import com.engine.salary.entity.salarysob.po.SalarySobCheckRulePO;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 薪资账套的校验规则
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalarySobCheckRuleBO {

    /**
     * 保存参数转换成薪资账套的校验规则po
     *
     * @param saveParam  保存参数
     * @param employeeId 人员id
     * @return
     */
    public static SalarySobCheckRulePO convert2PO(SalarySobCheckRuleSaveParam saveParam, Long employeeId) {
        if (Objects.isNull(saveParam)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, " 参数错误"));
        }
        Date now = new Date();
        return SalarySobCheckRulePO.builder()
                .salarySobId(saveParam.getSalarySobId())
                .name(saveParam.getName())
                .formulaId(saveParam.getFormulaId())
                .description(saveParam.getDescription())
                .creator(employeeId)
                .createTime(now)
                .updateTime(now)
                .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                .deleteType(NumberUtils.INTEGER_ZERO)
                .build();
    }

    /**
     * 薪资账套的校验规则po转换成校验规则列表dto
     *
     * @param salarySobCheckRulePOS 薪资账套的校验规则po
     * @param expressFormulas       公式详情
     * @return
     */
    public static List<SalarySobCheckRuleListDTO> convert2ListDTO(List<SalarySobCheckRulePO> salarySobCheckRulePOS, List<ExpressFormula> expressFormulas) {
        if (CollectionUtils.isEmpty(salarySobCheckRulePOS)) {
            return Collections.emptyList();
        }
        // key:公式id、value:公式详情
        Map<Long, String> formulaMap = SalaryEntityUtil.convert2Map(expressFormulas, ExpressFormula::getId, ExpressFormula::getFormula);
        return salarySobCheckRulePOS.stream()
                .map(salarySobCheckRulePO -> new SalarySobCheckRuleListDTO()
                        .setId(salarySobCheckRulePO.getId())
                        .setName(salarySobCheckRulePO.getName())
                        .setSalarySobId(salarySobCheckRulePO.getSalarySobId())
                        .setFormulaId(salarySobCheckRulePO.getFormulaId())
                        .setFormulaContent(formulaMap.getOrDefault(salarySobCheckRulePO.getFormulaId(), StringUtils.EMPTY))
                        .setDescription(salarySobCheckRulePO.getDescription())
                ).collect(Collectors.toList());
    }

    /**
     * 转换成校验规则详情dto
     *
     * @param salarySobCheckRulePO 校验规则po
     * @param expressFormula       公式详情
     * @return
     */
    public static SalarySobCheckRuleFormDTO convert2FormDTO(SalarySobCheckRulePO salarySobCheckRulePO, ExpressFormula expressFormula) {
        return new SalarySobCheckRuleFormDTO()
                .setId(salarySobCheckRulePO.getId())
                .setName(salarySobCheckRulePO.getName())
                .setSalarySobId(salarySobCheckRulePO.getSalarySobId())
                .setFormulaId(salarySobCheckRulePO.getFormulaId())
                .setFormulaContent(Optional.ofNullable(expressFormula).map(ExpressFormula::getFormula).orElse(StringUtils.EMPTY))
                .setDescription(salarySobCheckRulePO.getDescription());
    }
}
