package com.engine.salary.entity.salarysob.bo;
import com.engine.salary.annotation.SalaryFormulaVar;
import com.engine.salary.entity.salaryformula.ExpressFormula;
import com.engine.salary.entity.salaryformula.dto.SalaryFormulaEmployeeDTO;
import com.engine.salary.entity.salaryitem.bo.SalaryItemBO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.dto.SalarySobEmpFieldDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobItemAggregateDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobItemDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobItemGroupDTO;
import com.engine.salary.entity.salarysob.po.SalarySobEmpFieldPO;
import com.engine.salary.entity.salarysob.po.SalarySobItemGroupPO;
import com.engine.salary.entity.salarysob.po.SalarySobItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
* 将薪资账套的员工信息、薪资项目副本、薪资项目分类聚合在一起
*
Copyright: Copyright (c) 2022
* Company: 泛微软件
*
* @author qiantao
* @version 1.0
**/
@AllArgsConstructor
public class SalarySobItemAggregateBO {
/**
* 薪资账套
*/
private SalarySobPO salarySob;
/**
* 薪资账套的员工信息
*/
private List salarySobEmpFields;
/**
* 薪资账套的薪资项目分类
*/
private List salarySobItemGroups;
/**
* 薪资账套的薪资项目副本
*/
private List salarySobItems;
/**
* 薪资账套的薪资项目副本所用公式详情
*/
private List expressFormulas;
/**
* 薪资账套的薪资项目副本所关联的薪资项目
*/
private List salaryItems;
/**
* 转换成聚合dto
*
* @return
*/
public SalarySobItemAggregateDTO convert2AggregateDTO() {
List itemsWithoutGroup = Lists.newArrayList();
// 薪资账套的薪资项目分类po转换成dto
List salarySobItemGroupDTOS = salarySobItemGroups.stream()
.map(e -> SalarySobItemGroupDTO.builder()
.id(e.getId())
.salarySobId(e.getSalarySobId())
.name(e.getName())
.sortedIndex(e.getSortedIndex())
.build())
.collect(Collectors.toList());
Map salarySobItemGroupDTOMap = SalaryEntityUtil.convert2Map(salarySobItemGroupDTOS, SalarySobItemGroupDTO::getId);
Map salaryItemMap = SalaryEntityUtil.convert2Map(salaryItems, SalaryItemPO::getId);
Map> salarySobItemMap = SalaryEntityUtil.group2Map(salarySobItems, SalarySobItemPO::getSalarySobItemGroupId);
Map formulaMap = SalaryEntityUtil.convert2Map(expressFormulas, ExpressFormula::getId, ExpressFormula::getFormula);
// 薪资账套的薪资项目副本po转换成dto
salarySobItemMap.forEach((k, v) -> {
List items = Lists.newArrayList();
for (int i = 0; i < v.size(); i++) {
SalarySobItemPO salarySobItemPO = v.get(i);
SalaryItemPO salaryItemPO = salaryItemMap.get(salarySobItemPO.getSalaryItemId());
items.add(SalarySobItemDTO.builder()
.id(salarySobItemPO.getId())
.salarySobId(salarySob.getId())
.salaryItemGroupId(k)
.salaryItemId(salaryItemPO.getId())
.name(salaryItemPO.getName())
.formulaId(salarySobItemPO.getFormulaId())
.formulaContent(formulaMap.getOrDefault(salarySobItemPO.getFormulaId(), ""))
.taxDeclarationColumn(SalaryItemBO.buildTaxDeclarationColumn(salaryItemPO.getCode()))
.sortedIndex(i)
.canEdit(Objects.equals(salaryItemPO.getCanEdit(), 1))
.canDelete(Objects.equals(salarySobItemPO.getCanDelete(), 1))
.build());
}
if (!salarySobItemGroupDTOMap.containsKey(k)) {
itemsWithoutGroup.addAll(items);
} else {
SalarySobItemGroupDTO salarySobItemGroupDTO = salarySobItemGroupDTOMap.get(k);
salarySobItemGroupDTO.setItems(sortItem(items));
}
});
// 薪资账套的员工信息字段po转换成dto
List salarySobEmpFieldDTOS = buildEmpField(salarySobEmpFields);
return SalarySobItemAggregateDTO.builder()
.salarySobId(salarySob.getId())
.empFields(salarySobEmpFieldDTOS)
.items(sortItem(itemsWithoutGroup))
.itemGroups(sortItemGroup(salarySobItemGroupDTOMap.values()))
.build();
}
/**
* 薪资账套的员工信息字段po转换成dto
*
* @param salarySobEmpFields 薪资账套的员工信息字段
* @return
*/
private List buildEmpField(Collection salarySobEmpFields) {
if (CollectionUtils.isEmpty(salarySobEmpFields)) {
return Collections.emptyList();
}
Field[] declaredFields = SalaryFormulaEmployeeDTO.class.getDeclaredFields();
Map empFieldMap = Maps.newHashMapWithExpectedSize(declaredFields.length);
for (Field declaredField : declaredFields) {
if (!declaredField.isAnnotationPresent(SalaryFormulaVar.class)) {
continue;
}
SalaryFormulaVar annotation = declaredField.getAnnotation(SalaryFormulaVar.class);
empFieldMap.put(declaredField.getName(), SalaryI18nUtil.getI18nLabel(annotation.labelId(), annotation.defaultLabel()));
}
return salarySobEmpFields.stream()
.map(e -> SalarySobEmpFieldDTO.builder()
.id(e.getId())
.salarySobId(e.getSalarySobId())
.fieldId(e.getFieldCode())
.fieldName(empFieldMap.getOrDefault(e.getFieldCode(), ""))
.sortedIndex(e.getSortedIndex())
.canDelete(Objects.equals(e.getCanDelete(), NumberUtils.INTEGER_ONE))
.build())
.collect(Collectors.toList());
}
/**
* 薪资账套的薪资项目按照sortedIndex排序
*
* @param items 待排序的薪资账套的薪资项目
* @return
*/
private List sortItem(Collection items) {
if (CollectionUtils.isEmpty(items)) {
return Collections.emptyList();
}
return items.stream().sorted(Comparator.comparingInt(SalarySobItemDTO::getSortedIndex)).collect(Collectors.toList());
}
/**
* 薪资账套的薪资项目分类按照sortedIndex排序
*
* @param itemGroups 待排序的薪资账套的薪资项目分类
* @return
*/
private List sortItemGroup(Collection itemGroups) {
if (CollectionUtils.isEmpty(itemGroups)) {
return Collections.emptyList();
}
return itemGroups.stream().sorted(Comparator.comparingInt(SalarySobItemGroupDTO::getSortedIndex)).collect(Collectors.toList());
}
}