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

import com.engine.salary.entity.salaryformula.ExpressFormula;
import com.engine.salary.entity.salaryformula.dto.ExpressFormulaDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

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

/**
 * 薪酬管理公式
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalaryFormulaBO {

    public static List<ExpressFormulaDTO> convert2DTO(List<ExpressFormula> expressFormulas, Map<Long, String> returnTypeMap) {
        if (CollectionUtils.isEmpty(expressFormulas)) {
            return Collections.emptyList();
        }
        return expressFormulas.stream()
                .map(e -> {
                    String referenceType = returnTypeMap.getOrDefault(e.getId(), "");
                    ExpressFormulaDTO build = ExpressFormulaDTO.builder()
                            .id(e.getId())
                            .name(e.getName())
                            .formula(e.getFormula())
                            .description(e.getDescription())
                            .extendParam(e.getExtendParam())
                            .module(e.getModule())
                            .parameters(e.getParameters())
                            .referenceType(e.getReferenceType())
                            .returnType(e.getReturnType())
                            .useFor(e.getUseFor())
                            .validateType(e.getValidateType())
                            .build();
                    if (StringUtils.isNotBlank(referenceType) && !StringUtils.equals(e.getReferenceType(), referenceType)) {
                        // 更换了取值方式，返回空对象进行前端处理
                        build.setExtendParam("");
                        build.setFormula("");
                        build.setParameters(Collections.emptyList());
                        build.setReferenceType(referenceType);
                        return build;
                    }
                    return build;
                })
                .collect(Collectors.toList());
    }
}
