package com.fdage.service.impl;

import com.fdage.dao.cust.TbResourceMapperCust;
import com.fdage.dao.cust.TbRoleMapperCust;
import com.fdage.dao.cust.TbUserMapperCust;
import com.fdage.dto.ResourceTree;
import com.fdage.pojo.TbResource;
import com.fdage.pojo.TbUser;
import com.fdage.service.IResourceService;
import com.fdage.util.ResourceTreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.*;

/**
 * Created by Hb_zzZ on 2020/7/27.
 */
@Service
public class ResourceServiceImpl implements IResourceService {

    @Autowired
    private TbUserMapperCust userMapperCust;

    @Autowired
    private TbRoleMapperCust roleMapperCust;

    @Autowired
    private TbResourceMapperCust resourceMapperCust;


    @Override
    public List<TbResource> getResourcesByUserPermission(TbUser userEntity) {
        // 资源Mapper
        HashMap<Long, TbResource> allResourceMapper = getAllResourceMapper();
        // 获取用户菜单
        List<TbResource> resourcesUserPermission = new ArrayList<>();
        Set<Long> roleIds = userMapperCust.findUserRoleByUserId(userEntity.getId());
        for (Long i: roleIds) {
            Set<Long> resourceSet = roleMapperCust.findRoleResourceByRoleId(i.longValue());
            for (Long j : resourceSet ) {
                TbResource tbResource = allResourceMapper.get(j.longValue());

                // 有权限
                tbResource.setAuthority(true);
                resourcesUserPermission.add(tbResource);
            }
        }
        return resourcesUserPermission;
    }

    /**
     * 根据用户获取权限
     *
     * 会把所有权限都会列出来的，通过Authority判断是否有权限
     */
    @Override
    public List<ResourceTree> getResourcesAllByUserPermission(TbUser userEntity) {
        // 资源Mapper
        HashMap<Long, TbResource> allResourceMapper = getAllResourceMapper();

        // 设置用户权限
        List<TbResource> resourcePer = findResourceByUserId(userEntity.getId());
        for (TbResource i: resourcePer) {
            Long id = i.getId();
            TbResource resourceEntity = allResourceMapper.get(id);
            resourceEntity.setAuthority(true);
            allResourceMapper.put(id, resourceEntity);
        }

        // 获取用户菜单
        List<TbResource> permission = new ArrayList<>();

        for (Map.Entry<Long, TbResource> entry : allResourceMapper.entrySet()) {
            permission.add(entry.getValue());
        }

        ResourceTreeUtil tree = new ResourceTreeUtil(permission);
        return tree.buildTree();

    }

    /**
     * 根据角色获取权限
     *
     * 会把所有权限都会列出来的，通过Authority判断是否有权限
     */
    @Override
    public List<ResourceTree> getResourcesAllByRolePermission(Long roleId) {
        // 资源Mapper
        HashMap<Long, TbResource> allResourceMapper = getAllResourceMapper();

        // 设置用户权限
        List<TbResource> resourcePer = findResourceByRoleId(roleId);
        for (TbResource i: resourcePer) {
            Long id = i.getId();
            TbResource resourceEntity = allResourceMapper.get(id);
            resourceEntity.setAuthority(true);
            allResourceMapper.put(id, resourceEntity);
        }

        // 获取用户菜单
        List<TbResource> permission = new ArrayList<>();

        for (Map.Entry<Long, TbResource> entry : allResourceMapper.entrySet()) {
            permission.add(entry.getValue());
        }

        ResourceTreeUtil tree = new ResourceTreeUtil(permission);
        return tree.buildTree();

    }

    @Override
    public List<TbResource> findResourceByUserId(Long userId) {
        return resourceMapperCust.findResourceByUserId(userId);
    }

    @Override
    public List<TbResource> findResourceByRoleId(Long roleId) {
        return resourceMapperCust.findResourceByRoleId(roleId);
    }

    @Override
    public List<ResourceTree> getTree() throws Exception {
        List<TbResource> resourceEntities = resourceMapperCust.findAll();
        ResourceTreeUtil tree = new ResourceTreeUtil(resourceEntities);
        return tree.buildTree();
    }

    /**
     * 获取资源映射作为缓存
     * @return
     */
    private HashMap<Long, TbResource> getAllResourceMapper(){

//        Condition condition = new Condition(TbResource.class);
//        // 只查询普通角色
//        condition.and().andNotEqualTo("resourceKey","admin");
//        List<TbResource> TbResourceAll = this.findAll(condition);


        // 查找所有resources
        List<TbResource> TbResourceAll = resourceMapperCust.findAll();
        // 资源Mapper
        HashMap<Long, TbResource> resourceMapper = new HashMap<>();
        for (TbResource i : TbResourceAll) {
            i.setAuthority(false);
            resourceMapper.put(i.getId(), i);
        }
        return resourceMapper;
    }
}
