package jnpf.model.personnels.vo.roster;

import lombok.Data;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * sheet名称
 *
 * @author fantaibao
 * @date 2024/02/02
 */
@Data
public class FtbRosterCategoryVO implements Serializable {

    private static final long serialVersionUID = -3325950343507218064L;
    /**
     * 类别名称(等价于excel中的sheet名称)
     */
    private String classificationName;
    /**
     * sheet下标，从0开始
     */
    private Integer sheetIndex;
    /**
     * 表头
     */
    private Map<String, String> head;
    /**
     * 表单数据
     */
    private List<FtbRosterPageVO> ftbRosterPageVOS;

    public Integer getSorted() {
        return this.ftbRosterPageVOS.size();
    }

    public List<List<String>> convertHead() {
        return this.head.entrySet().stream()
                .sorted(Comparator.comparingInt(a -> Integer.parseInt(a.getKey())))
                .map(s -> {
            List<String> list = new ArrayList<>();
                    list.add(s.getValue());
            return list;
        }).collect(Collectors.toList());
    }

    public List<List<Object>> convertData() {
        List<List<Object>> result = new ArrayList<>();
        this.ftbRosterPageVOS.forEach(d -> {
            List<Object> temp = new ArrayList<>();
            this.head.entrySet().stream()
                    .sorted(Comparator.comparingInt(a -> Integer.parseInt(a.getKey())))
                    .forEach(a -> {
                        FtbRosterAttributesVO ftbRosterAttributesVO = d.getFtbRosterAttributesVOS().stream()
                                .filter(c -> c.getAttributeName().equals(a.getValue())).findFirst()
                                .orElse(null);
                        if (Objects.nonNull(ftbRosterAttributesVO)) {
                            temp.add(ftbRosterAttributesVO.getAttributeValue());
                        } else {
                            temp.add("");
                        }
                    });
            result.add(temp);
        });
        return result;
    }

}
