package com.fdkanfang.service.backend.impl;

import com.fdkanfang.dao.backend.*;
import com.fdkanfang.domain.backend.ResourceEntity;
import com.fdkanfang.domain.backend.UserEntity;
import com.fdkanfang.service.BaseMapperServiceImpl;
import com.fdkanfang.service.backend.ResourceService2;
import com.fdkanfang.service.backend.dto.ResourceTree;
import com.fdkanfang.service.backend.util.ResourceTreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;


/**
 * Created by owen on 2020/3/11 0011 16:16
 */
@Service
public class ResourceServiceImpl2 extends BaseMapperServiceImpl<ResourceEntity, Long> implements ResourceService2 {

    @Autowired
    private ResourceMapper entityMapper;

    @Override
    public IBaseMapper<ResourceEntity, Long> getBaseMapper() {
        return this.entityMapper;
    }


//    @Autowired
//    private ResourceRepository resourceRepository;
//
//    @Autowired
//    private UserRepository userRepository;
//
//    @Autowired
//    private RoleRepository roleRepository;

//    @Override
//    public IBaseRepository<Resource, Long> getBaseRepository() {
//        return this.resourceRepository;
//    }

    /**
     * 获取用户菜单
     * @param user
     * @return
     * @throws Exception
     */
//    @Override
//    @Cacheable(value = "resourcesUserCache", key = "#root.caches[0].name + ':' + #user.id")
//    public List<ResourceTree> getResourcesTreeByUserMenu(User user) throws Exception {
//        //Get user menu
//
//        // 资源Mapper
//        HashMap<Long, Resource> allResourceMapper = getAllResourceMapper();
//
//        // 获取用户菜单
//        List<Resource> resourcesUserMenu = new ArrayList<>();
//        Set<BigInteger> roleIds = userRepository.findUserRoleByUserId(user.getId());
//        for (BigInteger i: roleIds) {
//
//            Set<BigInteger> resourceSet = roleRepository.findRoleResourceByRoleId(i.longValue());
//            for (BigInteger j : resourceSet ) {
//                Resource resource = allResourceMapper.get(j.longValue());
//                if ("menu".equals(resource.getResourceType())) {
//                    resourcesUserMenu.add(resource);
//                }
//            }
//        }
//        ResourceTreeUtil tree = new ResourceTreeUtil(resourcesUserMenu);
//        return tree.buildTree();
//    }

    /**
     * 获取用户权限树
     * @return
     * @throws Exception
     */
    @Override
    public List<ResourceTree> getResourcesTreeByUserPermission(UserEntity user) throws Exception {
        ResourceTreeUtil tree = new ResourceTreeUtil(getResourcesByUserPermission(user));
        return tree.buildTree();
    }

    @Override
    @Cacheable(value = "resourcesCache")
    public List<ResourceTree> getTree() throws Exception {
//        List<Resource> resources = resourceRepository.findAll();
        List<ResourceEntity> resources = entityMapper.selectAll();
        ResourceTreeUtil tree = new ResourceTreeUtil(resources);
        return tree.buildTree();
    }

    /**
     * 获取用户权限
     * @param user
     * @return
     */
//    @Override
//    public List<Resource> getResourcesByUserPermission(User user) {
//        // 资源Mapper
//        HashMap<Long, Resource> allResourceMapper = getAllResourceMapper();
//        // 获取用户菜单
//        List<Resource> resourcesUserPermission = new ArrayList<>();
//        Set<BigInteger> roleIds = userRepository.findUserRoleByUserId(user.getId());
//        for (BigInteger i: roleIds) {
//            Set<BigInteger> resourceSet = roleRepository.findRoleResourceByRoleId(i.longValue());
//            for (BigInteger j : resourceSet ) {
//                Resource resource = allResourceMapper.get(j.longValue());
//                resourcesUserPermission.add(resource);
//            }
//        }
//        return resourcesUserPermission;
//    }

    @Override
    public List<ResourceEntity> getResourcesByUserPermission(UserEntity user) {
//        List<ResourceEntity> resources = entityMapper.findResourceByUserId(user.getId());
        Set<ResourceEntity> resources = entityMapper.findResourceByUserId(user.getId());

        List<ResourceEntity> list = new ArrayList<>(resources);
        return list;
    }

//    @Override
//    public Set<ResourceEntity> getResourcesByUserPermission(UserEntity user) {
//        Set<ResourceEntity> resources = entityMapper.findResourceByUserId(user.getId());
//        return resources;
//    }

    @Override
    public Set<String> findResourceKeyByUserId(Long id) {
        return entityMapper.findResourceKeyByUserId(id);
    }


    /**
     * 获取资源映射作为缓存
     * @return
     */
    private HashMap<Long, ResourceEntity> getAllResourceMapper(){
        // 查找所有resources
//        List<Resource> resourceAll = resourceRepository.findAll();
        List<ResourceEntity> resourceAll = entityMapper.selectAll();
        // 资源Mapper
        HashMap<Long, ResourceEntity> resourceMapper = new HashMap<>();
        for (ResourceEntity i : resourceAll) {
            resourceMapper.put(i.getId(), i);
        }
        return resourceMapper;
    }



}
