package org.jeecg.modules.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
public class PowerUtil {
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDepartService sysDepartService;

    public Map<String,List<String>> power(String userId){
        Map<String,List<String>> result = new HashMap<>();
        List<String> ids=new ArrayList<>();
        SysUser user = sysUserService.getById(userId);
        String post = user.getPost();
        if (StringUtils.isBlank(post)||post.equals("总经理")
                ||post.equals("meijie")){
            result.put("ids",ids);
            return result;
        }
        if (post.equals("devleader")||post.equals("fuzeren")||post.equals("yunyin")||post.equals("Z001")){
            //获取部门以及下辖成员
            List<String> subDepids = new ArrayList<>();
            subDepids = sysDepartService.getSubDepIdsByDepId(user.getDepartIds());
            if(subDepids != null && subDepids.size()>0){
//                ids.addAll(subDepids);
                List<SysUser> sysUsers = sysUserService.queryByDepIds(subDepids, null);
                for (SysUser sysUser : sysUsers) {
                    ids.add(sysUser.getId());
                }
            }
        }else {
            ids.add(userId);
        }
        List uniqueStr = ids.stream().distinct().collect(Collectors.toList());
        result.put("ids",uniqueStr);
        return result;
//        String roleCode = "";
//        List<String> roles = new ArrayList<>();
//        for (SysUserRole userRole : userRoles) {
//            SysRole role = sysRoleService.getById(userRole.getRoleId());
//            if(role != null){
//                roles.add(role.getRoleCode());
//                if(role.getRoleCode().equals("admin") || role.getRoleCode().equals("test") || role.getRoleCode().equals("operatorChief")){
//                    roleCode = "admin";
////                    return ids;
//                }
//            }
//        }

    }

    public Map<String,List<String>> power(String userId,String orgCode,String findPost){
        if (StringUtils.isBlank(findPost)&&StringUtils.isBlank(orgCode)){
            return power(userId);
        }
        List<String> subDepIdsByDepId = sysDepartService.getSubDepIdsByDepId(orgCode);
        String departIds ="";
        if (!subDepIdsByDepId.isEmpty()){
            for (String s : subDepIdsByDepId) {
                departIds+=","+s;
            }
        }
        Map<String,List<String>> result = new HashMap<>();
        List<String> ids=new ArrayList<>();
        SysUser user = sysUserService.getById(userId);
        String post = user.getPost();
        if (StringUtils.isBlank(post)||post.equals("meijie")){
            result.put("ids",ids);
            return result;
        }
        if (post.equals("总经理")||post.equals("devleader")||post.equals("fuzeren")||post.equals("yunyin")||post.equals("Z001")){
            //获取部门以及下辖成员
            List<String> subDepids = new ArrayList<>();
            subDepids = sysDepartService.getSubDepIdsByDepId(user.getDepartIds());
            if(subDepids != null && subDepids.size()>0){
//                ids.addAll(subDepids);
                List<SysUser> sysUsers = sysUserService.queryByDepIds(subDepids, null);
                for (SysUser sysUser : sysUsers) {
                    if (subDepIdsByDepId.isEmpty()){
                        if (StringUtils.isNotBlank(findPost)&&!findPost.equals(sysUser.getPost())){
                            continue;
                        }
                        ids.add(sysUser.getId());
                    }
                    if (!subDepIdsByDepId.isEmpty()){
                        String userDepartIds = sysUser.getDepartIds();
                        if (departIds.contains(userDepartIds)){
                            if (StringUtils.isNotBlank(findPost)&&!findPost.equals(sysUser.getPost())){
                                continue;
                            }
                            ids.add(sysUser.getId());
                        }
                    }
                }
            }
        }else {
            if (StringUtils.isNotBlank(findPost)&&!findPost.equals(user.getPost())){
                result.put("ids",ids);
                return result;
            }
            ids.add(userId);
        }
        result.put("ids",ids);
        return result;
    }

    /**
     * 递归获取所有的子账户
     * @param users
     * @param ids
     * @return
     */
    private List<String> recursion(List<SysUser> users,List<String> ids){
        if(users.size() <= 0){
            return ids;
        }
        for (SysUser user : users) {
            ids.add(user.getId());
            List<SysUser> list = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getCreateBy, user.getId()));
            recursion(list,ids);
        }
        return ids;
    }
}
