package cn.com.countrygarden.bi.main.service.districtReport.impl;

import cn.com.countrygarden.bi.main.dao.districtReport.RdDistrictInfoMapper;
import cn.com.countrygarden.bi.main.dao.districtReport.RdDistrictPrincipalMapper;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.districtReport.RdDistrictPrincipal;
import cn.com.countrygarden.bi.main.model.sys.UserRoles;
import cn.com.countrygarden.bi.main.service.districtReport.RdProjectpermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class RdProjectpermissionImpl implements RdProjectpermissionService {

    @Autowired
    private RdDistrictInfoMapper rdDistrictInfoMapper;

    @Autowired
    private RdDistrictPrincipalMapper rdDistrictPrincipalMapper;

    @Override
    public Map<String, Object> findRoleInfoMap(String sysUserCode) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (null == sysUserCode || sysUserCode.equals("")) {
            throw new ServiceException("请登录");
        }

        List<String> rolePermissions=new ArrayList<>();

        List<UserRoles> sysUserRole=rdDistrictInfoMapper.selectuserRoles(sysUserCode);
        for (UserRoles userRoles : sysUserRole) {
            if (userRoles.getRoleCode().equals("project_manager_admin")) {
                map.clear();
                map.put("project_manager_admin", "项目进度管理员");
                break;
            }else if(userRoles.getRoleCode().equals("admin")){
                map.clear();
                map.put("admin", "管理员");
                break;
            }else if(userRoles.getRoleCode().equals("project_manager_principalAdmin")){
                map.put("project_manager_principalAdmin", "项目进度片区管理员");
            }else if(userRoles.getRoleCode().equals("project_manager_regionAdmin")){
                map.put("project_manager_regionAdmin", "项目进度区域管理员");
            }else if(userRoles.getRoleCode().equals("project_manager_regionUser")){
                map.put("project_manager_regionUser", "项目进度区域用户");
            }else if(userRoles.getRoleCode().equals("project_manager_projectUser")){
                map.put("project_manager_projectUser", "项目进度_项目用户");
            }
        }
        if (map.size() < 1) {
            map.put("role", "noRole");
        }
        return map;
    }

    @Override
    public List<String> principalList(String sysUserCode) {
        List<String> principaString=new ArrayList<>();
        List<RdDistrictPrincipal> principalList=rdDistrictPrincipalMapper.rdDistrictPList(sysUserCode);
        if(principalList!=null&&principalList.size()!=0){
            for (int y=0;y<principalList.size();y++){
                principaString.add(principalList.get(y).getPrincipalCode());
            }
        }
        Set<String> principaSet=new HashSet<>(principaString);
        List<String> principa=new ArrayList<>(principaSet);
        return principa;
    }

    @Override
    public List<String> principalRegionList(String sysUserCode) {
        List<String> principaString=new ArrayList<>();
        List<RdDistrictPrincipal> principalList=rdDistrictPrincipalMapper.rdDistrictPList(sysUserCode);
        if(principalList!=null&&principalList.size()!=0){
            for (int y=0;y<principalList.size();y++){
                principaString.add(principalList.get(y).getRegionCode());
            }
        }
        Set<String> principaSet=new HashSet<>(principaString);
        List<String> principa=new ArrayList<>(principaSet);
        return principa;
    }

    @Override
    public List<Integer> roleInfoList(Map<String, Object> rolesMap) {
        List<Integer> paixu=new ArrayList<>();
        for (Map.Entry<String, Object> entry : rolesMap.entrySet()) {
            if(entry.getKey().equals("admin")){
                paixu.add(1);
            }
            if(entry.getKey().equals("project_manager_admin")){
                paixu.add(2);
            }
            if(entry.getKey().equals("project_manager_principalAdmin")){
                paixu.add(3);
            }
            if(entry.getKey().equals("project_manager_regionAdmin")){
                paixu.add(4);
            }
            if(entry.getKey().equals("project_manager_regionUser")){
                paixu.add(5);
            }
            if(entry.getKey().equals("project_manager_projectUser")){
                paixu.add(6);
            }
            if(entry.getKey().equals("role")){
                throw new ServiceException("权限不足,无法使用此模块");
            }
        }
        Collections.sort(paixu);

        return paixu;
    }
}
