package com.fuzamei.service.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fuzamei.entity.Param;
import com.fuzamei.entity.UserDetail;
import com.fuzamei.mapperInterface.UserAuthoricationMapper;
import com.fuzamei.service.UserAuthoricationService;
@Service
public class UserAuthoricationServiceImpl implements UserAuthoricationService {

	@Autowired
	private UserAuthoricationMapper userAuthoricationMapper;
	
	@Override
	public UserDetail queryUserDetail(Integer userId,String... roleNames) {
		UserDetail userDetail = userAuthoricationMapper.queryUserDetail(userId);
		if(userDetail==null){
			throw new RuntimeException("无此用户");
		}
		if (roleNames.length!=0) {
			String roleName = userDetail.getRoleName();
			boolean flag = false;
			for (String name : roleNames) {
				if (name.equals(roleName)) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				throw new RuntimeException("无权操作");
			} 
		}
		return userDetail;
	}

	@Override
	public UserDetail queryUserAuthority(Integer userId, String... roleNames) {
		UserDetail userDetail = userAuthoricationMapper.queryUserAuthority(userId);
		if(userDetail==null){
			throw new RuntimeException("无此用户");
		}
		if (roleNames.length!=0) {
			String roleName = userDetail.getRoleName();
			boolean flag = false;
			for (String name : roleNames) {
				if (name.equals(roleName)) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				throw new RuntimeException("无权操作");
			}
		}
		return userDetail;
	}

	@Override
	public List<UserDetail> showAllCarriersUnderSupplier(Param param) {
		return userAuthoricationMapper.showAllCarriersUnderSupplier(param);
	}

}
