package com.ruoyi.system.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;

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

/**
 * Treeselect树结构实体类
 * 
 * @author ruoyi
 */
public class TreeSelectNew implements Serializable
{
    private static final long serialVersionUID = 1L;

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

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

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

    public TreeSelectNew()
    {

    }

    public TreeSelectNew(SysDept dept)
    {
        this.id = dept.getDeptId();
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(dept.getDeptId());
        SysUserMapper userMapper = SpringUtils.getBean(SysUserMapper.class);
        // 查询所有用户
        List<SysUser> list = userMapper.selectUserServingList(sysUser);
        List<SysUser> allUsers = userMapper.selectUserServingList(new SysUser());
        // 筛选出属于当前部门的用户
        for (SysUser user : allUsers) {
            // 检查用户备注中的部门信息
            if (user.getRemark() != null) {
                String remark = user.getRemark();
                // 提取备注中的部门信息
                if (remark.contains("隶属部门：" + dept.getDeptName()) || 
                    remark.contains("合同部门：" + dept.getDeptName()) || 
                    remark.contains("工作部门：" + dept.getDeptName())) {
                    // 确保用户不重复添加
                    if (!containsUser(list, user)) {
                        list.add(user);
                    }
                }
            }
        }
        SysUserPostMapper userPostMapper = SpringUtils.getBean(SysUserPostMapper.class);
        SysUser dsSecretary = new SysUser();
        dsSecretary.setStatus("0");
        List<SysUser> dsSecretaryList = userPostMapper.selectDsSecretary(dsSecretary);
        SysUser jyLeader = new SysUser();
        jyLeader.setStatus("0");
        List<SysUser> jyLeaderList = userPostMapper.selectJyLeader(jyLeader);
        SysUser gmAssistant = new SysUser();
        gmAssistant.setStatus("0");
        List<SysUser> gmAssistantList = userPostMapper.selectGmAssistant(gmAssistant);
        if (dept.getDeptId() == 101L) {
            list.addAll(dsSecretaryList);
        }
        if (dept.getDeptId() == 102L) {
            list.addAll(jyLeaderList);
        }
        if (dept.getDeptId() == 103L) {
            list = gmAssistantList;
        }
        String count = "(" + (list == null ? 0 : list.size()) + ")";
        this.label = dept.getDeptName() + count;
        this.children = dept.getChildren().stream().map(TreeSelectNew::new).collect(Collectors.toList());
    }

    /**
     * 检查列表中是否包含指定用户
     */
    private boolean containsUser(List<SysUser> userList, SysUser user) {
        if (userList == null || user == null || user.getUserId() == null) {
            return false;
        }
        for (SysUser existingUser : userList) {
            if (existingUser.getUserId() != null && existingUser.getUserId().equals(user.getUserId())) {
                return true;
            }
        }
        return false;
    }

    public TreeSelectNew(SysMenu menu)
    {
        this.id = menu.getMenuId();
        this.label = menu.getMenuName();
        this.children = menu.getChildren().stream().map(TreeSelectNew::new).collect(Collectors.toList());
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getLabel()
    {
        return label;
    }

    public void setLabel(String label)
    {
        this.label = label;
    }

    public List<TreeSelectNew> getChildren()
    {
        return children;
    }

    public void setChildren(List<TreeSelectNew> children)
    {
        this.children = children;
    }
}
