package com.mortals.xhx.module.dept.model;

import java.util.List;
import java.util.ArrayList;

import com.mortals.xhx.module.dept.model.vo.DeptVo;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.mortals.xhx.module.staff.model.StaffEntity;
import lombok.Data;
import org.springframework.util.ObjectUtils;

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

/**
 * 部门信息前端映射树结构实体类
 *
 * @author zxfei
 * @date 2023-03-06
 */
@Data
public class DeptTreeSelect implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 节点ID
     */
    private Long id;

    /**
     * 节点名称
     */
    private String label;

    private Integer personNum;

    /**
     * 子节点
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<DeptTreeSelect> children;

    /**
     * 子节点
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<Map<String, Object>> personList;

    public DeptTreeSelect(DeptEntity entity) {
        this.id = entity.getId();
        this.label = entity.getDeptName();
        this.personNum = entity.getPersonNum();
        if (!ObjectUtils.isEmpty(entity.getChildren())) {
            this.children = entity.getChildren().stream().map(DeptTreeSelect::new).collect(Collectors.toList());

            int sum = entity.getChildren().stream().mapToInt(i -> i.getPersonNum()).sum();
            this.personNum = this.personNum + sum;
        }
        this.personList = entity.getPersonList();


    }

}
