package com.njcn.product.system.theme.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.product.system.theme.mapper.FunctionMapper;
import com.njcn.product.system.theme.mapper.RoleFunctionMapper;
import com.njcn.product.system.theme.pojo.po.RoleFunction;
import com.njcn.product.system.theme.pojo.vo.FunctionVO;
import com.njcn.product.system.theme.service.IRoleFunctionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Service
@AllArgsConstructor
public class RoleFunctionServiceImpl extends ServiceImpl<RoleFunctionMapper, RoleFunction> implements IRoleFunctionService {

    private final FunctionMapper functionMapper;

    @Override
    public List<FunctionVO> getFunctionsByRoleIndex(String id) {
        List<FunctionVO> result = new ArrayList<>();
        List<String> functionList = new ArrayList<>();
        QueryWrapper<RoleFunction> componentQueryWrapper = new QueryWrapper<>();
        componentQueryWrapper.eq("sys_role_function.role_id",id);
        functionList = this.baseMapper.selectList(componentQueryWrapper).stream().map(RoleFunction::getFunctionId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(functionList)){
            result = functionMapper.getByList(functionList);
        }
        return result;
    }

    @Override
    public List<String> getFunctionsByList(List<String> roleList) {
        return this.lambdaQuery().in(RoleFunction::getRoleId,roleList).list().stream().map(RoleFunction::getFunctionId).distinct().collect(Collectors.toList());
    }

}
