package com.bonus.material.common.utils;

import com.bonus.common.core.web.domain.BaseEntity;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.domain.SysRole;
import com.bonus.system.api.domain.SysUser;
import com.bonus.system.api.model.LoginUser;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CommonDataPermissionInfo {

    public static BaseEntity backMissionInfo(String dataScope){
        BaseEntity entity = new BaseEntity();
        Set<Long> roleIds = new HashSet<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null) {
            SysUser sysUser = loginUser.getSysUser();
            if (sysUser != null) {
                Long deptId = sysUser.getDeptId();
                entity.setNewUserId(sysUser.getUserId());
                List<SysRole> roles = sysUser.getRoles();
                if (roles != null) {
                    entity.setNewdeptId(deptId);
                    // 如果需要记录多个角色ID，可以使用Set或List
                    for (SysRole role : roles) {
                        roleIds.add(role.getRoleId());
                        entity.setMissionSource(role.getDataScope());
                        // 只在第一次迭代时设置dataScope，且dataScope不为空的情况下
                        if (dataScope.isEmpty() && !"".equals(role.getDataScope())) {
                            entity.setMissionSource(dataScope);
                            break;
                        }else if(roles.size()>1){
                            entity.setMissionSource(4+"");
                        }

                        // 设置角色ID集合
                        entity.setNewrole(roleIds);
                    }

                }
            }
        }

        return entity;

    }
}
