package com.cico.nest.lcdp.boot.core.system.api.role;

import com.cico.nest.lcdp.boot.core.system.dal.dataobject.permission.RoleDO;
import com.cico.nest.lcdp.boot.core.system.service.permission.PermissionService;
import com.cico.nest.lcdp.boot.core.system.service.permission.RoleService;
import com.cico.nest.lcdp.boot.core.system.service.user.AdminUserService;
import com.cico.nest.lcdp.framework.api.role.LcdpRoleApi;
import com.cico.nest.lcdp.framework.api.role.dto.LcdpRoleDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

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

/**
 * @author zws
 * @date 2024/7/25
 */

@Service
public class LcdpRoleApiImpl implements LcdpRoleApi {

    @Resource
    private RoleService roleService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private AdminUserService userService;
    @Override
    public List<LcdpRoleDTO> getAllRole() {
        List<RoleDO> roleList = roleService.getRoleList();
        return RoleDO.toRoleDTO(roleList);
    }

    @Override
    public List<String> getRolesByUsername(String username) {
        Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(userService.getUserByUsername(username).getId());
        List<String> roleIdList = new ArrayList<>();
        if (roleIds != null && !roleIds.isEmpty()) {
            roleIds.forEach(role -> roleIdList.add(role.toString()));
        }
        return roleIdList;
    }
}
