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

import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.dto.SalaryItemBaseDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobAdjustRuleListDTO;
import com.engine.salary.entity.salarysob.param.SalarySobAdjustRuleSaveParam;
import com.engine.salary.entity.salarysob.po.SalarySobAdjustRulePO;
import com.engine.salary.util.SalaryEntityUtil;
import org.apache.commons.collections4.CollectionUtils;
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 SalarySobAdjustRuleBO {

    /**
     * 调薪计薪规则po转换成调薪计薪规则列表dto
     *
     * @param salarySobAdjustRules 调薪计薪规则po
     * @param salaryItems          薪资项目po
     * @return
     */
    public static List<SalarySobAdjustRuleListDTO> convert2ListDTO(List<SalarySobAdjustRulePO> salarySobAdjustRules, List<SalaryItemPO> salaryItems) {
        if (CollectionUtils.isEmpty(salarySobAdjustRules)) {
            return Collections.emptyList();
        }
        // key：薪资项目id、value：薪资项目的名称
        Map<Long, String> salaryItemNameMap = SalaryEntityUtil.convert2Map(salaryItems, SalaryItemPO::getId, SalaryItemPO::getName);
        return salarySobAdjustRules.stream()
                .map(e -> SalarySobAdjustRuleListDTO.builder()
                        .id(e.getId())
                        .salaryItemId(e.getSalaryItemId())
                        .salaryItemName(salaryItemNameMap.getOrDefault(e.getSalaryItemId(), ""))
                        .dayOfMonth(e.getDayOfMonth())
                        .beforeAdjustmentType(e.getBeforeAdjustmentType())
                        .afterAdjustmentType(e.getAfterAdjustmentType())
                        .build())
                .collect(Collectors.toList());
    }

    /**
     * 转换成调薪计薪规则可选的薪资项目dto
     * 只有开启了"薪资档案引用"的薪资项目才可选
     *
     * @param salaryItems    薪资项目po
     * @return
     */
    public static List<SalaryItemBaseDTO> convertItemBaseDTO(List<SalaryItemPO> salaryItems) {
        if (CollectionUtils.isEmpty(salaryItems)) {
            return Collections.emptyList();
        }
        // 过滤开启了"薪资档案引用"的薪资项目
        return salaryItems.stream()
                .filter(salaryItemPO -> Objects.equals(salaryItemPO.getUseInEmployeeSalary(), NumberUtils.INTEGER_ONE))
                .map(salaryItemPO -> new SalaryItemBaseDTO()
                        .setSalaryItemId(salaryItemPO.getId())
                        .setSalaryItemName(salaryItemPO.getName()))
                .collect(Collectors.toList());

    }

    /**
     * 保存参数转换成薪资账套的调薪计薪规则po
     *
     * @param saveParam  保存参数
     * @param employeeId 人员id
     * @return
     */
    public static List<SalarySobAdjustRulePO> convert2PO(SalarySobAdjustRuleSaveParam saveParam, Long employeeId) {
        if (CollectionUtils.isEmpty(saveParam.getRuleParams())) {
            return Collections.emptyList();
        }
        Date now = new Date();
        return saveParam.getRuleParams().stream()
                .map(ruleParam -> new SalarySobAdjustRulePO()
                        .setSalarySobId(saveParam.getSalarySobId())
                        .setSalaryItemId(ruleParam.getSalaryItemId())
                        .setDayOfMonth(ruleParam.getDayOfMonth())
                        .setBeforeAdjustmentType(ruleParam.getBeforeAdjustmentType())
                        .setAfterAdjustmentType(ruleParam.getAfterAdjustmentType())
                        .setCreator(employeeId)
                        .setCreateTime(now)
                        .setUpdateTime(now)
                        .setDeleteType(NumberUtils.INTEGER_ZERO)
                        .setTenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                ).collect(Collectors.toList());
    }
}
