package cn.iocoder.byzs.module.web.service.role;

import cn.iocoder.byzs.framework.common.enums.CommonStatusEnum;
import cn.iocoder.byzs.framework.security.config.SecurityProperties;
import cn.iocoder.byzs.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.byzs.module.system.dal.dataobject.permission.RoleDO;
import cn.iocoder.byzs.module.system.service.permission.PermissionService;
import cn.iocoder.byzs.module.system.service.permission.RoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

/**
 * 角色权限 Service 实现类
 *
 * @author lyb
 */
@Service
@Validated
public class WebAuthRoleServiceImpl {

    @Resource
    private SecurityProperties securityProperties;
    @Resource
    private PermissionService permissionService;
    @Resource
    private RoleService roleService;

    /**
     * 获得用户角色列表
     * @return
     */
    private List<RoleDO> getUserRoleList() {
        // 获得角色列表
        Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(WebFrameworkUtils.getLoginUserId());
        List<RoleDO> roles = roleService.getRoleList(roleIds);
        roles.removeIf(role -> !CommonStatusEnum.ENABLE.getStatus().equals(role.getStatus())); // 移除禁用的角色

        return roles;
    }

    /**
     * 取课程数据权限
     * @return
     */
    public Set<Long> getCourseAuthRoleVO() {

        // 获得角色列表
        List<RoleDO> roles = getUserRoleList();

        // 将所有角色的dataScopeCourseIds拼接成一个Set集合
        Set<Long> allDataScopeCourseIds = new HashSet<>();
        for (RoleDO role : roles) {
            // 填充课程数据权限
            Set<Long> dataScopeCourseIds = role.getDataScopeCourseIds();
            if (dataScopeCourseIds != null && !dataScopeCourseIds.isEmpty()) {
                allDataScopeCourseIds.addAll(dataScopeCourseIds);
            }
        }
        return allDataScopeCourseIds;
    }

    /**
     * 取blockly数据权限
     * @return
     */
    public Set<Long> getBlocklyAuthRoleVO() {

        // 获得角色列表
        List<RoleDO> roles = getUserRoleList();

        // 将所有角色的dataScopeBlocklyIds拼接成一个Set集合
        Set<Long> allDataScopeBlocklyIds = new HashSet<>();
        for (RoleDO role : roles) {
            //blocklu课程数据权限
            Set<Long> dataScopeBlocklyIds = role.getDataScopeBlocklyIds();
            if (dataScopeBlocklyIds != null && !dataScopeBlocklyIds.isEmpty()) {
                allDataScopeBlocklyIds.addAll(dataScopeBlocklyIds);
            }
        }
        return allDataScopeBlocklyIds;
    }

    /**
     * 取AiCourse数据权限
     * @return
     */
    public Set<Long> getAiCourseAuthRoleVO() {

        // 获得角色列表
        List<RoleDO> roles = getUserRoleList();

        // 将所有角色的dataScopeAiCourseIds拼接成一个Set集合
        Set<Long> allDataScopeAiCourseIds = new HashSet<>();
        for (RoleDO role : roles) {
            //blocklu课程数据权限
            Set<Long> dataScopeAiCourseIds = role.getDataScopeAiCourseIds();
            if (dataScopeAiCourseIds != null && !dataScopeAiCourseIds.isEmpty()) {
                allDataScopeAiCourseIds.addAll(dataScopeAiCourseIds);
            }
        }
        return allDataScopeAiCourseIds;
    }
}