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

import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.taxrate.TaxRateBase;
import com.engine.salary.entity.taxrate.TaxRateDetail;
import com.engine.salary.entity.taxrate.param.TaxRateBaseSaveParam;
import com.engine.salary.entity.taxrate.param.TaxRateDetailSaveParam;
import com.engine.salary.enums.SalarySystemTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import org.apache.commons.collections4.CollectionUtils;

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

/**
 * 税率表业务方法
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class TaxRateBO {

    private TaxRateBO() {
        throw new SalaryRunTimeException("该类不需要创建实例");
    }

    /**
     * 转换成个税税率表的前端列表
     *
     * @param taxRateBatches
     * @return
     */
//    public static List<TaxRateListDTO> convert2ListDTO(Collection<TaxRateBasePO> taxRateBatches) {
//        if (CollectionUtils.isEmpty(taxRateBatches)) {
//            return Collections.emptyList();
//        }
//        return taxRateBatches.stream()
//                .map(taxRateBatchPO -> {
//                    SalarySystemTypeEnum typeEnum = SalarySystemTypeEnum.parseByValue(taxRateBatchPO.getSystemType());
//                    return TaxRateListDTO.builder()
//                            .id(taxRateBatchPO.getId())
//                            .name(taxRateBatchPO.getName())
//                            .systemType(Optional.ofNullable(typeEnum).map(e -> SalaryI18nUtil.getI18nLabel(e.getLabelId(), e.getDefaultLabel())).orElse(StringUtils.EMPTY))
//                            .createTime(SalaryDateUtil.getFormatLocalDate(taxRateBatchPO.getCreateTime()))
//                            .description(taxRateBatchPO.getDescription())
//                            .build();
//                })
//                .collect(Collectors.toList());
//    }

    /**
     * 转转成个税税率表的明细
     *
     * @param taxRateRecords
     * @return
     */
//    public static List<TaxRateRecordDTO> convert2RecordDTO(Collection<TaxRateDetailPO> taxRateRecords) {
//        if (CollectionUtils.isEmpty(taxRateRecords)) {
//            return Collections.emptyList();
//        }
//        return taxRateRecords.stream()
//                .map(e -> TaxRateRecordDTO.builder()
//                        .id(e.getId())
//                        .batchId(e.getBaseId())
//                        .indexNum(e.getIndexNum())
//                        .incomeUpperLimit(e.getIncomeUpperLimit())
//                        .incomeLowerLimit(e.getIncomeLowerLimit())
//                        .dutyFreeValue(e.getDutyFreeValue())
//                        .dutyFreeRate(e.getDutyFreeRate())
//                        .taxableIncomeULimit(e.getTaxableIncomeULimit())
//                        .taxableIncomeLLimit(e.getTaxableIncomeLLimit())
//                        .taxRate(e.getTaxRate())
//                        .taxDeduction(e.getTaxDeduction())
//                        .build())
//                .collect(Collectors.toList());
//    }

    /**
     * 个税税率表的基本信息的保存参数转换成对应的PO实体
     *
     * @param saveParam
     * @param employeeId
     * @return
     */
    public static TaxRateBase convert2BatchPO(TaxRateBaseSaveParam saveParam, Long employeeId) {
        if (saveParam == null) {
            return null;
        }
        return TaxRateBase.builder()
                .name(saveParam.getName())
                .systemType(SalarySystemTypeEnum.CUSTOM.getValue())
                .description(saveParam.getDescription())
                .createTime(new Date())
                .updateTime(new Date())
                .creator(employeeId)
                .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                .deleteType(0)
                .build();
    }

    /**
     * 个税税率表的明细的保存参数转换成对应的PO实体
     *
     * @param saveParams
     * @param employeeId
     * @return
     */
    public static List<TaxRateDetail> convert2RecordPO(Long taxRateBatchId, Collection<TaxRateDetailSaveParam> saveParams, Long employeeId) {
        if (CollectionUtils.isEmpty(saveParams)) {
            return Collections.emptyList();
        }
        return saveParams.stream()
                .map(e -> TaxRateDetail.builder()
                        .baseId(taxRateBatchId)
                        .indexNum(e.getIndexNum())
                        .incomeUpperLimit(e.getIncomeUpperLimit())
                        .incomeLowerLimit(e.getIncomeLowerLimit())
                        .dutyFreeValue(e.getDutyFreeValue())
                        .dutyFreeRate(e.getDutyFreeRate())
                        .taxableIncomeUl(Optional.ofNullable(e.getTaxableIncomeUl()).orElse(BigDecimal.ZERO))
                        .taxableIncomeLl(Optional.ofNullable(e.getTaxableIncomeLl()).orElse(BigDecimal.ZERO))
                        .taxRate(Optional.ofNullable(e.getTaxRate()).orElse(BigDecimal.ZERO))
                        .taxDeduction(Optional.ofNullable(e.getTaxDeduction()).orElse(BigDecimal.ZERO))
                        .createTime(new Date())
                        .updateTime(new Date())
                        .creator(employeeId)
                        .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                        .deleteType(0)
                        .build())
                .collect(Collectors.toList());
    }

}
