package com.winsun.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
import com.netflix.loadbalancer.ServerListSubsetFilter;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.auth.model.user.User;
import com.winsun.bean.School;
import com.winsun.bean.SysUser;
import com.winsun.mapper.SchoolMapper;
import com.winsun.mapper.SysUserMapper;

/**
 * 数据权限控制
 * @author 董有沛
 *
 */
@Component
public class DataAuthUtil {
	@Autowired
	private SchoolMapper schoolMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	
	public List<Map<String, Object>> authDeal(List<Map<String, Object>> targetList,ShiroUser user){
		SysUser sysUser = getSysUser(user);
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		
 		if(user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
			resultList = targetList;
		}else if(user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "县分管理员"))) {
			resultList = authData(targetList,"substName",sysUser.getSubstName());
		}else if(user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "督导"))) {
			Set<Integer> schoolIdSet = authSchool(sysUser.getId());
			resultList = authData(targetList,"schoolId",schoolIdSet);
		}else if(user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "合伙人"))) {
			resultList = authData(targetList,"userId", sysUser.getId());
		}else {
			resultList = targetList;
		}
		
		
		return resultList;
	}
	
	public Set<Integer> authSchool(String userId){
		Set<Integer> schoolIdSets = new HashSet<Integer>();
		List<Map<String, Object>> schoolRefList = schoolMapper.getSupervisorByUserId(userId);
//		Wrapper<School> wrapperSchool = new EntityWrapper<School>();
//		List<School> schoolList = schoolMapper.selectList(wrapperSchool);
		
		for(Map<String, Object> map : schoolRefList) {
			schoolIdSets.add((int)map.get("school"));
		}
		
		return schoolIdSets;
	}
	
	public List<Map<String, Object>> authData(List<Map<String, Object>> list,String authkey, String val){
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> map : list) {
			if(null != map.get(authkey)) {
				if(val.equals((String)map.get(authkey))) {
					resultList.add(map);
				}
			}else {
				// 数据筛选字段不存在时，默认返回数据
				resultList.add(map);
			}
		}
		
		
		return resultList;
	}
	
	public List<Map<String, Object>> authData(List<Map<String, Object>> list,String authkey, Set<Integer> valSet){
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> map : list) {
			if(null != map.get(authkey)) {
				
				if(valSet.contains((Integer)map.get(authkey))) {
					resultList.add(map);
				}
			}else {
				// 数据筛选字段不存在时，默认返回数据
				resultList.add(map);
			}
		}
		
		
		return resultList;
	}
	
	public SysUser getSysUser(ShiroUser user) {
		SysUser sysUser = sysUserMapper.selectById(user.getId());
		Map<String, Object> schoolRefMaps = MapUtil.listToMap(schoolMapper.getSchoolRef(), "user_id", "school_id");
		Wrapper<School> wrapperSchool = new EntityWrapper<School>();
		Map<String, Map<String, Object>> schoolMaps = MapUtil.listToMap(schoolMapper.selectMaps(wrapperSchool), "id");
		if(null != schoolRefMaps.get(sysUser.getId())) {
			String schoolId = schoolRefMaps.get(sysUser.getId()).toString();
			Map<String, Object> schoolMap = schoolMaps.get(schoolId);
			
			sysUser.setSubstName(schoolMap.get("subName").toString());
		}
		
		return sysUser;
	}
	
	public List<Map<String, Object>> roleAuth(List<Map<String, Object>> targetList, ShiroUser user){
		SysUser sysUser = getSysUser(user);
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		
 		if(user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
 			resultList = targetList;
 		}else if(user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "系统管理员"))){
 			resultList = targetList;
 		}else if(user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "县分管理员"))){
 			for(Map<String, Object> map : targetList) {
 				String roleName = map.get("name").toString();
 				
 				if(!"超级管理员".equals(roleName) && !"系统管理员".equals(roleName) && !"受理人员".equals(roleName)) {
 					resultList.add(map);
 				}
 			}
 		}else if(user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "督导"))){
 			for(Map<String, Object> map : targetList) {
 				String roleName = map.get("name").toString();
 				
 				if(!"超级管理员".equals(roleName) && !"系统管理员".equals(roleName) && !"受理人员".equals(roleName) && !"县分管理员".equals(roleName)) {
 					resultList.add(map);
 				}
 			}
 		}
		
		return resultList;
	}
 }
