package com.hzya.frame.sysnew.popedomInterface.service.impl;

import com.hzya.frame.sysnew.application.api.dao.ISysApplicationApiDao;
import com.hzya.frame.sysnew.application.api.entity.SysApplicationApiEntity;
import com.hzya.frame.sysnew.application.apiAuth.dao.ISysApplicationApiAuthDao;
import com.hzya.frame.sysnew.application.apiAuth.entity.SysApplicationApiAuthEntity;
import com.hzya.frame.sysnew.application.apiAythDetail.dao.ISysApplicationApiAuthDetailDao;
import com.hzya.frame.sysnew.application.apiAythDetail.entity.SysApplicationApiAuthDetailEntity;
import com.hzya.frame.sysnew.application.dao.ISysApplicationDao;
import com.hzya.frame.sysnew.application.entity.SysApplicationEntity;
import com.hzya.frame.sysnew.popedomInterface.dao.ISysPopedomInterfaceDao;
import com.hzya.frame.sysnew.popedomInterface.entity.SysPopedomInterfaceEntity;
import com.hzya.frame.sysnew.role.dao.ISysRoleDao;
import com.hzya.frame.sysnew.role.entity.SysRoleEntity;
import com.hzya.frame.sysnew.sysInterface.dao.ISysInterfaceDao;
import com.hzya.frame.sysnew.sysInterface.entity.SysInterfaceEntity;
import com.hzya.frame.sysnew.user.dao.ISysUserDao;
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
import com.hzya.frame.sysnew.userRoles.dao.ISysUserRolesDao;
import com.hzya.frame.sysnew.userRoles.entity.SysUserRolesEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Content 应用缓存
 * @Author 👻👻👻👻👻👻👻👻👻👻 llg
 * @Version 1.0
 */
@Service
public class InterfaceCache {
    Logger logger = LoggerFactory.getLogger(InterfaceCache.class);
    private final ConcurrentHashMap<String, Object> userMap = new ConcurrentHashMap<>();//用户
    private final ConcurrentHashMap<String, Object> roleMap = new ConcurrentHashMap<>();//角色
    private final List<Object> userRoleMap = new ArrayList<>();//用户角色
    private final ConcurrentHashMap<String, Object> userPopedomInterfaceMap = new ConcurrentHashMap<>();//用户接口权限
    private final ConcurrentHashMap<String, Object> rolePopedomInterfaceMap = new ConcurrentHashMap<>();//角色接口权限
    private final ConcurrentHashMap<String, Object> interfaceMap = new ConcurrentHashMap<>();//角色接口权限
    @Resource
    private ISysInterfaceDao sysInterfaceDao;
    @Resource
    private ISysUserDao sysUserDao;
    @Resource
    private ISysRoleDao sysRoleDao;
    @Resource
    private ISysUserRolesDao sysUserRolesDao;
    @Resource
    private ISysPopedomInterfaceDao sysPopedomInterfaceDao;
    public void saveData(String type,String key, Object value) {
        switch (type) {
            case "1": //
                userMap.put(key, value);
                break;
            case "2": //
                roleMap.put(key, value);
                break;
            case "3": //

                break;
            case "4": //
                userPopedomInterfaceMap.put(key, value);
                break;
            case "5": //
                rolePopedomInterfaceMap.put(key, value);
                break;
            case "6": //
                interfaceMap.put(key, value);
                break;
            default:
        }
    }

    public Object get(String type,String key) {
        Object o = null;
        switch (type) {
            case "1": //
                o = userMap.get(key);
                break;
            case "2": //
                o = roleMap.get(key);
                break;
            case "3": //
                o = userRoleMap;
                break;
            case "4": //
                o = userPopedomInterfaceMap.get(key);
                break;
            case "5": //
                o = rolePopedomInterfaceMap.get(key);
                break;
            case "6": //
                o = interfaceMap.get(key);
                break;
            default:
        }
        if(o == null){
            switch (type) {
                case "1": //
                    o = new SysUserEntity();
                    break;
                case "2": //
                    o = new SysRoleEntity();
                    break;
                case "3": //
                    o = new SysUserRolesEntity();
                    break;
                case "4": //
                    o = new SysPopedomInterfaceEntity();
                    break;
                case "5": //
                    o = new SysPopedomInterfaceEntity();
                    break;
                case "6": //
                    o = new SysInterfaceEntity();
                    break;
                default:
            }
        }
        return o;
    }
    public void reloadData(String type) {
        switch (type) {
            case "1": //
                userMap.clear();
                SysUserEntity sendEntity = new SysUserEntity();
                sendEntity.setSts("Y");
                sendEntity.setState("0");
                List<SysUserEntity> list = sysUserDao.queryBase(sendEntity);
                if(list != null && list.size() > 0){
                    for (int i = 0; i < list.size(); i++) {
                        userMap.put("id"+list.get(i).getId(), list.get(i));
                    }
                }
                break;
            case "2": //
                roleMap.clear();
                SysRoleEntity sysRoleEntity = new SysRoleEntity();
                sysRoleEntity.setSts("Y");
                sysRoleEntity.setEnableState("0");
                List<SysRoleEntity> sysRoleEntities = sysRoleDao.queryBase(sysRoleEntity);
                if(sysRoleEntities != null && sysRoleEntities.size() > 0){
                    for (int i = 0; i < sysRoleEntities.size(); i++) {
                        roleMap.put("id"+sysRoleEntities.get(i).getId(), sysRoleEntities.get(i));
                    }
                }
                break;
            case "3": //
                userRoleMap.clear();
                SysUserRolesEntity sysUserRolesEntity = new SysUserRolesEntity();
                sysUserRolesEntity.setSts("Y");
                List<SysUserRolesEntity> sysUserRolesEntities = sysUserRolesDao.queryBase(sysUserRolesEntity);
                if(sysUserRolesEntities != null && sysUserRolesEntities.size() > 0){
                    userRoleMap.addAll(sysUserRolesEntities);
                }
                break;
            case "4": //
                userPopedomInterfaceMap.clear();
                SysPopedomInterfaceEntity sysPopedomInterfaceEntity = new SysPopedomInterfaceEntity();
                sysPopedomInterfaceEntity.setSts("Y");
                sysPopedomInterfaceEntity.setKindId("1");
                List<SysPopedomInterfaceEntity> sysPopedomInterfaceEntities = sysPopedomInterfaceDao.queryBase(sysPopedomInterfaceEntity);
                if(sysPopedomInterfaceEntities != null && sysPopedomInterfaceEntities.size() > 0){
                    for (int i = 0; i < sysPopedomInterfaceEntities.size(); i++) {
                        userPopedomInterfaceMap.put("userId"+sysPopedomInterfaceEntities.get(i).getObjectId()+"interfaceId"+sysPopedomInterfaceEntities.get(i).getInterfaceId(), sysPopedomInterfaceEntities.get(i));
                    }
                }
                break;
            case "5": //
                rolePopedomInterfaceMap.clear();
                SysPopedomInterfaceEntity sysPopedomInterfaceEntity1 = new SysPopedomInterfaceEntity();
                sysPopedomInterfaceEntity1.setSts("Y");
                sysPopedomInterfaceEntity1.setKindId("2");
                List<SysPopedomInterfaceEntity> sysPopedomInterfaceEntities1 = sysPopedomInterfaceDao.queryBase(sysPopedomInterfaceEntity1);
                if(sysPopedomInterfaceEntities1 != null && sysPopedomInterfaceEntities1.size() > 0){
                    for (int i = 0; i < sysPopedomInterfaceEntities1.size(); i++) {
                        rolePopedomInterfaceMap.put("roleId"+sysPopedomInterfaceEntities1.get(i).getObjectId()+"interfaceId"+sysPopedomInterfaceEntities1.get(i).getInterfaceId(), sysPopedomInterfaceEntities1.get(i));
                    }
                }
                break;
            case "6": //
                interfaceMap.clear();
                SysInterfaceEntity sysInterfaceEntity = new SysInterfaceEntity();
                sysInterfaceEntity.setSts("Y");
                sysInterfaceEntity.setState("0");
                List<SysInterfaceEntity> sysInterfaceEntities = sysInterfaceDao.queryBase(sysInterfaceEntity);
                if(sysInterfaceEntities != null && sysInterfaceEntities.size() > 0){
                    for (int i = 0; i < sysInterfaceEntities.size(); i++) {
                        interfaceMap.put("beanName"+sysInterfaceEntities.get(i).getBeanName()+"interfacName"+sysInterfaceEntities.get(i).getInterfaceName(), sysInterfaceEntities.get(i));
                    }
                }
                break;
            default:
        }
    }

}
