package com.zxdmy.excite.system.entity.dto;

import com.zxdmy.excite.system.entity.SysUser;
import com.zxdmy.excite.system.entity.cloud.account.CloudDepartment;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class DepartmentUserTreeNode {
    private Long id;
    private String title;
    private String type; // 'department' 或 'user'
    private Integer sort;
    private Long parentId;
    private Long departmentId; // 用户所属部门ID
    private Boolean isLeaf; // 是否为叶子节点（用户节点为true）
    private Map<String, Object> attributes = new HashMap<>();
    private List<DepartmentUserTreeNode> children = new ArrayList<>();

    // 部门构造器
    public DepartmentUserTreeNode(CloudDepartment dept) {
        this.id = dept.getId();
        this.title = dept.getDepartmentName();
        this.type = "department";
        this.sort = dept.getDepartmentSort();
        this.parentId = dept.getDepartmentParentId();
        this.isLeaf = false;
        this.attributes.put("id", dept.getId());
        this.attributes.put("departmentName", dept.getDepartmentName());
        this.attributes.put("sort", dept.getDepartmentSort());
        this.attributes.put("parentId", dept.getDepartmentParentId());
        this.attributes.put("companyId", dept.getCompanyId());
    }

    // 用户构造器
    public DepartmentUserTreeNode(SysUser user, Long departmentId) {
        this.id = user.getId().longValue();
        this.title = user.getUsername();
        this.type = "user";
        this.sort = 0;
        this.parentId = departmentId; // 父节点是部门ID
        this.departmentId = departmentId;
        this.isLeaf = true;
        this.attributes.put("id", user.getId());
        this.attributes.put("username", user.getUsername());
        this.attributes.put("departmentId", departmentId);
    }
}
