package com.lightinit.hsdataplatformresdir.common;

import com.lightinit.hsdataplatformresdir.entity.AdminRole;
import com.lightinit.hsdataplatformresdir.entity.RoleModule;
import com.lightinit.hsdataplatformresdir.entity.SentryRole;
import com.lightinit.hsdataplatformresdir.entity.UserModule;
import com.lightinit.hsdataplatformresdir.service.IRoleService;
import com.lightinit.hsdataplatformresdir.service.IUserModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr.Yao on 2017/4/25.
 */
@Component
public class RoleModulesUtilsPro {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IUserModuleService userModuleService;

    public List<RoleModule> UserHasModules(long uid){
        List<RoleModule> result=new ArrayList<RoleModule>();
        if(uid>0) {
            List<UserModule> userModuleList=userModuleService.QueryUserModuleList(uid);
            if (userModuleList!=null && userModuleList.size()>0){
                for (UserModule mUserModule:userModuleList) {
                    RoleModule roleModule = new RoleModule();
                    roleModule.setModuleid(mUserModule.getModuleid());
                    result.add(roleModule);
                }
            }
            if (result.size()>0)
                return result;
            List<AdminRole> list= roleService.QueryRoleList(uid);
            List<Long> roleIds=new ArrayList<Long>();
            for (AdminRole role:list) {
                roleIds.add(role.getId());
            }
            List<SentryRole> sentryRoles= roleService.QueryManyRole(roleIds);
            for (SentryRole sentryRole:sentryRoles) {
                List<RoleModule> _result = roleService.QueryModuleList(sentryRole.getId());
                if((_result==null || _result.size()==0)&& sentryRole!=null && sentryRole.getParentid()!=null && sentryRole.getParentid()>0){
                    _result=new ArrayList<RoleModule>();
                    List<RoleModule> tmpList =RoleModules(sentryRole.getParentid());
                    if(tmpList!=null){
                        for (RoleModule item:tmpList) {
                            if(_result.contains(item)==false){
                                _result.add(item);
                            }
                        }
                    }
                }
                for (RoleModule item:_result) {
                    if(result.contains(item)==false){
                        result.add(item);
                    }
                }
            }
            return result;
        }else
            return null;
    }
    public List<RoleModule> RoleModules(long role){
        List<RoleModule> result=new ArrayList<RoleModule>();
        if(role>0) {
            SentryRole sentryRole= roleService.QueryOne(role);
            result = roleService.QueryModuleList(role);
            if((result==null || result.size()==0)&& sentryRole!=null && sentryRole.getParentid()!=null && sentryRole.getParentid()>0){
                List<RoleModule> tmpList =RoleModules(sentryRole.getParentid());
                if(tmpList!=null){
                    for (RoleModule item:tmpList) {
                        if(result.contains(item)==false){
                            result.add(item);
                        }
                    }
                }
            }
            return result;
        }else
        return null;
    }
    public List<Long> RoleHasModules(long role){
        List<Long> roleHasModules=new ArrayList<Long>();
        List<RoleModule> result=new ArrayList<RoleModule>();
        if(role>0) {
            SentryRole sentryRole= roleService.QueryOne(role);
            result = roleService.QueryModuleList(role);
            if((result==null || result.size()==0)&& sentryRole!=null && sentryRole.getParentid()!=null && sentryRole.getParentid()>0){
                List<RoleModule> tmpList =RoleModules(sentryRole.getParentid());
                if(tmpList!=null){
                    for (RoleModule item:tmpList) {
                        if(result.contains(item)==false){
                            result.add(item);
                        }
                    }
                }
            }
        }
        for (RoleModule _item:result) {
            if(roleHasModules.contains(_item.getModuleid())==false){
                roleHasModules.add(_item.getModuleid());
            }
        }
        return roleHasModules;
    }
    public List<Long> RoleIds(SentryRole role){
        List<Long> result=new ArrayList<Long>();
        result.add(role.getId());
        if(role.getChildren()!=null && role.getChildren().size()>0){
            for (SentryRole item:role.getChildren()) {
                List<Long> tmpResult=RoleIds(item);
                if(tmpResult.size()>0){
                    for (Long _item:tmpResult) {
                        if(result.contains(_item)==false){
                            result.add(_item);
                        }
                    }
                }
            }
        }
        return result;
    }
}
