package com.njcn.product.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;

import com.njcn.product.auth.mapper.RoleMapper;
import com.njcn.product.auth.pojo.po.Role;
import com.njcn.product.auth.pojo.po.UserRole;
import com.njcn.product.auth.service.IRoleService;
import com.njcn.product.auth.service.IUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    private final IUserRoleService iUserRoleService;



    @Override
    public List<String> getRoleNameByUserId(String id) {
        List<UserRole> userRoles = iUserRoleService.getUserRoleByUserId(id);
        if (CollectionUtils.isEmpty(userRoles)) {
            return new ArrayList<>();
        }
        List<Role> roles = this.lambdaQuery()
                .select(Role::getCode)
                .eq(Role::getState, DataStateEnum.ENABLE.getCode())
                .in(Role::getId, userRoles.stream()
                        .map(UserRole::getRoleId)
                        .collect(Collectors.toList())
                ).list();
        return roles
                .stream()
                .map(Role::getCode)
                .distinct()
                .collect(Collectors.toList());
    }


}
