package com.mosty.common.core.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.mosty.common.base.constant.enums.MenuTypeEnum;
import com.mosty.common.base.exception.Asserts;
import com.mosty.common.core.business.entity.*;
import com.mosty.common.core.business.service.*;
import com.mosty.common.core.login.LoginResponseVO;
import com.mosty.common.core.login.UserPermissionsInfo;
import com.mosty.common.core.login.dto.DeptDTO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class LoginServiceImpl implements LoginService {


    private final SysRoleDeptService sysRoleDeptService;
    private final SysUserRoleService sysUserRoleService;
    private final SysRoleMenuService sysRoleMenuService;
    private final SysMenuService sysMenuService;

    @Override
    public List<SysMenu> getMenus(DeptDTO currentLoginDept, SysUser sysUser, LoginResponseVO loginResponseVO) {
        if (!sysUser.getLoginName().contains("admin")) {
            if(StringUtils.isBlank(sysUser.getType()) || "01".equals(sysUser.getType())){
                List<SysRoleDept> roleDepts = sysRoleDeptService.listByDeptId(currentLoginDept.getDeptId());
                List<SysUserRole> userRoles = sysUserRoleService.listByUserId(sysUser.getId());
                List<Long> roleIds = new ArrayList<>();
                if (!roleDepts.isEmpty()) {
                    List<Long> deptRoleIds = roleDepts.stream().map(SysRoleDept::getRoleId).collect(Collectors.toList());
                    roleIds.addAll(deptRoleIds);
                }
                if (!userRoles.isEmpty()) {
                    List<Long> userRoleIds = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
                    roleIds.addAll(userRoleIds);
                }
                //  如果没有权限 ，添加默认权限
                if (CollectionUtils.isEmpty(roleIds)) {
                    SysUserRole role = new SysUserRole();
                    role.setUserId(sysUser.getId());
                    role.setRoleId(136L);
                    this.sysUserRoleService.save(role);
                    roleIds.add(136L);
                }
                Set<Long> rolesSet = new HashSet<>(roleIds);
                List<SysRoleMenu> roleMenus = sysRoleMenuService.listByRoleIds(rolesSet);
                if (roleMenus.isEmpty()) {
                    return new ArrayList<>();
                }
                Set<Long> menuIds = roleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toSet());
                //查询一级菜单组 递归
                return sysMenuService.listByType(menuIds, MenuTypeEnum.MENU_GROUP, loginResponseVO);
            }else{
                Set<Long> rolesSet = new HashSet<>();
                rolesSet.add(134L);
                List<SysRoleMenu> roleMenus = sysRoleMenuService.listByRoleIds(rolesSet);
                if (roleMenus.isEmpty()) {
                    return new ArrayList<>();
                }
                Set<Long> menuIds = roleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toSet());
                //查询一级菜单组 递归
                return sysMenuService.listByType(menuIds, MenuTypeEnum.MENU_GROUP, loginResponseVO);
            }
        } else {
            return sysMenuService.listByType(null, MenuTypeEnum.MENU_GROUP, loginResponseVO);
        }
    }

}
