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

import com.engine.salary.entity.salaryformula.ExpressFormula;
import com.engine.salary.entity.salaryitem.bo.SalaryItemBO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.dto.SalarySobBackItemDTO;
import com.engine.salary.entity.salarysob.po.SalarySobBackItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobDefaultBackItemPO;
import com.engine.salary.util.SalaryEntityUtil;
import com.google.common.collect.Lists;
import com.engine.salary.util.db.IdGenerator;
import org.apache.commons.collections.CollectionUtils;

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

/**
 * @author Harryxzy
 * @date 2022/11/15 17:42
 * @description
 */
public class SalarySobBackItemBO {

    public static List<SalarySobDefaultBackItemPO> getDefault() {
        return Lists.newArrayList(
                SalarySobDefaultBackItemPO.builder()
                        .id(IdGenerator.generate())
                        .sysSalaryItemId(746777981115629575L)
                        .formulaId(0L)
                        .backCalcType(0).build(),
                SalarySobDefaultBackItemPO.builder()
                        .id(IdGenerator.generate())
                        .sysSalaryItemId(746777981115629576L)
                        .formulaId(0L)
                        .backCalcType(1).build());
    }


    public static List<SalarySobBackItemDTO> convert2DTO(List<SalarySobBackItemPO> salarySobBackItems, List<SalaryItemPO> salaryItems, List<ExpressFormula> expressFormulas) {
        if (CollectionUtils.isEmpty(salarySobBackItems)) {
            return Collections.emptyList();
        }
        List<SalarySobBackItemDTO> dtos = Lists.newArrayList();
        Map<Long, SalaryItemPO> salaryItemMap = SalaryEntityUtil.convert2Map(salaryItems, SalaryItemPO::getId);
        Map<Long, String> expressFormulaMap = SalaryEntityUtil.convert2Map(expressFormulas, ExpressFormula::getId, ExpressFormula::getFormula);
        for (SalarySobBackItemPO salarySobBackItem : salarySobBackItems) {
            SalaryItemPO salaryItem = salaryItemMap.get(salarySobBackItem.getSalaryItemId());
            SalarySobBackItemDTO dto = SalarySobBackItemDTO.builder()
                    .id(salarySobBackItem.getId())
                    .salaryItemId(salaryItem.getId())
                    .sysSalaryItemId(salaryItem.getSysSalaryItemId())
                    .salarySobItemGroupId(0L)
                    .name(salaryItem.getName())
                    .formulaId(salarySobBackItem.getFormulaId())
                    .formulaContent(expressFormulaMap.getOrDefault(salarySobBackItem.getFormulaId(), "输入"))
                    .dataType(salaryItem.getDataType())
                    .pattern(salarySobBackItem.getPattern())
                    .taxDeclarationColumn(SalaryItemBO.buildTaxDeclarationColumn(salaryItem.getCode()))
                    .sortedIndex(0)
                    .canEdit(true)
                    .canDelete(false)
                    .incomeCategory("0")
                    .backCalcType(salarySobBackItem.getBackCalcType()).build();
            dtos.add(dto);
        }
        return dtos;
    }

}
