package org.ccpit.base.usergroup;

import org.ccpit.base.role.Role;
import org.ccpit.base.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Set;

/**
 * 用户组服务操作表
 * Created by Administrator on 2015/9/7.
 */
@Repository
@Transactional
public class UserGroupService {
    @Autowired
    private UserGroupDao userGroupDao;

    public boolean save(UserGroup org){
        return userGroupDao.save(org);
    }

    /**
     * 查询用户所在的用户组
     * @param user
     * @return
     */
    public List<UserGroup> getUserGroupsByUser(User user){
        return userGroupDao.getUserGroupsByUser(user);
    }

    /**
     * 查询所有拥有此角色的用户组
     * @param role
     * @return
     */
    public List<UserGroup> getUserGroupsByRole(Role role){
       return userGroupDao.getUserGroupsByRole(role);
    }

    /**
     * 用户所在的组是否具有role的角色
     * @param user
     * @param role
     * @return
     */
    public boolean canUserInGroup(User user,Role role){
        return userGroupDao.canUserInGroup(user, role);
    }


    /**
     * 获取用户所在组的所有角色
     * @param user
     * @return
     */
    public Set<Role> getRolesForUser(User user){
       return userGroupDao.getRolesForUser(user);
    }

    /**
     * Find UserGroup by id
     * @param id
     * @return
     */
    public UserGroup get(long id){
        return userGroupDao.queryById(id);
    }
}
