package com.sysu.admin.support.system.role_permission;

import com.sysu.admin.support.base.BaseService;
import com.xiesx.fastboot.core.jpa.JpaPlusRepository;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @title RolePermissionService
 * @description
 * @author XIE
 * @date 2020年4月24日下午10:07:21
 */
@Service
public class RolePermissionService extends BaseService<RolePermission, PKRP>{

    @Autowired
    RolePermissionRepository mRolePermissionRepository;

    /**
     * 查询Ids
     *
     * @return
     */
    public String[] getIds(@NonNull Long roleId) {
        List<RolePermission> list = mRolePermissionRepository.getListByRole(roleId);
        String[] ids = new String[list.size()];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = String.valueOf(list.get(i).getPk().getMenu());
        }
        return ids;
    }

    /**
     * 查询Permissions
     *
     * @return
     */
    public String[] getPermissions(@NonNull Long roleId) {
        List<RolePermission> list = mRolePermissionRepository.getListByRole(roleId);
        String[] permissions = new String[list.size()];
        for (int i = 0; i < permissions.length; i++) {
            permissions[i] = list.get(i).getPermission();
        }
        return permissions;
    }

    public List<RolePermission> getListByRole(Long roesId){
        return mRolePermissionRepository.getListByRole(roesId);
    }

    @Override
    public JpaPlusRepository<RolePermission, PKRP> r() {
        return this.mRolePermissionRepository;
    }
}
